AceSegment  0.7.0
A framework for rendering seven segment LED displays using the TM1637, MAX7219, HT16K33, or 74HC595 controller chips
Max7219Module.h
1 /*
2 MIT License
3 
4 Copyright (c) 2021 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_MAX7219_MODULE_H
26 #define ACE_SEGMENT_MAX7219_MODULE_H
27 
28 #include <stdint.h>
29 #include <string.h> // memset()
30 #include "../LedModule.h"
31 
32 namespace ace_segment {
33 
34 namespace internal {
35 
54 inline uint8_t convertPatternMax7219(uint8_t pattern) {
55  // Transfer the decimal point on bit 8.
56  // Amazingly, the AVR compiler is able to implement this using bit shifting
57  // operations so that no branching is performed.
58  uint8_t result = (pattern & 0x80) ? 0x1 : 0x0;
59 
60  // Reverse the remaining 7 bits.
61  for (uint8_t i = 0; i < 7; ++i) {
62  result <<= 1;
63  if (pattern & 0x1) {
64  result |= 0x1;
65  }
66 
67  // Division by 2 produces more efficient machine code on the AVR compiler (a
68  // single `lsr` instruction) compared to the right-shift-operator. For some
69  // reason, the right-shift operator (`pattern >>= 1`) produces code that
70  // uses 8 extra bytes of extraneous instructions, casting the uint8_t into a
71  // 16-bit word, setting the high byte to 0, then doing a `asr` and a `ror`,
72  // then ignoring the high byte. For 32-bit processors, they have so much
73  // flash memory that we don't need to optimize their code. But I suspect
74  // that their compilers optimize the integer division by 2 just as well as
75  // the AVR compiler.
76  pattern /= 2;
77  }
78 
79  return result;
80 }
81 
82 } // internal
83 
96 extern const uint8_t kDigitRemapArray8Max7219[8];
97 
105 template <typename T_SPII, uint8_t T_DIGITS>
106 class Max7219Module : public LedModule {
107  public:
114  explicit Max7219Module(
115  const T_SPII& spiInterface,
116  const uint8_t* remapArray = nullptr
117  ) :
118  LedModule(T_DIGITS),
119  mSpiInterface(spiInterface),
120  mRemapArray(remapArray),
121  mBrightness(1) // set to 1 to avoid using uninitialized value
122  {}
123 
124  //-----------------------------------------------------------------------
125  // Initialization and termination.
126  //-----------------------------------------------------------------------
127 
128  void begin() {
129  memset(mPatterns, 0, T_DIGITS);
130 
131  // **WARNING**: Do NOT set this smaller than 3, or you may damage the
132  // controller chip due to excessive current. See the MAX7219 datasheet for
133  // details.
134  mSpiInterface.send16(kRegisterScanLimit, 7); // scan all digits
135 
136  mSpiInterface.send16(kRegisterDecodeMode, 0); // no BCD decoding
137  mSpiInterface.send16(kRegisterShutdown, 0x1); // turn on
138  }
139 
140  void end() {
141  mSpiInterface.send16(kRegisterShutdown, 0x0); // turn off
142  }
143 
144  //-----------------------------------------------------------------------
145  // Implement the LedModule interface
146  //-----------------------------------------------------------------------
147 
149  uint8_t getNumDigits() const { return T_DIGITS; }
150 
151  void setPatternAt(uint8_t pos, uint8_t pattern) override {
152  mPatterns[pos] = pattern;
153  }
154 
155  uint8_t getPatternAt(uint8_t pos) override {
156  return mPatterns[pos];
157  }
158 
159  void setBrightness(uint8_t brightness) override {
160  mBrightness = brightness & 0xF;
161  }
162 
163  //-----------------------------------------------------------------------
164  // Methods related to rendering.
165  //-----------------------------------------------------------------------
166 
175  void flush() {
176  for (uint8_t chipPos = 0; chipPos < T_DIGITS; ++chipPos) {
177  // Remap the logical position used by the controller to the actual
178  // position. For example, if the controller digit 0 appears at physical
179  // digit 2, we need to display the segment pattern given by logical
180  // position 2 when sending the byte to controller digit 0.
181  uint8_t physicalPos = remapLogicalToPhysical(chipPos);
182  uint8_t convertedPattern = internal::convertPatternMax7219(
183  mPatterns[physicalPos]);
184  mSpiInterface.send16(chipPos + 1, convertedPattern);
185  }
186 
187  mSpiInterface.send16(kRegisterIntensity, mBrightness);
188  }
189 
190  private:
192  uint8_t remapLogicalToPhysical(uint8_t pos) const {
193  return mRemapArray ? mRemapArray[pos] : pos;
194  }
195 
196  private:
197  static uint8_t const kRegisterNoop = 0x00;
198  static uint8_t const kRegisterDigit0 = 0x01;
199  static uint8_t const kRegisterDigit1 = 0x02;
200  static uint8_t const kRegisterDigit2 = 0x03;
201  static uint8_t const kRegisterDigit3 = 0x04;
202  static uint8_t const kRegisterDigit4 = 0x05;
203  static uint8_t const kRegisterDigit5 = 0x06;
204  static uint8_t const kRegisterDigit6 = 0x07;
205  static uint8_t const kRegisterDigit7 = 0x08;
206  static uint8_t const kRegisterDecodeMode = 0x09;
207  static uint8_t const kRegisterIntensity = 0x0A;
208  static uint8_t const kRegisterScanLimit = 0x0B;
209  static uint8_t const kRegisterShutdown = 0x0C;
210  static uint8_t const kRegisterDisplayTest = 0x0F;
211 
213  const T_SPII& mSpiInterface;
214 
216  const uint8_t* const mRemapArray;
217 
219  uint8_t mPatterns[T_DIGITS];
220 
222  uint8_t mBrightness;
223 };
224 
225 }
226 
227 #endif
ace_segment::LedModule
General interface that represents a generic seven-segment LED module with multiple digits.
Definition: LedModule.h:44
ace_segment::Max7219Module::getNumDigits
uint8_t getNumDigits() const
Return the number of digits supported by this display instance.
Definition: Max7219Module.h:149
ace_segment::Max7219Module::flush
void flush()
Send segment patterns of all digits.
Definition: Max7219Module.h:175
ace_segment::Max7219Module::setBrightness
void setBrightness(uint8_t brightness) override
Set global brightness of all digits.
Definition: Max7219Module.h:159
ace_segment::Max7219Module
An implementation of LedModule using the MAX7219 chip.
Definition: Max7219Module.h:106
ace_segment::Max7219Module::Max7219Module
Max7219Module(const T_SPII &spiInterface, const uint8_t *remapArray=nullptr)
Constructor.
Definition: Max7219Module.h:114
ace_segment::Max7219Module::setPatternAt
void setPatternAt(uint8_t pos, uint8_t pattern) override
Set the led digit pattern at position pos.
Definition: Max7219Module.h:151
ace_segment::Max7219Module::getPatternAt
uint8_t getPatternAt(uint8_t pos) override
Get the led digit pattern at position pos.
Definition: Max7219Module.h:155