AceSegment  0.7.0
A framework for rendering seven segment LED displays using the TM1637, MAX7219, HT16K33, or 74HC595 controller chips
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_PATTERN_WRITER_H
26 #define ACE_SEGMENT_PATTERN_WRITER_H
27 
28 #include <stdint.h>
29 #include <Arduino.h> // pgm_read_byte()
30 #include "../LedModule.h"
31 
32 namespace ace_segment {
33 
44  public:
49  explicit PatternWriter(LedModule& ledModule) : mLedModule(ledModule) {}
50 
52  LedModule& ledModule() const { return mLedModule; }
53 
55  uint8_t getNumDigits() const { return mLedModule.getNumDigits(); }
56 
61  void writePatternAt(uint8_t pos, uint8_t pattern) {
62  if (pos >= mLedModule.getNumDigits()) return;
63  mLedModule.setPatternAt(pos, pattern);
64  }
65 
74  void writePatternsAt(uint8_t pos, const uint8_t patterns[], uint8_t len) {
75  for (uint8_t i = 0; i < len; i++) {
76  if (pos >= mLedModule.getNumDigits()) break;
77  mLedModule.setPatternAt(pos++, patterns[i]);
78  }
79  }
80 
90  void writePatternsAt_P(uint8_t pos, const uint8_t patterns[], uint8_t len) {
91  for (uint8_t i = 0; i < len; i++) {
92  if (pos >= mLedModule.getNumDigits()) break;
93  uint8_t pattern = pgm_read_byte(patterns + i);
94  mLedModule.setPatternAt(pos++, pattern);
95  }
96  }
97 
102  void writeDecimalPointAt(uint8_t pos, bool state = true) {
103  if (pos >= mLedModule.getNumDigits()) return;
104  uint8_t pattern = mLedModule.getPatternAt(pos);
105  if (state) {
106  pattern |= 0x80;
107  } else {
108  pattern &= ~0x80;
109  }
110  mLedModule.setPatternAt(pos, pattern);
111  }
112 
114  void clear() { clearToEnd(0); }
115 
117  void clearToEnd(uint8_t pos) {
118  for (uint8_t i = pos; i < mLedModule.getNumDigits(); ++i) {
119  mLedModule.setPatternAt(i, 0);
120  }
121  }
122 
123  private:
124  // disable copy-constructor and assignment operator
125  PatternWriter(const PatternWriter&) = delete;
126  PatternWriter& operator=(const PatternWriter&) = delete;
127 
128  private:
129  LedModule& mLedModule;
130 };
131 
132 } // ace_segment
133 
134 #endif
ace_segment::LedModule::getPatternAt
virtual uint8_t getPatternAt(uint8_t pos)=0
Get the led digit pattern at position pos.
ace_segment::PatternWriter::ledModule
LedModule & ledModule() const
Return the underlying LedModule.
Definition: PatternWriter.h:52
ace_segment::PatternWriter::clear
void clear()
Clear the entire display.
Definition: PatternWriter.h:114
ace_segment::PatternWriter::writePatternAt
void writePatternAt(uint8_t pos, uint8_t pattern)
Write the pattern for a given pos.
Definition: PatternWriter.h:61
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:74
ace_segment::LedModule
General interface that represents a generic seven-segment LED module with multiple digits.
Definition: LedModule.h:44
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:90
ace_segment::PatternWriter::PatternWriter
PatternWriter(LedModule &ledModule)
Constructor.
Definition: PatternWriter.h:49
ace_segment::LedModule::getNumDigits
uint8_t getNumDigits() const
Return the number of digits supported by this display instance.
Definition: LedModule.h:61
ace_segment::PatternWriter::clearToEnd
void clearToEnd(uint8_t pos)
Clear the display from pos to the end.
Definition: PatternWriter.h:117
ace_segment::LedModule::setPatternAt
virtual void setPatternAt(uint8_t pos, uint8_t pattern)=0
Set the led digit pattern at position pos.
ace_segment::PatternWriter::getNumDigits
uint8_t getNumDigits() const
Return the number of digits supported by this display instance.
Definition: PatternWriter.h:55
ace_segment::PatternWriter::writeDecimalPointAt
void writeDecimalPointAt(uint8_t pos, bool state=true)
Write the decimal point for the pos.
Definition: PatternWriter.h:102
ace_segment::PatternWriter
Write LED segment patterns to the underlying LedModule.
Definition: PatternWriter.h:43