AceSegmentWriter  0.1.0
Write decimal numbers, hex numbers, temperature, clock digits, characters, and strings to seven segment LED modules
StringScroller.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_STRING_SCROLLER_H
26 #define ACE_SEGMENT_WRITER_STRING_SCROLLER_H
27 
28 #include <stdint.h>
29 #include <Arduino.h> // pgm_read_byte(), strlen_P()
30 #include "StringWriter.h"
31 
32 class __FlashStringHelper;
33 
34 namespace ace_segment {
35 
42 template <typename T_LED_MODULE>
44  public:
47  mCharWriter(charWriter)
48  {}
49 
51  T_LED_MODULE& ledModule() { return mCharWriter.ledModule(); }
52 
55  return mCharWriter.patternWriter();
56  }
57 
59  CharWriter<T_LED_MODULE>& charWriter() { return mCharWriter; }
60 
62  uint8_t getNumDigits() const { return mCharWriter.getNumDigits(); }
63 
65  void initScrollLeft(const char* s) {
66  mString = s;
67  mIsFlashString = false;
68  mStringLength = strlen(s);
69  mStringPos = -getNumDigits(); // start with clear display
70  mCharWriter.clear();
71  }
72 
74  void initScrollLeft(const __FlashStringHelper* fs) {
75  mString = fs;
76  mIsFlashString = true;
77  mStringLength = strlen_P((const char*) fs);
78  mStringPos = -getNumDigits(); // start with clear display
79  mCharWriter.clear();
80  }
81 
86  bool scrollLeft() {
87  bool isDone = mStringPos >= mStringLength;
88  if (! isDone) {
89  mStringPos++;
90  }
91  writeString();
92  return isDone;
93  }
94 
96  void initScrollRight(const char* s) {
97  mString = s;
98  mIsFlashString = false;
99  mStringLength = strlen(s);
100  mStringPos = mStringLength; // start with clear display
101  mCharWriter.clear();
102  }
103 
105  void initScrollRight(const __FlashStringHelper* fs) {
106  mString = fs;
107  mIsFlashString = true;
108  mStringLength = strlen_P((const char*) fs);
109  mStringPos = mStringLength; // start with clear display
110  mCharWriter.clear();
111  }
112 
117  bool scrollRight() {
118  uint8_t numDigits = getNumDigits();
119  bool isDone = (mStringPos <= -numDigits);
120  if (! isDone) {
121  mStringPos--;
122  }
123  writeString();
124  return isDone;
125  }
126 
127  private:
128  // disable copy-constructor and assignment operator
129  StringScroller(const StringScroller&) = delete;
130  StringScroller& operator=(const StringScroller&) = delete;
131 
132  void writeString() {
133  uint8_t numDigits = getNumDigits();
134  int16_t stringPos = mStringPos;
135  for (uint8_t pos = 0; pos < numDigits; pos++) {
136  char c;
137  if (stringPos < 0 || stringPos >= mStringLength) {
138  c = ' ';
139  } else {
140  // Normally inserting an if-conditional inside a for-loop is not a
141  // good idea. But this is not a speed-critical loop and it loops over
142  // only the number of digits, which will almost always be <= 8. So I
143  // think this is better than creating a duplicate version of this
144  // function using a template function.
145  if (mIsFlashString) {
146  c = pgm_read_byte((const uint8_t*) mString + stringPos);
147  } else {
148  c = *((const char*) mString + stringPos);
149  }
150  }
151  mCharWriter.writeCharAt(pos, c);
152  stringPos++;
153  }
154  }
155 
156  private:
157  // The order of these fields is partially motivated to reduce memory
158  // consumption on 32-bit processors.
159  CharWriter<T_LED_MODULE>& mCharWriter;
160  const void* mString;
161  int16_t mStringPos; // can become negative
162  uint8_t mStringLength;
163  bool mIsFlashString;
164 };
165 
166 } // ace_segment
167 
168 #endif
ace_segment::StringScroller
Class that scrolls a string left or right.
Definition: StringScroller.h:43
ace_segment::StringScroller::getNumDigits
uint8_t getNumDigits() const
Return the number of digits supported by this display instance.
Definition: StringScroller.h:62
ace_segment::StringScroller::initScrollLeft
void initScrollLeft(const char *s)
Set scroll string, clear the display, and prepare to scroll left.
Definition: StringScroller.h:65
ace_segment::CharWriter
The CharWriter supports mapping of an 8-bit character set to segment patterns supported by LedModule.
Definition: CharWriter.h:58
ace_segment::StringScroller::scrollRight
bool scrollRight()
Scroll one position left.
Definition: StringScroller.h:117
ace_segment::StringScroller::StringScroller
StringScroller(CharWriter< T_LED_MODULE > &charWriter)
Constructor.
Definition: StringScroller.h:46
ace_segment::StringScroller::charWriter
CharWriter< T_LED_MODULE > & charWriter()
Get the underlying LedModule.
Definition: StringScroller.h:59
ace_segment::StringScroller::initScrollRight
void initScrollRight(const char *s)
Set scroll string, clear the display, and prepare to scroll right.
Definition: StringScroller.h:96
ace_segment::StringScroller::ledModule
T_LED_MODULE & ledModule()
Get the underlying LedModule.
Definition: StringScroller.h:51
ace_segment::StringScroller::initScrollRight
void initScrollRight(const __FlashStringHelper *fs)
Set scroll string, clear the display, and prepare to scroll right.
Definition: StringScroller.h:105
ace_segment::StringScroller::initScrollLeft
void initScrollLeft(const __FlashStringHelper *fs)
Set scroll string, clear the display, and prepare to scroll left.
Definition: StringScroller.h:74
ace_segment::StringScroller::patternWriter
PatternWriter< T_LED_MODULE > & patternWriter()
Get the underlying PatternWriter.
Definition: StringScroller.h:54
ace_segment::PatternWriter
Write LED segment patterns to the underlying LedModule.
Definition: PatternWriter.h:46
ace_segment::StringScroller::scrollLeft
bool scrollLeft()
Scroll one position left.
Definition: StringScroller.h:86