-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNeoPixel_WS2812B.c
154 lines (118 loc) · 3.06 KB
/
NeoPixel_WS2812B.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
#include "NeoPixel_WS2812B.h"
//#include "stm32f1xx_hal.h"
#include "string.h"
void WS28_init(WS28_HandleTypeDef *hws28)
{
hws28->BuffLen = hws28->PixelLen * 24;
hws28->pPixelBuffer = (uint32_t*)calloc(hws28->PixelLen, sizeof(uint32_t));
hws28->pSendBuffer = (uint8_t*)calloc(hws28->BuffLen, sizeof(uint8_t));
WS28_SET_COLOR_ALL(hws28, WS28_RGB2COLOR(0, 0, 0));
}
void WS28_REFRESH(WS28_HandleTypeDef *hws28)
{
if (hws28->state == READY)
{
while (HAL_SPI_GetState(hws28->hspi) != HAL_SPI_STATE_READY) {};
hws28->state = BUSY;
HAL_SPI_Transmit_DMA(hws28->hspi, hws28->pSendBuffer, hws28->BuffLen);
}
}
// Color Conversion
uint32_t WS28_RGB2COLOR(uint8_t r, uint8_t g, uint8_t b)
{
return (((uint32_t)g << 16) | ((uint32_t)r << 8) | b);
}
uint32_t WS28_HSL2COLOR(double H, double S, double L)
{
uint8_t r, g, b;
ColorSpace_HSL2RGB(H, S, L, &r, &g, &b);
return WS28_RGB2COLOR(r, g, b);
}
void WS28_COLOR2BIT(uint32_t color, uint8_t *colorbit)
{
//printf("COLORBIT: In COLOR:%6X\n", color);
for (int i = 24;i > 0;i--)
{
switch (color & 0x1)
{
case 0:
colorbit[i - 1] = BIT0;
//printf("0");
break;
case 1:
colorbit[i - 1] = BIT1;
//printf("1");
break;
default:
break;
}
/*
switch (i)
{
case 7:
printf(" ");
break;
case 15:
printf(" ");
break;
case 23:
printf(" \n");
break;
default:
break;
}
*/
color = color >> 1;
}
}
void WS28_GET_PIXEL_RGB(uint32_t color, uint8_t *r, uint8_t *g, uint8_t *b)
{
*b = (uint8_t)color;
*r = (uint8_t)(color >> 8);
*g = (uint8_t)(color >> 16);
}
// Set Color Section
void WS28_SET_COLOR_ALL(WS28_HandleTypeDef *hws28, uint32_t color)
{
uint8_t colorbit[24];
WS28_COLOR2BIT(color, colorbit);
for (int i = 0;i < hws28->PixelLen;i++)
{
memcpy(hws28->pSendBuffer + (24 * i), &colorbit, 24);
hws28->pPixelBuffer[i] = color;
}
hws28->state = READY;
WS28_REFRESH(hws28);
}
void WS28_SET_COLOR_PIXEL(WS28_HandleTypeDef *hws28, uint8_t pos, uint32_t color)
{
uint8_t colorbit[24];
WS28_COLOR2BIT(color, colorbit);
memcpy(hws28->pSendBuffer + (24 * pos), &colorbit, 24);
hws28->pPixelBuffer[pos] = color;
hws28->state = READY;
WS28_REFRESH(hws28);
}
void WS28_SET_BRIGHTNESS(WS28_HandleTypeDef *hws28, uint8_t scale)
{
if (scale >= 0 && scale <= 100) hws28->Brightness = scale;
uint8_t colorbit[24];
uint32_t pixelcolor;
uint8_t r, g, b;
double h, s, l;
for (uint8_t i = 0; i < hws28->PixelLen;i++)
{
pixelcolor = hws28->pPixelBuffer[i];
// Extract RGB from PIXEL
WS28_GET_PIXEL_RGB(hws28->pPixelBuffer[i], &r, &g, &b);
// Convert RGB to HSL
ColorSpace_RGB2HSL(r, g, b, &h, &s, &l);
// Update Pixel with new RGB.
hws28->pPixelBuffer[i] = WS28_HSL2COLOR(h, s, hws28->Brightness);
WS28_COLOR2BIT(hws28->pPixelBuffer[i], colorbit);
memcpy(hws28->pSendBuffer + (24 * i), &colorbit, 24);
hws28->pPixelBuffer[i] = WS28_RGB2COLOR(r, g, b);
}
hws28->state = READY;
WS28_REFRESH(hws28);
}