-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathxmastree.ino
583 lines (476 loc) Β· 15.7 KB
/
xmastree.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
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
/*
* Copyright 2017 Particle Industries, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/*
* Project: Xmas Tree
*
* Description:
* This is a Xmas tree with 25 LEDs, a piezo buzzer and a 5-way joystick designed for Particle Photon
* and Electron connected to the cloud which demostrates how to:
* 1. define cloud functions for controlling the tree over the Particle Cloud.
* 2. generate tones using the piezo buzzer
* 3. create beautiful effects using the LEDs
* 4. control the tree state using the 5-way joystick.
*
*/
#include "xmastree.h" // All macros (#define) are in xmastree.h
#include <dotstar.h>
/* Project name and version */
#define WHOAMI "XmasTree"
#define VERSION 1
/* LED brightness value */
/* Warning: less than 80 is highly recommended for safety and for your eyes */
#define LED_BRIGHTNESS 30
/* Project name and version */
const char projName[] = WHOAMI;
const int version = VERSION;
/* Current song selected */
int currentSong = 0;
/* Current animation selected */
int currentAnimation = 0;
/* Current state of the effect when pressing the center button */
int currentState = STATE_NONE;
/* Set to TRUE to play the current song repeatedly */
bool repeatSong = FALSE;
/* Current button down */
volatile int buttonState = BUTTON_NONE;
/* Create an instance for the xmasTree LEDs, each LED controlled by two pins, DATA and Clock */
Adafruit_DotStar leds = Adafruit_DotStar(TOTAL_LED, PIN_LED_DATA, PIN_LED_CLOCK, DOTSTAR_BGR);
/* Two threads will be used for processing songs and LED animations */
Thread *animationWorker, *songWorker;
/* LEDs hardware initialization */
void ledsInit()
{
leds.setBrightness(LED_BRIGHTNESS);
leds.begin();
leds.clear();
stopLED();
}
/* 5-way joystick hardware initialization */
void joystickPortInit()
{
/* Set pins to input with internal pull-up */
pinMode(PIN_CENTER, INPUT_PULLUP);
pinMode(PIN_UP, INPUT_PULLUP);
pinMode(PIN_DOWN, INPUT_PULLUP);
pinMode(PIN_LEFT, INPUT_PULLUP);
pinMode(PIN_RIGHT, INPUT_PULLUP);
/* Using interrupts for the pins */
attachInterrupt(PIN_CENTER, buttonHandler, FALLING);
attachInterrupt(PIN_UP, buttonHandler, FALLING);
attachInterrupt(PIN_DOWN, buttonHandler, FALLING);
attachInterrupt(PIN_LEFT, buttonHandler, FALLING);
attachInterrupt(PIN_RIGHT, buttonHandler, FALLING);
}
/* Piezo buzzer hardware initialization */
void buzzerPortInit(void)
{
pinMode(PIN_BUZZER, OUTPUT);
}
/* Set all the LEDs to color, e.g. "0xFF0000" = Red */
int playLED(String color)
{
for (int i = 0; i < TOTAL_LED; i++)
leds.setPixelColor(i, strtol(color, NULL, 0));
leds.show();
}
/* Turn off all LEDs */
void stopLED()
{
playLED("0x000000"); // R,G,B = 0,0,0
}
void playTone(int freq, int duration)
{
Serial.println("playTone");
tone(PIN_BUZZER, freq, duration);
}
void stopTone(String)
{
noTone(PIN_BUZZER);
}
/* For publishing the device-connected event to the cloud */
void publishConnected()
{
Serial.println("publishConnected");
Particle.publish("xtreeConnected", projName);
}
/* For publishing the effect state changed event to the cloud */
void publishStateChanged()
{
Serial.println("publishStateChanged");
Particle.publish("stateChanged", String(currentState));
}
/* For publishing the animation changed event to the cloud */
void publishAnimationChanged()
{
Serial.println("publishAnimationChanged");
Particle.publish("animChanged", String(currentAnimation));
}
/* For publishing the song changed event to the cloud */
void publishSongChanged()
{
Serial.println("publishSongChanged");
Particle.publish("songChanged", String(currentSong));
}
#include "songs.h"
#include "animations.h"
/* The no. of songs and LED animations, defined in songs.h and animations.h */
int songCount = SONG_COUNT;
int animationCount = ANIMATION_COUNT;
/* Play a song by the song ID */
int playSong(String songIndex)
{
Serial.println("playSong");
/* Stop the current playing song at once */
changeSong = true;
/* Set the current song and the song worker will play it */
currentSong = atoi(songIndex);
/* Update the tree current state */
if (currentState == STATE_NONE)
currentState = STATE_SONG;
else if (currentState == STATE_ANIMATION)
currentState = STATE_BOTH;
return 1;
}
/* Stop the current song playing */
int stopSong(String)
{
Serial.println("stopSong");
/* Stop the current playing song at once */
changeSong = true;
/* Update the tree current state */
if (currentState == STATE_SONG)
currentState = STATE_NONE;
else if (currentState == STATE_BOTH)
currentState = STATE_ANIMATION;
return 1;
}
/* Play a LED animation by the animation ID */
int playAnimation(String animationIndex)
{
Serial.println("playAnimation");
/* Stop the current playing animation at once */
changeAnimation = true;
/* Set the current song and the animation worker will play it */
currentAnimation = atoi(animationIndex);
/* Update the tree current state */
if (currentState == STATE_NONE)
currentState = STATE_ANIMATION;
else if (currentState == STATE_SONG)
currentState = STATE_BOTH;
return 1;
}
/* Stop the LED cureent playing animation */
int stopAnimation(String)
{
Serial.println("stopAnimation");
/* Stop the current playing animation at once */
changeAnimation = true;
stopLED();
/* Update the tree current state */
if (currentState == STATE_ANIMATION)
currentState = STATE_NONE;
else if (currentState == STATE_BOTH)
currentState = STATE_SONG;
return 1;
}
/* Toggle the state of the tree for the Center button */
int toggleState()
{
currentState++;
if (currentState > STATE_MAX)
currentState = 0;
Serial.println("toggleState");
if (currentState == STATE_NONE || currentState == STATE_SONG)
stopAnimation("");
if (currentState == STATE_NONE || currentState == STATE_ANIMATION)
stopSong("");
return currentState;
}
/* Set the state of the tree */
int setState(String state)
{
Serial.println("setState");
if (String(currentState) == state)
return 0;
if ( (state == "BOTH") || (state == String(STATE_BOTH)) ) {
playSong(String(currentSong));
playAnimation(String(currentAnimation));
currentState = STATE_BOTH;
} else if ( (state == "SONG") || (state == String(STATE_SONG)) ) {
playSong(String(currentSong));
stopAnimation("");
currentState = STATE_SONG;
} else if ( (state == "ANIMATION") || (state == String(STATE_ANIMATION)) ) {
stopSong("");
playAnimation(String(currentAnimation));
currentState = STATE_ANIMATION;
} else if ( (state == "NONE") || (state == String(STATE_NONE)) ) {
stopSong("");
stopAnimation("");
currentState = STATE_NONE;
} else
return -1;
return 1;
}
/* Start playing the next song */
int nextSong()
{
Serial.println("nextSong");
/* if the state is not playing song, then do nothing */
if ( (currentState == STATE_SONG) || (currentState == STATE_BOTH) ) {
currentSong++;
if (currentSong == songCount)
currentSong = 0;
changeSong = true;
}
return currentSong;
}
/* Start playing the previous song */
int prevSong()
{
Serial.println("prevSong");
if ( (currentState == STATE_SONG) || (currentState == STATE_BOTH) ) {
currentSong--;
if (currentSong < 0)
currentSong = songCount-1;
changeSong = true;
}
return currentSong;
}
/* Start playing the next animation */
int nextAnimation()
{
Serial.println("nextAnimation");
if ( (currentState == STATE_ANIMATION) || (currentState == STATE_BOTH) ) {
currentAnimation++;
if (currentAnimation == animationCount)
currentAnimation = 0;
changeAnimation = true;
}
return currentAnimation;
}
/* Start playing the previous LED animation */
int prevAnimation()
{
Serial.println("prevAnimation");
if ( (currentState == STATE_ANIMATION) || (currentState == STATE_BOTH) ) {
currentAnimation--;
if (currentAnimation < 0)
currentAnimation = animationCount-1;
changeAnimation = true;
}
return currentAnimation;
}
/* Process the button states */
int buttonPressed(String direction)
{
Serial.print("buttonPressed: ");
Serial.println(direction);
if ( direction == "CENTER" || direction == String(BUTTON_CENTER) )
return toggleState();
else if ( direction == "UP" || direction == String(BUTTON_UP) )
return prevAnimation();
else if ( direction == "DOWN" || direction == String(BUTTON_DOWN) )
return nextAnimation();
else if ( direction == "LEFT" || direction == String(BUTTON_LEFT) )
return prevSong();
else if ( direction == "RIGHT" || direction == String(BUTTON_RIGHT) )
return nextSong();
else
return -1;
}
/* Interrupt handler for the 5-way joystick */
void buttonHandler()
{
if (digitalRead(PIN_CENTER) == LOW)
buttonState = BUTTON_CENTER;
else if (digitalRead(PIN_UP) == LOW)
buttonState = BUTTON_UP;
else if (digitalRead(PIN_DOWN) == LOW)
buttonState = BUTTON_DOWN;
else if (digitalRead(PIN_LEFT) == LOW)
buttonState = BUTTON_LEFT;
else if (digitalRead(PIN_RIGHT) == LOW)
buttonState = BUTTON_RIGHT;
}
/* Process buttons for the 5-way joystick */
void processButtons()
{
if (buttonState != BUTTON_NONE) {
/* Wait until button up */
if ( (digitalRead(PIN_CENTER) == LOW) || (digitalRead(PIN_UP) == LOW) ||
(digitalRead(PIN_DOWN) == LOW) || (digitalRead(PIN_LEFT) == LOW) ||
(digitalRead(PIN_RIGHT) == LOW) )
return;
if (buttonState == BUTTON_CENTER)
buttonPressed("CENTER");
else if (buttonState == BUTTON_UP)
buttonPressed("UP");
else if (buttonState == BUTTON_DOWN)
buttonPressed("DOWN");
else if (buttonState == BUTTON_LEFT)
buttonPressed("LEFT");
else if (buttonState == BUTTON_RIGHT)
buttonPressed("RIGHT");
/* Clear the button state */
buttonState = BUTTON_NONE;
}
}
/* Button pressed from the cloud */
void buttonEventHandler(const char *event, const char *data)
{
if (String(event) == "btnPressed")
buttonPressed(String(data));
}
/* Process LED animations */
void processAnimations()
{
/* This is an endless loop to process the animations one by one */
while (1) {
changeAnimation = false;
if (currentState == STATE_BOTH || currentState == STATE_ANIMATION) {
if (currentAnimation == WHITEOVERRAINBOW)
whiteOverRainbow(20,75,5);
else if (currentAnimation == RAINBOWFADE2WHITE)
rainbowFade2White(3, 6, 1);
else if (currentAnimation == PULSEWHITE)
pulseWhite(5);
else if (currentAnimation == REDGREEN)
redGreen(500);
else if (currentAnimation == SPARKCYAN)
sparkCyan(3);
}
delay(250);
}
}
/* Process songs */
void processSongs()
{
/* This is an endless loop to process the songs one by one */
while (1) {
changeSong = false;
if ( (currentState == STATE_BOTH) || (currentState == STATE_SONG) ) {
if (currentSong == SONG_JOYTOTHEWORLD)
playJoyToTheWorld();
else if (currentSong == SONG_WEWHISHYOUAMERRYXMAS)
playWeWishYouAMerryXmas();
else if (currentSong == SONG_RUDOLFTHEREDNOSEDREINDEER)
playRudolfTheRedNosedReindeer();
else if (currentSong == SONG_JINGLEBELLS)
playJingleBells();
else if (currentSong == SONG_SILENTNIGHT)
playSilentNight();
}
delay(2000);
}
}
/* Cloud functions, variables, events initialization */
void cloudInit()
{
/* Cloud variables */
Particle.variable("whoami", projName);
Particle.variable("version", version);
Particle.variable("songList", songList);
Particle.variable("animList", animationList);
Particle.variable("currSong", currentSong);
Particle.variable("currAnim", currentAnimation);
Particle.variable("currState", currentState);
Particle.variable("songCount", songCount);
Particle.variable("animCount", animationCount);
/* Cloud functions */
Particle.function("playLED", playLED);
Particle.function("btnPressed", buttonPressed);
Particle.function("setState", setState);
Particle.function("playSong", playSong);
Particle.function("playAnim", playAnimation);
Particle.function("stopSong", stopSong);
Particle.function("stopAnim", stopAnimation);
/* Cloud subscription */
Particle.subscribe("btnPressed", buttonEventHandler);
/* Publish this event once connected to the cloud */
publishConnected();
}
/* Process cloud events */
void processCloud()
{
static int savedSong = -1;
static int savedAnimation = -1;
static int savedState = -1;
if (savedSong != currentSong) {
savedSong = currentSong;
publishSongChanged();
}
if (savedAnimation != currentAnimation) {
savedAnimation = currentAnimation;
publishAnimationChanged();
}
if (savedState != currentState) {
savedState = currentState;
publishStateChanged();
}
}
/* Print out the tree state to serial port */
void printTree()
{
static int savedSong = -1;
static int savedAnimation = -1;
static int savedState = -1;
if (savedState != currentState) {
Serial.print("Current State: ");
Serial.println(currentState);
savedState = currentState;
}
if (savedSong != currentSong) {
Serial.print("Current Song: ");
Serial.println(currentSong);
savedSong = currentSong;
}
if (savedAnimation != currentAnimation) {
Serial.print("Current Animation: ");
Serial.println(currentAnimation);
savedAnimation = currentAnimation;
}
}
/* Wiring programming starts from setup() and then calling loop() */
void setup()
{
/* LEDs setup */
ledsInit();
/* 5-way joystick setup */
joystickPortInit();
/* Buzzer setup */
buzzerPortInit();
/* Cloud functions and variables setup */
cloudInit();
/* Creating a thread for processing LED animations and songs */
/* So that I can just need to keep track of the buttons and the cloud activities */
animationWorker = new Thread("animation", processAnimations);
songWorker = new Thread("song", processSongs);
/* Start to play the 1st song and LED animation */
playSong("0");
playAnimation("0");
}
/* This will be called repeatedly */
void loop()
{
/* Print the tree info. */
printTree();
/* Check if button pressed */
processButtons();
/* Process the Cloud events */
processCloud();
}