-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathZenFlow.ino
More file actions
465 lines (391 loc) · 12.8 KB
/
ZenFlow.ino
File metadata and controls
465 lines (391 loc) · 12.8 KB
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/*
ZenFlow
by Move38, Inc. 2019
Lead development by Dan King
original game by Dan King, Jonathan Bobrow
Rules: https://github.com/Move38/ZenFlow/blob/master/README.md
--------------------
Blinks by Move38
Brought to life via Kickstarter 2018
@madewithblinks
www.move38.com
--------------------
*/
//#include "Serial.h"
//ServicePortSerial Serial;
enum modeStates {SPREAD, CONNECT};
byte currentMode;
enum commandStates {INERT, SEND_PERSIST, SEND_SPARKLE, RESOLVING};
byte commandState;//this is the state sent to neighbors
byte internalState;//this state is internal, and does not get sent
// Colors by hue
byte hues[7] = {0, 21, 42, 85, 110, 170, 210};
byte currentHue;
byte sparkleOffset[6] = {0, 3, 5, 1, 4, 2};
#define SEND_DELAY 100
#define SEND_DURATION 800
uint32_t timeOfSend = 0;
uint32_t timeOfPress = 0; // use this to show the button was pressed in connect mode
Timer sendTimer;
Timer transitionTimer;
byte sendData;
bool bChangeMode = false;
void setup() {
// put your setup code here, to run once:
//Serial.begin();
currentMode = SPREAD;
commandState = INERT;
internalState = INERT;
currentHue = 0;
randomize();
}
void loop() {
// discard the change mode from a force sleep
if (hasWoken()) {
bChangeMode = false;
}
// BUTTON HANDLING
//if single clicked, move to SEND_PERSIST
if (buttonSingleClicked()) {
//Serial.println("Single Click Registered");
if (currentMode == SPREAD) {
changeInternalState(SEND_PERSIST);
currentHue = nextHue(currentHue);
//Serial.print("hue: ");
//Serial.println(currentHue);
}
if (currentMode == CONNECT) {
timeOfPress = millis();
}
}
//if double clicked, move to SEND_SPARKLE
if (buttonDoubleClicked()) {
//Serial.println("Double Click Registered");
if (currentMode == SPREAD) {
changeInternalState(SEND_SPARKLE);
currentHue = millis() % COUNT_OF(hues); //generate a random color
//Serial.print("hue: ");
//Serial.println(currentHue);
}
if (currentMode == CONNECT) {
timeOfPress = millis();
}
}
//if long-pressed, move to CONNECT mode
if (buttonLongPressed()) {
bChangeMode = true;
}
// if change mode
if (buttonReleased()) {
if (bChangeMode) {
switch (currentMode) {
case CONNECT: currentMode = SPREAD; break;
case SPREAD: currentMode = CONNECT; break;
}
// reset our states
changeInternalState(INERT);
commandState = INERT;
bChangeMode = false;
}
}
// decide which loop to run
if (currentMode == SPREAD) {//spread logic loops
switch (internalState) {
case INERT:
inertLoop();
break;
case SEND_PERSIST:
sendPersistLoop();
break;
case SEND_SPARKLE:
sendSparkleLoop();
break;
case RESOLVING:
resolvingLoop();
break;
}
} else if (currentMode == CONNECT) {
connectLoop();
}
//communicate full state
sendData = (currentMode << 5) + (commandState << 3) + (currentHue);//bit-shifted data to fit in 6 bits
setValueSentOnAllFaces(sendData);
//do display work
if (currentMode == SPREAD) {
switch (internalState) {
case RESOLVING:
case INERT:
inertDisplay();//both inert and resolving share the same display logic
break;
case SEND_PERSIST:
sendPersistDisplay();
break;
case SEND_SPARKLE:
sendSparkleDisplay();
break;
}
} else if (currentMode == CONNECT) {
connectDisplay();
}
if (bChangeMode) {
setColor(WHITE);
}
}
void inertLoop() {
//now we evaluate neighbors. if our neighbor is in either send state, move to that send state
FOREACH_FACE(f) {
if (!isValueReceivedOnFaceExpired(f)) {
byte neighborData = getLastValueReceivedOnFace(f);
if (getMode(neighborData) == SPREAD) {
if (getCommandState(neighborData) == SEND_PERSIST) {
changeInternalState(SEND_PERSIST);
currentHue = getHue(neighborData);//you are going to take on the color of the commanding neighbor
break; // leave forloop
}
else if (getCommandState(neighborData) == SEND_SPARKLE) {
changeInternalState(SEND_SPARKLE);
currentHue = millis() % COUNT_OF(hues); //generate a random color
break; // leave forloop
}
} // end of mode in SPREAD
} // end of valid value
}// end of for each face
}
void sendPersistLoop() {
//first, check if it's been long enough to send the command
if (sendTimer.isExpired()) {
commandState = internalState;
}
if (transitionTimer.isExpired()) {
//now check neighbors. If they have all moved into SEND_PERSIST or RESOLVING, you can move to RESOLVING
//Only do this check if we are past the full display time
//if we've survived and are stil true, we transition to resolving
if (canResolve(SEND_PERSIST) && !isAlone()) {
changeInternalState(RESOLVING);
commandState = RESOLVING;
}
}
}
bool canResolve(byte a) {
bool canResolve = true;//default to true, set to false in the face loop
FOREACH_FACE(f) {
byte neighborData = getLastValueReceivedOnFace(f);//we do this before checking for expired so we can use it to evaluate mode below
if (!isValueReceivedOnFaceExpired(f) && getMode(neighborData) == SPREAD) {//something is here, and in a compatible mode. We ignore the others
if (getCommandState(neighborData) != a && getCommandState(neighborData) != RESOLVING) {//it is neither of the acceptable states
canResolve = false;
}
}
}//end of face loop
return canResolve;
}
void sendSparkleLoop() {
//first, check if it's been long enough to send the command
if (sendTimer.isExpired()) {
commandState = internalState;
}
//here we can transition from sparkle to persist spread
FOREACH_FACE(f) {
if (!isValueReceivedOnFaceExpired(f)) {
byte neighborData = getLastValueReceivedOnFace(f);
if (getCommandState(neighborData) == SEND_PERSIST && getMode(neighborData) == SPREAD) {
changeInternalState(SEND_PERSIST);
currentHue = getHue(neighborData);//you are going to take on the color of the commanding neighbor
}
}
}
//now check neighbors. If they have all moved into SEND_SPARKLE or RESOLVING, you can move to RESOLVING
//Only do this check if we are past the full display time
if (transitionTimer.isExpired()) {
//if we've survived and are stil true, we transition to resolving
if (canResolve(SEND_SPARKLE) && !isAlone()) {
changeInternalState(RESOLVING);
commandState = RESOLVING;
}
}
}
void resolvingLoop() {
//check neighbors. If they have all moved into RESOLVING or INERT, you can move to INERT
bool canInert = true;//default to true, set to false in the face loop
FOREACH_FACE(f) {
byte neighborData = getLastValueReceivedOnFace(f);//we do this before checking for expired so we can use it to evaluate mode below
if (!isValueReceivedOnFaceExpired(f) && getMode(neighborData) == SPREAD) {//something is here, and in a compatible mode. We ignore the others
if (getCommandState(neighborData) != RESOLVING && getCommandState(neighborData) != INERT) {//it is neither of the acceptable states
canInert = false;
}
}
}//end of face loop
//if we've survived and are stil true, we transition to resolving
if (canInert) {
changeInternalState(INERT);
commandState = INERT;
}
}
void connectLoop() {
// nothing to do here
}
/*
Data parsing
*/
byte getMode(byte data) {
return (data >> 5); //the value in the first bit
}
byte getCommandState (byte data) {
return ((data >> 3) & 3); //the value in the second and third bits
}
byte getHue(byte data) {
return (data & 7); //the value in the fourth, fifth, and sixth bits
}
/*
End Data parsing
*/
/*
Display Animations
*/
// this code uses ~100 Bytes
void inertDisplay() {
// setColor(makeColorHSB(hues[currentHue],255,255)); // much less interesting, but fits in memory
FOREACH_FACE(f) {
// minimum of 125, maximum of 255
byte phaseShift = 60 * f;
byte amplitude = 55;
byte midline = 185;
byte rate = 4;
byte brightness = midline + (amplitude * sin8_C( (phaseShift + millis() / rate) % 255)) / 255;
byte saturation = 255;
Color faceColor = makeColorHSB(hues[currentHue], 255, brightness);
setColorOnFace(faceColor, f);
}
}
// this code uses ~200 Bytes
void sendPersistDisplay() {
// go full white and then fade to new color
uint32_t delta = millis() - timeOfSend;
// show that we are charged up when alone
if (isAlone()) {
while (delta > SEND_DURATION * 3) {
delta -= SEND_DURATION * 3;
}
}
if (delta > SEND_DURATION) {
delta = SEND_DURATION;
}
FOREACH_FACE(f) {
// minimum of 125, maximum of 255
byte phaseShift = 60 * f;
byte amplitude = 55;
byte midline = 185;
byte rate = 4;
byte brightness = midline + (amplitude * sin8_C ( (phaseShift + millis() / rate) % 255)) / 255;
byte saturation = map(delta, 0, SEND_DURATION, 0, 255);
Color faceColor = makeColorHSB(hues[currentHue], saturation, brightness);
setColorOnFace(faceColor, f);
}
}
// this code uses ~400 Bytes
void sendSparkleDisplay() {
// go full white and then fade to new color, pixel by pixel
uint32_t delta = millis() - timeOfSend;
// show that we are charged up when alone
if (isAlone()) {
while (delta > SEND_DURATION * 3) {
delta -= SEND_DURATION * 3;
}
}
if (delta > SEND_DURATION) {
delta = SEND_DURATION;
}
byte offset = 50;
FOREACH_FACE(f) {
// if the face has started it's glow
uint16_t sparkleStart = sparkleOffset[f] * offset;
uint16_t sparkleEnd = sparkleStart + SEND_DURATION - (6 * offset);
if ( delta > sparkleStart ) {
// minimum of 125, maximum of 255
byte phaseShift = 60 * f;
byte amplitude = 55;
byte midline = 185;
byte rate = 4;
byte lowBri = midline + (amplitude * sin8_C( (phaseShift + millis() / rate) % 255)) / 255;
byte brightness;
byte saturation;
if ( delta < sparkleEnd ) {
brightness = lowBri + 255 - map(delta, sparkleStart, sparkleStart + SEND_DURATION - (6 * offset), lowBri, 255);
saturation = map(delta, sparkleStart, sparkleStart + SEND_DURATION - (6 * offset), 0, 255);
}
else {
brightness = lowBri;
saturation = 255;
}
Color faceColor = makeColorHSB(hues[currentHue], saturation, brightness);
setColorOnFace(faceColor, f);
}
}
}
// this code uses ~300 Bytes
void connectDisplay() {
// go full white and then fade to new color, pixel by pixel
uint32_t delta = millis() - timeOfPress;
if ( delta > 300) {
delta = 300;
}
if (isAlone()) { //so this is a lonely blink. we just set it to full white
// minimum of 125, maximum of 255
byte amplitude = 30;
byte midline = 100;
byte rate = 4;
byte brightness = midline + (amplitude * sin8_C( (millis() / rate) % 255)) / 255;
// if the button recently pressed, dip and then raise up
brightness = map(delta, 0, 300, 0, brightness);
Color faceColor = makeColorHSB(0, 0, brightness);
setColor(faceColor);
} else {
setColor(OFF);//later in the loop we'll add the colors
}
FOREACH_FACE(f) {
if (!isValueReceivedOnFaceExpired(f)) { //something there
byte neighborData = getLastValueReceivedOnFace(f);
//now we figure out what is there and what to do with it
if (getMode(neighborData) == SPREAD) { //this neighbor is in spread mode. Just display the color they are on that face
byte brightness = map(delta, 0, 300, 0, 255);
Color faceColor = makeColorHSB(hues[getHue(neighborData)], 255, brightness);
setColorOnFace(faceColor, f);
} else if (getMode(neighborData) == CONNECT) { //this neighbor is in connect mode. Display a white connection
byte brightness = map(delta, 0, 300, 0, 255);
setColorOnFace(makeColorHSB(0, 0, brightness), f);
}
}
}//end of face loop
}
/*
Data parsing
*/
void changeInternalState(byte state) {
//this is here for the moment of transition. mostly housekeeping
switch (state) {
case INERT:
////serial.println("I'm just hangin' tight.");
// nothing to do here
break;
case SEND_PERSIST:
//serial.println("show the next color and spread it");
timeOfSend = millis();
sendTimer.set(SEND_DELAY);
transitionTimer.set(SEND_DURATION);
break;
case SEND_SPARKLE:
//serial.println("a little sparkle for y'all");
timeOfSend = millis();
sendTimer.set(SEND_DELAY);
transitionTimer.set(SEND_DURATION);
break;
case RESOLVING:
//serial.println("I'm in my happy place :)");
// nothing to do here
break;
}
internalState = state;
}
byte nextHue(byte h) {
byte nextHue = (h + 1) % COUNT_OF(hues);
return nextHue;
}