-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClockDisplay.cpp
114 lines (89 loc) · 2.66 KB
/
ClockDisplay.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
#include "ClockDisplay.h"
#include "digits.h"
#include <Arduino.h>
#include <cstdint>
ClockDisplay::ClockDisplay(Adafruit_NeoPixel &pixels) {
_pixels = pixels;
}
void ClockDisplay::setBits(uint16_t offset, uint16_t r, uint16_t g, uint16_t b, uint8_t mask, bool update) {
uint32_t on = _pixels.Color(r, g, b);
uint32_t off = _pixels.Color(0, 0, 0);
for (int i = 0; i < 8; ++i) { // 7 bits
for (int p = 0; p < SEGMENT_SIZE; ++p) {
bool isSet = (1 << i) & mask;
_pixels.setPixelColor((i * SEGMENT_SIZE) + p + offset, isSet ? on : off);
}
}
if (update) {
_pixels.show();
}
}
void ClockDisplay::drawDigit(uint16_t offset, uint16_t r, uint16_t g, uint16_t b, int n, bool update) {
setBits(offset, r, g, b, digits[n], update);
}
void ClockDisplay::turnOffDigit(uint16_t offset, bool update) {
uint32_t off = _pixels.Color(0, 0, 0);
for (int i = 0; i < 8; ++i) { // 7 bits
for (int p = 0; p < SEGMENT_SIZE; ++p) {
_pixels.setPixelColor((i * SEGMENT_SIZE) + p + offset, off);
}
}
if (update) {
_pixels.show();
}
}
int ClockDisplay::nthDigit(int place, int number) {
while (place--) {
number /= 10;
}
return number % 10;
}
void ClockDisplay::drawNumber(uint16_t r, uint16_t g, uint16_t b, int number) {
int thousands = nthDigit(3, number);
int hundreds = nthDigit(2, number);
int tens = nthDigit(1, number);
int ones = nthDigit(0, number);
thousands > 0 ?
drawDigit(HOUR1, r, g, b, thousands) :
turnOffDigit(HOUR1);
thousands > 0 || hundreds > 0 ?
drawDigit(HOUR2, r, g, b, hundreds) :
turnOffDigit(HOUR2);
thousands > 0 || hundreds > 0 || tens > 0 ?
drawDigit(MINUTE1, r, g, b, tens) :
turnOffDigit(MINUTE1);
drawDigit(MINUTE2, r, g, b, ones, true);
}
void ClockDisplay::begin() {
_pixels.begin();
}
void ClockDisplay::clear() {
for(int8_t i = 0; i < NUMPIXELS; ++i) {
_pixels.setPixelColor(i, 0);
}
_pixels.show();
}
void ClockDisplay::setPixel(uint16_t offset, uint16_t r, uint16_t g, uint16_t b) {
_pixels.setPixelColor(offset, r, g, b);
_pixels.show();
}
void ClockDisplay::drawDots(uint16_t r, uint16_t g, uint16_t b, bool update) {
_pixels.setPixelColor(DOT1, r, g, b);
_pixels.setPixelColor(DOT2, r, g, b);
if (update) {
_pixels.show();
}
}
void ClockDisplay::test() {
for (int i = 0; i < 10; ++i) {
drawDigit(HOUR1, 255, 0, 0, i);
drawDigit(HOUR2, 255, 0, 0, i);
uint32_t dotColor = i % 2 ? _pixels.Color(0, 0, 0) : _pixels.Color(255, 0, 0);
_pixels.setPixelColor(DOT1, dotColor);
_pixels.setPixelColor(DOT2, dotColor);
drawDigit(MINUTE1, 255, 0, 0, i);
drawDigit(MINUTE2, 255, 0, 0, i, true);
delay(400);
}
clear();
}