AceSegment  0.8.2
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:
56  uint8_t elementOnPattern,
57  uint8_t groupOnPattern,
58  uint8_t numElements,
59  const uint8_t* elementPins,
60  uint8_t numGroups,
61  const uint8_t* groupPins
62  ) :
63  LedMatrixBase(elementOnPattern, groupOnPattern),
64  mElementPins(elementPins),
65  mGroupPins(groupPins),
66  mNumElements(numElements),
67  mNumGroups(numGroups)
68  {}
69 
70  void begin() const {
71  // Set element pins to OUTPUT mode but set LEDs to OFF.
72  uint8_t output = (0x00 ^ mElementXorMask) & 0x1;
73  for (uint8_t element = 0; element < mNumElements; element++) {
74  uint8_t elementPin = mElementPins[element];
75  T_GPIOI::pinMode(elementPin, OUTPUT);
76  T_GPIOI::digitalWrite(elementPin, output);
77  }
78 
79  // Set group pins to OUTPUT mode but set LEDs to OFF.
80  output = (0x00 ^ mGroupXorMask) & 0x1;
81  for (uint8_t group = 0; group < mNumGroups; group++) {
82  uint8_t pin = mGroupPins[group];
83  T_GPIOI::pinMode(pin, OUTPUT);
84  T_GPIOI::digitalWrite(pin, output);
85  }
86  }
87 
88  void end() const {
89  // Set element pins to INPUT mode.
90  for (uint8_t element = 0; element < mNumElements; element++) {
91  uint8_t elementPin = mElementPins[element];
92  T_GPIOI::pinMode(elementPin, INPUT);
93  }
94 
95  // Set group pins to INPUT mode.
96  for (uint8_t group = 0; group < mNumGroups; group++) {
97  uint8_t pin = mGroupPins[group];
98  T_GPIOI::pinMode(pin, INPUT);
99  }
100  }
101 
102  void draw(uint8_t group, uint8_t elementPattern) const {
103  if (group != mPrevGroup) {
104  disableGroup(mPrevGroup);
105  }
106 
107  drawElements(elementPattern);
108  enableGroup(group);
109  mPrevGroup = group;
110  }
111 
112  void enableGroup(uint8_t group) const {
113  writeGroupPin(group, 0x1);
114  mPrevGroup = group;
115  }
116 
117  void disableGroup(uint8_t group) const {
118  writeGroupPin(group, 0x0);
119  mPrevGroup = group;
120  }
121 
122  void clear() const {
123  for (uint8_t group = 0; group < mNumGroups; group++) {
124  disableGroup(group);
125  }
126  drawElements(0);
127  }
128 
129  private:
130  friend class ::LedMatrixDirectTest_drawElements;
131 
133  void drawElements(uint8_t pattern) const {
134  for (uint8_t element = 0; element < mNumElements; element++) {
135  writeElementPin(element, pattern);
136  pattern >>= 1;
137  }
138  }
139 
141  void writeElementPin(uint8_t element, uint8_t output) const {
142  uint8_t elementPin = mElementPins[element];
143  T_GPIOI::digitalWrite(elementPin, (output ^ mElementXorMask) & 0x1);
144  }
145 
147  void writeGroupPin(uint8_t group, uint8_t output) const {
148  uint8_t groupPin = mGroupPins[group];
149  T_GPIOI::digitalWrite(groupPin, (output ^ mGroupXorMask) & 0x1);
150  }
151 
152  private:
153  const uint8_t* const mElementPins;
154  const uint8_t* const mGroupPins;
155  uint8_t const mNumElements;
156  uint8_t const mNumGroups;
157 
159  mutable uint8_t mPrevGroup = 0;
160 };
161 
162 } // ace_segment
163 
164 #endif
ace_segment::LedMatrixDirect::LedMatrixDirect
LedMatrixDirect(uint8_t elementOnPattern, uint8_t groupOnPattern, uint8_t numElements, const uint8_t *elementPins, uint8_t numGroups, const uint8_t *groupPins)
Constructor.
Definition: LedMatrixDirect.h:55
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::LedMatrixDirect
An LedMatrixBase that whose group pins and element pins are wired directly to the MCU.
Definition: LedMatrixDirect.h:44