AceSegment  0.8.0
A framework for rendering seven segment LED displays using the TM1637, 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& wire,
79  uint8_t addr,
80  bool enableColon = false
81  ) :
82  LedModule(T_DIGITS),
83  mWire(wire),
84  mAddr(addr),
85  mBrightness(0),
86  mEnableColon(enableColon)
87  {}
88 
89  //-----------------------------------------------------------------------
90  // Initialization, termination, and configuration.
91  //-----------------------------------------------------------------------
92 
93  void begin() {
94  memset(mPatterns, 0, T_DIGITS);
95  writeCommand(kSystemOn);
96  writeCommand(kDisplayOn);
97  }
98 
99  void end() {
100  writeCommand(kDisplayOff);
101  writeCommand(kSystemOff);
102  }
103 
113  void enableColon(bool enable) {
114  mEnableColon = enable;
115  }
116 
117  //-----------------------------------------------------------------------
118  // Implement the LedModule interface
119  //-----------------------------------------------------------------------
120 
122  uint8_t getNumDigits() const { return T_DIGITS; }
123 
124  void setPatternAt(uint8_t pos, uint8_t pattern) override {
125  mPatterns[pos] = pattern;
126  }
127 
128  uint8_t getPatternAt(uint8_t pos) override {
129  return mPatterns[pos];
130  }
131 
132  void setBrightness(uint8_t brightness) override {
133  mBrightness = brightness & 0xF;
134  }
135 
136  //-----------------------------------------------------------------------
137  // Methods related to rendering.
138  //-----------------------------------------------------------------------
139 
159  void flush() {
160  // Write digits.
161  mWire.beginTransmission(mAddr);
162  mWire.write(0x00); // start at position 0
163  // Loop over the 5 physical digit lines of this module.
164  for (uint8_t chipPos = 0; chipPos < T_DIGITS + 1; ++chipPos) {
165  uint8_t pattern = patternForChipPos(chipPos, mPatterns, mEnableColon);
166  mWire.write(pattern); // ROW0-ROW7
167  mWire.write(0); // ROW8-ROW15 unused
168  }
169  mWire.endTransmission(false); // HT16K33 supports repeated START
170 
171  // Write brightness.
172  writeCommand(mBrightness | kBrightness);
173  }
174 
175  private:
176  friend class ::Ht16k33ModuleTest_patternForChipPos_colonDisabled;
177  friend class ::Ht16k33ModuleTest_patternForChipPos_colonEnabled;
178 
180  void writeCommand(uint8_t command) {
181  mWire.beginTransmission(mAddr);
182  mWire.write(command);
183  mWire.endTransmission();
184  }
185 
190  static uint8_t patternForChipPos(
191  uint8_t chipPos, const uint8_t patterns[], bool enableColon) {
192  switch (chipPos) {
193  case 0:
194  return patterns[0];
195  case 1: {
196  uint8_t pattern = patterns[1];
197  return (enableColon) ? pattern & 0x7F : pattern;
198  }
199  case 2: {
200  if (enableColon) {
201  // Connect the colon bit 7 on logical digit 1 (that was stripped
202  // above) and send to the colon segment at physical digit COM2. It
203  // looks like on the 4-digit HT16K33 modules that I got, the colon
204  // is connected to ROW1 (i.e. 0x02) instead of ROW7 (i.e. 0x80) that
205  // I would have expected.
206  bool hasColon = patterns[1] & 0x80;
207  return hasColon ? 0x02 : 0x00;
208  } else {
209  return 0;
210  }
211  }
212  case 3:
213  return patterns[2];
214  case 4:
215  return patterns[3];
216  default:
217  return 0;
218  }
219  }
220 
221  private:
222  static uint8_t const kSystemOff = 0x20;
223  static uint8_t const kSystemOn = 0x21;
224  static uint8_t const kDisplayOff = 0x80;
225  static uint8_t const kDisplayOn = 0x81;
226  static uint8_t const kBrightness = 0xE0;
227 
232  T_WIREI mWire;
233 
235  uint8_t const mAddr;
236 
238  uint8_t mPatterns[T_DIGITS];
239 
241  uint8_t mBrightness;
242 
244  bool mEnableColon;
245 };
246 
247 }
248 
249 #endif
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::Ht16k33Module::Ht16k33Module
Ht16k33Module(T_WIREI &wire, uint8_t addr, bool enableColon=false)
Constructor.
Definition: Ht16k33Module.h:77
ace_segment::Ht16k33Module::getPatternAt
uint8_t getPatternAt(uint8_t pos) override
Get the led digit pattern at position pos.
Definition: Ht16k33Module.h:128
ace_segment::Ht16k33Module::setBrightness
void setBrightness(uint8_t brightness) override
Set global brightness of all digits.
Definition: Ht16k33Module.h:132
ace_segment::Ht16k33Module::setPatternAt
void setPatternAt(uint8_t pos, uint8_t pattern) override
Set the led digit pattern at position pos.
Definition: Ht16k33Module.h:124
ace_segment::Ht16k33Module::getNumDigits
uint8_t getNumDigits() const
Return the number of digits supported by this display instance.
Definition: Ht16k33Module.h:122
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:113
ace_segment::Ht16k33Module::flush
void flush()
Send segment patterns of all digits.
Definition: Ht16k33Module.h:159