-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKYSevenSegment.cpp
185 lines (155 loc) · 3.87 KB
/
KYSevenSegment.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
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
#include "KYSevenSegment.h"
/**
* @brief
* is a library saving the display hex code
*/
const byte KYSevenSegment::_numberHex[] = {
B0111111, // 0
B0000110, // 1
B1011011, // 2
B1001111, // 3
B1100110, // 4
B1101101, // 5
B1111101, // 6
B0000111, // 7
B1111111, // 8
B1101111, // 9
B0000000 // Clear Screen
};
void KYSevenSegment::__displayHex_inv(const byte& value, const bool& dp, const bool& dp_only = false) const {
if (!dp_only) {
for (uint8_t i(0); i < 8; i++) {
digitalWrite(this->_pins[i], !bitRead(value, i));
}
}
digitalWrite(this->_pins[7], !dp);
}
void KYSevenSegment::__displayHex(const byte& value, const bool& dp, const bool& dp_only = false) const {
if (!dp_only) {
for (uint8_t i(0); i < 8; i++) {
digitalWrite(this->_pins[i], bitRead(value, i));
}
}
digitalWrite(this->_pins[7], dp);
}
void KYSevenSegment::__initPins() const {
bool setState = !this->_inv_mode;
for (const auto& pin : this->_pins) {
pinMode(pin, OUTPUT);
digitalWrite(pin, setState);
}
}
KYSevenSegment::KYSevenSegment(int a, int b, int c, int d, int e, int f, int g, int dp, bool inv_mode = false) {
this->_pins[0] = a;
this->_pins[1] = b;
this->_pins[2] = c;
this->_pins[3] = d;
this->_pins[4] = e;
this->_pins[5] = f;
this->_pins[6] = g;
this->_pins[7] = dp;
this->_inv_mode = inv_mode;
this->__initPins();
}
KYSevenSegment::KYSevenSegment(uint16_t pins[8], bool inv_mode = false) {
this->_inv_mode = inv_mode;
for (uint16_t i = 0; i < 8; i++) {
this->_pins[i] = pins[i];
}
this->__initPins();
}
KYSevenSegment::~KYSevenSegment() {
this->_inv_mode = false;
for (uint16_t i = 0; i < 8; i++) {
this->_pins[i] = 0;
}
}
/**
* @brief Set the Inv Mode object
* pins output need to inverse
*
* inv_mode = False pin and signal is same
* inv_mode = True pin and signal is inverse
* @param inv_mode about the inv_mode switch
*
*/
void KYSevenSegment::setInvMode(const bool& inv_mode) {
this->_inv_mode = inv_mode;
}
/**
* @brief Set the Pins object
*
* @param pins about the pins array obj
*/
void KYSevenSegment::setPins(uint16_t pints[8]) {
for (int i = 0; i < 8; i++) {
this->_pins[i] = pints[i];
}
}
/**
* @brief Get the Mode object
*
* get about the segment mode setting state
* @return true is in inv mode
* @return false not in inv mode
*/
bool KYSevenSegment::getMode() const {
return this->_inv_mode;
}
/**
* @brief
* Display the number
* @param id Display the number using class library
* @param dp About the dp state
*/
void KYSevenSegment::displayNumber(const uint16_t& id, const bool& dp = false) const {
if (id >= 10) {
return;
}
if (this->_inv_mode) {
this->__displayHex_inv(KYSevenSegment::_numberHex[id], dp);
}
else {
this->__displayHex(KYSevenSegment::_numberHex[id], dp);
}
}
/**
* @brief
* Display using hex code
* @param value Display hex code
* @param dp About the dp state
*/
void KYSevenSegment::displayHex(const byte& value, const bool& dp = false) const {
if (this->_inv_mode) {
this->__displayHex_inv(value, dp);
}
else {
this->__displayHex(value, dp);
}
}
/**
* @brief
* Only display the dp
* @param dp About the dp state
*/
void KYSevenSegment::onlyControlDP(const bool& dp = false) const {
if (this->_inv_mode) {
this->__displayHex_inv(0, dp, true);
}
else {
this->__displayHex(0, dp, true);
}
}
/**
* @brief
*
* clear display function
*/
void KYSevenSegment::clearDisplay() const {
if (this->_inv_mode) {
this->__displayHex_inv(KYSevenSegment::_numberHex[10], 0);
}
else {
this->__displayHex(KYSevenSegment::_numberHex[10], 0);
}
}