AceSegment  0.3.0
An adjustable, configurable, and extensible framework for rendering seven segment LED displays.
HexWriter.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 "HexWriter.h"
28 
29 namespace ace_segment {
30 
31 // Bit patterns for Hex characters (0x00 - 0x0F) plus a few symbols.
32 // Adapted from https://github.com/dmadison/LED-Segment-ASCII.
33 //
34 // 7-segment map:
35 // AAA 000
36 // F B 5 1
37 // F B 5 1
38 // GGG 666
39 // E C 4 2
40 // E C 4 2
41 // DDD DP 333 77
42 //
43 // Segment: DP G F E D C B A
44 // Bits: 7 6 5 4 3 2 1 0
45 //
46 const uint8_t HexWriter::kCharacterArray[] PROGMEM = {
47  0b00111111, /* 0 */
48  0b00000110, /* 1 */
49  0b01011011, /* 2 */
50  0b01001111, /* 3 */
51  0b01100110, /* 4 */
52  0b01101101, /* 5 */
53  0b01111101, /* 6 */
54  0b00000111, /* 7 */
55  0b01111111, /* 8 */
56  0b01101111, /* 9 */
57  0b01110111, /* A */
58  0b01111100, /* b */
59  0b00111001, /* C */
60  0b01011110, /* d */
61  0b01111001, /* E */
62  0b01110001, /* F */
63  0b00000000, /* (space) */
64  0b10000000, /* . */
65  0b01000000, /* - */
66 };
67 
68 const uint8_t HexWriter::kNumCharacters =
69  sizeof(kCharacterArray)/sizeof(kCharacterArray[0]);
70 
71 void HexWriter::writeHexAt(uint8_t digit, uint8_t c, uint8_t style) {
72  if (digit >= getNumDigits()) return;
73  uint8_t pattern = ((uint8_t) c < kNumCharacters)
74  ? pgm_read_byte(&kCharacterArray[(uint8_t) c])
75  : kMinus;
76  mRenderer->writePatternAt(digit, pattern, style);
77 }
78 
79 void HexWriter::writeHexAt(uint8_t digit, uint8_t c) {
80  if (digit >= getNumDigits()) return;
81  uint8_t pattern = ((uint8_t) c < kNumCharacters)
82  ? pgm_read_byte(&kCharacterArray[(uint8_t) c])
83  : kMinus;
84  mRenderer->writePatternAt(digit, pattern);
85 }
86 
87 }
uint8_t getNumDigits()
Get the number of digits.
Definition: HexWriter.h:52
void writePatternAt(uint8_t digit, uint8_t pattern, uint8_t style)
Write the pattern and style for a given digit.
Definition: Renderer.cpp:56
void writeHexAt(uint8_t digit, uint8_t c)
Write the hex at the specified position.
Definition: HexWriter.cpp:79