AceSegment  0.8.0
A framework for rendering seven segment LED displays using the TM1637, MAX7219, HT16K33, or 74HC595 controller chips
LevelWriter.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_LEVEL_WRITER_H
26 #define ACE_SEGMENT_LEVEL_WRITER_H
27 
28 #include <stdint.h>
29 #include "PatternWriter.h"
30 
31 namespace ace_segment {
32 
38 class LevelWriter {
39  private:
40  // left vertical bar
41  static const uint8_t kLevelLeftPattern = 0b00110000;
42  // right vertical bar
43  static const uint8_t kLevelRightPattern = 0b00000110;
44 
45  public:
48  mPatternWriter(ledModule)
49  {}
50 
52  LedModule& ledModule() const { return mPatternWriter.ledModule(); }
53 
58  uint8_t getMaxLevel() const {
59  return mPatternWriter.getNumDigits() * 2;
60  }
61 
63  void writeLevel(uint8_t level) {
64  uint8_t fullDigits = level / 2;
65  uint8_t partialDigit = level & 0x1;
66  uint8_t numDigits = mPatternWriter.getNumDigits();
67 
68  uint8_t pos = 0;
69  while (pos < fullDigits && pos < numDigits) {
70  mPatternWriter.writePatternAt(
71  pos++, kLevelLeftPattern | kLevelRightPattern);
72  }
73  if (partialDigit && pos < numDigits) {
74  mPatternWriter.writePatternAt(pos++, kLevelLeftPattern);
75  }
76  mPatternWriter.clearToEnd(pos);
77  }
78 
79  private:
80  // disable copy-constructor and assignment operator
81  LevelWriter(const LevelWriter&) = delete;
82  LevelWriter& operator=(const LevelWriter&) = delete;
83 
84  private:
85  PatternWriter mPatternWriter;
86 };
87 
88 }
89 
90 #endif
ace_segment::PatternWriter::ledModule
LedModule & ledModule() const
Return the underlying LedModule.
Definition: PatternWriter.h:52
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::LevelWriter::LevelWriter
LevelWriter(LedModule &ledModule)
Constructor.
Definition: LevelWriter.h:47
ace_segment::LedModule
General interface that represents a generic seven-segment LED module with multiple digits.
Definition: LedModule.h:44
ace_segment::LevelWriter::ledModule
LedModule & ledModule() const
Get the underlying LedModule.
Definition: LevelWriter.h:52
ace_segment::LevelWriter::writeLevel
void writeLevel(uint8_t level)
Write out the level bar, 2 levels per digit.
Definition: LevelWriter.h:63
ace_segment::PatternWriter::clearToEnd
void clearToEnd(uint8_t pos)
Clear the display from pos to the end.
Definition: PatternWriter.h:117
ace_segment::LevelWriter
Emulate a level led module using a left vertical bar and a right vertical bar on each digit.
Definition: LevelWriter.h:38
ace_segment::PatternWriter::getNumDigits
uint8_t getNumDigits() const
Return the number of digits supported by this display instance.
Definition: PatternWriter.h:55
ace_segment::LevelWriter::getMaxLevel
uint8_t getMaxLevel() const
Return the maximum level supported by this LED display.
Definition: LevelWriter.h:58
ace_segment::PatternWriter
Write LED segment patterns to the underlying LedModule.
Definition: PatternWriter.h:43