-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLedStrip_v2.ino
332 lines (289 loc) · 8.51 KB
/
LedStrip_v2.ino
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
// https://github.com/Makuna/NeoPixelBus
// https://github.com/Makuna/NeoPixelBus/wiki/NeoPixelBus-object-API
// https://github.com/Makuna/NeoPixelBus/wiki/HslColor-object-API
#include <NeoPixelBus.h>
#include <MemoryUsage.h>
#include <ArduinoJson.h>
#include <EEPROM.h>
#define MAX_PIXELS 300
//#define MAX_PIXELS 300
//#define DEBUG
uint16_t PixelCount;
const uint8_t PixelPin = 2;
uint16_t maxchars;
uint16_t frame;
uint32_t micros_start = micros(), micros_end, micros_diff;
uint8_t mode;
uint8_t brightness;
float speed = 2;
float periods = 2;
uint8_t fading = 1;
#define MODE_OFF 0
#define MODE_RAINBOW 1
#define MODE_FILL 2
#define MODE_BINARY 3
#define BIGGEST_MODE_NUMBER 3
struct Effect {
uint16_t pixels;
uint8_t mode;
uint8_t brightness;
float speed;
float periods;
uint8_t fading;
};
//#define MAX_BRIGHTNESS 128
#define MAX_BRIGHTNESS 255
//#define MAX_BRIGHTNESS 10
void ensureVariableSanity() {
if (PixelCount > MAX_PIXELS) PixelCount = MAX_PIXELS;
if (PixelCount < 4) PixelCount = MAX_PIXELS;
if (mode > BIGGEST_MODE_NUMBER) mode = 1;
if (brightness == 0) brightness = MAX_BRIGHTNESS;
maxchars = PixelCount*3;
}
#ifdef DEBUG
#define DBG_MSG(msg) Serial.println(msg);
#else
#define DBG_MSG(msg)
#endif
NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> strip(MAX_PIXELS, PixelPin);
//NeoGamma<NeoGammaTableMethod> colorGamma;
NeoGamma<NeoGammaEquationMethod> colorGamma;
void setup() {
Serial.begin(2000000);
Serial.setTimeout(10);
#ifdef DEBUG
while (!Serial) continue;
#endif
Serial.println();
Serial.println("Initializing...");
Serial.flush();
strip.Begin();
Serial.println();
Serial.println("Running...");
DBG_MSG("DEBUG!");
EEPROM.get(0, PixelCount);
EEPROM.get(2, mode);
EEPROM.get(3, brightness);
ensureVariableSanity();
/*for(uint16_t i = 0; i < PixelCount; i++) {
float c = ((float)i) / ((float)PixelCount);
//HslColor color = HslColor(c, 1.0f, ((float)brightness)/2.0f/MAX_BRIGHTNESS);
//HslColor color = HslColor(c, 1.0f, MAX_BRIGHTNESS / 255.0f);
HslColor color = HslColor(c, 1.0f, 0.5f);
strip.SetPixelColor(i, color);
}
strip.Show();*/
frame = 0;
fading = 1;
Serial.print("PixelCount:"); Serial.println(PixelCount);
Serial.print("mode:"); Serial.println(mode);
Serial.print("brightness:"); Serial.println(brightness);
FREERAM_PRINT;
}
#define HSV_HUE_SEXTANT 256
#define HSV_HUE_STEPS (6 * HSV_HUE_SEXTANT)
#define HSV_HUE_MIN 0
#define HSV_HUE_MAX (HSV_HUE_STEPS - 1)
#define HSV_SAT_MIN 0
#define HSV_SAT_MAX 255
#define HSV_VAL_MIN 0
#define HSV_VAL_MAX 255
void loop() {
uint16_t value;
if (fading) {
int16_t f = frame*2; if (f > 255) f = 255;
RgbColor gamma = colorGamma.Correct(RgbColor(f, 0, 0));
value = gamma.R;
//value = pow((double)frame, 3.0) / 8000.0;
} else {
value = brightness;
}
if (value >= brightness) {
value = brightness;
fading = 0;
}
if (mode == MODE_RAINBOW) {
uint16_t hue_moving = (((float)(frame))*speed)/((float)PixelCount) * HSV_HUE_MAX;
float hue_mul = HSV_HUE_MAX/(((float)PixelCount)/periods);
uint8_t *ptr = strip.Pixels();
///uint8_t r,g,b;
for (uint16_t i = 0; i < PixelCount; i++) {
uint16_t hue_temp = ((float)i)*hue_mul;
uint16_t hue = (hue_temp + hue_moving) % HSV_HUE_MAX;
fast_hsv2rgb_32bit(hue, 255, value, ptr++, ptr++, ptr++);
}
//strip.RotateLeft(1); delay(10);
strip.Dirty();
strip.Show();
}
if (mode == MODE_BINARY || mode == MODE_OFF) {
//delay(25);
delay(4);
}
pollSerial();
frame++;
if (frame == (PixelCount/speed)) {
frame = 0;
fading = 0;
//Serial.println("frame0");
}
//delay(20);
}
#define IF(a,b) if (!strcmp(root[a],b))
void pollSerial() {
if (!Serial.available()) return;
if (Serial.peek() == 'b') {
//Serial.println("BINARY");
//Serial.println(Serial.read());
Serial.read(); //throw 'b' away
mode = MODE_BINARY;
char *ptr = strip.Pixels();
uint16_t cnt = Serial.readBytes(ptr, maxchars);
strip.Dirty();
strip.Show();
} else {
//Serial.println("SOMETHINGELSE");
//ptr[cnt] = '\0';
/*DynamicJsonDocument doc; //ArduinoJson 6
DeserializationError error = deserializeJson(doc, ptr, cnt);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.c_str());
return;
}
JsonObject root = doc.as<JsonObject>();*/
/*StaticJsonBuffer<300> jsonBuffer; //ArduinoJson 5
JsonObject& root = jsonBuffer.parseObject(ptr);*/
DynamicJsonBuffer jsonBuffer(255); //ArduinoJson5
JsonObject& root = jsonBuffer.parseObject(Serial);
// Test if parsing succeeds.
if (!root.success()) {
Serial.println("parseObject() failed");
return;
}
if (root["mode"]) { Serial.print("Got mode:\""); Serial.print((const char*)root["mode"]); Serial.println("\""); }
if (root["pixels"]) {
PixelCount = root["pixels"];
Serial.println("PIXELS!");
}
IF("mode", "off") {
frame = 0;
mode = MODE_OFF;
Serial.println("OFFMODE!");
strip.ClearTo(RgbColor(0));
strip.Show();
}
IF("mode", "rainbow") {
frame = 0;
mode = MODE_RAINBOW;
Serial.println("RAINBOWMODE!");
}
IF("mode", "fill") {
mode = MODE_FILL;
uint8_t r = root["r"], g = root["g"], b = root["b"];
strip.ClearTo(RgbColor(r,g,b));
strip.Show();
Serial.println("FILLMODE!");
}
if (root["a"]) { //for Node-RED color picker, which sends r, g, b, a
mode = MODE_FILL;
uint8_t r = root["r"], g = root["g"], b = root["b"];
RgbColor gamma = colorGamma.Correct(RgbColor(r, g, b));
//strip.ClearTo(RgbColor(r,g,b));
strip.ClearTo(gamma);
strip.Show();
Serial.println("FILLMODE-A!");
}
IF("mode", "binary") {
mode = MODE_BINARY;
Serial.println("BINARYMODE!");
}
if (root["brightness"]) {
brightness = root["brightness"];
Serial.println("BRIGHTNESS!");
}
if (root["speed"]) {
speed = root["speed"];
Serial.println("SPEED!");
}
if (root["periods"]) {
periods = root["periods"];
Serial.println("PERIODS!");
}
IF("fading", "true") fading = 1; else fading = 0;
IF("save", "true") {
EEPROM.put(0, PixelCount);
EEPROM.put(2, mode);
EEPROM.put(3, brightness);
Serial.println("SAVE!");
}
ensureVariableSanity();
Serial.print("PixelCount:"); Serial.println(PixelCount);
Serial.print("mode:"); Serial.println(mode);
Serial.print("brightness:"); Serial.println(brightness);
Serial.print("frame:"); Serial.println(frame);
FREERAM_PRINT;
}
}
/*
* fast_hsv2rgb_32bit(uint16_t h, uint8_t s, uint8_t v, uint8_t *r, uint8_t *g , uint8_t *b)
*/
#define HSV_MONOCHROMATIC_TEST(s,v,r,g,b) \
do { \
if(!(s)) { \
*(r) = *(g) = *(b) = (v); \
return; \
} \
} while(0)
#define HSV_SWAPPTR(a,b) do { uint8_t *tmp = (a); (a) = (b); (b) = tmp; } while(0)
#define HSV_POINTER_SWAP(sextant,r,g,b) \
do { \
if((sextant) & 2) { \
HSV_SWAPPTR((r), (b)); \
} \
if((sextant) & 4) { \
HSV_SWAPPTR((g), (b)); \
} \
if(!((sextant) & 6)) { \
if(!((sextant) & 1)) { \
HSV_SWAPPTR((r), (g)); \
} \
} else { \
if((sextant) & 1) { \
HSV_SWAPPTR((r), (g)); \
} \
} \
} while(0)
void fast_hsv2rgb_32bit(uint16_t h, uint8_t s, uint8_t v, uint8_t *g, uint8_t *r , uint8_t *b) {
HSV_MONOCHROMATIC_TEST(s, v, r, g, b); // Exit with grayscale if s == 0
uint8_t sextant = h >> 8;
//HSV_SEXTANT_TEST(sextant); // Optional: Limit hue sextants to defined space
HSV_POINTER_SWAP(sextant, r, g, b); // Swap pointers depending which sextant we are in
*g = v; // Top level
// Perform actual calculations
/*
* Bottom level: v * (1.0 - s)
* --> (v * (255 - s) + error_corr + 1) / 256
*/
uint16_t ww; // Intermediate result
ww = v * (255 - s); // We don't use ~s to prevent size-promotion side effects
ww += 1; // Error correction
ww += ww >> 8; // Error correction
*b = ww >> 8;
uint8_t h_fraction = h & 0xff; // 0...255
uint32_t d; // Intermediate result
if(!(sextant & 1)) {
// *r = ...slope_up...;
d = v * (uint32_t)((255 << 8) - (uint16_t)(s * (256 - h_fraction)));
d += d >> 8; // Error correction
d += v; // Error correction
*r = d >> 16;
} else {
// *r = ...slope_down...;
d = v * (uint32_t)((255 << 8) - (uint16_t)(s * h_fraction));
d += d >> 8; // Error correction
d += v; // Error correction
*r = d >> 16;
}
}