AceSegmentWriter  0.4
Write decimal numbers, hex numbers, temperature, clock digits, characters, and strings to seven segment LED modules
PatternWriter.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_WRITER_PATTERN_WRITER_H
26 #define ACE_SEGMENT_WRITER_PATTERN_WRITER_H
27 
28 #include <stdint.h>
29 #include <Arduino.h> // pgm_read_byte()
30 
31 namespace ace_segment {
32 
33 // List of patterns for certain convenient symbols
34 
36 const uint8_t kPattern0 = 0b00111111;
37 
39 const uint8_t kPattern1 = 0b00000110;
40 
42 const uint8_t kPattern2 = 0b01011011;
43 
45 const uint8_t kPattern3 = 0b01001111;
46 
48 const uint8_t kPattern4 = 0b01100110;
49 
51 const uint8_t kPattern5 = 0b01101101;
52 
54 const uint8_t kPattern6 = 0b01111101;
55 
57 const uint8_t kPattern7 = 0b00000111;
58 
60 const uint8_t kPattern8 = 0b01111111;
61 
63 const uint8_t kPattern9 = 0b01101111;
64 
66 const uint8_t kPatternA = 0b01110111;
67 
69 const uint8_t kPatternB = 0b01111100;
70 
72 const uint8_t kPatternC = 0b00111001;
73 
75 const uint8_t kPatternD = 0b01011110;
76 
78 const uint8_t kPatternE = 0b01111001;
79 
81 const uint8_t kPatternF = 0b01110001;
82 
84 const uint8_t kPatternSpace = 0b00000000;
85 
87 const uint8_t kPatternMinus = 0b01000000;
88 
90 const uint8_t kPatternDeg = 0b01100011;
91 
93 const uint8_t kPatternP = 0b01110011;
94 
107 template <typename T_LED_MODULE>
109  public:
114  explicit PatternWriter(T_LED_MODULE& ledModule)
115  : mLedModule(ledModule),
116  mPos(0)
117  {}
118 
120  T_LED_MODULE& ledModule() const { return mLedModule; }
121 
123  uint8_t size() const { return mLedModule.size(); }
124 
126  void home() { mPos = 0; }
127 
129  uint8_t pos() const { return mPos; }
130 
132  void pos(uint8_t pos) { mPos = pos; }
133 
135  void writePattern(uint8_t pattern) {
136  if (mPos >= mLedModule.size()) return;
137  mLedModule.setPatternAt(mPos, pattern);
138  mPos++;
139  }
140 
142  void writePatterns(const uint8_t patterns[], uint8_t len) {
143  for (uint8_t i = 0; i < len; i++) {
144  if (mPos >= mLedModule.size()) break;
145  mLedModule.setPatternAt(mPos, patterns[i]);
146  mPos++;
147  }
148  }
149 
154  void writePatterns_P(const uint8_t patterns[], uint8_t len) {
155  for (uint8_t i = 0; i < len; i++) {
156  if (mPos >= mLedModule.size()) break;
157  uint8_t pattern = pgm_read_byte(patterns + i);
158  mLedModule.setPatternAt(mPos, pattern);
159  mPos++;
160  }
161  }
162 
167  void setDecimalPointAt(uint8_t pos, bool state = true) {
168  mLedModule.setDecimalPointAt(pos, state);
169  }
170 
172  void clear() { home(); clearToEnd(); }
173 
175  void clearToEnd() {
176  for (uint8_t i = mPos; i < mLedModule.size(); ++i) {
177  mLedModule.setPatternAt(i, 0);
178  }
179  home();
180  }
181 
182  private:
183  // disable copy-constructor and assignment operator
184  PatternWriter(const PatternWriter&) = delete;
185  PatternWriter& operator=(const PatternWriter&) = delete;
186 
187  private:
188  T_LED_MODULE& mLedModule;
189  uint8_t mPos;
190 };
191 
192 } // ace_segment
193 
194 #endif
Write LED segment patterns to the underlying LedModule.
void writePatterns_P(const uint8_t patterns[], uint8_t len)
Write the array patterns[] of length len to the led module.
void setDecimalPointAt(uint8_t pos, bool state=true)
Write the decimal point for the pos.
PatternWriter(T_LED_MODULE &ledModule)
Constructor.
uint8_t size() const
Return the number of digits supported by this display instance.
void home()
Set the cursor to the beginning.
void pos(uint8_t pos)
Set the current cursor position.
void writePattern(uint8_t pattern)
Write pattern at the current cursor.
void writePatterns(const uint8_t patterns[], uint8_t len)
Write the array patterns[] of length len to the led module.
void clear()
Clear the entire display.
uint8_t pos() const
Return the current cursor position.
T_LED_MODULE & ledModule() const
Return the underlying LedModule.
void clearToEnd()
Clear the display from pos to the end.