AceSegment  0.4.0
An adjustable, configurable, and extensible framework for rendering seven segment LED displays.
NumberWriter.h
1 /*
2 MIT License
3 
4 Copyright (c) 2018 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 "LedDisplay.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 
68  explicit NumberWriter(LedDisplay& ledDisplay) :
69  mLedDisplay(ledDisplay)
70  {}
71 
73  LedDisplay& display() const {
74  return mLedDisplay;
75  }
76 
82  void writeHexCharAt(uint8_t pos, hexchar_t c) {
83  writeInternalHexCharAt(
84  pos, ((uint8_t) c < kNumHexChars) ? c : kCharSpace);
85  }
86 
88  void writeHexCharsAt(uint8_t pos, const hexchar_t s[], uint8_t len) {
89  for (uint8_t i = 0; i < len; ++i) {
90  writeHexCharAt(pos++, s[i]);
91  }
92  }
93 
95  void writeHexByteAt(uint8_t pos, uint8_t b) {
96  uint8_t low = (b & 0x0F);
97  b >>= 4;
98  uint8_t high = (b & 0x0F);
99 
100  writeInternalHexCharAt(pos++, high);
101  writeInternalHexCharAt(pos++, low);
102  }
103 
105  void writeHexWordAt(uint8_t pos, uint16_t w) {
106  uint8_t low = (w & 0xFF);
107  uint8_t high = (w >> 8) & 0xFF;
108  writeHexByteAt(pos, high);
109  writeHexByteAt(pos + 2, low);
110  }
111 
126  uint8_t writeUnsignedDecimalAt(
127  uint8_t pos,
128  uint16_t num,
129  int8_t boxSize = 0);
130 
132  uint8_t writeSignedDecimalAt(
133  uint8_t pos,
134  int16_t num,
135  int8_t boxSize = 0);
136 
142  void clearToEnd(uint8_t pos) {
143  for (uint8_t i = pos; i < mLedDisplay.getNumDigits(); ++i) {
144  writeInternalHexCharAt(i, kCharSpace);
145  }
146  }
147 
148  private:
149  // disable copy-constructor and assignment operator
150  NumberWriter(const NumberWriter&) = delete;
151  NumberWriter& operator=(const NumberWriter&) = delete;
152 
154  void writeInternalHexCharAt(uint8_t pos, hexchar_t c);
155 
157  void writeInternalHexCharsAt(uint8_t pos, const hexchar_t s[],
158  uint8_t len) {
159  for (uint8_t i = 0; i < len; ++i) {
160  writeInternalHexCharAt(pos++, s[i]);
161  }
162  }
163 
170  uint8_t writeHexCharsInsideBoxAt(
171  uint8_t pos,
172  const hexchar_t s[],
173  uint8_t len,
174  int8_t boxSize);
175 
189  uint8_t toDecimal(uint16_t num, hexchar_t buf[], uint8_t bufSize) {
190  uint8_t pos = bufSize;
191  while (true) {
192  if (num < 10) {
193  buf[--pos] = num;
194  break;
195  }
196  uint16_t quot = num / 10;
197  buf[--pos] = num - quot * 10;
198  num = quot;
199  }
200  return pos;
201  }
202 
203  private:
205  static const uint8_t kHexCharPatterns[kNumHexChars];
206 
207  LedDisplay& mLedDisplay;
208 };
209 
210 }
211 
212 #endif
ace_segment::NumberWriter::display
LedDisplay & display() const
Get the underlying LedDisplay.
Definition: NumberWriter.h:73
ace_segment::NumberWriter::writeHexCharAt
void writeHexCharAt(uint8_t pos, hexchar_t c)
Write the hex character c at position pos.
Definition: NumberWriter.h:82
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::NumberWriter::clearToEnd
void clearToEnd(uint8_t pos)
Clear the display from pos to the end.
Definition: NumberWriter.h:142
ace_segment::NumberWriter::NumberWriter
NumberWriter(LedDisplay &ledDisplay)
Constructor.
Definition: NumberWriter.h:68
ace_segment::NumberWriter::kNumHexChars
static const uint8_t kNumHexChars
Total number of characters in the HexCharacter set.
Definition: NumberWriter.h:59
ace_segment::LedDisplay
General interface for writing LED segment patterns to the LED display module.
Definition: LedDisplay.h:42
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:105
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::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:88
ace_segment::LedDisplay::getNumDigits
uint8_t getNumDigits() const
Return the number of digits supported by this display instance.
Definition: LedDisplay.h:99
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::NumberWriter::writeHexByteAt
void writeHexByteAt(uint8_t pos, uint8_t b)
Write the 2-digit (8-bit) hexadecimal byte 'b' at pos.
Definition: NumberWriter.h:95