AceSegmentWriter  0.5
Write decimal numbers, hex numbers, temperature, clock digits, characters, and strings to seven segment LED modules
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_WRITER_STRING_WRITER_H
26 #define ACE_SEGMENT_WRITER_STRING_WRITER_H
27 
28 #include <stdint.h>
29 #include <AceCommon.h> // FlashString
30 #include "CharWriter.h"
31 
32 class __FlashStringHelper;
33 
34 namespace ace_segment {
35 
44 template <typename T_LED_MODULE>
45 class StringWriter {
46  public:
49  mCharWriter(charWriter)
50  {}
51 
53  T_LED_MODULE& ledModule() { return mCharWriter.ledModule(); }
54 
57  return mCharWriter.patternWriter();
58  }
59 
61  CharWriter<T_LED_MODULE>& charWriter() { return mCharWriter; }
62 
64  void home() { mCharWriter.home(); }
65 
72  uint8_t writeString(const char* cs, uint8_t numChar = 255) {
73  return writeStringInternal<const char*>(cs, numChar);
74  }
75 
82  uint8_t writeString(const __FlashStringHelper* fs, uint8_t numChar = 255) {
83  return writeStringInternal<ace_common::FlashString>(
84  ace_common::FlashString(fs), numChar);
85  }
86 
88  void clear() { mCharWriter.clear(); }
89 
91  void clearToEnd() { mCharWriter.clearToEnd(); }
92 
93  private:
94  // disable copy-constructor and assignment operator
95  StringWriter(const StringWriter&) = delete;
96  StringWriter& operator=(const StringWriter&) = delete;
97 
109  template <typename T>
110  uint8_t writeStringInternal(T s, uint8_t numChar) {
111  const uint8_t numDigits = mCharWriter.size();
112  bool charWasWritten = false;
113  uint8_t numWritten = 0;
114 
115  while (numChar--) {
116  char c = *s;
117  if (c == '\0') break;
118  uint8_t pos = patternWriter().pos();
119  if (pos >= numDigits) break;
120 
121  // Use the decimal point just after a digit to render the '.' character.
122  if (c == '.') {
123  if (charWasWritten) {
124  mCharWriter.patternWriter().writeDecimalPoint();
125  } else {
126  mCharWriter.writeChar('.');
127  charWasWritten = false;
128  numWritten++;
129  }
130  } else {
131  mCharWriter.writeChar(c);
132  charWasWritten = true;
133  numWritten++;
134  }
135  s++;
136  }
137 
138  return numWritten;
139  }
140 
141  private:
142  CharWriter<T_LED_MODULE>& mCharWriter;
143 };
144 
145 }
146 
147 #endif
The CharWriter supports mapping of an 8-bit character set to segment patterns supported by LedModule.
Definition: CharWriter.h:58
Write LED segment patterns to the underlying LedModule.
Class that writes out a string, collapsing '.
Definition: StringWriter.h:45
void home()
Set the cursor to the beginning.
Definition: StringWriter.h:64
StringWriter(CharWriter< T_LED_MODULE > &charWriter)
Constructor.
Definition: StringWriter.h:48
T_LED_MODULE & ledModule()
Get the underlying LedModule.
Definition: StringWriter.h:53
void clearToEnd()
Clear the display from pos to the end.
Definition: StringWriter.h:91
void clear()
Clear the entire display.
Definition: StringWriter.h:88
uint8_t writeString(const __FlashStringHelper *fs, uint8_t numChar=255)
Write flash string fs at specified position pos up to numChar characters.
Definition: StringWriter.h:82
uint8_t writeString(const char *cs, uint8_t numChar=255)
Write c-string cs at specified position pos up to numChar characters.
Definition: StringWriter.h:72
PatternWriter< T_LED_MODULE > & patternWriter()
Get the underlying PatternWriter.
Definition: StringWriter.h:56
CharWriter< T_LED_MODULE > & charWriter()
Get the underlying CharWriter.
Definition: StringWriter.h:61