AceSegment  0.12.0
A library for rendering seven segment LED displays using the TM1637, TM1638, MAX7219, HT16K33, or 74HC595 controller chips
Ht16k33Module.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_HT16K33_MODULE_H
26 #define ACE_SEGMENT_HT16K33_MODULE_H
27 
28 #include <stdint.h>
29 #include <string.h> // memset()
30 #include "../LedModule.h"
31 
32 class Ht16k33ModuleTest_patternForChipPos_colonDisabled;
33 class Ht16k33ModuleTest_patternForChipPos_colonEnabled;
34 
35 namespace ace_segment {
36 
65 template <typename T_WIREI, uint8_t T_DIGITS>
66 class Ht16k33Module : public LedModule {
67  public:
77  explicit Ht16k33Module(
78  T_WIREI& wireInterface,
79  uint8_t addr,
80  bool enableColon = false
81  ) :
82  LedModule(mPatterns, T_DIGITS),
83  mWireInterface(wireInterface),
84  mAddr(addr),
85  mEnableColon(enableColon)
86  {}
87 
88  //-----------------------------------------------------------------------
89  // Initialization, termination, and configuration.
90  //-----------------------------------------------------------------------
91 
92  void begin() {
94 
95  memset(mPatterns, 0, T_DIGITS);
96  writeCommand(kSystemOn);
97  writeCommand(kDisplayOn);
98  }
99 
100  void end() {
101  writeCommand(kDisplayOff);
102  writeCommand(kSystemOff);
103 
104  LedModule::end();
105  }
106 
116  void enableColon(bool enable) {
117  mEnableColon = enable;
118  }
119 
120  //-----------------------------------------------------------------------
121  // Methods related to rendering.
122  //-----------------------------------------------------------------------
123 
125  bool isFlushRequired() const {
126  return isAnyDigitDirty() || isBrightnessDirty();
127  }
128 
151  void flush() {
152  // Write digits.
153  mWireInterface.beginTransmission(mAddr);
154  mWireInterface.write(0x00); // start at position 0
155  // Loop over the 5 physical digit lines of this module.
156  for (uint8_t chipPos = 0; chipPos < T_DIGITS + 1; ++chipPos) {
157  uint8_t pattern = patternForChipPos(chipPos, mPatterns, mEnableColon);
158  mWireInterface.write(pattern); // ROW0-ROW7
159  mWireInterface.write(0); // ROW8-ROW15 unused
160  }
161  mWireInterface.endTransmission(false); // HT16K33 supports repeated START
162 
163  // Write brightness.
164  writeCommand(getBrightness() | kBrightness);
165 
168  }
169 
170  private:
171  friend class ::Ht16k33ModuleTest_patternForChipPos_colonDisabled;
172  friend class ::Ht16k33ModuleTest_patternForChipPos_colonEnabled;
173 
175  void writeCommand(uint8_t command) {
176  mWireInterface.beginTransmission(mAddr);
177  mWireInterface.write(command);
178  mWireInterface.endTransmission();
179  }
180 
185  static uint8_t patternForChipPos(
186  uint8_t chipPos, const uint8_t patterns[], bool enableColon) {
187  switch (chipPos) {
188  case 0:
189  return patterns[0];
190  case 1: {
191  uint8_t pattern = patterns[1];
192  return (enableColon) ? pattern & 0x7F : pattern;
193  }
194  case 2: {
195  if (enableColon) {
196  // Connect the colon bit 7 on logical digit 1 (that was stripped
197  // above) and send to the colon segment at physical digit COM2. It
198  // looks like on the 4-digit HT16K33 modules that I got, the colon
199  // is connected to ROW1 (i.e. 0x02) instead of ROW7 (i.e. 0x80) that
200  // I would have expected.
201  bool hasColon = patterns[1] & 0x80;
202  return hasColon ? 0x02 : 0x00;
203  } else {
204  return 0;
205  }
206  }
207  case 3:
208  return patterns[2];
209  case 4:
210  return patterns[3];
211  default:
212  return 0;
213  }
214  }
215 
216  private:
217  static uint8_t const kSystemOff = 0x20;
218  static uint8_t const kSystemOn = 0x21;
219  static uint8_t const kDisplayOff = 0x80;
220  static uint8_t const kDisplayOn = 0x81;
221  static uint8_t const kBrightness = 0xE0;
222 
227  T_WIREI mWireInterface;
228 
230  uint8_t const mAddr;
231 
233  uint8_t mPatterns[T_DIGITS];
234 
236  bool mEnableColon;
237 };
238 
239 }
240 
241 #endif
ace_segment::LedModule::clearDigitsDirty
void clearDigitsDirty()
Clear dirty bits of all digits.
Definition: LedModule.h:126
ace_segment::Ht16k33Module::isFlushRequired
bool isFlushRequired() const
Return true if flushing required.
Definition: Ht16k33Module.h:125
ace_segment::LedModule
General interface that represents a generic seven-segment LED module with multiple digits.
Definition: LedModule.h:44
ace_segment::Ht16k33Module
An implementation of LedModule using the HT16K33 chip.
Definition: Ht16k33Module.h:66
ace_segment::LedModule::isAnyDigitDirty
bool isAnyDigitDirty() const
Return true if any digits are dirty.
Definition: LedModule.h:131
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::end
void end()
Subclasses should call this from its own end().
Definition: LedModule.h:108
ace_segment::Ht16k33Module::Ht16k33Module
Ht16k33Module(T_WIREI &wireInterface, uint8_t addr, bool enableColon=false)
Constructor.
Definition: Ht16k33Module.h:77
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::Ht16k33Module::enableColon
void enableColon(bool enable)
Set true to enable the colon segment on the module, which replaces the decimal point on digit 1 (seco...
Definition: Ht16k33Module.h:116
ace_segment::Ht16k33Module::flush
void flush()
Send segment patterns of all digits.
Definition: Ht16k33Module.h:151