forked from ForsakenNGS/PicoLED
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPicoLedController.cpp
More file actions
276 lines (239 loc) · 7.97 KB
/
PicoLedController.cpp
File metadata and controls
276 lines (239 loc) · 7.97 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
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
#include "PicoLed.hpp"
#include "PicoLedController.hpp"
#include "VirtualStrip.hpp"
using std::min;
using std::shared_ptr;
namespace PicoLed {
PicoLedController::PicoLedController(shared_ptr<PicoLedTarget> target): target(target), mode(MODE_SET)
{
}
PicoLedController::PicoLedController(PicoLedController& controller, uint start, uint end):
target(new VirtualStrip(controller.target, start, end)), mode(MODE_SET)
{
}
PicoLedController::~PicoLedController() {
}
uint PicoLedController::getNumLeds() {
return target->getNumLeds();
}
uint8_t PicoLedController::getBrightness() {
return target->getBrightness();
}
void PicoLedController::setBrightness(uint8_t brightness) {
target->setBrightness(brightness);
}
DrawMode PicoLedController::getDrawMode() {
return mode;
}
void PicoLedController::setDrawMode(DrawMode mode) {
this->mode = mode;
}
Color PicoLedController::getPixelColor(uint index) {
return target->getPixelColor(index);
}
void PicoLedController::setPixelColor(uint index, Color color) {
setPixelColor(index, color, mode);
}
void PicoLedController::setPixelColor(uint index, Color color, DrawMode mode) {
switch (mode) {
default:
case MODE_SET:
{
target->setPixelColor(index, color);
break;
}
case MODE_ADD:
{
Color mixed = target->getPixelColor(index);
mixed.red += min<uint8_t>(color.red, 255 - mixed.red);
mixed.green += min<uint8_t>(color.green, 255 - mixed.green);
mixed.blue += min<uint8_t>(color.blue, 255 - mixed.blue);
mixed.white += min<uint8_t>(color.white, 255 - mixed.white);
target->setPixelColor(index, mixed);
break;
}
case MODE_SUB:
{
Color mixed = target->getPixelColor(index);
mixed.red -= min<uint8_t>(color.red, mixed.red);
mixed.green -= min<uint8_t>(color.green, mixed.green);
mixed.blue -= min<uint8_t>(color.blue, mixed.blue);
mixed.white -= min<uint8_t>(color.white, mixed.white);
target->setPixelColor(index, mixed);
break;
}
}
}
void PicoLedController::clear() {
clear(RGBW(0, 0, 0, 0));
}
void PicoLedController::clear(Color color) {
target->fill(color, 0, target->getNumLeds());
}
void PicoLedController::fill(Color color) {
fill(color, 0, target->getNumLeds());
}
void PicoLedController::fill(Color color, uint first) {
fill(color, first, target->getNumLeds() - first);
}
void PicoLedController::fill(Color color, uint first, uint count) {
target->fill(color, first, count);
}
void PicoLedController::fillGradient(Color colorStart, Color colorEnd) {
fillGradient(colorStart, colorEnd, 0, target->getNumLeds());
}
void PicoLedController::fillGradient(Color colorStart, Color colorEnd, uint first) {
fillGradient(colorStart, colorEnd, first, target->getNumLeds() - first);
}
void PicoLedController::fillGradient(Color colorStart, Color colorEnd, uint first, uint count) {
uint last = (first + count);
if (last > target->getNumLeds()) {
last = target->getNumLeds();
}
for (uint i = first; i < last; i++) {
double ratio = (double)(i - first) / (double)(last - first);
setPixelColor(i, MixColors(colorStart, colorEnd, ratio));
}
}
void PicoLedController::fillRainbow(uint8_t initialHue, uint8_t deltaHue) {
fillRainbow(initialHue, deltaHue, 0, target->getNumLeds());
}
void PicoLedController::fillRainbow(uint8_t initialHue, uint8_t deltaHue, uint first) {
fillRainbow(initialHue, deltaHue, first, target->getNumLeds() - first);
}
void PicoLedController::fillRainbow(uint8_t initialHue, uint8_t deltaHue, uint first, uint count) {
uint last = (first + count);
if (last > target->getNumLeds()) {
last = target->getNumLeds();
}
for (uint i = first; i < last; i++) {
setPixelColor(i, HSV(initialHue, 255, 255));
initialHue += deltaHue;
}
}
void PicoLedController::fade(Color color, double factor) {
fade(color, 0, target->getNumLeds(), factor);
}
void PicoLedController::fade(Color color, uint first, double factor) {
fade(color, first, target->getNumLeds() - first, factor);
}
void PicoLedController::fade(Color color, uint first, uint count, double factor) {
uint last = first + count;
if (last > target->getNumLeds()) {
last = target->getNumLeds();
}
for (uint i = first; i < last; i++) {
fadePixel(i, color, factor);
}
}
void PicoLedController::fadeLine(Color color, double first, double factor) {
fadeLine(color, first, (double)target->getNumLeds() - first, factor);
}
void PicoLedController::fadeLine(Color color, double first, double count, double factor) {
if (first < 0.0) {
// Clamp to start
count += first;
first = 0.0;
}
if ((first + count) > target->getNumLeds()) {
// Clamp to end
count = (double)target->getNumLeds() - first;
}
if (count <= 0) {
// Out of bounds / zero length
return;
}
double last = first + count;
if (last <= ceil(first)) {
// Single pixel
fadePixel(floor(first), color, (last - first) * factor);
} else {
double middleStart = ceil(first);
double middleEnd = floor(last);
// First pixel
fadePixel(floor(first), color, (middleStart - first) * factor);
// Middle section
if (middleEnd - middleStart >= 1.0) {
fade(color, middleStart, middleEnd - middleStart, factor);
}
// Last pixel
if (last > middleEnd) {
fadePixel(middleEnd, color, (last - middleEnd) * factor);
}
}
}
void PicoLedController::fadePixel(uint index, Color color, double factor) {
switch (mode) {
default:
case MODE_SET:
{
Color pixelColor = target->getPixelColor(index);
setPixelColor(index, MixColors(color, pixelColor, factor));
break;
}
case MODE_ADD:
case MODE_SUB:
{
setPixelColor(index, MixColors(color, RGB(0, 0, 0), factor));
break;
}
}
}
void PicoLedController::fadeValue(Color color, uint8_t value) {
fadeValue(color, 0, target->getNumLeds(), value);
}
void PicoLedController::fadeValue(Color color, uint first, uint8_t value) {
fadeValue(color, first, target->getNumLeds() - first, value);
}
void PicoLedController::fadeValue(Color color, uint first, uint count, uint8_t value) {
uint last = first + count;
if (last > target->getNumLeds()) {
last = target->getNumLeds();
}
for (uint i = first; i < last; i++) {
fadePixelValue(i, color, value);
}
}
void PicoLedController::fadePixelValue(uint index, Color color, uint8_t value) {
Color pixelColor = target->getPixelColor(index);
int diffRed = (int)color.red - (int)pixelColor.red;
int diffGreen = (int)color.green - (int)pixelColor.green;
int diffBlue = (int)color.blue - (int)pixelColor.blue;
int diffWhite = (int)color.white - (int)pixelColor.white;
if (abs(diffRed) <= value) {
pixelColor.red = color.red;
} else if (diffRed < 0) {
pixelColor.red -= value;
} else {
pixelColor.red += value;
}
if (abs(diffGreen) <= value) {
pixelColor.green = color.green;
} else if (diffGreen < 0) {
pixelColor.green -= value;
} else {
pixelColor.green += value;
}
if (abs(diffBlue) <= value) {
pixelColor.blue = color.blue;
} else if (diffBlue < 0) {
pixelColor.blue -= value;
} else {
pixelColor.blue += value;
}
if (abs(diffWhite) <= value) {
pixelColor.white = color.white;
} else if (diffWhite < 0) {
pixelColor.white -= value;
} else {
pixelColor.white += value;
}
setPixelColor(index, pixelColor, MODE_SET);
}
void PicoLedController::show() {
target->show();
}
PicoLedController PicoLedController::slice(uint start, uint end) {
return PicoLedController(*this, start, end);
}
}