AceSegment  0.4.0
An adjustable, configurable, and extensible framework for rendering seven segment LED displays.
ClockWriter.h
1 /*
2 MIT License
3 
4 Copyright (c) 2018 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 "LedDisplay.h"
30 #include "NumberWriter.h"
31 
32 namespace ace_segment {
33 
38 class ClockWriter {
39  public:
40  using hexchar_t = NumberWriter::hexchar_t;
41 
43  static const hexchar_t kCharSpace = NumberWriter::kCharSpace;
44 
46  static const hexchar_t kCharMinus = NumberWriter::kCharMinus;
47 
49  static const uint8_t kPatternA = 0b01110111;
50 
52  static const uint8_t kPatternP = 0b01110011;
53 
55  static const uint8_t kPatternDegree = 0b01100011;
56 
66  explicit ClockWriter(
67  LedDisplay& ledDisplay,
68  uint8_t colonDigit = 1
69  ) :
70  mNumberWriter(ledDisplay),
71  mColonDigit(colonDigit)
72  {}
73 
75  LedDisplay& display() const {
76  return mNumberWriter.display();
77  }
78 
80  void writeCharAt(uint8_t pos, hexchar_t c) {
81  mNumberWriter.writeHexCharAt(pos, c);
82  }
83 
89  void writeBcd2At(uint8_t pos, uint8_t bcd) {
90  uint8_t high = (bcd & 0xF0) >> 4;
91  uint8_t low = (bcd & 0x0F);
92  if (high > 9) high = kCharSpace;
93  if (low > 9) low = kCharSpace;
94  mNumberWriter.writeHexCharAt(pos++, high);
95  mNumberWriter.writeHexCharAt(pos++, low);
96  }
97 
103  void writeDec2At(uint8_t pos, uint8_t d) {
104  if (d >= 100) {
105  mNumberWriter.writeHexCharAt(pos++, kCharSpace);
106  mNumberWriter.writeHexCharAt(pos++, kCharSpace);
107  } else {
108  uint8_t bcd = ace_common::decToBcd(d);
109  writeBcd2At(pos, bcd);
110  }
111  }
112 
117  void writeDec4At(uint8_t pos, uint16_t dd) {
118  uint8_t high = dd / 100;
119  uint8_t low = dd - high * 100;
120  writeDec2At(pos, high);
121  writeDec2At(pos + 2, low);
122  }
123 
128  void writeHourMinute(uint8_t hh, uint8_t mm) {
129  writeDec2At(0, hh);
130  writeDec2At(2, mm);
131  writeColon();
132  }
133 
139  void writeColon(bool state = true) {
140  display().writeDecimalPointAt(mColonDigit, state);
141  }
142 
143  private:
144  // disable copy-constructor and assignment operator
145  ClockWriter(const ClockWriter&) = delete;
146  ClockWriter& operator=(const ClockWriter&) = delete;
147 
148  NumberWriter mNumberWriter;
149  uint8_t const mColonDigit;
150 };
151 
152 } // ace_segment
153 
154 #endif
ace_segment::NumberWriter::display
LedDisplay & display() const
Get the underlying LedDisplay.
Definition: NumberWriter.h:73
ace_segment::NumberWriter::writeHexCharAt
void writeHexCharAt(uint8_t pos, hexchar_t c)
Write the hex character c at position pos.
Definition: NumberWriter.h:82
ace_segment::ClockWriter::writeCharAt
void writeCharAt(uint8_t pos, hexchar_t c)
Write the hexchar_t 'c' at 'pos'.
Definition: ClockWriter.h:80
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:89
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:128
ace_segment::ClockWriter::writeColon
void writeColon(bool state=true)
Write the colon symbol between 'hh' and 'mm'.
Definition: ClockWriter.h:139
ace_segment::ClockWriter::kCharMinus
static const hexchar_t kCharMinus
A minus ("-") sign.
Definition: ClockWriter.h:46
ace_segment::LedDisplay
General interface for writing LED segment patterns to the LED display module.
Definition: LedDisplay.h:42
ace_segment::ClockWriter::kPatternP
static const uint8_t kPatternP
The "P" character for "PM".
Definition: ClockWriter.h:52
ace_segment::ClockWriter::kCharSpace
static const hexchar_t kCharSpace
Blank digit.
Definition: ClockWriter.h:43
ace_segment::LedDisplay::writeDecimalPointAt
virtual void writeDecimalPointAt(uint8_t pos, bool state=true)=0
Write the decimal point for the pos.
ace_segment::ClockWriter::display
LedDisplay & display() const
Get the underlying LedDisplay.
Definition: ClockWriter.h:75
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:49
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:117
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:103
ace_segment::ClockWriter::kPatternDegree
static const uint8_t kPatternDegree
The superscript degree symbol for temperature.
Definition: ClockWriter.h:55
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::ClockWriter
The ClockWriter writes "hh:mm" and "yyyy" to the LedDisplay.
Definition: ClockWriter.h:38
ace_segment::ClockWriter::ClockWriter
ClockWriter(LedDisplay &ledDisplay, uint8_t colonDigit=1)
Constructor.
Definition: ClockWriter.h:66