AceSegmentWriter  0.1.0
Write decimal numbers, hex numbers, temperature, clock digits, characters, and strings to seven segment LED modules
CharWriter.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_WRITER_CHAR_WRITER_H
26 #define ACE_SEGMENT_WRITER_CHAR_WRITER_H
27 
28 #include <stdint.h>
29 #include <Arduino.h> // pgm_read_byte()
30 #include "PatternWriter.h"
31 
32 namespace ace_segment {
33 
41 const uint8_t kPatternUnknown = 0b00000000;
42 
44 const uint8_t kNumCharPatterns = 128;
45 
47 extern const uint8_t kCharPatterns[kNumCharPatterns];
48 
57 template <typename T_LED_MODULE>
58 class CharWriter {
59  public:
68  explicit CharWriter(
69  T_LED_MODULE& ledModule,
70  const uint8_t charPatterns[] = kCharPatterns,
71  uint8_t numChars = kNumCharPatterns
72  ) :
73  mPatternWriter(ledModule),
74  mCharPatterns(charPatterns),
75  mNumChars(numChars)
76  {}
77 
79  T_LED_MODULE& ledModule() { return mPatternWriter.ledModule(); }
80 
82  PatternWriter<T_LED_MODULE>& patternWriter() { return mPatternWriter; }
83 
85  uint8_t getNumDigits() const { return mPatternWriter.getNumDigits(); }
86 
88  uint8_t getNumChars() const { return mNumChars; }
89 
91  void writeCharAt(uint8_t pos, char c) {
92  if (pos >= mPatternWriter.getNumDigits()) return;
93  mPatternWriter.writePatternAt(pos, getPattern(c));
94  }
95 
97  uint8_t getPattern(char c) const {
98  uint8_t pattern = ((mNumChars == 0) || ((uint8_t) c < mNumChars))
99  ? pgm_read_byte(&mCharPatterns[(uint8_t) c])
100  : kPatternUnknown;
101  return pattern;
102  }
103 
105  void writeDecimalPointAt(uint8_t pos, bool state = true) {
106  mPatternWriter.writeDecimalPointAt(pos, state);
107  }
108 
110  void clear() { clearToEnd(0); }
111 
113  void clearToEnd(uint8_t pos) { mPatternWriter.clearToEnd(pos); }
114 
115  private:
116  // disable copy-constructor and assignment operator
117  CharWriter(const CharWriter&) = delete;
118  CharWriter& operator=(const CharWriter&) = delete;
119 
120  private:
121  PatternWriter<T_LED_MODULE> mPatternWriter;
122  const uint8_t* const mCharPatterns;
123  uint8_t const mNumChars;
124 };
125 
126 }
127 
128 #endif
ace_segment::CharWriter::ledModule
T_LED_MODULE & ledModule()
Get the underlying LedModule.
Definition: CharWriter.h:79
ace_segment::CharWriter::getPattern
uint8_t getPattern(char c) const
Get segment pattern for character 'c'.
Definition: CharWriter.h:97
ace_segment::CharWriter::writeDecimalPointAt
void writeDecimalPointAt(uint8_t pos, bool state=true)
Write the decimal point for the pos.
Definition: CharWriter.h:105
ace_segment::CharWriter::clear
void clear()
Clear the entire display.
Definition: CharWriter.h:110
ace_segment::CharWriter
The CharWriter supports mapping of an 8-bit character set to segment patterns supported by LedModule.
Definition: CharWriter.h:58
ace_segment::CharWriter::patternWriter
PatternWriter< T_LED_MODULE > & patternWriter()
Get the underlying PatternWriter.
Definition: CharWriter.h:82
ace_segment::CharWriter::writeCharAt
void writeCharAt(uint8_t pos, char c)
Write the character at the specified position.
Definition: CharWriter.h:91
ace_segment::CharWriter::getNumDigits
uint8_t getNumDigits() const
Return the number of digits supported by this display instance.
Definition: CharWriter.h:85
ace_segment::CharWriter::CharWriter
CharWriter(T_LED_MODULE &ledModule, const uint8_t charPatterns[]=kCharPatterns, uint8_t numChars=kNumCharPatterns)
Constructor.
Definition: CharWriter.h:68
ace_segment::CharWriter::clearToEnd
void clearToEnd(uint8_t pos)
Clear the display from pos to the end.
Definition: CharWriter.h:113
ace_segment::CharWriter::getNumChars
uint8_t getNumChars() const
Get number of characters in current character set.
Definition: CharWriter.h:88
ace_segment::PatternWriter
Write LED segment patterns to the underlying LedModule.
Definition: PatternWriter.h:46