AceSegment  0.8.2
A framework for rendering seven segment LED displays using the TM1637, MAX7219, HT16K33, or 74HC595 controller chips
LedModule.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_LED_MODULE_H
26 #define ACE_SEGMENT_LED_MODULE_H
27 
28 #include <stdint.h>
29 
30 namespace ace_segment {
31 
44 class LedModule {
45  public:
58  explicit LedModule(uint8_t* patterns, uint8_t numDigits) :
59  mPatterns(patterns),
60  mNumDigits(numDigits)
61  {}
62 
64  uint8_t getNumDigits() const { return mNumDigits; }
65 
67  void setPatternAt(uint8_t pos, uint8_t pattern) {
68  mPatterns[pos] = pattern;
69  setDigitDirty(pos);
70  }
71 
73  uint8_t getPatternAt(uint8_t pos) const {
74  return mPatterns[pos];
75  }
76 
81  void setBrightness(uint8_t brightness) {
82  mBrightness = brightness;
83  mIsBrightnessDirty = true;
84  }
85 
87  uint8_t getBrightness() const {
88  return mBrightness;
89  }
90 
91  protected:
93  void begin() {
94  // Dirty bits are set to true so that the first refresh sends the current
95  // pattern to the LED module. Otherwise, nothing will be displayed until
96  // a setPatternAt() or setBrightness() is called.
97  mDigitDirtyBits = 0xFF;
98  mIsBrightnessDirty = true;
99 
100  // On some LEDs, level 0 turns off the display, but on others level 0 is
101  // the lowest brightness level. Let's set the initial brightness to 1.
102  mBrightness = 1;
103  }
104 
108  void end() {}
109 
111  void setDigitDirty(uint8_t pos) {
112  mDigitDirtyBits |= (1 << pos);
113  }
114 
116  void clearDigitDirty(uint8_t pos) {
117  mDigitDirtyBits &= ~(1 << pos);
118  }
119 
121  bool isDigitDirty(uint8_t pos) const {
122  return mDigitDirtyBits & (1 << pos);
123  }
124 
127  mDigitDirtyBits = 0x0;
128  }
129 
131  bool isAnyDigitDirty() const {
132  return mDigitDirtyBits != 0;
133  }
134 
136  bool isBrightnessDirty() const {
137  return mIsBrightnessDirty;
138  }
139 
142  mIsBrightnessDirty = true;
143  }
144 
147  mIsBrightnessDirty = false;
148  }
149 
150  private:
151  // disable copy-constructor and assignment operator
152  LedModule(const LedModule&) = delete;
153  LedModule& operator=(const LedModule&) = delete;
154 
155  private:
156  // The order of these instance variables is partially motivated to save
157  // memory on 32-bit processors.
158  uint8_t* const mPatterns;
159  uint8_t const mNumDigits;
160 
161  uint8_t mDigitDirtyBits; // array of 8 dirty bits
162  uint8_t mBrightness;
163  bool mIsBrightnessDirty;
164 };
165 
166 } // ace_segment
167 
168 #endif
ace_segment::LedModule::clearDigitsDirty
void clearDigitsDirty()
Clear dirty bits of all digits.
Definition: LedModule.h:126
ace_segment::LedModule::setBrightnessDirty
void setBrightnessDirty()
Mark the brightness as dirty.
Definition: LedModule.h:141
ace_segment::LedModule
General interface that represents a generic seven-segment LED module with multiple digits.
Definition: LedModule.h:44
ace_segment::LedModule::isAnyDigitDirty
bool isAnyDigitDirty() const
Return true if any digits are dirty.
Definition: LedModule.h:131
ace_segment::LedModule::setDigitDirty
void setDigitDirty(uint8_t pos)
Set the dirty bit of digit pos.
Definition: LedModule.h:111
ace_segment::LedModule::clearDigitDirty
void clearDigitDirty(uint8_t pos)
Clear the dirty bit of digit pos.
Definition: LedModule.h:116
ace_segment::LedModule::begin
void begin()
Subclasses should call this from its own begin().
Definition: LedModule.h:93
ace_segment::LedModule::clearBrightnessDirty
void clearBrightnessDirty()
Clear the dirty bit for brightness.
Definition: LedModule.h:146
ace_segment::LedModule::setBrightness
void setBrightness(uint8_t brightness)
Set global brightness of all digits.
Definition: LedModule.h:81
ace_segment::LedModule::getNumDigits
uint8_t getNumDigits() const
Return the number of digits supported by this display instance.
Definition: LedModule.h:64
ace_segment::LedModule::setPatternAt
void setPatternAt(uint8_t pos, uint8_t pattern)
Set the led digit pattern at position pos.
Definition: LedModule.h:67
ace_segment::LedModule::end
void end()
Subclasses should call this from its own end().
Definition: LedModule.h:108
ace_segment::LedModule::LedModule
LedModule(uint8_t *patterns, uint8_t numDigits)
Constructor.
Definition: LedModule.h:58
ace_segment::LedModule::getBrightness
uint8_t getBrightness() const
Get the current brightness.
Definition: LedModule.h:87
ace_segment::LedModule::isBrightnessDirty
bool isBrightnessDirty() const
Check if the brightness level is dirty.
Definition: LedModule.h:136
ace_segment::LedModule::isDigitDirty
bool isDigitDirty(uint8_t pos) const
Check the dirty bit of digit pos.
Definition: LedModule.h:121
ace_segment::LedModule::getPatternAt
uint8_t getPatternAt(uint8_t pos) const
Get the led digit pattern at position pos.
Definition: LedModule.h:73