AceSegment  0.8.2
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(mPatterns, T_DIGITS),
120  mSpiInterface(spiInterface),
121  mRemapArray(remapArray)
122  {}
123 
124  //-----------------------------------------------------------------------
125  // Initialization and termination.
126  //-----------------------------------------------------------------------
127 
128  void begin() {
130 
131  memset(mPatterns, 0, T_DIGITS);
132 
133  // Set to a non-zero value to avoid using uninitialized value.
134  setBrightness(1);
135 
136  // **WARNING**: Do NOT set this smaller than 3, or you may damage the
137  // controller chip due to excessive current. See the MAX7219 datasheet for
138  // details.
139  mSpiInterface.send16(kRegisterScanLimit, 7); // scan all digits
140 
141  mSpiInterface.send16(kRegisterDecodeMode, 0); // no BCD decoding
142  mSpiInterface.send16(kRegisterShutdown, 0x1); // turn on
143  }
144 
145  void end() {
146  mSpiInterface.send16(kRegisterShutdown, 0x0); // turn off
147 
148  LedModule::end();
149  }
150 
151  //-----------------------------------------------------------------------
152  // Methods related to rendering.
153  //-----------------------------------------------------------------------
154 
156  bool isFlushRequired() const {
157  return isAnyDigitDirty() || isBrightnessDirty();
158  }
159 
171  void flush() {
172  for (uint8_t chipPos = 0; chipPos < T_DIGITS; ++chipPos) {
173  // Remap the logical position used by the controller to the actual
174  // position. For example, if the controller digit 0 appears at physical
175  // digit 2, we need to display the segment pattern given by logical
176  // position 2 when sending the byte to controller digit 0.
177  uint8_t physicalPos = remapLogicalToPhysical(chipPos);
178  uint8_t convertedPattern = internal::convertPatternMax7219(
179  mPatterns[physicalPos]);
180  mSpiInterface.send16(chipPos + 1, convertedPattern);
181  }
182 
183  mSpiInterface.send16(kRegisterIntensity, getBrightness());
184 
187  }
188 
189  private:
191  uint8_t remapLogicalToPhysical(uint8_t pos) const {
192  return mRemapArray ? mRemapArray[pos] : pos;
193  }
194 
195  private:
196  static uint8_t const kRegisterNoop = 0x00;
197  static uint8_t const kRegisterDigit0 = 0x01;
198  static uint8_t const kRegisterDigit1 = 0x02;
199  static uint8_t const kRegisterDigit2 = 0x03;
200  static uint8_t const kRegisterDigit3 = 0x04;
201  static uint8_t const kRegisterDigit4 = 0x05;
202  static uint8_t const kRegisterDigit5 = 0x06;
203  static uint8_t const kRegisterDigit6 = 0x07;
204  static uint8_t const kRegisterDigit7 = 0x08;
205  static uint8_t const kRegisterDecodeMode = 0x09;
206  static uint8_t const kRegisterIntensity = 0x0A;
207  static uint8_t const kRegisterScanLimit = 0x0B;
208  static uint8_t const kRegisterShutdown = 0x0C;
209  static uint8_t const kRegisterDisplayTest = 0x0F;
210 
215  const T_SPII mSpiInterface;
216 
218  const uint8_t* const mRemapArray;
219 
221  uint8_t mPatterns[T_DIGITS];
222 };
223 
224 }
225 
226 #endif
ace_segment::LedModule::clearDigitsDirty
void clearDigitsDirty()
Clear dirty bits of all digits.
Definition: LedModule.h:126
ace_segment::LedModule
General interface that represents a generic seven-segment LED module with multiple digits.
Definition: LedModule.h:44
ace_segment::LedModule::isAnyDigitDirty
bool isAnyDigitDirty() const
Return true if any digits are dirty.
Definition: LedModule.h:131
ace_segment::Max7219Module::flush
void flush()
Send segment patterns of all digits.
Definition: Max7219Module.h:171
ace_segment::LedModule::begin
void begin()
Subclasses should call this from its own begin().
Definition: LedModule.h:93
ace_segment::LedModule::clearBrightnessDirty
void clearBrightnessDirty()
Clear the dirty bit for brightness.
Definition: LedModule.h:146
ace_segment::LedModule::setBrightness
void setBrightness(uint8_t brightness)
Set global brightness of all digits.
Definition: LedModule.h:81
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::LedModule::end
void end()
Subclasses should call this from its own end().
Definition: LedModule.h:108
ace_segment::LedModule::getBrightness
uint8_t getBrightness() const
Get the current brightness.
Definition: LedModule.h:87
ace_segment::LedModule::isBrightnessDirty
bool isBrightnessDirty() const
Check if the brightness level is dirty.
Definition: LedModule.h:136
ace_segment::Max7219Module::isFlushRequired
bool isFlushRequired() const
Return true if flushing required.
Definition: Max7219Module.h:156