AceSegment  0.8.0
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 
37 class ClockWriter {
38  public:
39  using hexchar_t = NumberWriter::hexchar_t;
40 
42  static const hexchar_t kCharSpace = NumberWriter::kCharSpace;
43 
45  static const hexchar_t kCharMinus = NumberWriter::kCharMinus;
46 
48  static const uint8_t kPatternA = 0b01110111;
49 
51  static const uint8_t kPatternP = 0b01110011;
52 
62  explicit ClockWriter(
64  uint8_t colonDigit = 1
65  ) :
66  mNumberWriter(ledModule),
67  mColonDigit(colonDigit)
68  {}
69 
71  LedModule& ledModule() { return mNumberWriter.ledModule(); }
72 
74  void writeCharAt(uint8_t pos, hexchar_t c) {
75  mNumberWriter.writeHexCharAt(pos, c);
76  }
77 
83  void writeChars2At(uint8_t pos, hexchar_t c0, hexchar_t c1) {
84  writeCharAt(pos++, c0);
85  writeCharAt(pos++, c1);
86  }
87 
93  void writeBcd2At(uint8_t pos, uint8_t bcd) {
94  uint8_t high = (bcd & 0xF0) >> 4;
95  uint8_t low = (bcd & 0x0F);
96  if (high > 9) high = kCharSpace;
97  if (low > 9) low = kCharSpace;
98  writeChars2At(pos, high, low);
99  }
100 
106  void writeDec2At(uint8_t pos, uint8_t d) {
107  if (d >= 100) {
109  } else {
110  uint8_t bcd = ace_common::decToBcd(d);
111  writeBcd2At(pos, bcd);
112  }
113  }
114 
119  void writeDec4At(uint8_t pos, uint16_t dd) {
120  uint8_t high = dd / 100;
121  uint8_t low = dd - high * 100;
122  writeDec2At(pos, high);
123  writeDec2At(pos + 2, low);
124  }
125 
130  void writeHourMinute(uint8_t hh, uint8_t mm) {
131  writeDec2At(0, hh);
132  writeDec2At(2, mm);
133  writeColon();
134  }
135 
141  void writeColon(bool state = true) {
142  mNumberWriter.writeDecimalPointAt(mColonDigit, state);
143  }
144 
146  void clear() { mNumberWriter.clear(); }
147 
149  void clearToEnd(uint8_t pos) { mNumberWriter.clearToEnd(pos); }
150 
151  private:
152  // disable copy-constructor and assignment operator
153  ClockWriter(const ClockWriter&) = delete;
154  ClockWriter& operator=(const ClockWriter&) = delete;
155 
156  NumberWriter mNumberWriter;
157  uint8_t const mColonDigit;
158 };
159 
160 } // ace_segment
161 
162 #endif
ace_segment::NumberWriter::writeHexCharAt
void writeHexCharAt(uint8_t pos, hexchar_t c)
Write the hex character c at position pos.
Definition: NumberWriter.h:83
ace_segment::NumberWriter::clearToEnd
void clearToEnd(uint8_t pos)
Clear the display from pos to the end.
Definition: NumberWriter.h:164
ace_segment::ClockWriter::writeCharAt
void writeCharAt(uint8_t pos, hexchar_t c)
Write the hexchar_t 'c' at 'pos'.
Definition: ClockWriter.h:74
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:93
ace_segment::LedModule
General interface that represents a generic seven-segment LED module with multiple digits.
Definition: LedModule.h:44
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:130
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:83
ace_segment::ClockWriter::writeColon
void writeColon(bool state=true)
Write the colon symbol between 'hh' and 'mm'.
Definition: ClockWriter.h:141
ace_segment::ClockWriter::kCharMinus
static const hexchar_t kCharMinus
A minus ("-") sign.
Definition: ClockWriter.h:45
ace_segment::ClockWriter::clear
void clear()
Clear the entire display.
Definition: ClockWriter.h:146
ace_segment::ClockWriter::kPatternP
static const uint8_t kPatternP
The "P" character for "PM".
Definition: ClockWriter.h:51
ace_segment::ClockWriter::kCharSpace
static const hexchar_t kCharSpace
Blank digit.
Definition: ClockWriter.h:42
ace_segment::NumberWriter::clear
void clear()
Clear the entire display.
Definition: NumberWriter.h:157
ace_segment::NumberWriter::writeDecimalPointAt
void writeDecimalPointAt(uint8_t pos, bool state=true)
Write the decimal point for the pos.
Definition: NumberWriter.h:152
ace_segment::ClockWriter::ledModule
LedModule & ledModule()
Get the underlying LedModule.
Definition: ClockWriter.h:71
ace_segment::NumberWriter::hexchar_t
uint8_t hexchar_t
The type of the character set supported by many methods in this class, usually containing the string ...
Definition: NumberWriter.h:56
ace_segment::NumberWriter::kCharMinus
static const hexchar_t kCharMinus
A minus character.
Definition: NumberWriter.h:65
ace_segment::ClockWriter::kPatternA
static const uint8_t kPatternA
The "A" character for "AM".
Definition: ClockWriter.h:48
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:119
ace_segment::ClockWriter::ClockWriter
ClockWriter(LedModule &ledModule, uint8_t colonDigit=1)
Constructor.
Definition: ClockWriter.h:62
ace_segment::ClockWriter::clearToEnd
void clearToEnd(uint8_t pos)
Clear the display from pos to the end.
Definition: ClockWriter.h:149
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:106
ace_segment::NumberWriter
The NumberWriter supports converting decimal and hexadecimal numbers to segment patterns expected by ...
Definition: NumberWriter.h:39
ace_segment::NumberWriter::kCharSpace
static const hexchar_t kCharSpace
A space character.
Definition: NumberWriter.h:62
ace_segment::NumberWriter::ledModule
LedModule & ledModule()
Get the underlying LedModule.
Definition: NumberWriter.h:73
ace_segment::ClockWriter
The ClockWriter writes "hh:mm" and "yyyy" to the LedModule.
Definition: ClockWriter.h:37