This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDisplay.cpp
137 lines (109 loc) · 3.85 KB
/
Display.cpp
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
/*
* Copyright (c) 2018 Patrick Pedersen <[email protected]>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
*/
#include "Display.h"
/* Segment states for values 0-9 */
num_segs_t num_segs
{
//{a, b, c, d, e, f, g},
{true, true, true, true, true, true, false}, // 0
{false, true, true, false, false, false, false}, // 1
{true, true, false, true, true, false, true}, // 2
{true, true, true, true, false, false, true}, // 3
{false, true, true, false, false, true, true}, // 4
{true, false, true, true, false, true, true}, // 5
{true, false, true, true, true, true, true}, // 6
{true, true, true, false, false, false, false}, // 7
{true, true, true, true, true, true, true}, // 8
{true, true, true, true, false, true, true} // 9
};
#ifdef DISABLE_DP
void Display::print(const char num[4]) {
toggle(false);
/* Display one digit at a time */
for (int i = 0; i < strlen(num); i++) {
/* Toggle segments for current digit */
for (int j = 0; j < 7; j++) {
segments[j]->toggle(num_segs[num[i] - 0x30][j]);
}
/* Add 1ms offset for previous digit to clear, in order to prevent flickering from upcoming digit */
delay(1);
digits[i]->toggle(true);
/* Provide digit sufficient time to display */
delay(1);
digits[i]->toggle(false);
}
}
#else
void Display::print(const char num[5]) {
toggle(false);
bool next_char_dp = false;
int is_dp_num = 0;
/* Display one digit at a time */
for (int i = 0; i < strlen(num); i++) {
/* Toggle segments for current digit */
for (int j = 0; j < 7; j++) {
segments[j]->toggle(num_segs[num[i] - 0x30][j]);
}
/* Next character is decimal point */
next_char_dp = (i + 1 < 5 && num[i + 1] == '.');
/* Add 1ms offset for previous digit to clear, in order to prevent flickering from upcoming digit */
delay(1);
digits[i - is_dp_num]->toggle(true);
dp->toggle(next_char_dp); // Turn decimal point on if next character is decimal point
/* Provide digit sufficient time to display */
delay(1);
digits[i - is_dp_num]->toggle(false);
dp->toggle(false);
/* Indicate that we're wokring with a decimal point number */
if (next_char_dp && !is_dp_num && ++i) {
is_dp_num = 1;
}
}
}
#endif
/* Enable/Disable all digits */
void Display::toggle() {
state = !state;
for (int i = 0; i < 4; i++) {
digits[i]->toggle(state);
}
}
void Display::toggle(bool arg_state) {
if (state != arg_state) {
state = arg_state;
for (int i = 0; i < 4; i++) {
digits[i]->toggle(state);
}
}
}
/* Enable (all) segments */
void Display::fill(bool all) {
/* Turn on all displays */
if (all) {
toggle(true);
}
for (int i = 0; i < 7; i++) {
segments[i]->toggle(true);
}
}
/* Disable (all) segments */
void Display::empty() {
for (int i = 0; i < 7; i++) {
segments[i]->toggle(false);
}
}