AceSegment  0.7.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 
64 template <typename T_WIREI, uint8_t T_DIGITS>
65 class Ht16k33Module : public LedModule {
66  public:
77  explicit Ht16k33Module(T_WIREI& wire, bool enableColon = false) :
78  LedModule(T_DIGITS),
79  mWire(wire),
80  mBrightness(0),
81  mEnableColon(enableColon)
82  {}
83 
84  //-----------------------------------------------------------------------
85  // Initialization, termination, and configuration.
86  //-----------------------------------------------------------------------
87 
88  void begin() {
89  memset(mPatterns, 0, T_DIGITS);
90  writeCommand(kSystemOn);
91  writeCommand(kDisplayOn);
92  }
93 
94  void end() {
95  writeCommand(kDisplayOff);
96  writeCommand(kSystemOff);
97  }
98 
108  void enableColon(bool enable) {
109  mEnableColon = enable;
110  }
111 
112  //-----------------------------------------------------------------------
113  // Implement the LedModule interface
114  //-----------------------------------------------------------------------
115 
117  uint8_t getNumDigits() const { return T_DIGITS; }
118 
119  void setPatternAt(uint8_t pos, uint8_t pattern) override {
120  mPatterns[pos] = pattern;
121  }
122 
123  uint8_t getPatternAt(uint8_t pos) override {
124  return mPatterns[pos];
125  }
126 
127  void setBrightness(uint8_t brightness) override {
128  mBrightness = brightness & 0xF;
129  }
130 
131  //-----------------------------------------------------------------------
132  // Methods related to rendering.
133  //-----------------------------------------------------------------------
134 
154  void flush() {
155  // Write digits.
156  mWire.beginTransmission();
157  mWire.write(0x00); // start at position 0
158  // Loop over the 5 physical digit lines of this module.
159  for (uint8_t chipPos = 0; chipPos < T_DIGITS + 1; ++chipPos) {
160  uint8_t pattern = patternForChipPos(chipPos, mPatterns, mEnableColon);
161  mWire.write(pattern); // ROW0-ROW7
162  mWire.write(0); // ROW8-ROW15 unused
163  }
164  mWire.endTransmission();
165 
166  // Write brightness.
167  writeCommand(mBrightness | kBrightness);
168  }
169 
170  private:
171  friend class ::Ht16k33ModuleTest_patternForChipPos_colonDisabled;
172  friend class ::Ht16k33ModuleTest_patternForChipPos_colonEnabled;
173 
175  void writeCommand(uint8_t command) {
176  mWire.beginTransmission();
177  mWire.write(command);
178  mWire.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 
224  T_WIREI& mWire;
225 
227  uint8_t mPatterns[T_DIGITS];
228 
230  uint8_t mBrightness;
231 
233  bool mEnableColon;
234 };
235 
236 }
237 
238 #endif
ace_segment::LedModule
General interface that represents a generic seven-segment LED module with multiple digits.
Definition: LedModule.h:44
ace_segment::Ht16k33Module::Ht16k33Module
Ht16k33Module(T_WIREI &wire, bool enableColon=false)
Constructor.
Definition: Ht16k33Module.h:77
ace_segment::Ht16k33Module
An implementation of LedModule using the HT16K33 chip.
Definition: Ht16k33Module.h:65
ace_segment::Ht16k33Module::getPatternAt
uint8_t getPatternAt(uint8_t pos) override
Get the led digit pattern at position pos.
Definition: Ht16k33Module.h:123
ace_segment::Ht16k33Module::setBrightness
void setBrightness(uint8_t brightness) override
Set global brightness of all digits.
Definition: Ht16k33Module.h:127
ace_segment::Ht16k33Module::setPatternAt
void setPatternAt(uint8_t pos, uint8_t pattern) override
Set the led digit pattern at position pos.
Definition: Ht16k33Module.h:119
ace_segment::Ht16k33Module::getNumDigits
uint8_t getNumDigits() const
Return the number of digits supported by this display instance.
Definition: Ht16k33Module.h:117
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:108
ace_segment::Ht16k33Module::flush
void flush()
Send segment patterns of all digits.
Definition: Ht16k33Module.h:154