-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClockLEDs.cpp
129 lines (102 loc) · 3.08 KB
/
ClockLEDs.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
#include "ClockLEDs.h"
#include "Config.h"
#include "LedConfig.hpp"
#include <Arduino.h>
// #define DEBUGLOG_DISABLE_LOG
#define DEBUGLOG_DEFAULT_LOG_LEVEL_TRACE
#include <DebugLog.h>
void ClockLEDs::displayNumber(uint16_t number, uint8_t onTimeMs, bool showColon, bool showBottomDot, bool showTopDot)
{
auto t = getTupleToWrite(number);
uint16_t left = ~std::get<0>(t);
uint16_t right = ~std::get<1>(t);
if(showColon)
right |= LED_MASK::COLON_MASK;
if(showBottomDot)
left |= LED_MASK::DOT_BOTTOM_MASK;
if(showTopDot)
right |= LED_MASK::DOT_TOP_MASK;
writeToLeds(~left, ~right, onTimeMs / 2);
}
void ClockLEDs::writeToLeds(uint16_t first, uint16_t second, uint8_t onTimePerHalf)
{
writeToRegister(0, first);
delay(onTimePerHalf);
writeToRegister(1, second);
delay(onTimePerHalf);
// clear so both cathodes have the same "on" time to have roughly the same brightness
writeToRegister(1, ~0);
}
void ClockLEDs::writeToRegister(uint8_t cathode, uint16_t data)
{
uint16_t x = data;
x <<= 1;
for(uint8_t i = 0; i < 15; i++)
{
// LOG_DEBUG("i",i);
// LOG_DEBUG("x",x);
// LOG_DEBUG("x&",(x & (1<<15)) ? HIGH : LOW);
digitalWrite(SERIAL_DATA_PIN, (x & (1 << 15)) ? HIGH : LOW);
cyclePin(SHIFT_CLOCK_PIN);
x <<= 1;
}
//
writeCathode(cathode);
cyclePin(LATCH_CLOCK_PIN);
}
void ClockLEDs::writeCathode(uint8_t cathode)
{
digitalWrite(SERIAL_DATA_PIN, cathode ? HIGH : LOW);
cyclePin(SHIFT_CLOCK_PIN);
}
void ClockLEDs::cyclePin(uint8_t pin)
{
digitalWrite(pin, HIGH);
// delay(2);
digitalWrite(pin, LOW);
// delay(2);
}
std::tuple<uint16_t, uint16_t> ClockLEDs::getTupleToWrite(uint16_t num)
{
/*writeTuple({
DOT_BOTTOM_MASK|std::get<0>(CHAR_0_8)|std::get<0>(CHAR_1_8)|std::get<0>(CHAR_2_8)|std::get<0>(CHAR_3_8),
COLON_MASK|DOT_TOP_MASK|std::get<1>(CHAR_0_8)|std::get<1>(CHAR_1_8)|std::get<1>(CHAR_2_8)|std::get<1>(CHAR_3_8)
});
*/
auto a{LED_MAP.at({0, num % 10})};
auto b{LED_MAP.at({1, (num / 10) % 10})};
auto c{LED_MAP.at({2, (num / 100) % 10})};
auto d{LED_MAP.at({3, (num / 1000) % 10})};
return {
~(std::get<0>(a) | std::get<0>(b) | std::get<0>(c) | std::get<0>(d)),
~(std::get<1>(a) | std::get<1>(b) | std::get<1>(c) | std::get<1>(d))};
}
////////////////////////////////////////////////////////////
/// removal/debug/test section
////////////////////////////////////////////////////////////
void ClockLEDs::powerSingleLed(int led)
{
uint8_t cathode{0};
if(led >= LED_COUNT)
{
led = led - LED_COUNT;
cathode = 1;
}
uint8_t data = ~(1 << led);
writeToRegister(data, cathode);
}
void ClockLEDs::allSingleLEDs()
{
static uint32_t last_millis{0};
uint32_t curr_millis{millis()};
static int curr_led{0};
if(curr_millis - last_millis > 500)
{
last_millis = curr_millis;
curr_led++;
if(curr_led == NUM_LED_POS)
curr_led = 0;
powerSingleLed(led_vec[curr_led]);
delay(500);
}
}