AceSegmentWriter  0.1.0
Write decimal numbers, hex numbers, temperature, clock digits, characters, and strings to seven segment LED modules
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_WRITER_CLOCK_WRITER_H
26 #define ACE_SEGMENT_WRITER_CLOCK_WRITER_H
27 
28 #include <stdint.h>
29 #include <AceCommon.h>
30 #include "NumberWriter.h"
31 
32 namespace ace_segment {
33 
35 const uint8_t kPatternA = 0b01110111;
36 
38 const uint8_t kPatternP = 0b01110011;
39 
44 template <typename T_LED_MODULE>
45 class ClockWriter {
46  public:
56  explicit ClockWriter(
57  T_LED_MODULE& ledModule,
58  uint8_t colonDigit = 1
59  ) :
60  mNumberWriter(ledModule),
61  mColonDigit(colonDigit)
62  {}
63 
65  T_LED_MODULE& ledModule() { return mNumberWriter.ledModule(); }
66 
69  return mNumberWriter.patternWriter();
70  }
71 
73  NumberWriter<T_LED_MODULE>& numberWriter() { return mNumberWriter; }
74 
76  void writeCharAt(uint8_t pos, hexchar_t c) {
77  mNumberWriter.writeHexCharAt(pos, c);
78  }
79 
85  void writeChars2At(uint8_t pos, hexchar_t c0, hexchar_t c1) {
86  writeCharAt(pos++, c0);
87  writeCharAt(pos++, c1);
88  }
89 
95  void writeBcd2At(uint8_t pos, uint8_t bcd) {
96  uint8_t high = (bcd & 0xF0) >> 4;
97  uint8_t low = (bcd & 0x0F);
98  if (high > 9) high = kHexCharSpace;
99  if (low > 9) low = kHexCharSpace;
100  writeChars2At(pos, high, low);
101  }
102 
108  void writeDec2At(uint8_t pos, uint8_t d) {
109  if (d >= 100) {
110  writeChars2At(pos++, kHexCharSpace, kHexCharSpace);
111  } else {
112  uint8_t bcd = ace_common::decToBcd(d);
113  writeBcd2At(pos, bcd);
114  }
115  }
116 
121  void writeDec4At(uint8_t pos, uint16_t dd) {
122  uint8_t high = dd / 100;
123  uint8_t low = dd - high * 100;
124  writeDec2At(pos, high);
125  writeDec2At(pos + 2, low);
126  }
127 
132  void writeHourMinute(uint8_t hh, uint8_t mm) {
133  writeDec2At(0, hh);
134  writeDec2At(2, mm);
135  writeColon();
136  }
137 
143  void writeColon(bool state = true) {
144  mNumberWriter.writeDecimalPointAt(mColonDigit, state);
145  }
146 
148  void clear() { mNumberWriter.clear(); }
149 
151  void clearToEnd(uint8_t pos) { mNumberWriter.clearToEnd(pos); }
152 
153  private:
154  // disable copy-constructor and assignment operator
155  ClockWriter(const ClockWriter&) = delete;
156  ClockWriter& operator=(const ClockWriter&) = delete;
157 
158  NumberWriter<T_LED_MODULE> mNumberWriter;
159  uint8_t const mColonDigit;
160 };
161 
162 } // ace_segment
163 
164 #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:121
ace_segment::ClockWriter::writeColon
void writeColon(bool state=true)
Write the colon symbol between 'hh' and 'mm'.
Definition: ClockWriter.h:143
ace_segment::ClockWriter::writeCharAt
void writeCharAt(uint8_t pos, hexchar_t c)
Write the hexchar_t 'c' at 'pos'.
Definition: ClockWriter.h:76
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:132
ace_segment::ClockWriter::clearToEnd
void clearToEnd(uint8_t pos)
Clear the display from pos to the end.
Definition: ClockWriter.h:151
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:108
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:95
ace_segment::ClockWriter::numberWriter
NumberWriter< T_LED_MODULE > & numberWriter()
Get the underlying NumberWriter.
Definition: ClockWriter.h:73
ace_segment::ClockWriter::clear
void clear()
Clear the entire display.
Definition: ClockWriter.h:148
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:68
ace_segment::ClockWriter::ClockWriter
ClockWriter(T_LED_MODULE &ledModule, uint8_t colonDigit=1)
Constructor.
Definition: ClockWriter.h:56
ace_segment::ClockWriter::ledModule
T_LED_MODULE & ledModule()
Get the underlying LedModule.
Definition: ClockWriter.h:65
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:45
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:85