AceSegment  0.4.0
An adjustable, configurable, and extensible framework for rendering seven segment LED displays.
StringWriter.cpp
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 #include "StringWriter.h"
26 
27 namespace ace_segment {
28 
29 void StringWriter::writeStringAt(uint8_t digit, const char* s, bool padRight) {
30  LedDisplay& ledDisplay = mCharWriter.display();
31  bool charWasWritten = false;
32  uint8_t numDigits = ledDisplay.getNumDigits();
33 
34  while (true) {
35  char c = *s;
36  if (c == 0) break;
37  if (digit >= numDigits) break;
38 
39  // Use the decimal point just after a digit to render the '.' character.
40  if (c == '.') {
41  if (charWasWritten) {
42  ledDisplay.writeDecimalPointAt(digit - 1);
43  } else {
44  mCharWriter.writeCharAt(digit, '.');
45  charWasWritten = false;
46  digit++;
47  }
48  } else {
49  mCharWriter.writeCharAt(digit, c);
50  charWasWritten = true;
51  digit++;
52  }
53  s++;
54  }
55 
56  if (padRight) {
57  for (; digit < numDigits; digit++) {
58  mCharWriter.writeCharAt(digit, ' ');
59  }
60  }
61 }
62 
63 }
ace_segment::StringWriter::writeStringAt
void writeStringAt(uint8_t pos, const char *s, bool padRight=false)
Write the string beginning at the specified position, filling up to numDigits.
Definition: StringWriter.cpp:29
ace_segment::LedDisplay
General interface for writing LED segment patterns to the LED display module.
Definition: LedDisplay.h:42
ace_segment::LedDisplay::writeDecimalPointAt
virtual void writeDecimalPointAt(uint8_t pos, bool state=true)=0
Write the decimal point for the pos.
ace_segment::CharWriter::writeCharAt
void writeCharAt(uint8_t pos, char c)
Write the character at the specified position.
Definition: CharWriter.cpp:179
ace_segment::CharWriter::display
LedDisplay & display() const
Get the underlying LedDisplay.
Definition: CharWriter.h:50
ace_segment::LedDisplay::getNumDigits
uint8_t getNumDigits() const
Return the number of digits supported by this display instance.
Definition: LedDisplay.h:99