AceSegment  0.8.2
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 
34 const uint8_t kNumHexCharPatterns = 18;
35 
37 extern const uint8_t kHexCharPatterns[kNumHexCharPatterns];
38 
54 typedef uint8_t hexchar_t;
55 
57 const hexchar_t kHexCharSpace = 0x10;
58 
60 const hexchar_t kHexCharMinus = 0x11;
61 
71 template <typename T_LED_MODULE>
72 class NumberWriter {
73  public:
75  explicit NumberWriter(T_LED_MODULE& ledModule) :
76  mPatternWriter(ledModule)
77  {}
78 
80  T_LED_MODULE& ledModule() { return mPatternWriter.ledModule(); }
81 
83  PatternWriter<T_LED_MODULE>& patternWriter() { return mPatternWriter; }
84 
90  void writeHexCharAt(uint8_t pos, hexchar_t c) {
91  writeInternalHexCharAt(
92  pos, ((uint8_t) c < kNumHexCharPatterns) ? c : kHexCharSpace);
93  }
94 
96  void writeHexCharsAt(uint8_t pos, const hexchar_t s[], uint8_t len) {
97  for (uint8_t i = 0; i < len; ++i) {
98  writeHexCharAt(pos++, s[i]);
99  }
100  }
101 
103  void writeHexByteAt(uint8_t pos, uint8_t b) {
104  uint8_t low = (b & 0x0F);
105  b >>= 4;
106  uint8_t high = (b & 0x0F);
107 
108  writeInternalHexCharAt(pos++, high);
109  writeInternalHexCharAt(pos++, low);
110  }
111 
113  void writeHexWordAt(uint8_t pos, uint16_t w) {
114  uint8_t low = (w & 0xFF);
115  uint8_t high = (w >> 8) & 0xFF;
116  writeHexByteAt(pos, high);
117  writeHexByteAt(pos + 2, low);
118  }
119 
135  uint8_t pos,
136  uint16_t num,
137  int8_t boxSize = 0) {
138 
139  const uint8_t bufSize = 5;
140  hexchar_t buf[bufSize];
141  uint8_t start = toDecimal(num, buf, bufSize);
142 
143  return writeHexCharsInsideBoxAt(
144  pos, &buf[start], bufSize - start, boxSize);
145  }
146 
149  uint8_t pos,
150  int16_t num,
151  int8_t boxSize = 0) {
152 
153  // Even -32768 turns into +32768, which is exactly what we want
154  bool negative = num < 0;
155  uint16_t absNum = negative ? -num : num;
156 
157  const uint8_t bufSize = 6;
158  hexchar_t buf[bufSize];
159  uint8_t start = toDecimal(absNum, buf, bufSize);
160  if (negative) {
161  buf[--start] = kHexCharMinus;
162  }
163 
164  return writeHexCharsInsideBoxAt(
165  pos, &buf[start], bufSize - start, boxSize);
166  }
167 
176  void writeUnsignedDecimal2At(uint8_t pos, uint8_t num) {
177  if (num >= 100) {
178  writeHexCharAt(pos++, 9);
179  writeHexCharAt(pos++, 9);
180  } else {
181  uint8_t high = num / 10;
182  uint8_t low = num - high * 10;
183  writeHexCharAt(pos++, (high == 0) ? kHexCharSpace : high);
184  writeHexCharAt(pos++, low);
185  }
186  }
187 
192  void writeDecimalPointAt(uint8_t pos, bool state = true) {
193  mPatternWriter.writeDecimalPointAt(pos, state);
194  }
195 
197  void clear() { clearToEnd(0); }
198 
204  void clearToEnd(uint8_t pos) { mPatternWriter.clearToEnd(pos); }
205 
206  private:
207  // disable copy-constructor and assignment operator
208  NumberWriter(const NumberWriter&) = delete;
209  NumberWriter& operator=(const NumberWriter&) = delete;
210 
212  void writeInternalHexCharAt(uint8_t pos, hexchar_t c) {
213  uint8_t pattern = pgm_read_byte(&kHexCharPatterns[(uint8_t) c]);
214  mPatternWriter.writePatternAt(pos, pattern);
215  }
216 
218  void writeInternalHexCharsAt(uint8_t pos, const hexchar_t s[],
219  uint8_t len) {
220  for (uint8_t i = 0; i < len; ++i) {
221  writeInternalHexCharAt(pos++, s[i]);
222  }
223  }
224 
231  uint8_t writeHexCharsInsideBoxAt(
232  uint8_t pos,
233  const hexchar_t s[],
234  uint8_t len,
235  int8_t boxSize) {
236 
237  uint8_t absBoxSize = (boxSize < 0) ? -boxSize : boxSize;
238 
239  // if the box is too small, print normally
240  if (len >= absBoxSize) {
241  writeInternalHexCharsAt(pos, s, len);
242  return len;
243  }
244 
245  // Print either left justified or right justified inside box
246  uint8_t padSize = absBoxSize - len;
247  if (boxSize < 0) {
248  // left justified
249  writeInternalHexCharsAt(pos, s, len);
250  pos += len;
251  while (padSize--) writeInternalHexCharAt(pos++, kHexCharSpace);
252  } else {
253  // right justified
254  while (padSize--) writeInternalHexCharAt(pos++, kHexCharSpace);
255  writeInternalHexCharsAt(pos, s, len);
256  }
257 
258  return absBoxSize;
259  }
260 
274  uint8_t toDecimal(uint16_t num, hexchar_t buf[], uint8_t bufSize) {
275  uint8_t pos = bufSize;
276  while (true) {
277  if (num < 10) {
278  buf[--pos] = num;
279  break;
280  }
281  uint16_t quot = num / 10;
282  buf[--pos] = num - quot * 10;
283  num = quot;
284  }
285  return pos;
286  }
287 
288  private:
289  PatternWriter<T_LED_MODULE> mPatternWriter;
290 };
291 
292 }
293 
294 #endif
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.h:134
ace_segment::NumberWriter::writeUnsignedDecimal2At
void writeUnsignedDecimal2At(uint8_t pos, uint8_t num)
Write the 2 digit decimal number at pos.
Definition: NumberWriter.h:176
ace_segment::NumberWriter::NumberWriter
NumberWriter(T_LED_MODULE &ledModule)
Constructor.
Definition: NumberWriter.h:75
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.h:148
ace_segment::NumberWriter::clearToEnd
void clearToEnd(uint8_t pos)
Clear the display from pos to the end.
Definition: NumberWriter.h:204
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:96
ace_segment::NumberWriter::clear
void clear()
Clear the entire display.
Definition: NumberWriter.h:197
ace_segment::NumberWriter::patternWriter
PatternWriter< T_LED_MODULE > & patternWriter()
Get the underlying PatternWriter.
Definition: NumberWriter.h:83
ace_segment::NumberWriter::writeDecimalPointAt
void writeDecimalPointAt(uint8_t pos, bool state=true)
Write the decimal point for the pos.
Definition: NumberWriter.h:192
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:113
ace_segment::NumberWriter
The NumberWriter supports converting decimal and hexadecimal numbers to segment patterns expected by ...
Definition: NumberWriter.h:72
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:103
ace_segment::PatternWriter
Write LED segment patterns to the underlying LedModule.
Definition: PatternWriter.h:46
ace_segment::NumberWriter::writeHexCharAt
void writeHexCharAt(uint8_t pos, hexchar_t c)
Write the hex character c at position pos.
Definition: NumberWriter.h:90
ace_segment::NumberWriter::ledModule
T_LED_MODULE & ledModule()
Get the underlying LedModule.
Definition: NumberWriter.h:80