-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathws_pixels.cpp
460 lines (419 loc) · 16.8 KB
/
ws_pixels.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*!
* @file ws_pixels.cpp
*
* High-level interface for wippersnapper to manage addressable RGB pixel
* strands
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
*
* Written by Brent Rubell for Adafruit Industries, 2022-2023
*
* MIT license, all text here must be included in any redistribution.
*
*/
#include "ws_pixels.h"
strand_s strands[MAX_PIXEL_STRANDS]{
nullptr,
nullptr,
wippersnapper_pixels_v1_PixelsType_PIXELS_TYPE_UNSPECIFIED,
0,
0,
wippersnapper_pixels_v1_PixelsOrder_PIXELS_ORDER_UNSPECIFIED,
-1,
-1,
-1}; ///< Contains all pixel strands used by WipperSnapper
/**************************************************************************/
/*!
@brief Destructor
*/
/**************************************************************************/
ws_pixels::~ws_pixels() {
// de-allocate all strands
for (size_t i = 0; i < sizeof(strands) / sizeof(strands[0]); i++)
deallocateStrand(i);
}
/******************************************************************************/
/*!
@brief Allocates an index of a free strand_t within the strand array.
@returns Index of a free strand_t, ERR_INVALID_STRAND if strand array is
full.
*/
/******************************************************************************/
int16_t ws_pixels::allocateStrand() {
for (size_t strandIdx = 0; strandIdx < sizeof(strands) / sizeof(strands[0]);
strandIdx++) {
if (strands[strandIdx].type ==
wippersnapper_pixels_v1_PixelsType_PIXELS_TYPE_UNSPECIFIED) {
return strandIdx;
}
}
return ERR_INVALID_STRAND;
}
/**************************************************************************/
/*!
@brief Deallocates a `strand_t` within `strands`, provided an index.
@param strandIdx
The desired index of a `strand_t` within `strands`.
*/
/**************************************************************************/
void ws_pixels::deallocateStrand(int16_t strandIdx) {
// delete the pixel object
if (strands[strandIdx].neoPixelPtr != nullptr)
delete strands[strandIdx].neoPixelPtr;
if ((strands[strandIdx].dotStarPtr != nullptr))
delete strands[strandIdx].dotStarPtr;
// re-initialize status pixel (if pixel was prvsly used)
if (strands[strandIdx].pinNeoPixel == getStatusNeoPixelPin() ||
strands[strandIdx].pinDotStarData == getStatusDotStarDataPin()) {
initStatusLED();
}
// reset the strand
strands[strandIdx] = {
nullptr,
nullptr,
wippersnapper_pixels_v1_PixelsType_PIXELS_TYPE_UNSPECIFIED,
0,
0,
wippersnapper_pixels_v1_PixelsOrder_PIXELS_ORDER_UNSPECIFIED,
-1,
-1,
-1};
}
/**************************************************************************/
/*!
@brief Returns the `neoPixelType` provided the strand's pixelOrder
@param pixelOrder
Desired pixel order, from init. message.
@returns Type of NeoPixel strand, usable by Adafruit_NeoPixel
constructor
*/
/**************************************************************************/
neoPixelType ws_pixels::getNeoPixelStrandOrder(
wippersnapper_pixels_v1_PixelsOrder pixelOrder) {
switch (pixelOrder) {
case wippersnapper_pixels_v1_PixelsOrder_PIXELS_ORDER_GRB:
return NEO_GRB + NEO_KHZ800;
case wippersnapper_pixels_v1_PixelsOrder_PIXELS_ORDER_GRBW:
return NEO_GRBW + NEO_KHZ800;
case wippersnapper_pixels_v1_PixelsOrder_PIXELS_ORDER_RGB:
return NEO_RGB + NEO_KHZ800;
case wippersnapper_pixels_v1_PixelsOrder_PIXELS_ORDER_RGBW:
return NEO_RGBW + NEO_KHZ800;
case wippersnapper_pixels_v1_PixelsOrder_PIXELS_ORDER_BRG:
return NEO_BRG + NEO_KHZ800;
default:
return NEO_GRB + NEO_KHZ800;
}
}
/**************************************************************************/
/*!
@brief Returns the color order of the DotStar strand.
@param pixelOrder
Desired pixel order, from init. message.
@returns Type of DotStar strand.
*/
/**************************************************************************/
uint8_t ws_pixels::getDotStarStrandOrder(
wippersnapper_pixels_v1_PixelsOrder pixelOrder) {
switch (pixelOrder) {
case wippersnapper_pixels_v1_PixelsOrder_PIXELS_ORDER_GRB:
return DOTSTAR_GRB;
case wippersnapper_pixels_v1_PixelsOrder_PIXELS_ORDER_RGB:
return DOTSTAR_RGB;
case wippersnapper_pixels_v1_PixelsOrder_PIXELS_ORDER_BRG:
return DOTSTAR_BRG;
case wippersnapper_pixels_v1_PixelsOrder_PIXELS_ORDER_RBG:
return DOTSTAR_RBG;
case wippersnapper_pixels_v1_PixelsOrder_PIXELS_ORDER_GBR:
return DOTSTAR_GBR;
case wippersnapper_pixels_v1_PixelsOrder_PIXELS_ORDER_BGR:
return DOTSTAR_BGR;
default:
return DOTSTAR_BRG;
}
}
/**************************************************************************/
/*!
@brief Creates a PixelsResponse message and publishes it to IO.
@param is_success
True if `addStrand()` succeeded, False otherwise.
@param pixels_pin_data
The strand's data pin..
*/
/**************************************************************************/
void ws_pixels::publishAddStrandResponse(bool is_success,
char *pixels_pin_data) {
// Create response message
wippersnapper_signal_v1_PixelsResponse msgInitResp =
wippersnapper_signal_v1_PixelsResponse_init_zero;
msgInitResp.which_payload =
wippersnapper_signal_v1_PixelsResponse_resp_pixels_create_tag;
// Fill response message
msgInitResp.payload.resp_pixels_create.is_success = is_success;
memcpy(msgInitResp.payload.resp_pixels_create.pixels_pin_data,
pixels_pin_data, sizeof(char) * 6);
// Encode `wippersnapper_pixels_v1_PixelsCreateResponse` message
memset(WS._buffer_outgoing, 0, sizeof(WS._buffer_outgoing));
pb_ostream_t ostream =
pb_ostream_from_buffer(WS._buffer_outgoing, sizeof(WS._buffer_outgoing));
if (!ws_pb_encode(&ostream, wippersnapper_signal_v1_PixelsResponse_fields,
&msgInitResp)) {
WS_DEBUG_PRINTLN("ERROR: Unable to encode "
"wippersnapper_signal_v1_PixelsResponse message!");
return;
}
// Publish message to broker
size_t msgSz;
pb_get_encoded_size(&msgSz, wippersnapper_signal_v1_PixelsResponse_fields,
&msgInitResp);
WS_DEBUG_PRINT("-> wippersnapper_signal_v1_PixelsResponse...");
if (!WS.publish(WS._topic_signal_pixels_device, WS._buffer_outgoing, msgSz,
1))
{
WS_DEBUG_PRINTLN("ERROR: Unable to publish PixelsResponse message - "
"check MQTT connection!");
}
else
{
WS_DEBUG_PRINTLN("Published! (PixelsResponse)");
}
}
/**************************************************************************/
/*!
@brief Initializes a strand of addressable RGB Pixels.
@param pixelsCreateReqMsg
Pointer to strand init. request message
@returns True if successfully initialized, False otherwise.
*/
/**************************************************************************/
bool ws_pixels::addStrand(
wippersnapper_pixels_v1_PixelsCreateRequest *pixelsCreateReqMsg) {
// attempt to allocate a free strand from array of strands
int16_t strandIdx = allocateStrand();
if (strandIdx == ERR_INVALID_STRAND) { // unable to allocate a strand
if (pixelsCreateReqMsg->pixels_type ==
wippersnapper_pixels_v1_PixelsType_PIXELS_TYPE_NEOPIXEL)
publishAddStrandResponse(false, pixelsCreateReqMsg->pixels_pin_neopixel);
else if (pixelsCreateReqMsg->pixels_type ==
wippersnapper_pixels_v1_PixelsType_PIXELS_TYPE_DOTSTAR)
publishAddStrandResponse(false,
pixelsCreateReqMsg->pixels_pin_dotstar_data);
return false;
}
// fill generic members of the strand obj.
strands[strandIdx].type = pixelsCreateReqMsg->pixels_type;
strands[strandIdx].brightness = pixelsCreateReqMsg->pixels_brightness;
strands[strandIdx].numPixels = pixelsCreateReqMsg->pixels_num;
strands[strandIdx].ordering = pixelsCreateReqMsg->pixels_ordering;
// fill strand pins
if (pixelsCreateReqMsg->pixels_type ==
wippersnapper_pixels_v1_PixelsType_PIXELS_TYPE_NEOPIXEL) {
strands[strandIdx].pinNeoPixel =
atoi(pixelsCreateReqMsg->pixels_pin_neopixel + 1);
} else if (pixelsCreateReqMsg->pixels_type ==
wippersnapper_pixels_v1_PixelsType_PIXELS_TYPE_DOTSTAR) {
strands[strandIdx].pinDotStarData =
atoi(pixelsCreateReqMsg->pixels_pin_dotstar_data + 1);
strands[strandIdx].pinDotStarClock =
atoi(pixelsCreateReqMsg->pixels_pin_dotstar_clock + 1);
} else {
WS_DEBUG_PRINTLN("ERROR: Invalid strand type provided!");
publishAddStrandResponse(false, pixelsCreateReqMsg->pixels_pin_neopixel);
return false;
}
// Fill specific members of strand obj.
if (pixelsCreateReqMsg->pixels_type ==
wippersnapper_pixels_v1_PixelsType_PIXELS_TYPE_NEOPIXEL) {
// Release status LED
// is requested pin in-use by the status pixel?
if (getStatusNeoPixelPin() == strands[strandIdx].pinNeoPixel &&
WS.lockStatusNeoPixel)
releaseStatusLED(); // release it!
// Create a new strand of NeoPixels
strands[strandIdx].neoPixelPtr = new Adafruit_NeoPixel(
pixelsCreateReqMsg->pixels_num, strands[strandIdx].pinNeoPixel,
getNeoPixelStrandOrder(pixelsCreateReqMsg->pixels_ordering));
// Initialize strand
strands[strandIdx].neoPixelPtr->begin();
strands[strandIdx].neoPixelPtr->setBrightness(
strands[strandIdx].brightness);
strands[strandIdx].neoPixelPtr->clear();
strands[strandIdx].neoPixelPtr->show();
// Check that we've correctly initialized the strand
if (strands[strandIdx].neoPixelPtr->numPixels() == 0) {
publishAddStrandResponse(false, pixelsCreateReqMsg->pixels_pin_neopixel);
return false;
}
WS_DEBUG_PRINT("Created NeoPixel strand of length ");
WS_DEBUG_PRINT(pixelsCreateReqMsg->pixels_num);
WS_DEBUG_PRINT(" on GPIO #");
WS_DEBUG_PRINTLN(pixelsCreateReqMsg->pixels_pin_neopixel);
#ifdef USE_DISPLAY
char buffer[100];
snprintf(buffer, 100, "[Pixel] Added NeoPixel strand on Pin %s\n.",
pixelsCreateReqMsg->pixels_pin_neopixel);
WS._ui_helper->add_text_to_terminal(buffer);
#endif
publishAddStrandResponse(true, pixelsCreateReqMsg->pixels_pin_neopixel);
} else if (pixelsCreateReqMsg->pixels_type ==
wippersnapper_pixels_v1_PixelsType_PIXELS_TYPE_DOTSTAR) {
// release the status dotstar, if it is both in-use and the pin within
// `pixelsCreateReqMsg`
if ((strands[strandIdx].pinDotStarData == getStatusDotStarDataPin()) &&
WS.lockStatusDotStar) {
releaseStatusLED();
}
// Create Dotstar strand
strands[strandIdx].dotStarPtr = new Adafruit_DotStar(
strands[strandIdx].numPixels, strands[strandIdx].pinDotStarData,
strands[strandIdx].pinDotStarClock,
getDotStarStrandOrder(strands[strandIdx].ordering));
// initialize strand
strands[strandIdx].dotStarPtr->begin();
strands[strandIdx].dotStarPtr->setBrightness(strands[strandIdx].brightness);
strands[strandIdx].dotStarPtr->clear();
strands[strandIdx].dotStarPtr->show();
// post-init sanity check
if (strands[strandIdx].dotStarPtr->numPixels() == 0) {
publishAddStrandResponse(false,
pixelsCreateReqMsg->pixels_pin_dotstar_data);
return false;
}
WS_DEBUG_PRINT("Created DotStar strand of length ");
WS_DEBUG_PRINT(strands[strandIdx].numPixels);
WS_DEBUG_PRINT(" on Data GPIO #");
WS_DEBUG_PRINTLN(strands[strandIdx].pinDotStarData);
#ifdef USE_DISPLAY
char buffer[100];
snprintf(buffer, 100, "[Pixel] Added NeoPixel strand on Pin %s\n.",
pixelsCreateReqMsg->pixels_pin_neopixel);
WS._ui_helper->add_text_to_terminal(buffer);
#endif
publishAddStrandResponse(true, pixelsCreateReqMsg->pixels_pin_dotstar_data);
} else {
WS_DEBUG_PRINTLN("ERROR: Invalid strand type provided!");
publishAddStrandResponse(false,
pixelsCreateReqMsg->pixels_pin_dotstar_data);
return false;
}
return true;
}
/**************************************************************************/
/*!
@brief Obtains the index of a `strand_t` within array of `strands`.
@param dataPin
strand_t's data dataPin
@param type
Type of strand_t, NeoPixel or DotStar.
@returns The index of a strand_t if within strands[],
ERR_INVALID_STRAND otherwise.
*/
/**************************************************************************/
int ws_pixels::getStrandIdx(int16_t dataPin,
wippersnapper_pixels_v1_PixelsType type) {
for (size_t strandIdx = 0; strandIdx < sizeof(strands) / sizeof(strands[0]);
strandIdx++) {
if (type == wippersnapper_pixels_v1_PixelsType_PIXELS_TYPE_NEOPIXEL &&
strands[strandIdx].pinNeoPixel == dataPin)
return strandIdx;
if (type == wippersnapper_pixels_v1_PixelsType_PIXELS_TYPE_DOTSTAR &&
strands[strandIdx].pinDotStarData == dataPin)
return strandIdx;
}
return ERR_INVALID_STRAND;
}
/**************************************************************************/
/*!
@brief Deletes a `strand_t` from `strands`, deinitializes a strand,
and frees its resources.
@param pixelsDeleteMsg
Protobuf message from Adafruit IO containing a
`wippersnapper_pixels_v1_PixelsDeleteRequest`.
*/
/**************************************************************************/
void ws_pixels::deleteStrand(
wippersnapper_pixels_v1_PixelsDeleteRequest *pixelsDeleteMsg) {
int strandIdx = getStrandIdx(atoi(pixelsDeleteMsg->pixels_pin_data + 1),
pixelsDeleteMsg->pixels_type);
if (strandIdx == ERR_INVALID_STRAND) {
WS_DEBUG_PRINTLN("ERROR: Strand not found, unable to delete strand!");
return;
}
// deallocate and release resources of strand object
deallocateStrand(strandIdx);
WS_DEBUG_PRINT("Deleted strand on data pin ");
WS_DEBUG_PRINTLN(pixelsDeleteMsg->pixels_pin_data);
#ifdef USE_DISPLAY
char buffer[100];
snprintf(buffer, 100, "[Pixel] Deleted strand on pin %s\n.",
pixelsDeleteMsg->pixels_pin_data);
WS._ui_helper->add_text_to_terminal(buffer);
#endif
}
/**************************************************************************/
/*!
@brief Gets the gamma-corrected color, provided a pixel_color
@param pixel_color
Strand's color from Adafruit IO.
@param strand
Desired strand struct. to access.
@returns A gamma-corrected strand color
*/
/**************************************************************************/
uint32_t ws_pixels::getGammaCorrectedColor(uint32_t pixel_color,
strand_s strand) {
if (strand.neoPixelPtr != nullptr) {
return strand.neoPixelPtr->gamma32(pixel_color);
} else if (strand.dotStarPtr != nullptr) {
return strand.dotStarPtr->gamma32(pixel_color);
} else {
WS_DEBUG_PRINTLN(
"ERROR: Unable to perform gamma correction, unknown strand type!");
return 0;
}
}
/**************************************************************************/
/*!
@brief Writes a color from Adafruit IO to a strand of
addressable pixels
@param pixelsWriteMsg
Protobuf message from Adafruit IO containing a
`wippersnapper_pixels_v1_PixelsWriteRequest`.
*/
/**************************************************************************/
void ws_pixels::fillStrand(
wippersnapper_pixels_v1_PixelsWriteRequest *pixelsWriteMsg) {
// Get index of pixel strand
int strandIdx = getStrandIdx(atoi(pixelsWriteMsg->pixels_pin_data + 1),
pixelsWriteMsg->pixels_type);
if (strandIdx == ERR_INVALID_STRAND) {
WS_DEBUG_PRINTLN(
"ERROR: Pixel strand not found, can not write a color to the strand!");
return;
}
uint32_t rgbColorGamma =
getGammaCorrectedColor(pixelsWriteMsg->pixels_color, strands[strandIdx]);
WS_DEBUG_PRINT("Filling color: ");
WS_DEBUG_PRINTLN(pixelsWriteMsg->pixels_color);
#ifdef USE_DISPLAY
char buffer[100];
snprintf(buffer, 100, "[Pixel] Filling strand on pin %s with color %u\n",
pixelsWriteMsg->pixels_pin_data,
(unsigned int)pixelsWriteMsg->pixels_color);
WS._ui_helper->add_text_to_terminal(buffer);
#endif
if (pixelsWriteMsg->pixels_type ==
wippersnapper_pixels_v1_PixelsType_PIXELS_TYPE_NEOPIXEL) {
strands[strandIdx].neoPixelPtr->fill(rgbColorGamma);
strands[strandIdx].neoPixelPtr->show();
} else if (pixelsWriteMsg->pixels_type ==
wippersnapper_pixels_v1_PixelsType_PIXELS_TYPE_DOTSTAR) {
strands[strandIdx].dotStarPtr->fill(rgbColorGamma);
strands[strandIdx].dotStarPtr->show();
} else {
WS_DEBUG_PRINTLN("ERROR: Unable to determine pixel type to write to!");
}
}