AceSegment  0.8.0
A framework for rendering seven segment LED displays using the TM1637, MAX7219, HT16K33, or 74HC595 controller chips
NumberWriter.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_NUMBER_WRITER_H
26 #define ACE_SEGMENT_NUMBER_WRITER_H
27 
28 #include <stdint.h>
29 #include "PatternWriter.h"
30 
31 namespace ace_segment {
32 
39 class NumberWriter {
40  public:
56  typedef uint8_t hexchar_t;
57 
59  static const uint8_t kNumHexChars = 18;
60 
62  static const hexchar_t kCharSpace = 0x10;
63 
65  static const hexchar_t kCharMinus = 0x11;
66 
69  mPatternWriter(ledModule)
70  {}
71 
73  LedModule& ledModule() { return mPatternWriter.ledModule(); }
74 
76  PatternWriter& patternWriter() { return mPatternWriter; }
77 
83  void writeHexCharAt(uint8_t pos, hexchar_t c) {
84  writeInternalHexCharAt(
85  pos, ((uint8_t) c < kNumHexChars) ? c : kCharSpace);
86  }
87 
89  void writeHexCharsAt(uint8_t pos, const hexchar_t s[], uint8_t len) {
90  for (uint8_t i = 0; i < len; ++i) {
91  writeHexCharAt(pos++, s[i]);
92  }
93  }
94 
96  void writeHexByteAt(uint8_t pos, uint8_t b) {
97  uint8_t low = (b & 0x0F);
98  b >>= 4;
99  uint8_t high = (b & 0x0F);
100 
101  writeInternalHexCharAt(pos++, high);
102  writeInternalHexCharAt(pos++, low);
103  }
104 
106  void writeHexWordAt(uint8_t pos, uint16_t w) {
107  uint8_t low = (w & 0xFF);
108  uint8_t high = (w >> 8) & 0xFF;
109  writeHexByteAt(pos, high);
110  writeHexByteAt(pos + 2, low);
111  }
112 
127  uint8_t writeUnsignedDecimalAt(
128  uint8_t pos,
129  uint16_t num,
130  int8_t boxSize = 0);
131 
133  uint8_t writeSignedDecimalAt(
134  uint8_t pos,
135  int16_t num,
136  int8_t boxSize = 0);
137 
146  void writeUnsignedDecimal2At(uint8_t pos, uint8_t num);
147 
152  void writeDecimalPointAt(uint8_t pos, bool state = true) {
153  mPatternWriter.writeDecimalPointAt(pos, state);
154  }
155 
157  void clear() { clearToEnd(0); }
158 
164  void clearToEnd(uint8_t pos) { mPatternWriter.clearToEnd(pos); }
165 
166  private:
167  // disable copy-constructor and assignment operator
168  NumberWriter(const NumberWriter&) = delete;
169  NumberWriter& operator=(const NumberWriter&) = delete;
170 
172  void writeInternalHexCharAt(uint8_t pos, hexchar_t c);
173 
175  void writeInternalHexCharsAt(uint8_t pos, const hexchar_t s[],
176  uint8_t len) {
177  for (uint8_t i = 0; i < len; ++i) {
178  writeInternalHexCharAt(pos++, s[i]);
179  }
180  }
181 
188  uint8_t writeHexCharsInsideBoxAt(
189  uint8_t pos,
190  const hexchar_t s[],
191  uint8_t len,
192  int8_t boxSize);
193 
207  uint8_t toDecimal(uint16_t num, hexchar_t buf[], uint8_t bufSize) {
208  uint8_t pos = bufSize;
209  while (true) {
210  if (num < 10) {
211  buf[--pos] = num;
212  break;
213  }
214  uint16_t quot = num / 10;
215  buf[--pos] = num - quot * 10;
216  num = quot;
217  }
218  return pos;
219  }
220 
221  private:
223  static const uint8_t kHexCharPatterns[kNumHexChars];
224 
225  PatternWriter mPatternWriter;
226 };
227 
228 }
229 
230 #endif
ace_segment::NumberWriter::writeHexCharAt
void writeHexCharAt(uint8_t pos, hexchar_t c)
Write the hex character c at position pos.
Definition: NumberWriter.h:83
ace_segment::NumberWriter::writeUnsignedDecimalAt
uint8_t writeUnsignedDecimalAt(uint8_t pos, uint16_t num, int8_t boxSize=0)
Write the 16-bit unsigned number num as a decimal number at pos.
Definition: NumberWriter.cpp:71
ace_segment::PatternWriter::ledModule
LedModule & ledModule() const
Return the underlying LedModule.
Definition: PatternWriter.h:52
ace_segment::NumberWriter::clearToEnd
void clearToEnd(uint8_t pos)
Clear the display from pos to the end.
Definition: NumberWriter.h:164
ace_segment::NumberWriter::NumberWriter
NumberWriter(LedModule &ledModule)
Constructor.
Definition: NumberWriter.h:68
ace_segment::LedModule
General interface that represents a generic seven-segment LED module with multiple digits.
Definition: LedModule.h:44
ace_segment::NumberWriter::kNumHexChars
static const uint8_t kNumHexChars
Total number of characters in the HexCharacter set.
Definition: NumberWriter.h:59
ace_segment::NumberWriter::writeSignedDecimalAt
uint8_t writeSignedDecimalAt(uint8_t pos, int16_t num, int8_t boxSize=0)
Same as writeUnsignedDecimalAt() but prepends a '-' sign if negative.
Definition: NumberWriter.cpp:84
ace_segment::NumberWriter::writeHexWordAt
void writeHexWordAt(uint8_t pos, uint16_t w)
Write the 4 digit (16-bit) hexadecimal word at pos.
Definition: NumberWriter.h:106
ace_segment::NumberWriter::clear
void clear()
Clear the entire display.
Definition: NumberWriter.h:157
ace_segment::NumberWriter::writeDecimalPointAt
void writeDecimalPointAt(uint8_t pos, bool state=true)
Write the decimal point for the pos.
Definition: NumberWriter.h:152
ace_segment::NumberWriter::patternWriter
PatternWriter & patternWriter()
Get the underlying LedModule.
Definition: NumberWriter.h:76
ace_segment::NumberWriter::hexchar_t
uint8_t hexchar_t
The type of the character set supported by many methods in this class, usually containing the string ...
Definition: NumberWriter.h:56
ace_segment::NumberWriter::kCharMinus
static const hexchar_t kCharMinus
A minus character.
Definition: NumberWriter.h:65
ace_segment::PatternWriter::clearToEnd
void clearToEnd(uint8_t pos)
Clear the display from pos to the end.
Definition: PatternWriter.h:117
ace_segment::NumberWriter::writeHexCharsAt
void writeHexCharsAt(uint8_t pos, const hexchar_t s[], uint8_t len)
Write the len hex characters given by s starting at pos.
Definition: NumberWriter.h:89
ace_segment::NumberWriter
The NumberWriter supports converting decimal and hexadecimal numbers to segment patterns expected by ...
Definition: NumberWriter.h:39
ace_segment::NumberWriter::kCharSpace
static const hexchar_t kCharSpace
A space character.
Definition: NumberWriter.h:62
ace_segment::PatternWriter::writeDecimalPointAt
void writeDecimalPointAt(uint8_t pos, bool state=true)
Write the decimal point for the pos.
Definition: PatternWriter.h:102
ace_segment::PatternWriter
Write LED segment patterns to the underlying LedModule.
Definition: PatternWriter.h:43
ace_segment::NumberWriter::ledModule
LedModule & ledModule()
Get the underlying LedModule.
Definition: NumberWriter.h:73
ace_segment::NumberWriter::writeHexByteAt
void writeHexByteAt(uint8_t pos, uint8_t b)
Write the 2-digit (8-bit) hexadecimal byte 'b' at pos.
Definition: NumberWriter.h:96
ace_segment::NumberWriter::writeUnsignedDecimal2At
void writeUnsignedDecimal2At(uint8_t pos, uint8_t num)
Write the 2 digit decimal number at pos.
Definition: NumberWriter.cpp:134