-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TM1637.cpp
293 lines (242 loc) · 7.6 KB
/
TM1637.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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*******************************************************************************
* @file TM1637.cpp
* @author Cristian Cristea
* @date July 21, 2021
* @brief Source file for the TM1637 class.
*
* @copyright Copyright (C) 2021 Cristian Cristea. All rights reserved.
******************************************************************************/
#include "TM1637.hpp"
TM1637::TM1637(byte DIO, byte CLK, PIO pio) noexcept : pio(pio)
{
gpio_pull_up(DIO);
gpio_pull_up(CLK);
pio_gpio_init(pio, DIO);
pio_gpio_init(pio, CLK);
Init(DIO, CLK);
}
inline void TM1637::Init(byte DIO, byte CLK) noexcept
{
static constexpr size_t PULL_THRESHOLD = 32;
state_machine = static_cast<byte>(pio_claim_unused_sm(pio, true));
auto offset = pio_add_program(pio, &TM1637_program);
state_machine_config = TM1637_program_get_default_config(offset);
sm_config_set_sideset_pins(&state_machine_config, CLK);
uint32_t both_pins = (1 << CLK) | (1 << DIO);
pio_sm_set_pins_with_mask(pio, state_machine, both_pins, both_pins);
pio_sm_set_pindirs_with_mask(pio, state_machine, both_pins, both_pins);
sm_config_set_out_pins(&state_machine_config, DIO, 1);
sm_config_set_set_pins(&state_machine_config, DIO, 1);
sm_config_set_out_shift(&state_machine_config, true, false, PULL_THRESHOLD);
Set_Clock_Divider();
pio_sm_init(pio, state_machine, offset, &state_machine_config);
pio_sm_set_enabled(pio, state_machine, true);
}
inline void TM1637::Set_Clock_Divider() noexcept
{
static constexpr size_t FREQUENCY = 45'000;
static constexpr size_t MAX_DIVIDER_VALUE = 65'536;
data system_frequency = clock_get_hz(clk_sys);
float divider = static_cast<float>(system_frequency) / FREQUENCY;
if (divider > MAX_DIVIDER_VALUE)
{
divider = MAX_DIVIDER_VALUE;
}
else if (divider < 1)
{
divider = 1;
}
sm_config_set_clkdiv(&state_machine_config, divider);
}
void TM1637::Send_4_Bytes(data value) const noexcept
{
static constexpr size_t BIT_MASK = 0xFF'FF;
static constexpr size_t SHIFT_POSITIONS = 16;
static data data_1;
static data data_2;
data_1 = value & BIT_MASK;
data_2 = value >> SHIFT_POSITIONS;
pio_sm_put_blocking(pio, state_machine, (data_1 << (2 * BYTE_SIZE)) +
(WRITE_ADDRESS << BYTE_SIZE) + WRITE_MODE);
pio_sm_put_blocking(pio, state_machine, data_2 << (2 * BYTE_SIZE));
pio_sm_put_blocking(pio, state_machine, BRIGHTNESS_BASE + brightness);
}
auto TM1637::Number_To_Segments(data number, bool hex, data bitmask) noexcept -> TM1637::data
{
auto base = hex ? BASE_HEX : BASE_TEN;
auto max_one_digit = base - 1;
static data segments;
static data temp_segments;
if (std::cmp_less_equal(number, max_one_digit))
{
segments = DIGIT_TO_SEGMENTS.at(number);
}
else
{
while (number != 0)
{
temp_segments = DIGIT_TO_SEGMENTS.at(number % base);
number /= base;
segments = temp_segments + (segments << BYTE_SIZE);
}
}
if (bitmask != 0)
{
segments &= bitmask;
}
return segments;
}
auto TM1637::Two_Digits_To_Segment(data number, bool hex, bool leading_zeros) noexcept -> TM1637::data
{
static constexpr size_t BIT_MASK = 0xFF'FF;
data segments = Number_To_Segments(number, hex, BIT_MASK);
auto base = hex ? BASE_HEX : BASE_TEN;
auto num_div = number / base;
if (num_div == 0)
{
if (leading_zeros)
{
segments = DIGIT_TO_SEGMENTS[0] + (segments << BYTE_SIZE);
}
else
{
segments = segments << BYTE_SIZE;
}
}
return segments;
}
void TM1637::Internal_Display(int16_t number, bool hex, bool leading_zeros) noexcept
{
static constexpr byte DASH = 0x40;
bool is_positive {true};
uint16_t number_copy {0};
int16_t base = hex ? BASE_HEX : BASE_TEN;
if (number >= 0)
{
is_positive = true;
number_copy = static_cast<uint16_t>(number);
}
else
{
is_positive = false;
number_copy = static_cast<uint16_t>(-number);
}
byte length = 0;
while (number_copy != 0)
{
++length;
number_copy /= base;
}
if (length > 3 + static_cast<byte>(is_positive))
{
length = 3 + static_cast<byte>(is_positive);
}
auto segments = Number_To_Segments(static_cast<data>(number), hex);
byte start_position = 0;
if (leading_zeros && length < MAX_DIGITS)
{
if (is_positive)
{
for (size_t i = length; i < MAX_DIGITS; ++i)
{
segments = (segments << BYTE_SIZE) + DIGIT_TO_SEGMENTS[0];
}
}
else
{
for (size_t i = length; i < MAX_DIGITS - 1; ++i)
{
segments = (segments << BYTE_SIZE) + DIGIT_TO_SEGMENTS[0];
}
segments = (segments << BYTE_SIZE) + DASH;
++length;
}
}
else
{
if (!is_positive)
{
segments = (segments << BYTE_SIZE) + DASH;
++length;
}
start_position = MAX_DIGITS - length;
}
current_segments = segments << (start_position * BYTE_SIZE);
if (is_colon)
{
ColonOn();
}
Send_4_Bytes(current_segments);
}
void TM1637::Internal_Display_Left(data number, bool hex, bool leading_zeros) noexcept
{
static constexpr size_t LEFT_BYTE_MASK = 0xFF'FF'00'00;
current_segments = (current_segments & LEFT_BYTE_MASK) + Two_Digits_To_Segment(number, hex, leading_zeros);
if (is_colon)
{
ColonOn();
}
Send_4_Bytes(current_segments);
}
void TM1637::Internal_Display_Right(data number, bool hex, bool leading_zeros) noexcept
{
static constexpr size_t RIGHT_BYTE_MASK = 0x00'00'FF'FF;
current_segments = (current_segments & RIGHT_BYTE_MASK) +
(Two_Digits_To_Segment(number, hex, leading_zeros) << (2 * BYTE_SIZE));
if (is_colon)
{
ColonOn();
}
Send_4_Bytes(current_segments);
}
void TM1637::Display(int16_t number, bool leading_zeros) noexcept
{
Internal_Display(number, false, leading_zeros);
}
void TM1637::DisplayHex(int16_t number, bool leading_zeros) noexcept
{
Internal_Display(number, true, leading_zeros);
}
void TM1637::DisplayLeft(data number, bool leading_zeros) noexcept
{
Internal_Display_Left(number, false, leading_zeros);
}
void TM1637::DisplayLeftHex(data number, bool leading_zeros) noexcept
{
Internal_Display_Left(number, true, leading_zeros);
}
void TM1637::DisplayRight(data number, bool leading_zeros) noexcept
{
Internal_Display_Right(number, false, leading_zeros);
}
void TM1637::DisplayRightHex(data number, bool leading_zeros) noexcept
{
Internal_Display_Right(number, true, leading_zeros);
}
void TM1637::SetBrightness(byte brightness_level) noexcept
{
brightness = std::min(brightness_level, MAX_BRIGHTNESS);
Send_4_Bytes(current_segments);
}
void TM1637::ColonOn() noexcept
{
static constexpr size_t CONTROL_DISPLAY = 0x80'00;
is_colon = true;
current_segments |= CONTROL_DISPLAY;
Send_4_Bytes(current_segments);
}
void TM1637::ColonOff() noexcept
{
static constexpr size_t CONTROL_DISPLAY = 0x80'00;
is_colon = false;
current_segments &= ~CONTROL_DISPLAY;
Send_4_Bytes(current_segments);
}
void TM1637::Clear() const noexcept
{
static constexpr size_t CONTROL_DISPLAY = 0x80;
static constexpr size_t WRITE_ADDRESS_AND_MODE = 0xC0'40;
pio_sm_put_blocking(pio, state_machine, CONTROL_DISPLAY);
pio_sm_put_blocking(pio, state_machine, WRITE_ADDRESS_AND_MODE);
pio_sm_put_blocking(pio, state_machine, 0);
}