AceSegment  0.8.0
A framework for rendering seven segment LED displays using the TM1637, MAX7219, HT16K33, or 74HC595 controller chips
StringWriter.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_STRING_WRITER_H
26 #define ACE_SEGMENT_STRING_WRITER_H
27 
28 #include <stdint.h>
29 #include <AceCommon.h> // FlashString
30 #include "../LedModule.h"
31 #include "CharWriter.h"
32 
33 class __FlashStringHelper;
34 
35 namespace ace_segment {
36 
42 class StringWriter {
43  public:
46  mCharWriter(charWriter)
47  {}
48 
50  LedModule& ledModule() { return mCharWriter.ledModule(); }
51 
53  CharWriter& charWriter() { return mCharWriter; }
54 
61  uint8_t writeStringAt(
62  uint8_t pos,
63  const char* cs,
64  uint8_t numChar = 255
65  ) {
66  return writeStringInternalAt<const char*>(pos, cs, numChar);
67  }
68 
75  uint8_t writeStringAt(
76  uint8_t pos,
77  const __FlashStringHelper* fs,
78  uint8_t numChar = 255
79  ) {
80  return writeStringInternalAt<ace_common::FlashString>(
81  pos, ace_common::FlashString(fs), numChar);
82  }
83 
85  void clear() { mCharWriter.clear(); }
86 
88  void clearToEnd(uint8_t pos) { mCharWriter.clearToEnd(pos); }
89 
90  private:
91  // disable copy-constructor and assignment operator
92  StringWriter(const StringWriter&) = delete;
93  StringWriter& operator=(const StringWriter&) = delete;
94 
106  template <typename T>
107  uint8_t writeStringInternalAt(uint8_t pos, T s, uint8_t numChar) {
108  const uint8_t numDigits = mCharWriter.getNumDigits();
109  const uint8_t originalPos = pos;
110  bool charWasWritten = false;
111 
112  while (numChar--) {
113  char c = *s;
114  if (c == '\0') break;
115  if (pos >= numDigits) break;
116 
117  // Use the decimal point just after a digit to render the '.' character.
118  if (c == '.') {
119  if (charWasWritten) {
120  mCharWriter.writeDecimalPointAt(pos - 1);
121  } else {
122  mCharWriter.writeCharAt(pos, '.');
123  charWasWritten = false;
124  pos++;
125  }
126  } else {
127  mCharWriter.writeCharAt(pos, c);
128  charWasWritten = true;
129  pos++;
130  }
131  s++;
132  }
133 
134  return pos - originalPos;
135  }
136 
137  private:
138  CharWriter& mCharWriter;
139 };
140 
141 }
142 
143 #endif
ace_segment::CharWriter::writeDecimalPointAt
void writeDecimalPointAt(uint8_t pos, bool state=true)
Write the decimal point for the pos.
Definition: CharWriter.h:80
ace_segment::CharWriter::clear
void clear()
Clear the entire display.
Definition: CharWriter.h:85
ace_segment::LedModule
General interface that represents a generic seven-segment LED module with multiple digits.
Definition: LedModule.h:44
ace_segment::CharWriter
The CharWriter supports mapping of an 8-bit character set to segment patterns supported by LedModule.
Definition: CharWriter.h:38
ace_segment::StringWriter::writeStringAt
uint8_t writeStringAt(uint8_t pos, const char *cs, uint8_t numChar=255)
Write c-string cs at specified position pos up to numChar characters.
Definition: StringWriter.h:61
ace_segment::StringWriter::clear
void clear()
Clear the entire display.
Definition: StringWriter.h:85
ace_segment::CharWriter::ledModule
LedModule & ledModule() const
Get the underlying LedModule.
Definition: CharWriter.h:65
ace_segment::CharWriter::clearToEnd
void clearToEnd(uint8_t pos)
Clear the display from pos to the end.
Definition: CharWriter.h:88
ace_segment::StringWriter
Class that writes out a string, collapsing '.
Definition: StringWriter.h:42
ace_segment::StringWriter::StringWriter
StringWriter(CharWriter &charWriter)
Constructor.
Definition: StringWriter.h:45
ace_segment::CharWriter::writeCharAt
void writeCharAt(uint8_t pos, char c)
Write the character at the specified position.
Definition: CharWriter.cpp:191
ace_segment::StringWriter::ledModule
LedModule & ledModule()
Get the underlying LedModule.
Definition: StringWriter.h:50
ace_segment::StringWriter::clearToEnd
void clearToEnd(uint8_t pos)
Clear the display from pos to the end.
Definition: StringWriter.h:88
ace_segment::StringWriter::charWriter
CharWriter & charWriter()
Get the underlying LedModule.
Definition: StringWriter.h:53
ace_segment::CharWriter::getNumDigits
uint8_t getNumDigits() const
Return the number of digits supported by this display instance.
Definition: CharWriter.h:68
ace_segment::StringWriter::writeStringAt
uint8_t writeStringAt(uint8_t pos, const __FlashStringHelper *fs, uint8_t numChar=255)
Write flash string fs at specified position pos up to numChar characters.
Definition: StringWriter.h:75