-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLED7Seg.cpp
More file actions
242 lines (225 loc) · 6.65 KB
/
Copy pathLED7Seg.cpp
File metadata and controls
242 lines (225 loc) · 6.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
#include "LED7Seg.h"
/// Constructor
SevSeg::SevSeg()
{
m_index = 0;
m_digits = 0;
}
/// Initialization function
/// @param conf Bit mask (Bit0, Bit1..Bit m_digits)
/// @param digits Number of digits (typically 1-4)
/// @param pins Output pin array
void SevSeg::begin(enum led_config conf, uint8_t digits, const uint8_t* pins)
{
m_config = conf;
m_digits = digits;
m_pins = pins;
// Set all pins as outputs
for (uint8_t i=0 ; i < SEGMENTS + digits; ++i)
{
int pin = m_pins[i];
pinMode(pin, OUTPUT);
}
// Turn all digits off
setDigits(DIG_NONE);
// Turn all segments off
setSegments(SEG_NONE);
m_last = millis();
}
///
/// Set all LED segment pins based on mask
/// @param mask Bit mask (SEG_A,SEG_B,SEG_C,SEG_D,SEG_E,SEG_F,SEG_G,SEG_DP)
///
void SevSeg::setSegments(enum led_seg mask)
{
if (m_config & SEG_INVERT)
mask = (enum led_seg)(~mask);
for (uint8_t i = 0; i < SEGMENTS; ++i)
{
boolean pinState = (mask & 0x01);
int pin = m_pins[i];
digitalWrite(pin, pinState);
mask = (enum led_seg) (mask >> 1);
}
}
///
/// Set LED Digit pins based on mask.
/// @param mask Bit mask (DIG_0..DIG_n-1)
///
void SevSeg::setDigits(enum led_dig mask)
{
if (m_config & DIG_INVERT)
mask = (enum led_dig)(~mask);
for (uint8_t i = 0; i < m_digits; ++i)
{
boolean pinState = (mask & 0x01);
int pin = m_pins[i + SEGMENTS];
digitalWrite(pin, pinState);
mask = (enum led_dig)(mask >> 1);
}
}
///
/// Refresh/multiplex all the digits on the LED Display. Use this method
/// when the current limiting resistors are in series with the segment pins.
///
void SevSeg::refreshDigits()
{
unsigned long now = millis();
if (now != m_last)
{
m_last = now;
// Turn off all digits
setDigits(DIG_NONE);
// Increment m_index modulo m_digits
uint8_t index = m_index + 1;
if (index >= m_digits)
index = 0;
m_index = index;
// Set segments for digit
setSegments(m_buf[index]);
// Turn on one digit at a time
setDigits((enum led_dig)(DIG_0 << index));
}
}
///
/// Refresh/multiplex all the segments on the LED Display. Use this method
/// when the current limiting resistors are in series with the digit pins.
///
void SevSeg::refreshSegments()
{
unsigned long now = millis();
if (now != m_last)
{
enum led_dig digitMask = DIG_NONE;
enum led_seg segMask;
m_last = now;
// Turn off all digits
setDigits(DIG_NONE);
// Increment m_index modulo m_digits
uint8_t index = m_index + 1;
if (index >= SEGMENTS)
index = 0;
m_index = index;
// Turn on one segment at a time
segMask = (enum led_seg)(SEG_A << index);
setSegments(segMask);
// Determines digits containing segment
for (uint8_t dig = 0; dig < MAX_DIGITS; ++dig)
{
if (m_buf[dig] & segMask)
{
digitMask = (enum led_dig)(digitMask | (DIG_0 << dig));
}
}
// Set segment for digit(s)
setDigits(digitMask);
}
}
enum led_seg getHexDigit(int num, uint8_t shift)
{
enum led_seg mask = (enum led_seg)pgm_read_byte_near(LED_HexFont + ((num >> shift) & 15));
return mask;
}
///
/// Show number as hexadecimal right justified on the LED display
/// @param num Number to display
///
void SevSeg::showHex(unsigned long num)
{
char str[MAX_DIGITS+1];
str[MAX_DIGITS] = '\0';
for (uint8_t d = 0; d < m_digits; ++d)
{
m_buf[m_digits - 1 - d] = getHexDigit(num, 4 * d);
}
}
///
/// Show number as unsigned decimal with [optional] decimal point
/// right justified on the LED display.
/// @param num Number to display
/// @param dp Number of decimal places (-1 if no decimal point)
/// @param fill Fill char (' ', '-' or '+')
///
void SevSeg::showNumber(unsigned long num, uint8_t dp, enum led_seg fill/*=SEG_NONE*/)
{
char str[MAX_DIGITS + 1];
str[MAX_DIGITS] = '\0';
uint8_t d = 0;
while (d < m_digits)
{
uint8_t mask = pgm_read_byte_near(LED_HexFont + (num % 10));
uint8_t index = m_digits - d - 1;
m_buf[index] = (enum led_seg)((d == dp) ? (mask | SEG_DP) : mask);
++d;
num = num / 10;
if (num == 0 && d > dp)
{
// Fill leading 0s
while (d < m_digits)
{
uint8_t index = m_digits - d - 1;
m_buf[index] = (enum led_seg)((d == dp) ? (fill | SEG_DP) : fill);
++d;
// Insure only one '-' sign
if (fill != LED_0)
{
fill = LED_BLANK;
}
}
}
}
}
///
/// Show number as signed decimal with [optional] decimal point
/// right justified on the LED display.
/// @param num Number to display
/// @param dp Decimal Places (-1 if no decimal point)
///
void SevSeg::showDecimal(signed long num, uint8_t dp)
{
if (num < 0)
{
showNumber(-num, dp, LED_MINUS);
}
else
{
showNumber(num, dp, LED_BLANK);
}
}
///
/// Show text string as ASCII characters with [optional] decimal
/// point left justified on the LED display.
/// @param str Text string to display
///
void SevSeg::showText(const char* str)
{
for (uint8_t d = 0; d < m_digits; ++d)
{
const uint8_t c = str[d] - ' ';
m_buf[d] = LED_GET_FONT(LED_AsciiFont, c);
}
}
///
/// Show raw segments (A-F + decimal point) left justified on the LED display.
/// Useful for displaying graphics and other special symbols.
/// @param buf Buffer of segment masks
///
void SevSeg::showRaw(const enum led_seg* buf)
{
for (uint8_t b = 0; b < m_digits; ++b)
{
m_buf[b] = buf[b];
}
}