-
Notifications
You must be signed in to change notification settings - Fork 0
/
I2CtoFastLEDs.ino
411 lines (404 loc) · 11.6 KB
/
I2CtoFastLEDs.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
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
/*
* @todo I2CtoFastLEDs reset
* @body Allow devices Running I2CtoFastLEDs code to be reset by their routers.
*/
// Varidaic Debug Macro
#define DEBUG //Comment this line to disable Debug output
#ifdef DEBUG // Debug is on
#define DBEGIN(...) Serial.begin(__VA_ARGS__)
#define DPRINT(...) Serial.print(__VA_ARGS__) //Sends our arguments to DPRINT()
#define DPRINTLN(...) Serial.println(__VA_ARGS__) //Sends our arguments to DPRINTLN()
#define DFLUSH(...) Serial.flush()
#else // Debug is off
#define DBEGIN(...)
#define DPRINT(...) //Nothing Happens
#define DPRINTLN(...) //Nothing Happens
#define DFLUSH(...)
#endif // end macro
#define DEBUG_LED 13
#include "FastLED.h"
#include <Wire.h>
#define NUM_LEDS 28
#define DATA_PIN 2
#define DATA_PIN_2 3
//#define DATA_PIN_3 4
//#define DATA_PIN_4 5
#define FRAMES_PER_SECOND 120
#define ZOOMING_BEATS_PER_MINUTE 122
#define STROBE_BEATS_PER_MINUTE 97.5
#define I2C_ADDR 4
#define CONNECTED_STRIPS 2
#define FL(aa,bb) for (int i = aa; i < bb; i++)
CRGB ledsA[NUM_LEDS];
CRGB ledsB[NUM_LEDS];
//CRGB ledsC[NUM_LEDS];
//CRGB ledsD[NUM_LEDS];
CRGB ourCol = CRGB(255, 255, 255);
CRGB startup[] = {CRGB(255, 123, 0), CRGB(0, 255, 45), CRGB(0, 123, 255), CRGB(0, 255, 255)};
int x = 0; // holder for i2c message
String string = "";
uint8_t gHue = 0; // rotating "base color" used by many of the patterns
int timeSinceBt = 0;
int autoMode = 1;
int autoSecs = 2;
bool debugLED = false;
void(* resetFunc) (void) = 0; // Software reset hack
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
DPRINT("gotMessage ");
while (Wire.available()) { // loop through all but the last
char c = Wire.read(); // receive byte as a character
string.concat(c); // print the character
}
timeSinceBt = 0;
DPRINTLN(string);
string.trim();
parser();
}
void parser(){
DPRINTLN("PARSER");
while(string.length() >= 1){ // While there message left to read.
DPRINT("Message is ");
DPRINT(string);
DPRINT(" With length ");
DPRINTLN(string.length());
if(!string.endsWith(","))string.concat(","); // STOP CRASHING;
String subs = string.substring(0,string.indexOf(',')); // get everything until the first comma.
DPRINT("SubStr - ");
DPRINT(subs);
string.remove(0,string.indexOf(',')+1); // remove everything up to and including the first comma
DPRINT(" String - ");
DPRINTLN(string);
if(subs.startsWith("Pul")){
doPulse();
string = "";
}
if (subs.startsWith("Ptn")){ // next value is pattern.
DPRINT("PTN ");
String ptn = string.substring(0,string.indexOf(",")); // get everything until the comma
DPRINT(ptn);
DPRINT(" - ");
x = ptn.toInt(); // Its going to be an integer. its the pattern number,
DPRINTLN(x);
string.remove(0,string.indexOf(",")+1); // Remove the value
}
if (subs.startsWith("Atm")){ // next value is boolean for automode
DPRINT("ATM ");
String atm = string.substring(0,string.indexOf(",")); // get until first comma
autoMode = atm.toInt(); // also an integer
DPRINT(atm);
DPRINT(" - ");
DPRINTLN(autoMode);
string.remove(0,string.indexOf(",")+1); // remove it
}
if (subs.startsWith("Ats")){ // next value is autoSecs
DPRINT("ATS ");
String ats = string.substring(0,string.indexOf(",")); // get the value,
autoSecs = ats.toInt();
string.remove(0,string.indexOf(",")+1); // remove the value and trailing comma
}
if (subs.startsWith("Col")){ // its the color
String r = string.substring(0,string.indexOf(",")); // first bit is red,
ourCol.r = r.toInt(); // convert to an int
string.remove(0,string.indexOf(",")+1); // remove red and comma
String b = string.substring(0,string.indexOf(",")); // next up its blue
ourCol.b = b.toInt(); // to integer
string.remove(0,string.indexOf(",")+1); // remove blue and comma
String g = string.substring(0,string.indexOf(",")); // then green
ourCol.g = g.toInt(); // conver to int
string.remove(0,string.indexOf(",")+1); // thats colour done, remove the value and the comma
}
DPRINTLN(string.length()); // prints the length of the command each iteration
}
DPRINT("STR = "); // prints after length < 1
DPRINTLN(string);
string = ""; // empty it
}
void doPulse() {
turnOff();
FastLED.show();
FL(0,NUM_LEDS){
ledsA[i]=CRGB::White;
}
copyLeds();
FastLED.show();
FastLED.delay(100);
turnOff();
FastLED.show();
}
void randX() {
if (autoMode == 1) {
x = random(2, 10);
DPRINT("RANDOM ");
DPRINTLN(x);
}
}
void setup() {
Wire.begin(I2C_ADDR);
Wire.onReceive(receiveEvent);
pinMode(DEBUG_LED, OUTPUT);
DBEGIN(115200);
DPRINTLN("Ready for i2c");
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(ledsA, NUM_LEDS);
FastLED.addLeds<WS2812B, DATA_PIN_2, GRB>(ledsB, NUM_LEDS);
//FastLED.addLeds<WS2812B, DATA_PIN_3, GRB>(ledsC, NUM_LEDS);
//FastLED.addLeds<WS2812B, DATA_PIN_4, GRB>(ledsD, NUM_LEDS);
FastLED.setBrightness(128);
int i = 0;
while (i < 4) {
ourCol = startup[i];
turnOn();
copyLeds();
FastLED.show();
FastLED.delay(300);
i++;
}
turnOff();
copyLeds();
FastLED.show();
}
void loop() {
EVERY_N_SECONDS(2) {
if(debugLED){
debugLED = false;
digitalWrite(DEBUG_LED,HIGH);
}
else{
debugLED = true;
digitalWrite(DEBUG_LED,LOW);
}
}
if(autoMode != 2){
EVERY_N_MILLISECONDS(30) {
gHue++;
}
EVERY_N_SECONDS(1) {
timeSinceBt++;
if (timeSinceBt == autoSecs) {
timeSinceBt = 0;
randX();
}
}
switch (x) {
case 0:
turnOff();
break;
case 1:
turnOn();
break;
case 2:
theLights();
break;
case 3:
rainbow();
break;
case 4:
rainbowWithGlitter();
break;
case 5:
confetti();
break;
case 6:
sinelon();
break;
case 7:
bpm();
break;
case 8:
juggle();
break;
case 9:
simpleStrobe();
break;
}
//call function
copyLeds();
FastLED.show();
FastLED.delay(1000 / FRAMES_PER_SECOND); // insert a delay to keep the framerate modest
// EVERY_N_MILLISECONDS( 20 )
}
}
void copyLeds() {
///for (int i = 0; i < NUM_LEDS; i++)
FL(0,NUM_LEDS){
ledsB[i] = ledsA[i];
// ledsC[i] = ledsA[i];
// ledsD[i] = ledsA[i];
}
}
void turnOff() {
FL(0,NUM_LEDS) {
ledsA[i] = CRGB( 0, 0, 0);
}
}
void turnOn() {
FL(0,NUM_LEDS) {
ledsA[i] = ourCol;
}
}
void theLights() { // speckles and strobes
fadeToBlackBy( ledsA, NUM_LEDS, 10);
int pos = random16(NUM_LEDS);
ledsA[pos] = ourCol;
}
void rainbow()
{
// FastLED's built-in rainbow generator
fill_rainbow( ledsA, NUM_LEDS, gHue, 7);
}
void addGlitter( fract8 chanceOfGlitter)
{
if ( random8() < chanceOfGlitter) {
ledsA[ random16(NUM_LEDS) ] += CRGB::White;
}
}
void rainbowWithGlitter()
{
// built-in FastLED rainbow, plus some random sparkly glitter
rainbow();
addGlitter(80);
}
void confetti()
{
// random colored speckles that blink in and fade smoothly
fadeToBlackBy( ledsA, NUM_LEDS, 10);
int pos = random16(NUM_LEDS);
ledsA[pos] = ourCol;
ledsA[pos] += CHSV( gHue + random8(64), 200, 255);
}
void sinelon()
{
// a colored dot sweeping back and forth, with fading trails
fadeToBlackBy( ledsA, NUM_LEDS, 20);
int pos = beatsin16( 13, 0, NUM_LEDS - 1 );
ledsA[pos] = ourCol;
ledsA[pos] += CHSV( gHue, 255, 192);
}
void bpm()
{
// colored stripes pulsing at a defined Beats-Per-Minute (BPM)
uint8_t BeatsPerMinute = 62;
CRGBPalette16 palette = PartyColors_p;
uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
FL(0,NUM_LEDS) { //9948
ledsA[i] = ourCol;
ledsA[i] = ColorFromPalette(palette, gHue + (i * 2), beat - gHue + (i * 10));
}
}
void juggle() {
// eight colored dots, weaving in and out of sync with each other
fadeToBlackBy( ledsA, NUM_LEDS, 20);
byte dothue = 0;
FL(0,8) {
ledsA[beatsin16( i + 7, 0, NUM_LEDS - 1 )] |= ourCol;
ledsA[beatsin16( i + 7, 0, NUM_LEDS - 1 )] |= CHSV(dothue, 200, 255);
dothue += 32;
}
}
void simpleStrobe () {
fill_solid( ledsA, NUM_LEDS, CRGB::Black);
const uint8_t kStrobeCycleLength = 6; // light every Nth frame
static uint8_t sStrobePhase = 0;
sStrobePhase = sStrobePhase + 1;
if( sStrobePhase >= kStrobeCycleLength ) {
sStrobePhase = 0;
}
if( sStrobePhase == 0 ) {
uint8_t dashperiod= beatsin8( 8/*cycles per minute*/, 4,10);
uint8_t dashwidth = (dashperiod / 4) + 1;
uint8_t zoomBPM = STROBE_BEATS_PER_MINUTE;
int8_t dashmotionspeed = beatsin8( (zoomBPM /2), 1,dashperiod);
if( dashmotionspeed >= (dashperiod/2)) {
dashmotionspeed = 0 - (dashperiod - dashmotionspeed );
}
uint8_t cycle = beat8(2); // two cycles per minute
uint8_t easedcycle = ease8InOutCubic( ease8InOutCubic( cycle));
uint8_t wavecycle = cubicwave8( easedcycle);
// uint8_t hueShift = 0; // NO SHIFT OF HUE IN COLOUR (we should rebuild in RGB...)
uint8_t hueShift = scale8( wavecycle,130); // METHOD HOW HUE VALUE SHIFTS
uint8_t strobesPerPosition = 2; // try 1..4
strobeCore( dashperiod, dashwidth, dashmotionspeed, strobesPerPosition, hueShift);
}
}
void strobeCore(
uint8_t dashperiod, uint8_t dashwidth, int8_t dashmotionspeed, uint8_t stroberepeats,
uint8_t huedelta) {
static uint8_t sRepeatCounter = 0;
static int8_t sStartPosition = 0;
static uint8_t sStartHue = 0;
sStartHue += 1; //Shift the Colour little by little
sRepeatCounter = sRepeatCounter + 1;
if( sRepeatCounter>= stroberepeats) {
sRepeatCounter = 0;
sStartPosition = sStartPosition + dashmotionspeed;
if( sStartPosition >= dashperiod ) {
while( sStartPosition >= dashperiod) { sStartPosition -= dashperiod; }
sStartHue -= huedelta;
} else if( sStartPosition < 0) {
while( sStartPosition < 0) { sStartPosition += dashperiod; }
sStartHue += huedelta;
}
}
const uint8_t kSaturation = 208; // WHITE >> CURRENT COLOUR control (def 208)
const uint8_t kValue = 200; // Brightness??
strobeDraw( sStartPosition, NUM_LEDS-1, dashperiod, dashwidth, sStartHue, huedelta, kSaturation, kValue);
}
static void strobeDraw(
uint8_t startpos, uint16_t lastpos, uint8_t period, uint8_t width,
uint8_t huestart, uint8_t huedelta, uint8_t saturation, uint8_t value)
{
uint8_t hue = huestart;
for( uint16_t i = startpos; i <= lastpos; i += period) {
CRGB color = CHSV( hue, saturation, value);
//CRGB color = CRGB::Blue; // USE TO COMPLETELY BYPASS HSV Change Scheme
uint16_t pos = i;
for( uint8_t w = 0; w < width; w++) {
ledsA[ pos ] = color;
pos++;
if( pos >= NUM_LEDS) {
break;
}
}
hue += huedelta;
}
}
//function where a red pixle moves down the LED strips from A1-D28. RE Issue #44
void pixleRef1(){
FL(0,NUM_LEDS){
ledsA[i]=CRGB::Red;
ledsA[i-1]=ourCol;
FastLED.show();
delay(250);
if (i==28){
ledsA[28]=ourCol;
}
}
FL(0,NUM_LEDS){
ledsB[i]=CRGB::Red;
ledsB[i-1]=ourCol;
FastLED.show();
delay(250);
if (i==28){
ledsB[28]=ourCol;
}
}
/*FL(0,NUM_LEDS){
ledsC[i]=CRGB::Red;
ledsC[i-1]=ourCol;
FastLED.show();
delay(250);
if (i==28){
ledsC[28]=ourCol;
}
}
FL(0,NUM_LEDS){
ledsD[i]=CRGB::Red;
ledsD[i-1]=ourCol;
FastLED.show();
delay(250);
if (i==28){
ledsD[28]=ourCol;
}
}*/
}