AceSegmentWriter  0.5
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 
112 template <typename T_LED_MODULE>
114  public:
119  explicit PatternWriter(T_LED_MODULE& ledModule)
120  : mLedModule(ledModule),
121  mPos(0)
122  {}
123 
125  T_LED_MODULE& ledModule() const { return mLedModule; }
126 
128  uint8_t size() const { return mLedModule.size(); }
129 
131  void home() { mPos = 0; }
132 
134  uint8_t pos() const { return mPos; }
135 
137  void pos(uint8_t pos) { mPos = pos; }
138 
140  void writePattern(uint8_t pattern) {
141  if (mPos >= mLedModule.size()) return;
142  mLedModule.setPatternAt(mPos, pattern);
143  mPos++;
144  }
145 
147  void writePatterns(const uint8_t patterns[], uint8_t len) {
148  for (uint8_t i = 0; i < len; i++) {
149  if (mPos >= mLedModule.size()) break;
150  mLedModule.setPatternAt(mPos, patterns[i]);
151  mPos++;
152  }
153  }
154 
159  void writePatterns_P(const uint8_t patterns[], uint8_t len) {
160  for (uint8_t i = 0; i < len; i++) {
161  if (mPos >= mLedModule.size()) break;
162  uint8_t pattern = pgm_read_byte(patterns + i);
163  mLedModule.setPatternAt(mPos, pattern);
164  mPos++;
165  }
166  }
167 
174  void writeDecimalPoint(bool state = true) {
175  if (mPos == 0) return; // cannot write before start
176  if (mPos > mLedModule.size()) return; // cannot write beyond end
177  mLedModule.setDecimalPointAt(mPos - 1, state);
178  }
179 
184  void setDecimalPointAt(uint8_t pos, bool state = true) {
185  if (pos >= mLedModule.size()) return;
186  mLedModule.setDecimalPointAt(pos, state);
187  }
188 
190  void clear() { home(); clearToEnd(); }
191 
193  void clearToEnd() {
194  for (uint8_t i = mPos; i < mLedModule.size(); ++i) {
195  mLedModule.setPatternAt(i, 0);
196  }
197  home();
198  }
199 
200  private:
201  // disable copy-constructor and assignment operator
202  PatternWriter(const PatternWriter&) = delete;
203  PatternWriter& operator=(const PatternWriter&) = delete;
204 
205  private:
206  T_LED_MODULE& mLedModule;
207  uint8_t mPos;
208 };
209 
210 } // ace_segment
211 
212 #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.
void writeDecimalPoint(bool state=true)
Write a decimal point to the digit previous to the current position.
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, set the cursor to home().
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, set the cursor to home().