-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathledDriver.cpp
More file actions
148 lines (131 loc) · 3.6 KB
/
ledDriver.cpp
File metadata and controls
148 lines (131 loc) · 3.6 KB
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
/*
* Flybrix Flight Controller -- Copyright 2018 Flying Selfie Inc. d/b/a Flybrix
*
* http://www.flybrix.com
*/
#include "ledDriver.h"
LEDDriver LED_driver;
LEDDriver::LEDDriver() {
setColor(CRGB::Black);
setPattern(LEDPattern::SOLID);
FastLED.addLeds<WS2812B, board::led::DATA_PIN>(leds, board::led::COUNT);
}
uint8_t LEDDriver::getCycleIndex() const {
return cycleIndex;
}
uint8_t LEDDriver::getPattern() const {
return pattern;
}
inline bool isInside(const board::led::Position& p, const board::led::Position& p_min, const board::led::Position& p_max) {
return p.x >= p_min.x && p.y >= p_min.y && p.x <= p_max.x && p.y <= p_max.y;
}
void LEDDriver::setColor(CRGB color, board::led::Position lower_left, board::led::Position upper_right) {
color = CRGB(color.green, color.red, color.blue);
for (size_t idx = 0; idx < board::led::COUNT; ++idx) {
if (!isInside(board::led::POSITION[idx], lower_left, upper_right))
continue;
if (colors[idx].red == color.red && colors[idx].green == color.green && colors[idx].blue == color.blue)
continue;
hasChanges = true;
colors[idx] = color;
}
}
void LEDDriver::setPattern(LEDPattern::Pattern pattern) {
if (pattern == this->pattern)
return;
this->pattern = pattern;
hasChanges = true;
cycleIndex = 255;
scale = 255;
for (size_t idx = 0; idx < board::led::COUNT; ++idx) {
shining[idx] = true;
}
}
void LEDDriver::set(LEDPattern::Pattern pattern, CRGB color) {
setColor(color);
setPattern(pattern);
}
void LEDDriver::update() {
++cycleIndex;
writeToDisplay();
if (!hasChanges)
return;
for (size_t idx = 0; idx < board::led::COUNT; ++idx) {
leds[idx] = shining[idx] ? colors[idx] : CRGB::Black;
}
FastLED.show(scale);
hasChanges = false;
}
void LEDDriver::updateAlternate() {
if (cycleIndex & 15) {
return;
}
board::led::Position lower_left = board::led::Position::Min();
board::led::Position upper_right = board::led::Position::Max();
if (cycleIndex & 16) {
lower_left.x = 0;
} else {
upper_right.x = 0;
}
for (size_t idx = 0; idx < board::led::COUNT; ++idx) {
shining[idx] = isInside(board::led::POSITION[idx], lower_left, upper_right);
}
hasChanges = true;
}
void LEDDriver::updateFlash() {
if (cycleIndex & 3)
return;
scale = (cycleIndex & 4) ? 0 : 255;
hasChanges = true;
}
void LEDDriver::updateBeacon() {
switch ((cycleIndex & 63) >> 2) { // two second period
case 1:
case 4:
scale = 255;
hasChanges = true;
break;
case 2:
case 5:
scale = 0;
hasChanges = true;
break;
default:
break;
}
}
void LEDDriver::updateBreathe() {
uint16_t multiplier = cycleIndex & 127;
if (multiplier > 31)
return;
scale = multiplier;
if (scale > 15)
scale = 31 - scale;
scale <<= 4;
hasChanges = true;
}
void LEDDriver::updateSolid() {
if (scale == 255)
return;
scale = 255;
hasChanges = true;
}
void LEDDriver::writeToDisplay() {
switch (pattern) {
case LEDPattern::FLASH:
updateFlash();
break;
case LEDPattern::BEACON:
updateBeacon();
break;
case LEDPattern::BREATHE:
updateBreathe();
break;
case LEDPattern::ALTERNATE:
updateAlternate();
break;
case LEDPattern::SOLID:
updateSolid();
break;
}
}