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