AceSegment  0.8.0
A framework for rendering seven segment LED displays using the TM1637, MAX7219, HT16K33, or 74HC595 controller chips
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_STRING_SCROLLER_H
26 #define ACE_SEGMENT_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 
40  public:
43  mCharWriter(charWriter)
44  {}
45 
47  LedModule& ledModule() const { return mCharWriter.ledModule(); }
48 
50  CharWriter& charWriter() { return mCharWriter; }
51 
53  uint8_t getNumDigits() const { return mCharWriter.getNumDigits(); }
54 
56  void initScrollLeft(const char* s) {
57  mString = s;
58  mIsFlashString = false;
59  mStringLength = strlen(s);
60  mStringPos = -getNumDigits(); // start with clear display
61  mCharWriter.clear();
62  }
63 
65  void initScrollLeft(const __FlashStringHelper* fs) {
66  mString = fs;
67  mIsFlashString = true;
68  mStringLength = strlen_P((const char*) fs);
69  mStringPos = -getNumDigits(); // start with clear display
70  mCharWriter.clear();
71  }
72 
77  bool scrollLeft() {
78  bool isDone = mStringPos >= mStringLength;
79  if (! isDone) {
80  mStringPos++;
81  }
82  writeString();
83  return isDone;
84  }
85 
87  void initScrollRight(const char* s) {
88  mString = s;
89  mIsFlashString = false;
90  mStringLength = strlen(s);
91  mStringPos = mStringLength; // start with clear display
92  mCharWriter.clear();
93  }
94 
96  void initScrollRight(const __FlashStringHelper* fs) {
97  mString = fs;
98  mIsFlashString = true;
99  mStringLength = strlen_P((const char*) fs);
100  mStringPos = mStringLength; // start with clear display
101  mCharWriter.clear();
102  }
103 
108  bool scrollRight() {
109  uint8_t numDigits = getNumDigits();
110  bool isDone = (mStringPos <= -numDigits);
111  if (! isDone) {
112  mStringPos--;
113  }
114  writeString();
115  return isDone;
116  }
117 
118  private:
119  // disable copy-constructor and assignment operator
120  StringScroller(const StringScroller&) = delete;
121  StringScroller& operator=(const StringScroller&) = delete;
122 
123  void writeString() {
124  uint8_t numDigits = getNumDigits();
125  int16_t stringPos = mStringPos;
126  for (uint8_t pos = 0; pos < numDigits; pos++) {
127  char c;
128  if (stringPos < 0 || stringPos >= mStringLength) {
129  c = ' ';
130  } else {
131  // Normally inserting an if-conditional inside a for-loop is not a
132  // good idea. But this is not a speed-critical loop and it loops over
133  // only the number of digits, which will almost always be <= 8. So I
134  // think this is better than creating a duplicate version of this
135  // function using a template function.
136  if (mIsFlashString) {
137  c = pgm_read_byte((const uint8_t*) mString + stringPos);
138  } else {
139  c = *((const char*) mString + stringPos);
140  }
141  }
142  mCharWriter.writeCharAt(pos, c);
143  stringPos++;
144  }
145  }
146 
147  private:
148  // The order of these fields is partially motivated to reduce memory
149  // consumption on 32-bit processors.
150  CharWriter& mCharWriter;
151  const void* mString;
152  int16_t mStringPos; // can become negative
153  uint8_t mStringLength;
154  bool mIsFlashString;
155 };
156 
157 } // ace_segment
158 
159 #endif
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::StringScroller
Class that scrolls a string left or right.
Definition: StringScroller.h:39
ace_segment::StringScroller::ledModule
LedModule & ledModule() const
Get the underlying LedModule.
Definition: StringScroller.h:47
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::StringScroller::charWriter
CharWriter & charWriter()
Get the underlying LedModule.
Definition: StringScroller.h:50
ace_segment::StringScroller::scrollLeft
bool scrollLeft()
Scroll one position left.
Definition: StringScroller.h:77
ace_segment::StringScroller::initScrollRight
void initScrollRight(const char *s)
Set scroll string, clear the display, and prepare to scroll right.
Definition: StringScroller.h:87
ace_segment::CharWriter::ledModule
LedModule & ledModule() const
Get the underlying LedModule.
Definition: CharWriter.h:65
ace_segment::StringScroller::StringScroller
StringScroller(CharWriter &charWriter)
Constructor.
Definition: StringScroller.h:42
ace_segment::CharWriter::writeCharAt
void writeCharAt(uint8_t pos, char c)
Write the character at the specified position.
Definition: CharWriter.cpp:191
ace_segment::StringScroller::scrollRight
bool scrollRight()
Scroll one position left.
Definition: StringScroller.h:108
ace_segment::StringScroller::initScrollLeft
void initScrollLeft(const __FlashStringHelper *fs)
Set scroll string, clear the display, and prepare to scroll left.
Definition: StringScroller.h:65
ace_segment::CharWriter::getNumDigits
uint8_t getNumDigits() const
Return the number of digits supported by this display instance.
Definition: CharWriter.h:68
ace_segment::StringScroller::initScrollLeft
void initScrollLeft(const char *s)
Set scroll string, clear the display, and prepare to scroll left.
Definition: StringScroller.h:56
ace_segment::StringScroller::initScrollRight
void initScrollRight(const __FlashStringHelper *fs)
Set scroll string, clear the display, and prepare to scroll right.
Definition: StringScroller.h:96
ace_segment::StringScroller::getNumDigits
uint8_t getNumDigits() const
Return the number of digits supported by this display instance.
Definition: StringScroller.h:53