AceSegment  0.7.0
A framework for rendering seven segment LED displays using the TM1637, MAX7219, HT16K33, or 74HC595 controller chips
LedMatrixDirect.h
1 /*
2 MIT License
3 
4 Copyright (c) 2018 Brian T. Park
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24 
25 #ifndef ACE_SEGMENT_LED_MATRIX_DIRECT_H
26 #define ACE_SEGMENT_LED_MATRIX_DIRECT_H
27 
28 #include <Arduino.h> // OUTPUT, INPUT
29 #include "../hw/GpioInterface.h"
30 #include "LedMatrixBase.h"
31 
32 class LedMatrixDirectTest_drawElements;
33 
34 namespace ace_segment {
35 
43 template <typename T_GPIOI = GpioInterface>
45  public:
47  uint8_t elementOnPattern,
48  uint8_t groupOnPattern,
49  uint8_t numElements,
50  const uint8_t* elementPins,
51  uint8_t numGroups,
52  const uint8_t* groupPins
53  ) :
54  LedMatrixBase(elementOnPattern, groupOnPattern),
55  mElementPins(elementPins),
56  mGroupPins(groupPins),
57  mNumElements(numElements),
58  mNumGroups(numGroups)
59  {}
60 
61  void begin() const {
62  // Set element pins to OUTPUT mode but set LEDs to OFF.
63  uint8_t output = (0x00 ^ mElementXorMask) & 0x1;
64  for (uint8_t element = 0; element < mNumElements; element++) {
65  uint8_t elementPin = mElementPins[element];
66  T_GPIOI::pinMode(elementPin, OUTPUT);
67  T_GPIOI::digitalWrite(elementPin, output);
68  }
69 
70  // Set group pins to OUTPUT mode but set LEDs to OFF.
71  output = (0x00 ^ mGroupXorMask) & 0x1;
72  for (uint8_t group = 0; group < mNumGroups; group++) {
73  uint8_t pin = mGroupPins[group];
74  T_GPIOI::pinMode(pin, OUTPUT);
75  T_GPIOI::digitalWrite(pin, output);
76  }
77  }
78 
79  void end() const {
80  // Set element pins to INPUT mode.
81  for (uint8_t element = 0; element < mNumElements; element++) {
82  uint8_t elementPin = mElementPins[element];
83  T_GPIOI::pinMode(elementPin, INPUT);
84  }
85 
86  // Set group pins to INPUT mode.
87  for (uint8_t group = 0; group < mNumGroups; group++) {
88  uint8_t pin = mGroupPins[group];
89  T_GPIOI::pinMode(pin, INPUT);
90  }
91  }
92 
93  void draw(uint8_t group, uint8_t elementPattern) const {
94  if (group != mPrevGroup) {
95  disableGroup(mPrevGroup);
96  }
97 
98  drawElements(elementPattern);
99  enableGroup(group);
100  mPrevGroup = group;
101  }
102 
103  void enableGroup(uint8_t group) const {
104  writeGroupPin(group, 0x1);
105  mPrevGroup = group;
106  }
107 
108  void disableGroup(uint8_t group) const {
109  writeGroupPin(group, 0x0);
110  mPrevGroup = group;
111  }
112 
113  void clear() const {
114  for (uint8_t group = 0; group < mNumGroups; group++) {
115  disableGroup(group);
116  }
117  drawElements(0);
118  }
119 
120  private:
121  friend class ::LedMatrixDirectTest_drawElements;
122 
124  void drawElements(uint8_t pattern) const {
125  for (uint8_t element = 0; element < mNumElements; element++) {
126  writeElementPin(element, pattern);
127  pattern >>= 1;
128  }
129  }
130 
132  void writeElementPin(uint8_t element, uint8_t output) const {
133  uint8_t elementPin = mElementPins[element];
134  T_GPIOI::digitalWrite(elementPin, (output ^ mElementXorMask) & 0x1);
135  }
136 
138  void writeGroupPin(uint8_t group, uint8_t output) const {
139  uint8_t groupPin = mGroupPins[group];
140  T_GPIOI::digitalWrite(groupPin, (output ^ mGroupXorMask) & 0x1);
141  }
142 
143  private:
144  const uint8_t* const mElementPins;
145  const uint8_t* const mGroupPins;
146  uint8_t const mNumElements;
147  uint8_t const mNumGroups;
148 
150  mutable uint8_t mPrevGroup = 0;
151 };
152 
153 } // ace_segment
154 
155 #endif
ace_segment::LedMatrixBase
Class that represents the abstraction of a particular LED display wiring, and knows how to turn off a...
Definition: LedMatrixBase.h:79
ace_segment::LedMatrixBase::LedMatrixBase
LedMatrixBase(uint8_t elementOnPattern, uint8_t groupOnPattern)
Definition: LedMatrixBase.h:86
ace_segment::LedMatrixDirect
An LedMatrixBase that whose group pins and element pins are wired directly to the MCU.
Definition: LedMatrixDirect.h:44