-
Notifications
You must be signed in to change notification settings - Fork 3
/
WS2812.cpp
250 lines (229 loc) · 5.72 KB
/
WS2812.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
/*
* another light weight WS2812 lib
*
* Controls WS2811/WS2812/WS2812B RGB-LEDs
*
* Based on the code by Matthias Riegler, Windell H. Oskay and Freezy
* Added HSV conversion from FastLED lib by Daniel Garcia and Mark Kriegsman; see fastled.io
*
* May 15: Changed hue parameter from to range 0..255
*
* Marv aka eMGoz or MGOS
*
*/
#include "WS2812.h"
#include <stdlib.h>
WS2812::WS2812(uint16_t num_leds)
{
count_led = num_leds;
pixels = (uint8_t*)malloc(count_led*3);
#ifdef RGB_ORDER_ON_RUNTIME
offsetGreen = 0;
offsetRed = 1;
offsetBlue = 2;
#endif
#ifdef USE_GLOBAL_BRIGHTNESS
brightness = 255;
#endif
}
void WS2812::setRGB(uint16_t index, uint8_t r, uint8_t g, uint8_t b)
{
if(index < count_led)
{
uint16_t tmp = index * 3;
pixels[OFFSET_R(tmp)] = r;
pixels[OFFSET_G(tmp)] = g;
pixels[OFFSET_B(tmp)] = b;
}
}
void WS2812::setRGB(uint16_t index, uint32_t rgb)
{
if(index < count_led)
{
uint16_t tmp = index * 3;
pixels[OFFSET_R(tmp)] = (rgb >> 16) & 0xFF;
pixels[OFFSET_G(tmp)] = (rgb >> 8) & 0xFF;
pixels[OFFSET_B(tmp)] = rgb & 0xFF;
}
}
uint32_t WS2812::getRGB(uint16_t index)
{
if(index < count_led)
{
uint16_t tmp = index * 3;
return ((uint32_t)(pixels[OFFSET_R(tmp)])<<16) | (pixels[OFFSET_G(tmp)] << 8) | pixels[OFFSET_B(tmp)];
}
return 0;
}
uint8_t WS2812::getR(uint16_t index)
{
if(index < count_led)
{
uint16_t tmp = index * 3;
return pixels[OFFSET_R(tmp)];
}
return 0;
}
uint8_t WS2812::getG(uint16_t index)
{
if(index < count_led)
{
uint16_t tmp = index * 3;
return pixels[OFFSET_G(tmp)];
}
return 0;
}
uint8_t WS2812::getB(uint16_t index)
{
if(index < count_led)
{
uint16_t tmp = index * 3;
return pixels[OFFSET_B(tmp)];
}
return 0;
}
#define HSV_SECTION_3 (256)
void WS2812::setHSV(uint16_t index, uint8_t hue, uint8_t sat, uint8_t val)
{
uint16_t h = hue*3;
uint8_t r,g,b;
uint8_t value = dim_curve[ val ];
uint8_t invsat = dim_curve[ 255 - sat ];
uint8_t brightness_floor = (value * invsat) / 256;
uint8_t color_amplitude = value - brightness_floor;
uint8_t section = h >> 8; // / HSV_SECTION_3; // 0..2
uint8_t offset = h & 0xFF ; // % HSV_SECTION_3; // 0..255
uint8_t rampup = offset; // 0..255
uint8_t rampdown = (HSV_SECTION_3 - 1) - offset; // 255..0
uint8_t rampup_amp_adj = (rampup * color_amplitude) / (256);
uint8_t rampdown_amp_adj = (rampdown * color_amplitude) / (256);
uint8_t rampup_adj_with_floor = rampup_amp_adj + brightness_floor;
uint8_t rampdown_adj_with_floor = rampdown_amp_adj + brightness_floor;
if( section )
{
if( section == 1)
{
// section 1: 0x40..0x7F
r = brightness_floor;
g = rampdown_adj_with_floor;
b = rampup_adj_with_floor;
}
else
{
// section 2; 0x80..0xBF
r = rampup_adj_with_floor;
g = brightness_floor;
b = rampdown_adj_with_floor;
}
}
else
{
// section 0: 0x00..0x3F
r = rampdown_adj_with_floor;
g = rampup_adj_with_floor;
b = brightness_floor;
}
setRGB(index,r,g,b);
}
uint8_t WS2812::getH(uint16_t index)
{
if(index < count_led)
{
uint16_t tmp = index * 3;
uint8_t r = pixels[OFFSET_R(tmp)];
uint8_t g = pixels[OFFSET_G(tmp)];
uint8_t b = pixels[OFFSET_B(tmp)];
uint8_t rgbMin, v;
rgbMin = r < g ? (r < b ? r : b) : (g < b ? g : b);
v = r > g ? (r > b ? r : b) : (g > b ? g : b);
if (v == 0)
{
return 0;
}
if (v == rgbMin)
{
return 0;
}
if (v == r)
return 43 * (g - b) / (v - rgbMin);
else if (v == g)
return 85 + 43 * (b - r) / (v - rgbMin);
else
return 171 + 43 * (r - g) / (v - rgbMin);
}
return 0;
}
uint8_t WS2812::getS(uint16_t index)
{
if(index < count_led)
{
uint16_t tmp = index * 3;
uint8_t r = pixels[OFFSET_R(tmp)];
uint8_t g = pixels[OFFSET_G(tmp)];
uint8_t b = pixels[OFFSET_B(tmp)];
uint8_t rgbMin, v;
rgbMin = r < g ? (r < b ? r : b) : (g < b ? g : b);
v = r > g ? (r > b ? r : b) : (g > b ? g : b);
if (v == 0)
{
return 0;
}
return 255 * int(v - rgbMin) / v;
}
return 0;
}
uint8_t WS2812::getV(uint16_t index)
{
if(index < count_led)
{
uint16_t tmp = index * 3;
uint8_t r = pixels[OFFSET_R(tmp)];
uint8_t g = pixels[OFFSET_G(tmp)];
uint8_t b = pixels[OFFSET_B(tmp)];
return r > g ? (r > b ? r : b) : (g > b ? g : b);
}
return 0;
}
void WS2812::sync()
{
*ws2812_port_reg |= pinMask; // Enable DDR
ws2812_sendarray_mask(pixels,3*count_led,pinMask,(uint8_t*) ws2812_port,(uint8_t*) ws2812_port_reg);
}
#ifdef RGB_ORDER_ON_RUNTIME
void WS2812::setColorOrderGRB() // Default color order
{
offsetGreen = 0;
offsetRed = 1;
offsetBlue = 2;
}
void WS2812::setColorOrderRGB()
{
offsetRed = 0;
offsetGreen = 1;
offsetBlue = 2;
}
void WS2812::setColorOrderBRG()
{
offsetBlue = 0;
offsetRed = 1;
offsetGreen = 2;
}
#endif
WS2812::~WS2812()
{
}
#ifndef ARDUINO
void WS2812::setOutput(const volatile uint8_t* port, volatile uint8_t* reg, uint8_t pin)
{
pinMask = (1<<pin);
ws2812_port = port;
ws2812_port_reg = reg;
}
#else
void WS2812::setOutput(uint8_t pin)
{
pinMask = digitalPinToBitMask(pin);
ws2812_port = portOutputRegister(digitalPinToPort(pin));
ws2812_port_reg = portModeRegister(digitalPinToPort(pin));
}
#endif