-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
ws28xx.c
501 lines (460 loc) · 15.4 KB
/
ws28xx.c
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/************************************************************************************************************
************** Include Headers
************************************************************************************************************/
#include "ws28xx.h"
#include <string.h>
#if WS28XX_RTOS == WS28XX_RTOS_DISABLE
#elif WS28XX_RTOS == WS28XX_RTOS_CMSIS_V1
#include "cmsis_os.h"
#include "freertos.h"
#elif WS28XX_RTOS == WS28XX_RTOS_CMSIS_V2
#include "cmsis_os2.h"
#include "freertos.h"
#elif WS28XX_RTOS == WS28XX_RTOS_THREADX
#include "app_threadx.h"
#endif
/************************************************************************************************************
************** Private Definitions
************************************************************************************************************/
// none
/************************************************************************************************************
************** Private Variables
************************************************************************************************************/
#if (WS28XX_GAMMA == true)
const uint8_t WS28XX_GammaTable[] =
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2,
2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5,
6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 11, 11,
11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18,
19, 19, 20, 21, 21, 22, 22, 23, 23, 24, 25, 25, 26, 27, 27, 28,
29, 29, 30, 31, 31, 32, 33, 34, 34, 35, 36, 37, 37, 38, 39, 40,
40, 41, 42, 43, 44, 45, 46, 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, 76, 77, 78, 79, 80, 81, 83, 84, 85, 86, 88, 89,
90, 91, 93, 94, 95, 96, 98, 99,100,102,103,104,106,107,109,110,
111,113,114,116,117,119,120,121,123,124,126,128,129,131,132,134,
135,137,138,140,142,143,145,146,148,150,151,153,155,157,158,160,
162,163,165,167,169,170,172,174,176,178,179,181,183,185,187,189,
191,193,194,196,198,200,202,204,206,208,210,212,214,216,218,220,
222,224,227,229,231,233,235,237,239,241,244,246,248,250,252,255
};
#endif
/************************************************************************************************************
************** Private Functions
************************************************************************************************************/
void WS28XX_Delay(uint32_t Delay);
void WS28XX_Lock(WS28XX_HandleTypeDef *hLed);
void WS28XX_UnLock(WS28XX_HandleTypeDef *hLed);
/***********************************************************************************************************/
void WS28XX_Delay(uint32_t Delay)
{
#if WS28XX_RTOS == WS28XX_RTOS_DISABLE
HAL_Delay(Delay);
#elif (WS28XX_RTOS == WS28XX_RTOS_CMSIS_V1) || (WS28XX_RTOS == WS28XX_RTOS_CMSIS_V2)
uint32_t d = (configTICK_RATE_HZ * Delay) / 1000;
if (d == 0)
d = 1;
osDelay(d);
#elif WS28XX_RTOS == WS28XX_RTOS_THREADX
uint32_t d = (TX_TIMER_TICKS_PER_SECOND * Delay) / 1000;
if (d == 0)
d = 1;
tx_thread_sleep(d);
#endif
}
/***********************************************************************************************************/
void WS28XX_Lock(WS28XX_HandleTypeDef *hLed)
{
while (hLed->Lock)
{
WS28XX_Delay(1);
}
hLed->Lock = 1;
}
/***********************************************************************************************************/
void WS28XX_UnLock(WS28XX_HandleTypeDef *hLed)
{
hLed->Lock = 0;
}
/************************************************************************************************************
************** Public Functions
************************************************************************************************************/
/**
* @brief Initialize WS28XX hLed
* @note Initialize WS28XX hLed and set the Channel and number of Pixels
*
* @param *hLed: Pointer to WS28XX_hLedTypeDef structure
* @param *hTim: Pointer to TIM_hLedTypeDef structure
* @param Channel: Selected PWM channel
* TIM_CHANNEL_1
* TIM_CHANNEL_2
* TIM_CHANNEL_3
* TIM_CHANNEL_4
* TIM_CHANNEL_5
* TIM_CHANNEL_6
* @param Pixel: Number of pixels
*
* @retval bool: true or false
*/
bool WS28XX_Init(WS28XX_HandleTypeDef *hLed, TIM_HandleTypeDef *hTim,
uint16_t TimerBusFrequencyMHz, uint8_t Channel, uint16_t Pixel)
{
bool answer = false;
uint32_t aar_value;
do
{
if (hLed == NULL || hTim == NULL)
{
break;
}
if (Pixel > WS28XX_PIXEL_MAX)
{
break;
}
hLed->Channel = Channel;
hLed->MaxPixel = Pixel;
hLed->hTim = hTim;
aar_value = (TimerBusFrequencyMHz / (1.0f / (WS28XX_PULSE_LENGTH_NS / 1000.0f))) - 1;
__HAL_TIM_SET_AUTORELOAD(hLed->hTim ,aar_value);
__HAL_TIM_SET_PRESCALER(hLed->hTim, 0);
hLed->Pulse0 = ((WS28XX_PULSE_0_NS / 1000.0f) * aar_value) / (WS28XX_PULSE_LENGTH_NS / 1000.0f);
hLed->Pulse1 = ((WS28XX_PULSE_1_NS / 1000.0f) * aar_value) / (WS28XX_PULSE_LENGTH_NS / 1000.0f);
memset(hLed->Pixel, 0, sizeof(hLed->Pixel));
memset(hLed->Buffer, 0, sizeof(hLed->Buffer));
HAL_TIM_PWM_Start_DMA(hLed->hTim, hLed->Channel, (const uint32_t*)hLed->Buffer, Pixel);
answer = true;
}
while (0);
return answer;
}
/***********************************************************************************************************/
/**
* @brief Set Pixel
* @note Fill the pixel By RGB Values
*
* @param *hLed: Pointer to WS28XX_hLedTypeDef structure
* @param Pixel: Pixel Starts from 0 to Max - 1
* @param Red: Red Value, 0 to 255
* @param Green: Green Value, 0 to 255
* @param Blue: Blue Value, 0 to 255
*
* @retval bool: true or false
*/
bool WS28XX_SetPixel_RGB(WS28XX_HandleTypeDef *hLed, uint16_t Pixel, uint8_t Red, uint8_t Green, uint8_t Blue)
{
bool answer = true;
do
{
if (Pixel >= hLed->MaxPixel)
{
answer = false;
break;
}
#if (WS28XX_GAMMA == false)
#if WS28XX_ORDER == WS28XX_ORDER_RGB
hLed->Pixel[Pixel][0] = Red;
hLed->Pixel[Pixel][1] = Green;
hLed->Pixel[Pixel][2] = Blue;
#elif WS28XX_ORDER == WS28XX_ORDER_BGR
hLed->Pixel[Pixel][0] = Blue;
hLed->Pixel[Pixel][1] = Green;
hLed->Pixel[Pixel][2] = Red;
#elif WS28XX_ORDER == WS28XX_ORDER_GRB
hLed->Pixel[Pixel][0] = Green;
hLed->Pixel[Pixel][1] = Red;
hLed->Pixel[Pixel][2] = Blue;
#endif
#else
#if WS28XX_ORDER == WS28XX_ORDER_RGB
hLed->Pixel[Pixel][0] = WS28XX_GammaTable[Red];
hLed->Pixel[Pixel][1] = WS28XX_GammaTable[Green];
hLed->Pixel[Pixel][2] = WS28XX_GammaTable[Blue];
#elif WS28XX_ORDER == WS28XX_ORDER_BGR
hLed->Pixel[Pixel][0] = WS28XX_GammaTable[Blue];
hLed->Pixel[Pixel][1] = WS28XX_GammaTable[Green];
hLed->Pixel[Pixel][2] = WS28XX_GammaTable[Red];
#elif WS28XX_ORDER == WS28XX_ORDER_GRB
hLed->Pixel[Pixel][0] = WS28XX_GammaTable[Green];
hLed->Pixel[Pixel][1] = WS28XX_GammaTable[Red];
hLed->Pixel[Pixel][2] = WS28XX_GammaTable[Blue];
#endif
#endif
}
while (0);
return answer;
}
/***********************************************************************************************************/
/**
* @brief Set Pixel
* @note Fill the pixel By RGB Values
*
* @param *hLed: Pointer to WS28XX_hLedTypeDef structure
* @param Pixel: Pixel Starts from 0 to Max - 1
* @param Color: RGB565 Color Code
*
* @retval bool: true or false
*/
bool WS28XX_SetPixel_RGB_565(WS28XX_HandleTypeDef *hLed, uint16_t Pixel, uint16_t Color)
{
bool answer = true;
uint8_t Red, Green, Blue;
do
{
if (Pixel >= hLed->MaxPixel)
{
answer = false;
break;
}
Red = ((Color >> 8) & 0xF8);
Green = ((Color >> 3) & 0xFC);
Blue = ((Color << 3) & 0xF8);
#if (WS28XX_GAMMA == false)
#if WS28XX_ORDER == WS28XX_ORDER_RGB
hLed->Pixel[Pixel][0] = Red;
hLed->Pixel[Pixel][1] = Green;
hLed->Pixel[Pixel][2] = Blue;
#elif WS28XX_ORDER == WS28XX_ORDER_BGR
hLed->Pixel[Pixel][0] = Blue;
hLed->Pixel[Pixel][1] = Green;
hLed->Pixel[Pixel][2] = Red;
#elif WS28XX_ORDER == WS28XX_ORDER_GRB
hLed->Pixel[Pixel][0] = Green;
hLed->Pixel[Pixel][1] = Red;
hLed->Pixel[Pixel][2] = Blue;
#endif
#else
#if WS28XX_ORDER == WS28XX_ORDER_RGB
hLed->Pixel[Pixel][0] = WS28XX_GammaTable[Red];
hLed->Pixel[Pixel][1] = WS28XX_GammaTable[Green];
hLed->Pixel[Pixel][2] = WS28XX_GammaTable[Blue];
#elif WS28XX_ORDER == WS28XX_ORDER_BGR
hLed->Pixel[Pixel][0] = WS28XX_GammaTable[Blue];
hLed->Pixel[Pixel][1] = WS28XX_GammaTable[Green];
hLed->Pixel[Pixel][2] = WS28XX_GammaTable[Red];
#elif WS28XX_ORDER == WS28XX_ORDER_GRB
hLed->Pixel[Pixel][0] = WS28XX_GammaTable[Green];
hLed->Pixel[Pixel][1] = WS28XX_GammaTable[Red];
hLed->Pixel[Pixel][2] = WS28XX_GammaTable[Blue];
#endif
#endif
}
while (0);
return answer;
}
/***********************************************************************************************************/
/**
* @brief Set Pixel
* @note Fill the pixel By RGB Values
*
* @param *hLed: Pointer to WS28XX_hLedTypeDef structure
* @param Pixel: Pixel Starts from 0 to Max - 1
* @param Color: RGB888 Color Code
*
* @retval bool: true or false
*/
bool WS28XX_SetPixel_RGB_888(WS28XX_HandleTypeDef *hLed, uint16_t Pixel, uint32_t Color)
{
bool answer = true;
uint8_t Red, Green, Blue;
do
{
if (Pixel >= hLed->MaxPixel)
{
answer = false;
break;
}
Red = ((Color & 0xFF0000) >> 16);
Green = ((Color & 0x00FF00) >> 8);
Blue = (Color & 0x0000FF);
#if (WS28XX_GAMMA == false)
#if WS28XX_ORDER == WS28XX_ORDER_RGB
hLed->Pixel[Pixel][0] = Red;
hLed->Pixel[Pixel][1] = Green;
hLed->Pixel[Pixel][2] = Blue;
#elif WS28XX_ORDER == WS28XX_ORDER_BGR
hLed->Pixel[Pixel][0] = Blue;
hLed->Pixel[Pixel][1] = Green;
hLed->Pixel[Pixel][2] = Red;
#elif WS28XX_ORDER == WS28XX_ORDER_GRB
hLed->Pixel[Pixel][0] = Green;
hLed->Pixel[Pixel][1] = Red;
hLed->Pixel[Pixel][2] = Blue;
#endif
#else
#if WS28XX_ORDER == WS28XX_ORDER_RGB
hLed->Pixel[Pixel][0] = WS28XX_GammaTable[Red];
hLed->Pixel[Pixel][1] = WS28XX_GammaTable[Green];
hLed->Pixel[Pixel][2] = WS28XX_GammaTable[Blue];
#elif WS28XX_ORDER == WS28XX_ORDER_BGR
hLed->Pixel[Pixel][0] = WS28XX_GammaTable[Blue];
hLed->Pixel[Pixel][1] = WS28XX_GammaTable[Green];
hLed->Pixel[Pixel][2] = WS28XX_GammaTable[Red];
#elif WS28XX_ORDER == WS28XX_ORDER_GRB
hLed->Pixel[Pixel][0] = WS28XX_GammaTable[Green];
hLed->Pixel[Pixel][1] = WS28XX_GammaTable[Red];
hLed->Pixel[Pixel][2] = WS28XX_GammaTable[Blue];
#endif
#endif
}
while (0);
return answer;
}
/***********************************************************************************************************/
/**
* @brief Set Pixel and Brightness
* @note Fill the pixel By RGB Values
*
* @param *hLed: Pointer to WS28XX_hLedTypeDef structure
* @param Pixel: Pixel Starts from 0 to Max - 1
* @param Color: RGB565 Color Code
* @param Brightness: Brightness level, 0 to 255
*
* @retval bool: true or false
*/
bool WS28XX_SetPixel_RGBW_565(WS28XX_HandleTypeDef *hLed, uint16_t Pixel, uint16_t Color, uint8_t Brightness)
{
bool answer = true;
uint8_t Red, Green, Blue;
do
{
if (Pixel >= hLed->MaxPixel)
{
answer = false;
break;
}
Red = ((Color >> 8) & 0xF8) * Brightness / 255;
Green = ((Color >> 3) & 0xFC)* Brightness / 255;
Blue = ((Color << 3) & 0xF8) * Brightness / 255;
#if (WS28XX_GAMMA == false)
#if WS28XX_ORDER == WS28XX_ORDER_RGB
hLed->Pixel[Pixel][0] = Red;
hLed->Pixel[Pixel][1] = Green;
hLed->Pixel[Pixel][2] = Blue;
#elif WS28XX_ORDER == WS28XX_ORDER_BGR
hLed->Pixel[Pixel][0] = Blue;
hLed->Pixel[Pixel][1] = Green;
hLed->Pixel[Pixel][2] = Red;
#elif WS28XX_ORDER == WS28XX_ORDER_GRB
hLed->Pixel[Pixel][0] = Green;
hLed->Pixel[Pixel][1] = Red;
hLed->Pixel[Pixel][2] = Blue;
#endif
#else
#if WS28XX_ORDER == WS28XX_ORDER_RGB
hLed->Pixel[Pixel][0] = WS28XX_GammaTable[Red];
hLed->Pixel[Pixel][1] = WS28XX_GammaTable[Green];
hLed->Pixel[Pixel][2] = WS28XX_GammaTable[Blue];
#elif WS28XX_ORDER == WS28XX_ORDER_BGR
hLed->Pixel[Pixel][0] = WS28XX_GammaTable[Blue];
hLed->Pixel[Pixel][1] = WS28XX_GammaTable[Green];
hLed->Pixel[Pixel][2] = WS28XX_GammaTable[Red];
#elif WS28XX_ORDER == WS28XX_ORDER_GRB
hLed->Pixel[Pixel][0] = WS28XX_GammaTable[Green];
hLed->Pixel[Pixel][1] = WS28XX_GammaTable[Red];
hLed->Pixel[Pixel][2] = WS28XX_GammaTable[Blue];
#endif
#endif
}
while (0);
return answer;
}
/***********************************************************************************************************/
/**
* @brief Set Pixel and Brightness
* @note Fill the pixel By RGB Values
*
* @param *hLed: Pointer to WS28XX_hLedTypeDef structure
* @param Pixel: Pixel Starts from 0 to Max - 1
* @param Color: RGB888 Color Code
* @param Brightness: Brightness level, 0 to 255
*
* @retval bool: true or false
*/
bool WS28XX_SetPixel_RGBW_888(WS28XX_HandleTypeDef *hLed, uint16_t Pixel, uint32_t Color, uint8_t Brightness)
{
bool answer = true;
uint8_t Red, Green, Blue;
do
{
if (Pixel >= hLed->MaxPixel)
{
answer = false;
break;
}
Red = ((Color & 0xFF0000) >> 16) * Brightness / 255 ;
Green = ((Color & 0x00FF00) >> 8) * Brightness / 255;
Blue = (Color & 0x0000FF) * Brightness / 255;
#if (WS28XX_GAMMA == false)
#if WS28XX_ORDER == WS28XX_ORDER_RGB
hLed->Pixel[Pixel][0] = Red;
hLed->Pixel[Pixel][1] = Green;
hLed->Pixel[Pixel][2] = Blue;
#elif WS28XX_ORDER == WS28XX_ORDER_BGR
hLed->Pixel[Pixel][0] = Blue;
hLed->Pixel[Pixel][1] = Green;
hLed->Pixel[Pixel][2] = Red;
#elif WS28XX_ORDER == WS28XX_ORDER_GRB
hLed->Pixel[Pixel][0] = Green;
hLed->Pixel[Pixel][1] = Red;
hLed->Pixel[Pixel][2] = Blue;
#endif
#else
#if WS28XX_ORDER == WS28XX_ORDER_RGB
hLed->Pixel[Pixel][0] = WS28XX_GammaTable[Red];
hLed->Pixel[Pixel][1] = WS28XX_GammaTable[Green];
hLed->Pixel[Pixel][2] = WS28XX_GammaTable[Blue];
#elif WS28XX_ORDER == WS28XX_ORDER_BGR
hLed->Pixel[Pixel][0] = WS28XX_GammaTable[Blue];
hLed->Pixel[Pixel][1] = WS28XX_GammaTable[Green];
hLed->Pixel[Pixel][2] = WS28XX_GammaTable[Red];
#elif WS28XX_ORDER == WS28XX_ORDER_GRB
hLed->Pixel[Pixel][0] = WS28XX_GammaTable[Green];
hLed->Pixel[Pixel][1] = WS28XX_GammaTable[Red];
hLed->Pixel[Pixel][2] = WS28XX_GammaTable[Blue];
#endif
#endif
}
while (0);
return answer;
}
/***********************************************************************************************************/
/**
* @brief Send Buffer to LEDs
* @note This function use PWM+DMA for Sending data
*
* @param *hLed: Pointer to WS28XX_hLedTypeDef structure
*
* @retval bool: true or false
*/
bool WS28XX_Update(WS28XX_HandleTypeDef *hLed)
{
bool answer = true;
uint32_t i = 2;
WS28XX_Lock(hLed);
for (uint16_t pixel = 0; pixel < hLed->MaxPixel; pixel++)
{
for (int rgb = 0; rgb < 3; rgb ++)
{
for (int b = 7; b >= 0 ; b--)
{
if ((hLed->Pixel[pixel][rgb] & (1 << b)) == 0)
{
hLed->Buffer[i] = hLed->Pulse0;
}
else
{
hLed->Buffer[i] = hLed->Pulse1;
}
i++;
}
}
}
if (HAL_TIM_PWM_Start_DMA(hLed->hTim, hLed->Channel, (const uint32_t*)hLed->Buffer, (hLed->MaxPixel * 24) + 4) != HAL_OK)
{
answer = false;
}
WS28XX_UnLock(hLed);
return answer;
}
/***********************************************************************************************************/