AceSegment  0.8.2
A framework for rendering seven segment LED displays using the TM1637, MAX7219, HT16K33, or 74HC595 controller chips
ClockWriter.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_CLOCK_WRITER_H
26 #define ACE_SEGMENT_CLOCK_WRITER_H
27 
28 #include <stdint.h>
29 #include "NumberWriter.h"
30 
31 namespace ace_segment {
32 
34 const uint8_t kPatternA = 0b01110111;
35 
37 const uint8_t kPatternP = 0b01110011;
38 
43 template <typename T_LED_MODULE>
44 class ClockWriter {
45  public:
55  explicit ClockWriter(
56  T_LED_MODULE& ledModule,
57  uint8_t colonDigit = 1
58  ) :
59  mNumberWriter(ledModule),
60  mColonDigit(colonDigit)
61  {}
62 
64  T_LED_MODULE& ledModule() { return mNumberWriter.ledModule(); }
65 
68  return mNumberWriter.patternWriter();
69  }
70 
72  NumberWriter<T_LED_MODULE>& numberWriter() { return mNumberWriter; }
73 
75  void writeCharAt(uint8_t pos, hexchar_t c) {
76  mNumberWriter.writeHexCharAt(pos, c);
77  }
78 
84  void writeChars2At(uint8_t pos, hexchar_t c0, hexchar_t c1) {
85  writeCharAt(pos++, c0);
86  writeCharAt(pos++, c1);
87  }
88 
94  void writeBcd2At(uint8_t pos, uint8_t bcd) {
95  uint8_t high = (bcd & 0xF0) >> 4;
96  uint8_t low = (bcd & 0x0F);
97  if (high > 9) high = kHexCharSpace;
98  if (low > 9) low = kHexCharSpace;
99  writeChars2At(pos, high, low);
100  }
101 
107  void writeDec2At(uint8_t pos, uint8_t d) {
108  if (d >= 100) {
109  writeChars2At(pos++, kHexCharSpace, kHexCharSpace);
110  } else {
111  uint8_t bcd = ace_common::decToBcd(d);
112  writeBcd2At(pos, bcd);
113  }
114  }
115 
120  void writeDec4At(uint8_t pos, uint16_t dd) {
121  uint8_t high = dd / 100;
122  uint8_t low = dd - high * 100;
123  writeDec2At(pos, high);
124  writeDec2At(pos + 2, low);
125  }
126 
131  void writeHourMinute(uint8_t hh, uint8_t mm) {
132  writeDec2At(0, hh);
133  writeDec2At(2, mm);
134  writeColon();
135  }
136 
142  void writeColon(bool state = true) {
143  mNumberWriter.writeDecimalPointAt(mColonDigit, state);
144  }
145 
147  void clear() { mNumberWriter.clear(); }
148 
150  void clearToEnd(uint8_t pos) { mNumberWriter.clearToEnd(pos); }
151 
152  private:
153  // disable copy-constructor and assignment operator
154  ClockWriter(const ClockWriter&) = delete;
155  ClockWriter& operator=(const ClockWriter&) = delete;
156 
157  NumberWriter<T_LED_MODULE> mNumberWriter;
158  uint8_t const mColonDigit;
159 };
160 
161 } // ace_segment
162 
163 #endif
ace_segment::ClockWriter::writeDec4At
void writeDec4At(uint8_t pos, uint16_t dd)
Write the 4 digit decimal number at pos, right justified, padded with a '0' character.
Definition: ClockWriter.h:120
ace_segment::ClockWriter::writeColon
void writeColon(bool state=true)
Write the colon symbol between 'hh' and 'mm'.
Definition: ClockWriter.h:142
ace_segment::ClockWriter::writeCharAt
void writeCharAt(uint8_t pos, hexchar_t c)
Write the hexchar_t 'c' at 'pos'.
Definition: ClockWriter.h:75
ace_segment::ClockWriter::writeHourMinute
void writeHourMinute(uint8_t hh, uint8_t mm)
Write the hour and minutes, and the colon in one-shot, assuming the LED module is a 4-digit clock mod...
Definition: ClockWriter.h:131
ace_segment::ClockWriter::clearToEnd
void clearToEnd(uint8_t pos)
Clear the display from pos to the end.
Definition: ClockWriter.h:150
ace_segment::ClockWriter::writeDec2At
void writeDec2At(uint8_t pos, uint8_t d)
Write a 2-digit decimal number at position digit, right justified.
Definition: ClockWriter.h:107
ace_segment::ClockWriter::writeBcd2At
void writeBcd2At(uint8_t pos, uint8_t bcd)
Write a 2-digit BCD number at position, which involves just printing the number as a hexadecimal numb...
Definition: ClockWriter.h:94
ace_segment::ClockWriter::numberWriter
NumberWriter< T_LED_MODULE > & numberWriter()
Get the underlying NumberWriter.
Definition: ClockWriter.h:72
ace_segment::ClockWriter::clear
void clear()
Clear the entire display.
Definition: ClockWriter.h:147
ace_segment::NumberWriter
The NumberWriter supports converting decimal and hexadecimal numbers to segment patterns expected by ...
Definition: NumberWriter.h:72
ace_segment::ClockWriter::patternWriter
PatternWriter< T_LED_MODULE > & patternWriter()
Get the underlying PatternWriter.
Definition: ClockWriter.h:67
ace_segment::ClockWriter::ClockWriter
ClockWriter(T_LED_MODULE &ledModule, uint8_t colonDigit=1)
Constructor.
Definition: ClockWriter.h:55
ace_segment::ClockWriter::ledModule
T_LED_MODULE & ledModule()
Get the underlying LedModule.
Definition: ClockWriter.h:64
ace_segment::PatternWriter
Write LED segment patterns to the underlying LedModule.
Definition: PatternWriter.h:46
ace_segment::ClockWriter
The ClockWriter writes "hh:mm" and "yyyy" to the LedModule.
Definition: ClockWriter.h:44
ace_segment::ClockWriter::writeChars2At
void writeChars2At(uint8_t pos, hexchar_t c0, hexchar_t c1)
Write the 2 hexchar_t 'c0' and 'c1' at 'pos' and 'pos+1'.
Definition: ClockWriter.h:84