AceSegment  0.7.0
A framework for rendering seven segment LED displays using the TM1637, MAX7219, HT16K33, or 74HC595 controller chips
Tm1637Module.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_TM1637_MODULE_H
26 #define ACE_SEGMENT_TM1637_MODULE_H
27 
28 #include <Arduino.h>
29 #include <AceCommon.h> // incrementMod()
30 #include "../LedModule.h"
31 
32 class Tm1637ModuleTest_flushIncremental;
33 class Tm1637ModuleTest_flush;
34 
35 namespace ace_segment {
36 
47 static const uint16_t kDefaultTm1637DelayMicros = 100;
48 
62 extern const uint8_t kDigitRemapArray6Tm1637[6];
63 
64 namespace internal {
65 
74 constexpr uint8_t initialDirtyBits(uint8_t numBits) {
75  return ((uint16_t) 0x1 << numBits) - 1;
76 }
77 
78 } // namespace internal
79 
90 template <typename T_TMII, uint8_t T_DIGITS>
91 class Tm1637Module : public LedModule {
92  public:
93 
102  explicit Tm1637Module(
103  const T_TMII& tmiInterface,
104  const uint8_t* remapArray = nullptr
105  ) :
106  LedModule(T_DIGITS),
107  mTmiInterface(tmiInterface),
108  mRemapArray(remapArray)
109  {}
110 
111  //-----------------------------------------------------------------------
112  // Initialization and termination.
113  //-----------------------------------------------------------------------
114 
124  void begin() {
125  memset(mPatterns, 0, T_DIGITS);
126  mBrightness = kBrightnessCmd | kBrightnessLevelOn | 0x7;
127 
128  // Initially, we want to set all the dirty bits for digits and brightness,
129  // so that they are sent to the LED module upon the first flush() or
130  // flushIncremental(). The number of dirty bits required is `T_DIGITS +
131  // 1` because we use an extra bit for the brightness.
132  mIsDirty = internal::initialDirtyBits(T_DIGITS + 1);
133 
134  mFlushStage = 0;
135  }
136 
138  void end() {}
139 
140  //-----------------------------------------------------------------------
141  // Implement the LedModule interface
142  //-----------------------------------------------------------------------
143 
145  uint8_t getNumDigits() const { return T_DIGITS; }
146 
147  void setPatternAt(uint8_t pos, uint8_t pattern) override {
148  mPatterns[pos] = pattern;
149  setDirtyBit(pos);
150  }
151 
152  uint8_t getPatternAt(uint8_t pos) override {
153  return mPatterns[pos];
154  }
155 
156  void setBrightness(uint8_t brightness) override {
157  mBrightness = (mBrightness & ~0x7) | (brightness & 0x7);
158  setDirtyBit(kBrightnessDirtyBit);
159  }
160 
161  //-----------------------------------------------------------------------
162  // Additional brightness control supported by the TM1637 chip.
163  //-----------------------------------------------------------------------
164 
169  void setDisplayOn(bool on = true) {
170  if (on) {
171  mBrightness |= kBrightnessLevelOn;
172  } else {
173  mBrightness &= ~kBrightnessLevelOn;
174  }
175  setDirtyBit(kBrightnessDirtyBit);
176  }
177 
178  //-----------------------------------------------------------------------
179  // Methods related to rendering.
180  //-----------------------------------------------------------------------
181 
187  void flush() {
188  if (! mIsDirty) return;
189 
190  // Update the brightness first
191  mTmiInterface.startCondition();
192  mTmiInterface.sendByte(mBrightness);
193  mTmiInterface.stopCondition();
194 
195  // Update the digits using auto incrementing mode.
196  mTmiInterface.startCondition();
197  mTmiInterface.sendByte(kDataCmdAutoAddress);
198  mTmiInterface.stopCondition();
199 
200  mTmiInterface.startCondition();
201  mTmiInterface.sendByte(kAddressCmd);
202  for (uint8_t chipPos = 0; chipPos < T_DIGITS; ++chipPos) {
203  // Remap the logical position used by the controller to the actual
204  // position. For example, if the controller digit 0 appears at physical
205  // digit 2, we need to display the segment pattern given by logical
206  // position 2 when sending the byte to controller digit 0.
207  uint8_t physicalPos = remapLogicalToPhysical(chipPos);
208  uint8_t effectivePattern = mPatterns[physicalPos];
209  mTmiInterface.sendByte(effectivePattern);
210  }
211  mTmiInterface.stopCondition();
212 
213  mIsDirty = 0x0;
214  }
215 
250  if (mFlushStage == T_DIGITS) {
251  // Update brightness.
252  if (isDirtyBit(T_DIGITS)) {
253  mTmiInterface.startCondition();
254  mTmiInterface.sendByte(mBrightness);
255  mTmiInterface.stopCondition();
256  clearDirtyBit(T_DIGITS);
257  }
258  } else {
259  // Remap the logical position used by the controller to the actual
260  // position. For example, if the controller digit 0 appears at physical
261  // digit 2, we need to display the segment pattern given by logical
262  // position 2 when sending the byte to controller digit 0.
263  const uint8_t chipPos = mFlushStage;
264  const uint8_t physicalPos = remapLogicalToPhysical(chipPos);
265  if (isDirtyBit(physicalPos)) {
266  // Update changed digit.
267  mTmiInterface.startCondition();
268  mTmiInterface.sendByte(kDataCmdFixedAddress);
269  mTmiInterface.stopCondition();
270 
271  mTmiInterface.startCondition();
272  mTmiInterface.sendByte(kAddressCmd | chipPos);
273  mTmiInterface.sendByte(mPatterns[physicalPos]);
274  mTmiInterface.stopCondition();
275  clearDirtyBit(physicalPos);
276  }
277  }
278 
279  // An extra dirty bit is used for the brightness so use `T_DIGITS + 1`.
280  ace_common::incrementMod(mFlushStage, (uint8_t) (T_DIGITS + 1));
281  }
282 
283  private:
284  void setDirtyBit(uint8_t bit) {
285  mIsDirty |= (0x1 << bit);
286  }
287 
288  void clearDirtyBit(uint8_t bit) {
289  mIsDirty &= ~(0x1 << bit);
290  }
291 
292  bool isDirtyBit(uint8_t bit) const {
293  return mIsDirty & (0x1 << bit);
294  }
295 
297  uint8_t remapLogicalToPhysical(uint8_t pos) const {
298  return mRemapArray ? mRemapArray[pos] : pos;
299  }
300 
301  private:
302  // Give access to mIsDirty and mFlushStage
303  friend class ::Tm1637ModuleTest_flushIncremental;
304  friend class ::Tm1637ModuleTest_flush;
305 
306  static uint8_t const kDataCmdWriteDisplay = 0b01000000;
307  static uint8_t const kDataCmdReadKeys = 0b01000010;
308  static uint8_t const kDataCmdAutoAddress = 0b01000000;
309  static uint8_t const kDataCmdFixedAddress = 0b01000100;
310  static uint8_t const kAddressCmd = 0b11000000;
311  static uint8_t const kBrightnessCmd = 0b10000000;
312  static uint8_t const kBrightnessLevelOn = 0b00001000;
313 
314  // Use the bit at position 'T_DIGITS' as the dirty bit for brightness.
315  // A TM1637 can have a maximum of 6 T_DIGITS, so we are safe.
316  static uint8_t const kBrightnessDirtyBit = T_DIGITS;
317 
318  // The ordering of these fields is partially determined to save memory on
319  // 32-bit processors.
320  const T_TMII& mTmiInterface;
321  const uint8_t* const mRemapArray;
322  uint8_t mPatterns[T_DIGITS]; // maps to dirty bits [0, T_DIGITS-1]
323  uint8_t mBrightness; // maps to dirty bit at T_DIGITS
324  uint8_t mIsDirty; // bit array
325  uint8_t mFlushStage; // [0, T_DIGITS], with T_DIGITS for brightness update
326 };
327 
328 } // ace_segment
329 
330 #endif
ace_segment::LedModule
General interface that represents a generic seven-segment LED module with multiple digits.
Definition: LedModule.h:44
ace_segment::Tm1637Module::flushIncremental
void flushIncremental()
Update only a single digit or the brightness.
Definition: Tm1637Module.h:249
ace_segment::Tm1637Module::setDisplayOn
void setDisplayOn(bool on=true)
Turn off the entire display.
Definition: Tm1637Module.h:169
ace_segment::Tm1637Module::setPatternAt
void setPatternAt(uint8_t pos, uint8_t pattern) override
Set the led digit pattern at position pos.
Definition: Tm1637Module.h:147
ace_segment::Tm1637Module::begin
void begin()
Initialize the module.
Definition: Tm1637Module.h:124
ace_segment::Tm1637Module::end
void end()
Signal end of usage.
Definition: Tm1637Module.h:138
ace_segment::Tm1637Module::getPatternAt
uint8_t getPatternAt(uint8_t pos) override
Get the led digit pattern at position pos.
Definition: Tm1637Module.h:152
ace_segment::Tm1637Module::flush
void flush()
Send segment patterns of all digits plus the brightness to the display, if any of the digits or brigh...
Definition: Tm1637Module.h:187
ace_segment::Tm1637Module::setBrightness
void setBrightness(uint8_t brightness) override
Set global brightness of all digits.
Definition: Tm1637Module.h:156
ace_segment::Tm1637Module
An implementation of LedModule using the TM1637 chip.
Definition: Tm1637Module.h:91
ace_segment::Tm1637Module::getNumDigits
uint8_t getNumDigits() const
Return the number of digits supported by this display instance.
Definition: Tm1637Module.h:145
ace_segment::Tm1637Module::Tm1637Module
Tm1637Module(const T_TMII &tmiInterface, const uint8_t *remapArray=nullptr)
Constructor.
Definition: Tm1637Module.h:102