-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathambience_node.ino
292 lines (243 loc) · 9.17 KB
/
ambience_node.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
// Hey! Particle IDE put these include lines in but you still have to to do the "Add to App" thing.
#include <SparkJson.h>
#include <neopixel.h>
SYSTEM_MODE(SEMI_AUTOMATIC);
#define MAIN_GROUP 0
#define ACCENT_GROUP 1
#define TEST_GROUP 2
#define OVERHEAD_GROUP 3
// ---- CHANGE THESE DEPENDING ON YOUR WIRING AND NEOPIXEL HARDWARE ----
#define PIXEL_PIN D6
// SK6812RGBW is RGBW
// WS2812B are the older ones you own
// #define PIXEL_TYPE WS2812B
#define PIXEL_TYPE SK6812RGBW
// int PIXEL_COUNT = 24;
int PIXEL_COUNT = 60;
// int PIXEL_COUNT = 100;
// int PIXEL_COUNT = 120;
int GROUP = ACCENT_GROUP;
// int GROUP = MAIN_GROUP;
// int GROUP = TEST_GROUP;
// int GROUP = OVERHEAD_GROUP;
// the aux strip is fragile, tone down the brightness
// comment this out for every other one
double BRIGHTNESS_SCALE = 0.20;
// double BRIGHTNESS_SCALE = 1.0
// Globals of sorts
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
int colorStart = 0;
int colorEnd = 255;
int brightness = defaultBrightness();
char theme[15];
boolean connectToCloud = false;
uint32_t white = strip.Color(100, 100, 100);
uint32_t black = strip.Color(0, 0, 0);
uint32_t green = strip.Color(45, 210, 0);
uint32_t red = strip.Color(255, 0, 0);
uint32_t lime = strip.Color(25, 150, 20);
uint32_t orange = strip.Color(180, 45, 0);
uint32_t blacklight = strip.Color(95, 0, 140);
uint32_t yellow = strip.Color(150, 105, 0);
uint32_t blue = strip.Color(30, 0, 225);
uint32_t bluegreen = strip.Color(0, 110, 165);
uint32_t pink = strip.Color(230, 0, 25);
static uint8_t getWhite(uint32_t c) { return (uint8_t)(c >> 24); }
static uint8_t getRed(uint32_t c) { return (uint8_t)(c >> 16); }
static uint8_t getGreen(uint32_t c) { return (uint8_t)(c >> 8); }
static uint8_t getBlue(uint32_t c) { return (uint8_t)c; }
void setup() {
strcmp(theme, "unknownTheme");
// careful with OSH Park PCBs - what is the total budget? 80?
// red and green are swapped, plus this has white
if (PIXEL_TYPE == SK6812RGBW) {
white = strip.Color(0, 0, 0, 90);
blacklight = strip.Color(0, 25, 75, 0);
green = strip.Color(70, 10, 0, 0);
red = strip.Color(0, 90, 0, 0);
lime = strip.Color(70, 25, 5, 0);
orange = strip.Color(5, 65, 0, 0);
yellow = strip.Color(25, 55, 0, 0);
blue = strip.Color(2, 2, 75, 0);
bluegreen = strip.Color(35, 0, 65, 0);
pink = strip.Color(10, 70, 10, 0);
}
// overrides for the 60 pixel accent strip that can be brighter
if (PIXEL_TYPE == SK6812RGBW && PIXEL_COUNT == 60) {
white = strip.Color(0, 0, 0, 255);
blacklight = strip.Color(0, 115, 175, 10);
green = strip.Color(200, 20, 0, 20);
red = strip.Color(0, 255, 0, 0);
lime = strip.Color(200, 110, 0, 20);
orange = strip.Color(90, 220, 0, 0);
yellow = strip.Color(135, 175, 0, 60);
blue = strip.Color(0, 0, 255, 2);
bluegreen = strip.Color(110, 0, 165, 0);
pink = strip.Color(0, 200, 10, 100);
}
strip.begin();
// I don't know why it's so hard to get it to boot to all pixels as off
strip.show();
strip.clear();
strip.show();
// strip.setBrightness(brightness);
Particle.connect();
String eventChannel;
if (GROUP == MAIN_GROUP) {
eventChannel = "squarism/blinkwon";
}
if (GROUP == ACCENT_GROUP) {
eventChannel = "squarism/ambient_strip";
}
if (GROUP == TEST_GROUP) {
eventChannel = "squarism/test";
}
if (GROUP == OVERHEAD_GROUP) {
eventChannel = "squarism/overhead";
}
// subscribe to private events - this finally seems to work like I want.
Particle.subscribe(eventChannel, eventHandler, MY_DEVICES);
}
void loop() {
// we don't need to call process or connect here
// the particle defaults to automatic mode.
// more here about it: http://blog.particle.io/2014/08/06/control-the-connection/
Particle.process();
if(connectToCloud && Particle.connected() == false) {
Particle.connect();
connectToCloud = false;
}
if (strcmp(theme, "cylon") == 0) {
strip.setBrightness(120);
if (PIXEL_TYPE == SK6812RGBW) {
dotChase(45, 90, 3);
} else {
dotChase(90, 45, 3);
}
}
}
void connect() {
connectToCloud = true;
}
int defaultBrightness() {
return 60;
}
void eventHandler(const char * event, const char * data) {
int len = strlen(data);
char dataCopy[len + 1];
strcpy(dataCopy, data);
StaticJsonBuffer<500> jsonBuffer;
JsonObject & json = jsonBuffer.parseObject(dataCopy);
if (json.success()) {
if (json.containsKey("brightness")) {
// set brightness here later but avoid calling strip.setBrightness()
// because that causes a jarring flicker effect and it's very hard to blend
// or fade brightness changes. Instead we are going to prefer running
// down the strip and setting a scaled brightness value. The defaultBrightness
// will be 255 and the scaling will be scaling/255.
// Also attempted but given up on is tweening and fading state as an animation of sorts.
// Tweening to brightness causes threading type problems to solve.
// Careful - The larger strips can't be super bright without crashing because of the PCB boards.
brightness = (int)((int)json["brightness"] * BRIGHTNESS_SCALE);
}
if (json.containsKey("theme")) {
strcpy(theme, json["theme"].asString());
if (strcmp(theme, "off") == 0) {
brightness = 0;
Particle.process();
blankWipe(100);
Particle.process();
} else {
Particle.process();
setColor(theme, 100);
Particle.process();
}
}
}
}
void setColor(const char * theme, int wipeSpeed) {
// wow, you can't easily do switch statements in C?
if (strcmp(theme, "bluegreen") == 0) {
colorWipe(bluegreen, wipeSpeed);
} else if (strcmp(theme, "white") == 0) {
colorWipe(white, wipeSpeed);
} else if (strcmp(theme, "blacklight") == 0) {
colorWipe(blacklight, wipeSpeed);
} else if (strcmp(theme, "orange") == 0) {
colorWipe(orange, wipeSpeed);
} else if (strcmp(theme, "yellow") == 0) {
colorWipe(yellow, wipeSpeed);
} else if (strcmp(theme, "red") == 0) {
colorWipe(red, wipeSpeed);
} else if (strcmp(theme, "lime") == 0) {
colorWipe(lime, wipeSpeed);
} else if (strcmp(theme, "blue") == 0) {
colorWipe(blue, wipeSpeed);
} else if (strcmp(theme, "green") == 0) {
colorWipe(green, wipeSpeed);
} else if (strcmp(theme, "pink") == 0) {
colorWipe(pink, wipeSpeed);
}
}
// Fill the dots one after the other with a color
void colorWipe(uint32_t color, uint16_t wait) {
for (uint16_t i = 0; i < strip.numPixels(); i++) {
// set the pixel based off unpacked color values and a scaled brightness globalset earlier
if (PIXEL_TYPE == SK6812RGBW) {
// if (color == white) {
// huh
// strip.setColorScaled(i, 0, 0, 0, 220, (255 * brightness/255));
// } else {
strip.setColorScaled(i, getRed(color), getGreen(color), getBlue(color), getWhite(color), (255 * brightness/255));
// }
} else {
// wtf - where is this even coming from
strip.setColorScaled(i, getRed(color), getGreen(color), getBlue(color), brightness);
}
// show immediately in the loop to update the pixel strip as the loop runs, this is how we go down the strip
strip.show();
Particle.process();
delay(wait);
}
}
// Turns all pixels off sequentially
void blankWipe(uint16_t slowness) {
for (uint16_t i = 0; i < strip.numPixels(); i++) {
if (PIXEL_TYPE == SK6812RGBW) {
strip.setPixelColor(i, strip.Color(0, 0, 0, 0));
} else {
strip.setPixelColor(i, strip.Color(0, 0, 0));
}
strip.show();
Particle.process();
delay(slowness);
}
}
// dot chase visual effect
void dotChase(uint32_t c, uint16_t wait, int dots) {
int offset = strip.numPixels() / dots; // pixel separation
uint32_t color = Wheel(c & 255);
for (int q = 0; q < offset; q++) {
for (int i = 0; i < strip.numPixels(); i = i + offset) {
strip.setPixelColor(i + q, color); // turn every third pixel on
}
strip.show();
delay(wait);
for (int i = 0; i < strip.numPixels(); i = i + offset) {
strip.setPixelColor(i + q, 0); // turn every n-offset pixel off
}
}
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
if (WheelPos < 85) {
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if (WheelPos < 170) {
WheelPos -= 85;
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}