25 #ifndef ACE_SEGMENT_WRITER_NUMBER_WRITER_H
26 #define ACE_SEGMENT_WRITER_NUMBER_WRITER_H
29 #include <AceCommon.h>
30 #include "PatternWriter.h"
32 namespace ace_segment {
35 const uint8_t kNumDigitPatterns = 18;
38 extern const uint8_t kDigitPatterns[kNumDigitPatterns];
54 typedef uint8_t digit_t;
57 const digit_t kDigitSpace = 16;
60 const digit_t kDigitMinus = 17;
71 template <
typename T_LED_MODULE>
80 T_LED_MODULE&
ledModule() {
return mPatternWriter.ledModule(); }
86 void home() { mPatternWriter.home(); }
94 write(((uint8_t) c < kNumDigitPatterns) ? c : kDigitSpace);
99 for (uint8_t i = 0; i < len; ++i) {
113 void writeDec2(uint8_t d, uint8_t padPattern = kPattern0) {
115 mPatternWriter.writePattern(kPatternSpace);
116 mPatternWriter.writePattern(kPatternSpace);
118 uint8_t tens = d / 10;
119 uint8_t ones = d - 10 * tens;
121 mPatternWriter.writePattern(padPattern);
134 void writeDec4(uint16_t dd, uint8_t padPattern = kPattern0) {
136 mPatternWriter.writePattern(kPatternSpace);
137 mPatternWriter.writePattern(kPatternSpace);
138 mPatternWriter.writePattern(kPatternSpace);
139 mPatternWriter.writePattern(kPatternSpace);
141 uint8_t high = dd / 100;
142 uint8_t low = dd - high * 100;
144 mPatternWriter.writePattern(padPattern);
145 mPatternWriter.writePattern(padPattern);
160 uint8_t high = (bcd & 0xF0) >> 4;
161 uint8_t low = (bcd & 0x0F);
162 if (high > 9) high = kDigitSpace;
163 if (low > 9) low = kDigitSpace;
170 uint8_t low = (b & 0x0F);
171 uint8_t high = ((b >> 4) & 0x0F);
178 uint8_t low = (w & 0xFF);
179 uint8_t high = (w >> 8) & 0xFF;
200 const uint8_t bufSize = 5;
201 digit_t buf[bufSize];
202 uint8_t start = toDecimal(num, buf, bufSize);
204 return writeDigitsInsideBox(&buf[start], bufSize - start, boxSize);
210 bool negative = num < 0;
211 uint16_t absNum = negative ? -num : num;
213 const uint8_t bufSize = 6;
214 digit_t buf[bufSize];
215 uint8_t start = toDecimal(absNum, buf, bufSize);
217 buf[--start] = kDigitMinus;
220 return writeDigitsInsideBox(&buf[start], bufSize - start, boxSize);
232 ace_common::PrintStr<16> buf;
234 for (
const char *s = buf.cstr(); *s !=
'\0'; s++) {
251 if (c >=
'0' && c <=
'9') {
253 }
else if (c ==
'-') {
268 mPatternWriter.writeDecimalPoint(state);
272 void clear() { mPatternWriter.clear(); }
287 void write(digit_t c) {
288 uint8_t pattern = pgm_read_byte(&kDigitPatterns[(uint8_t) c]);
289 mPatternWriter.writePattern(pattern);
293 void writeInternalDigits(
const digit_t s[], uint8_t len) {
294 for (uint8_t i = 0; i < len; ++i) {
306 uint8_t writeDigitsInsideBox(
307 const digit_t s[], uint8_t len, int8_t boxSize) {
309 uint8_t absBoxSize = (boxSize < 0) ? -boxSize : boxSize;
312 if (len >= absBoxSize) {
313 writeInternalDigits(s, len);
318 uint8_t padSize = absBoxSize - len;
321 writeInternalDigits(s, len);
322 while (padSize--) write(kDigitSpace);
325 while (padSize--) write(kDigitSpace);
326 writeInternalDigits(s, len);
345 static uint8_t toDecimal(uint16_t num, digit_t buf[], uint8_t bufSize) {
346 uint8_t pos = bufSize;
352 uint16_t quot = num / 10;
353 buf[--pos] = num - quot * 10;
360 PatternWriter<T_LED_MODULE> &mPatternWriter;
The NumberWriter supports converting decimal and hexadecimal numbers to segment patterns expected by ...
void writeFloat(float x, uint8_t prec=2)
Write a float using the same format as the Print class.
void writeDec2(uint8_t d, uint8_t padPattern=kPattern0)
Write a 2-digit decimal number at position digit, right justified with the given padPattern (default ...
void writeDigits(const digit_t s[], uint8_t len)
Write the len hex characters given by s at LED pos.
PatternWriter< T_LED_MODULE > & patternWriter()
Get the underlying PatternWriter.
void clearToEnd()
Clear the display from the current position to the end.
uint8_t writeSignedDecimal(int16_t num, int8_t boxSize=0)
Same as writeUnsignedDecimal() but prepends a '-' sign if negative.
uint8_t writeUnsignedDecimal(uint16_t num, int8_t boxSize=0)
Write the 16-bit unsigned number num as a decimal number at pos.
void clear()
Clear the entire display.
NumberWriter(PatternWriter< T_LED_MODULE > &patternWriter)
Constructor.
T_LED_MODULE & ledModule()
Get the underlying LedModule.
void writeHexWord(uint16_t w)
Write the 4 digit (16-bit) hexadecimal word at pos.
void writeDigit(digit_t c)
Write the digit c at position pos.
void writeHexByte(uint8_t b)
Write the 2-digit (8-bit) hexadecimal byte 'b' at pos.
void writeDec4(uint16_t dd, uint8_t padPattern=kPattern0)
Write the 4 digit decimal number dd at pos, right justified, padded with a the padPattern (default kP...
void writeDecimalPoint(bool state=true)
Write the decimal point at the digit before the current position.
void writeChar(char c)
Write a limited set of ASCII characters, enough to support floating point numbers without scientific ...
void home()
Reset cursor to home.
void writeBcd(uint8_t bcd)
Write a 2-digit BCD number at position, which involves just printing the number as a hexadecimal numb...
Write LED segment patterns to the underlying LedModule.