AceSegmentWriter  0.1.0
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 
45 template <typename T_LED_MODULE>
47  public:
52  explicit PatternWriter(T_LED_MODULE& ledModule) : mLedModule(ledModule) {}
53 
55  T_LED_MODULE& ledModule() const { return mLedModule; }
56 
58  uint8_t getNumDigits() const { return mLedModule.getNumDigits(); }
59 
64  void writePatternAt(uint8_t pos, uint8_t pattern) {
65  if (pos >= mLedModule.getNumDigits()) return;
66  mLedModule.setPatternAt(pos, pattern);
67  }
68 
77  void writePatternsAt(uint8_t pos, const uint8_t patterns[], uint8_t len) {
78  for (uint8_t i = 0; i < len; i++) {
79  if (pos >= mLedModule.getNumDigits()) break;
80  mLedModule.setPatternAt(pos++, patterns[i]);
81  }
82  }
83 
93  void writePatternsAt_P(uint8_t pos, const uint8_t patterns[], uint8_t len) {
94  for (uint8_t i = 0; i < len; i++) {
95  if (pos >= mLedModule.getNumDigits()) break;
96  uint8_t pattern = pgm_read_byte(patterns + i);
97  mLedModule.setPatternAt(pos++, pattern);
98  }
99  }
100 
105  void writeDecimalPointAt(uint8_t pos, bool state = true) {
106  if (pos >= mLedModule.getNumDigits()) return;
107  uint8_t pattern = mLedModule.getPatternAt(pos);
108  if (state) {
109  pattern |= 0x80;
110  } else {
111  pattern &= ~0x80;
112  }
113  mLedModule.setPatternAt(pos, pattern);
114  }
115 
117  void clear() { clearToEnd(0); }
118 
120  void clearToEnd(uint8_t pos) {
121  for (uint8_t i = pos; i < mLedModule.getNumDigits(); ++i) {
122  mLedModule.setPatternAt(i, 0);
123  }
124  }
125 
126  private:
127  // disable copy-constructor and assignment operator
128  PatternWriter(const PatternWriter&) = delete;
129  PatternWriter& operator=(const PatternWriter&) = delete;
130 
131  private:
132  T_LED_MODULE& mLedModule;
133 };
134 
135 } // ace_segment
136 
137 #endif
ace_segment::PatternWriter::clearToEnd
void clearToEnd(uint8_t pos)
Clear the display from pos to the end.
Definition: PatternWriter.h:120
ace_segment::PatternWriter::writePatternAt
void writePatternAt(uint8_t pos, uint8_t pattern)
Write the pattern for a given pos.
Definition: PatternWriter.h:64
ace_segment::PatternWriter::PatternWriter
PatternWriter(T_LED_MODULE &ledModule)
Constructor.
Definition: PatternWriter.h:52
ace_segment::PatternWriter::writePatternsAt
void writePatternsAt(uint8_t pos, const uint8_t patterns[], uint8_t len)
Write the array of patterns of length len, starting at pos.
Definition: PatternWriter.h:77
ace_segment::PatternWriter::writeDecimalPointAt
void writeDecimalPointAt(uint8_t pos, bool state=true)
Write the decimal point for the pos.
Definition: PatternWriter.h:105
ace_segment::PatternWriter::clear
void clear()
Clear the entire display.
Definition: PatternWriter.h:117
ace_segment::PatternWriter::ledModule
T_LED_MODULE & ledModule() const
Return the underlying LedModule.
Definition: PatternWriter.h:55
ace_segment::PatternWriter::getNumDigits
uint8_t getNumDigits() const
Return the number of digits supported by this display instance.
Definition: PatternWriter.h:58
ace_segment::PatternWriter::writePatternsAt_P
void writePatternsAt_P(uint8_t pos, const uint8_t patterns[], uint8_t len)
Write the array of patterns of length len, which are stored in flash memory through PROGMEM,...
Definition: PatternWriter.h:93
ace_segment::PatternWriter
Write LED segment patterns to the underlying LedModule.
Definition: PatternWriter.h:46