AceSegmentWriter  0.4
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:
69  explicit CharWriter(
71  const uint8_t charPatterns[] = kCharPatterns,
72  uint8_t numChars = kNumCharPatterns
73  ) :
74  mPatternWriter(patternWriter),
75  mCharPatterns(charPatterns),
76  mNumChars(numChars)
77  {}
78 
80  T_LED_MODULE& ledModule() { return mPatternWriter.ledModule(); }
81 
83  PatternWriter<T_LED_MODULE>& patternWriter() { return mPatternWriter; }
84 
86  uint8_t size() const { return mPatternWriter.size(); }
87 
89  uint8_t getNumChars() const { return mNumChars; }
90 
92  void home() { mPatternWriter.home(); }
93 
95  void writeChar(char c) {
96  mPatternWriter.writePattern(getPattern(c));
97  }
98 
100  uint8_t getPattern(char c) const {
101  uint8_t pattern = ((uint8_t) c < mNumChars)
102  ? pgm_read_byte(&mCharPatterns[(uint8_t) c])
103  : kPatternUnknown;
104  return pattern;
105  }
106 
108  void setDecimalPointAt(uint8_t pos, bool state = true) {
109  mPatternWriter.setDecimalPointAt(pos, state);
110  }
111 
113  void clear() { mPatternWriter.clear(); }
114 
116  void clearToEnd() { mPatternWriter.clearToEnd(); }
117 
118  private:
119  // disable copy-constructor and assignment operator
120  CharWriter(const CharWriter&) = delete;
121  CharWriter& operator=(const CharWriter&) = delete;
122 
123  private:
124  PatternWriter<T_LED_MODULE>& mPatternWriter;
125  const uint8_t* const mCharPatterns;
126  uint8_t const mNumChars;
127 };
128 
129 }
130 
131 #endif
The CharWriter supports mapping of an 8-bit character set to segment patterns supported by LedModule.
Definition: CharWriter.h:58
uint8_t getPattern(char c) const
Get segment pattern for character 'c'.
Definition: CharWriter.h:100
void writeChar(char c)
Write the character at the specified position.
Definition: CharWriter.h:95
T_LED_MODULE & ledModule()
Get the underlying LedModule.
Definition: CharWriter.h:80
void home()
Set the cursor to the beginning.
Definition: CharWriter.h:92
void clear()
Clear the entire display.
Definition: CharWriter.h:113
CharWriter(PatternWriter< T_LED_MODULE > &patternWriter, const uint8_t charPatterns[]=kCharPatterns, uint8_t numChars=kNumCharPatterns)
Constructor.
Definition: CharWriter.h:69
uint8_t size() const
Return the number of digits supported by this display instance.
Definition: CharWriter.h:86
PatternWriter< T_LED_MODULE > & patternWriter()
Get the underlying PatternWriter.
Definition: CharWriter.h:83
void clearToEnd()
Clear the display from pos to the end.
Definition: CharWriter.h:116
void setDecimalPointAt(uint8_t pos, bool state=true)
Write the decimal point for the pos.
Definition: CharWriter.h:108
uint8_t getNumChars() const
Get number of characters in current character set.
Definition: CharWriter.h:89
Write LED segment patterns to the underlying LedModule.