-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLEDStrip.ino
121 lines (100 loc) · 2.87 KB
/
LEDStrip.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
/*
* Arduino interface for the use of WS2812 strip LEDs
* Uses Adalight protocol and is compatible with Boblight, Prismatik etc...
* "Magic Word" for synchronisation is 'Ada' followed by LED High, Low and Checksum
* @author: Wifsimster <[email protected]>
* @library: FastLED v3.001
* @date: 11/22/2015
*/
#include "FastLED.h"
// Baudrate, higher rate allows faster refresh rate and more LEDs (defined in /etc/boblight.conf)
#define serialRate 115200
// #define NUM_LEDS 96
#define S1_PIN 4
#define S2_PIN 5
#define S3_PIN 11
#define S1_LEDS 30
#define S2_LEDS 18
#define S3_LEDS 7
#define NUM_LEDS S1_LEDS + S2_LEDS + S3_LEDS
// Adalight sends a "Magic Word" (defined in /etc/boblight.conf) before sending the pixel data
uint8_t prefix[] = {'A', 'd', 'a'},
hi, lo, chk, i;
// Initialise LED-array
CRGB leds[NUM_LEDS];
DEFINE_GRADIENT_PALETTE(vaporwave){
0, 255, 113, 206,
64, 185, 103, 255,
128, 1, 205, 254,
192, 5, 255, 161,
255, 255, 251, 150};
CRGBPalette16 myPal = vaporwave;
void initAnimation(int ledI, int ledNum)
{
for (int i = 0; i < ledNum; i++)
{
uint8_t step = map(i, 0, ledNum - 1, 0, 255);
leds[ledI + i] = ColorFromPalette(myPal, step);
FastLED.show();
delay(20);
}
for (int i = 0; i < ledNum; i++)
{
leds[ledI + i] = CRGB(0, 0, 0);
FastLED.show();
delay(20);
}
}
void setup()
{
// Use NEOPIXEL to keep true colors / WS2801
FastLED.addLeds<NEOPIXEL, S1_PIN>(leds, 0, S1_LEDS);
FastLED.addLeds<NEOPIXEL, S2_PIN>(leds, S1_LEDS, S2_LEDS);
FastLED.addLeds<NEOPIXEL, S3_PIN>(leds, S1_LEDS + S2_LEDS, S3_LEDS);
// Initial RGB flash
initAnimation(0, S1_LEDS);
initAnimation(S1_LEDS, S2_LEDS);
initAnimation(S1_LEDS + S2_LEDS, S3_LEDS);
Serial.begin(serialRate);
// Send "Magic Word" string to host
Serial.print("Ada\n");
}
void loop() {
// Wait for first byte of Magic Word
for(i = 0; i < sizeof prefix; ++i) {
waitLoop: while (!Serial.available()) ;;
// Check next byte in Magic Word
if(prefix[i] == Serial.read()) continue;
// otherwise, start over
i = 0;
goto waitLoop;
}
// Hi, Lo, Checksum
while (!Serial.available()) ;;
hi=Serial.read();
while (!Serial.available()) ;;
lo=Serial.read();
while (!Serial.available()) ;;
chk=Serial.read();
// If checksum does not match go back to wait
if (chk != (hi ^ lo ^ 0x55)) {
i=0;
goto waitLoop;
}
memset(leds, 0, NUM_LEDS * sizeof(struct CRGB));
// Read the transmission data and set LED values
for (uint8_t i = 0; i < NUM_LEDS; i++) {
byte r, g, b;
while(!Serial.available());
r = Serial.read();
while(!Serial.available());
g = Serial.read();
while(!Serial.available());
b = Serial.read();
leds[i].r = r;
leds[i].g = g;
leds[i].b = b;
}
// Shows new values
FastLED.show();
}