AceSegment  0.4.0
An adjustable, configurable, and extensible framework for rendering seven segment LED displays.
CharWriter.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 <Arduino.h>
26 #include "CharWriter.h"
27 
28 namespace ace_segment {
29 
30 // A poor-man's '?': 0b10000011
31 static const uint8_t UNKNOWN = 0b10000011;
32 
33 // Bit patterns for ASCII characters (0 - 127).
34 // Adapted from https://github.com/dmadison/LED-Segment-ASCII.
35 //
36 // 7-segment map:
37 // AAA 000
38 // F B 5 1
39 // F B 5 1
40 // GGG 666
41 // E C 4 2
42 // E C 4 2
43 // DDD DP 333 77
44 //
45 // Segment: DP G F E D C B A
46 // Bits: 7 6 5 4 3 2 1 0
47 //
48 const uint8_t CharWriter::kCharacterArray[] PROGMEM = {
49  UNKNOWN, /* 00 */
50  UNKNOWN, /* 01 */
51  UNKNOWN, /* 02 */
52  UNKNOWN, /* 03 */
53  UNKNOWN, /* 04 */
54  UNKNOWN, /* 05 */
55  UNKNOWN, /* 06 */
56  UNKNOWN, /* 07 */
57  UNKNOWN, /* 08 */
58  UNKNOWN, /* 09 */
59  UNKNOWN, /* 10 */
60  UNKNOWN, /* 11 */
61  UNKNOWN, /* 12 */
62  UNKNOWN, /* 13 */
63  UNKNOWN, /* 14 */
64  UNKNOWN, /* 15 */
65  UNKNOWN, /* 16 */
66  UNKNOWN, /* 17 */
67  UNKNOWN, /* 18 */
68  UNKNOWN, /* 19 */
69  UNKNOWN, /* 20 */
70  UNKNOWN, /* 21 */
71  UNKNOWN, /* 22 */
72  UNKNOWN, /* 23 */
73  UNKNOWN, /* 24 */
74  UNKNOWN, /* 25 */
75  UNKNOWN, /* 26 */
76  UNKNOWN, /* 27 */
77  UNKNOWN, /* 28 */
78  UNKNOWN, /* 29 */
79  UNKNOWN, /* 30 */
80  UNKNOWN, /* 31 */
81  0b00000000, /* (space) */
82  0b10000110, /* ! */
83  0b00100010, /* " */
84  0b01111110, /* # */
85  0b01101101, /* $ */
86  0b11010010, /* % */
87  0b01000110, /* & */
88  0b00100000, /* ' */
89  0b00101001, /* ( */
90  0b00001011, /* ) */
91  0b00100001, /* * */
92  0b01110000, /* + */
93  0b00010000, /* , */
94  0b01000000, /* - */
95  0b10000000, /* . */
96  0b01010010, /* / */
97  0b00111111, /* 0 */
98  0b00000110, /* 1 */
99  0b01011011, /* 2 */
100  0b01001111, /* 3 */
101  0b01100110, /* 4 */
102  0b01101101, /* 5 */
103  0b01111101, /* 6 */
104  0b00000111, /* 7 */
105  0b01111111, /* 8 */
106  0b01101111, /* 9 */
107  0b00001001, /* : */
108  0b00001101, /* ; */
109  0b01100001, /* < */
110  0b01001000, /* = */
111  0b01000011, /* > */
112  0b11010011, /* ? */
113  0b01011111, /* @ */
114  0b01110111, /* A */
115  0b01111100, /* B */
116  0b00111001, /* C */
117  0b01011110, /* D */
118  0b01111001, /* E */
119  0b01110001, /* F */
120  0b00111101, /* G */
121  0b01110110, /* H */
122  0b00110000, /* I */
123  0b00011110, /* J */
124  0b01110101, /* K */
125  0b00111000, /* L */
126  0b00010101, /* M */
127  0b00110111, /* N */
128  0b00111111, /* O */
129  0b01110011, /* P */
130  0b01101011, /* Q */
131  0b00110011, /* R */
132  0b01101101, /* S */
133  0b01111000, /* T */
134  0b00111110, /* U */
135  0b00111110, /* V */
136  0b00101010, /* W */
137  0b01110110, /* X */
138  0b01101110, /* Y */
139  0b01011011, /* Z */
140  0b00111001, /* [ */
141  0b01100100, /* \ */
142  0b00001111, /* ] */
143  0b00100011, /* ^ */
144  0b00001000, /* _ */
145  0b00000010, /* ` */
146  0b01011111, /* a */
147  0b01111100, /* b */
148  0b01011000, /* c */
149  0b01011110, /* d */
150  0b01111011, /* e */
151  0b01110001, /* f */
152  0b01101111, /* g */
153  0b01110100, /* h */
154  0b00010000, /* i */
155  0b00001100, /* j */
156  0b01110101, /* k */
157  0b00110000, /* l */
158  0b00010100, /* m */
159  0b01010100, /* n */
160  0b01011100, /* o */
161  0b01110011, /* p */
162  0b01100111, /* q */
163  0b01010000, /* r */
164  0b01101101, /* s */
165  0b01111000, /* t */
166  0b00011100, /* u */
167  0b00011100, /* v */
168  0b00010100, /* w */
169  0b01110110, /* x */
170  0b01101110, /* y */
171  0b01011011, /* z */
172  0b01000110, /* { */
173  0b00110000, /* | */
174  0b01110000, /* } */
175  0b00000001, /* ~ */
176  UNKNOWN, /* (del) */
177 };
178 
179 void CharWriter::writeCharAt(uint8_t pos, char c) {
180  if (pos >= mLedDisplay.getNumDigits()) return;
181  uint8_t pattern = ((uint8_t) c < kNumCharacters)
182  ? pgm_read_byte(&kCharacterArray[(uint8_t) c])
183  : UNKNOWN;
184  mLedDisplay.writePatternAt(pos, pattern);
185 }
186 
187 }
ace_segment::CharWriter::writeCharAt
void writeCharAt(uint8_t pos, char c)
Write the character at the specified position.
Definition: CharWriter.cpp:179
ace_segment::LedDisplay::getNumDigits
uint8_t getNumDigits() const
Return the number of digits supported by this display instance.
Definition: LedDisplay.h:99
ace_segment::LedDisplay::writePatternAt
virtual void writePatternAt(uint8_t pos, uint8_t pattern)=0
Write the pattern for a given pos.