AceSegmentWriter  0.5
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 size() const { return mCharWriter.size(); }
63 
65  void initScrollLeft(const char* s) {
66  mString = s;
67  mIsFlashString = false;
68  mStringLength = strlen(s);
69  mStringPos = -size(); // 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 = -size(); // 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 = size();
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 = size();
134  int16_t stringPos = mStringPos;
135  mCharWriter.home();
136  for (uint8_t i = 0; i < numDigits; i++) {
137  char c;
138  if (stringPos < 0 || stringPos >= mStringLength) {
139  c = ' ';
140  } else {
141  // Normally inserting an if-conditional inside a for-loop is not a
142  // good idea. But this is not a speed-critical loop and it loops over
143  // only the number of digits, which will almost always be <= 8. So I
144  // think this is better than creating a duplicate version of this
145  // function using a template function.
146  if (mIsFlashString) {
147  c = pgm_read_byte((const uint8_t*) mString + stringPos);
148  } else {
149  c = *((const char*) mString + stringPos);
150  }
151  }
152  mCharWriter.writeChar(c);
153  stringPos++;
154  }
155  }
156 
157  private:
158  // The order of these fields is partially motivated to reduce memory
159  // consumption on 32-bit processors.
160  CharWriter<T_LED_MODULE>& mCharWriter;
161  const void* mString;
162  int16_t mStringPos; // can become negative
163  uint8_t mStringLength;
164  bool mIsFlashString;
165 };
166 
167 } // ace_segment
168 
169 #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 scrolls a string left or right.
void initScrollLeft(const char *s)
Set scroll string, clear the display, and prepare to scroll left.
StringScroller(CharWriter< T_LED_MODULE > &charWriter)
Constructor.
bool scrollRight()
Scroll one position left.
void initScrollRight(const char *s)
Set scroll string, clear the display, and prepare to scroll right.
bool scrollLeft()
Scroll one position left.
CharWriter< T_LED_MODULE > & charWriter()
Get the underlying LedModule.
uint8_t size() const
Return the number of digits supported by this display instance.
void initScrollRight(const __FlashStringHelper *fs)
Set scroll string, clear the display, and prepare to scroll right.
T_LED_MODULE & ledModule()
Get the underlying LedModule.
void initScrollLeft(const __FlashStringHelper *fs)
Set scroll string, clear the display, and prepare to scroll left.
PatternWriter< T_LED_MODULE > & patternWriter()
Get the underlying PatternWriter.