AceSegment  0.8.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 
106 template <typename T_SPII, uint8_t T_DIGITS>
107 class Max7219Module : public LedModule {
108  public:
115  explicit Max7219Module(
116  const T_SPII& spiInterface,
117  const uint8_t* remapArray = nullptr
118  ) :
119  LedModule(T_DIGITS),
120  mSpiInterface(spiInterface),
121  mRemapArray(remapArray),
122  mBrightness(1) // set to 1 to avoid using uninitialized value
123  {}
124 
125  //-----------------------------------------------------------------------
126  // Initialization and termination.
127  //-----------------------------------------------------------------------
128 
129  void begin() {
130  memset(mPatterns, 0, T_DIGITS);
131 
132  // **WARNING**: Do NOT set this smaller than 3, or you may damage the
133  // controller chip due to excessive current. See the MAX7219 datasheet for
134  // details.
135  mSpiInterface.send16(kRegisterScanLimit, 7); // scan all digits
136 
137  mSpiInterface.send16(kRegisterDecodeMode, 0); // no BCD decoding
138  mSpiInterface.send16(kRegisterShutdown, 0x1); // turn on
139  }
140 
141  void end() {
142  mSpiInterface.send16(kRegisterShutdown, 0x0); // turn off
143  }
144 
145  //-----------------------------------------------------------------------
146  // Implement the LedModule interface
147  //-----------------------------------------------------------------------
148 
150  uint8_t getNumDigits() const { return T_DIGITS; }
151 
152  void setPatternAt(uint8_t pos, uint8_t pattern) override {
153  mPatterns[pos] = pattern;
154  }
155 
156  uint8_t getPatternAt(uint8_t pos) override {
157  return mPatterns[pos];
158  }
159 
160  void setBrightness(uint8_t brightness) override {
161  mBrightness = brightness & 0xF;
162  }
163 
164  //-----------------------------------------------------------------------
165  // Methods related to rendering.
166  //-----------------------------------------------------------------------
167 
176  void flush() {
177  for (uint8_t chipPos = 0; chipPos < T_DIGITS; ++chipPos) {
178  // Remap the logical position used by the controller to the actual
179  // position. For example, if the controller digit 0 appears at physical
180  // digit 2, we need to display the segment pattern given by logical
181  // position 2 when sending the byte to controller digit 0.
182  uint8_t physicalPos = remapLogicalToPhysical(chipPos);
183  uint8_t convertedPattern = internal::convertPatternMax7219(
184  mPatterns[physicalPos]);
185  mSpiInterface.send16(chipPos + 1, convertedPattern);
186  }
187 
188  mSpiInterface.send16(kRegisterIntensity, mBrightness);
189  }
190 
191  private:
193  uint8_t remapLogicalToPhysical(uint8_t pos) const {
194  return mRemapArray ? mRemapArray[pos] : pos;
195  }
196 
197  private:
198  static uint8_t const kRegisterNoop = 0x00;
199  static uint8_t const kRegisterDigit0 = 0x01;
200  static uint8_t const kRegisterDigit1 = 0x02;
201  static uint8_t const kRegisterDigit2 = 0x03;
202  static uint8_t const kRegisterDigit3 = 0x04;
203  static uint8_t const kRegisterDigit4 = 0x05;
204  static uint8_t const kRegisterDigit5 = 0x06;
205  static uint8_t const kRegisterDigit6 = 0x07;
206  static uint8_t const kRegisterDigit7 = 0x08;
207  static uint8_t const kRegisterDecodeMode = 0x09;
208  static uint8_t const kRegisterIntensity = 0x0A;
209  static uint8_t const kRegisterScanLimit = 0x0B;
210  static uint8_t const kRegisterShutdown = 0x0C;
211  static uint8_t const kRegisterDisplayTest = 0x0F;
212 
217  const T_SPII mSpiInterface;
218 
220  const uint8_t* const mRemapArray;
221 
223  uint8_t mPatterns[T_DIGITS];
224 
226  uint8_t mBrightness;
227 };
228 
229 }
230 
231 #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:150
ace_segment::Max7219Module::flush
void flush()
Send segment patterns of all digits.
Definition: Max7219Module.h:176
ace_segment::Max7219Module::setBrightness
void setBrightness(uint8_t brightness) override
Set global brightness of all digits.
Definition: Max7219Module.h:160
ace_segment::Max7219Module
An implementation of LedModule using the MAX7219 chip.
Definition: Max7219Module.h:107
ace_segment::Max7219Module::Max7219Module
Max7219Module(const T_SPII &spiInterface, const uint8_t *remapArray=nullptr)
Constructor.
Definition: Max7219Module.h:115
ace_segment::Max7219Module::setPatternAt
void setPatternAt(uint8_t pos, uint8_t pattern) override
Set the led digit pattern at position pos.
Definition: Max7219Module.h:152
ace_segment::Max7219Module::getPatternAt
uint8_t getPatternAt(uint8_t pos) override
Get the led digit pattern at position pos.
Definition: Max7219Module.h:156