-
Notifications
You must be signed in to change notification settings - Fork 0
/
bm2016.ino
371 lines (310 loc) · 8.36 KB
/
bm2016.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
#include <avr/sleep.h>
#include <RF24.h>
#include <FastLED.h>
#include <EEPROM.h>
#include "btn.h"
#include "timer.h"
#include "sparkle_receiver.h"
//----------------------------------------------------------------------------------
// General stuff
//----------------------------------------------------------------------------------
#define NUM_LEDS 100
#define FPS 120
#define INITIAL_BRIGHTNESS 200
#define PATTERN_CYCLE_TIME 12000
#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
//----------------------------------------------------------------------------------
// Controller specific definitions
//----------------------------------------------------------------------------------
// uncomment just one of these
//#define LED_KEYCHAIN
#define PRO_MINI_WEARABLE
//#define ARTBOAT
#ifdef LED_KEYCHAIN
#define LEDS_PIN 11
#define BUTTON1_PIN 3
#define BUTTON2_PIN 2
#define BUTTON3_PIN 0
#define BUTTON4_PIN 1
#define MODE_PIN BUTTON4_PIN
#define LED_EN_PIN 5 // PC6
#define USB_POWERED
#endif
#ifdef PRO_MINI_WEARABLE
#define LEDS_PIN 9
#define MODE_PIN 10
#define BRIGHTNESS_PIN A0
#define USB_POWERED
#endif
#ifdef ARTBOAT
#define LEDS_PIN 15
#define MODE_PIN 16
#define HAS_RADIO
#endif
//----------------------------------------------------------------------------------
// Globals
//----------------------------------------------------------------------------------
CRGB leds[NUM_LEDS];
CRGB * leds2 = (leds + (NUM_LEDS/2));
Btn btn_mode(MODE_PIN);
#ifdef LED_KEYCHAIN
Btn btn_brightness_up(BUTTON2_PIN);
Btn btn_brightness_down(BUTTON3_PIN);
#endif
#ifdef HAS_RADIO
RF24 radio(9, 8);
#endif
uint8_t g_brightness = INITIAL_BRIGHTNESS;
uint32_t g_now = 0;
//----------------------------------------------------------------------------------
// LED Keychain
//----------------------------------------------------------------------------------
#ifdef LED_KEYCHAIN
void leds_wake_up() { }
void leds_sleep() {
for (int j = g_brightness; j > 0; j-=2) {
FastLED.setBrightness(j);
FastLED.show();
}
FastLED.clear();
pinMode(LEDS_PIN, INPUT);
digitalWrite(LED_EN_PIN, LOW);
USBCON |= _BV(FRZCLK); //freeze USB clock
PLLCSR &= ~_BV(PLLE); // turn off USB PLL
USBCON &= ~_BV(USBE); // disable USB
delay(500);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
attachInterrupt(0, leds_wake_up, LOW);
sleep_mode();
/* SLEEP */
sleep_disable();
detachInterrupt(0);
/* ON */
sei();
USBDevice.attach();
delay(100);
FastLED.clear();
digitalWrite(LED_EN_PIN, HIGH);
pinMode(LEDS_PIN, OUTPUT);
}
#endif
//----------------------------------------------------------------------------------
// Patterns
//----------------------------------------------------------------------------------
#include "blinkypants_patterns.h"
#include "fastled_patterns.h"
#include "tinybee_patterns.h"
#include "artemiid_patterns.h"
//----------------------------------------------------------------------------------
// Mode / state
//----------------------------------------------------------------------------------
typedef int (*SimplePatternList[])();
uint8_t g_current_pattern = 0;
MillisTimer pattern_timer;
SimplePatternList patterns = {
collision,
moving_palette,
rainbow,
confetti,
mode_yalda,
};
void next_pattern()
{
g_current_pattern = (g_current_pattern + 1) % ARRAY_SIZE( patterns);
write_state();
FastLED.clear();
}
void enable_autocycle() {
if (pattern_timer.running()) return;
pattern_timer.start(PATTERN_CYCLE_TIME, true);
next_pattern();
}
void disable_autocycle() {
if (!pattern_timer.running()) return;
pattern_timer.stop();
write_state();
}
void brightness_up() {
switch (g_brightness) {
case 0 ... 15: g_brightness += 1; break;
case 16 ... 100: g_brightness += 15; break;
case 101 ... (0xff - 30): g_brightness += 30; break;
case (0xff - 30 + 1) ... 254: g_brightness = 255; break;
case 255: break;
}
}
void brightness_down() {
switch (g_brightness) {
case 0: break;
case 1 ... 15: g_brightness -= 1; break;
case 16 ... 100: g_brightness -= 15; break;
case 101 ... 255: g_brightness -= 30; break;
}
}
void mode_button() {
if (pattern_timer.running()) {
disable_autocycle();
} else {
next_pattern();
}
}
void read_state() {
uint8_t buffer = 0;
// autocycle
EEPROM.get(0, buffer);
if (buffer) {
pattern_timer.start(PATTERN_CYCLE_TIME, true);
}
// current pattern
EEPROM.get(1, buffer);
if (buffer == 255) {
g_current_pattern = 0;
} else {
g_current_pattern = buffer % ARRAY_SIZE( patterns);
}
// brightness
EEPROM.get(2, buffer);
if (buffer == 255) {
g_brightness = INITIAL_BRIGHTNESS;
} else {
g_brightness = buffer;
}
}
void write_state() {
EEPROM.update(0, pattern_timer.running());
EEPROM.update(1, g_current_pattern);
EEPROM.update(2, g_brightness);
}
//----------------------------------------------------------------------------------
// Setup & loop
//----------------------------------------------------------------------------------
void setup() {
// Leds
FastLED.addLeds<WS2811, LEDS_PIN, RGB>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
#ifdef USB_POWERED
FastLED.setMaxPowerInVoltsAndMilliamps(5, 2000);
#endif
#ifdef LED_KEYCHAIN
pinMode(BUTTON1_PIN, INPUT_PULLUP);
pinMode(LED_EN_PIN, OUTPUT);
digitalWrite(LED_EN_PIN, LOW);
delay(2000);
digitalWrite(LED_EN_PIN, HIGH);
#endif
#ifdef HAS_RADIO
radio.begin();
radio.setChannel(2);
radio.setPayloadSize(4);
radio.setAutoAck(false);
radio.setCRCLength(RF24_CRC_8);
radio.openReadingPipe(1,0xE7E7E7E7E7LL);
radio.startListening();
#endif
read_state();
}
void loop() {
g_now = millis();
// mode button. long press enables auto_cycle, short press changes to next pattern
btn_mode.poll(
[]() {
mode_button();
},
[]() {
enable_autocycle();
}
);
if (pattern_timer.fired()) {
next_pattern();
}
// brightness pot
#ifdef BRIGHTNESS_PIN
uint16_t brightness_raw = analogRead(BRIGHTNESS_PIN);
g_brightness = map(brightness_raw, 0, 1023, 0, 255);
#endif
// radio stuff
#ifdef HAS_RADIO
if (radio.available()) {
radio.read(&event, sizeof(event));
// sparkle stuff
if (event.button == 1 || event.button == 15) {
if (event.event == 1) {
receive_sparkle(event.pendant_id);
}
if (event.event == 2) {
clear_sparkle(event.pendant_id);
}
}
// keyfob stuff
// right
if (event.button == 23) {
mode_button();
}
// left
if (event.button == 27) {
enable_autocycle();
}
// down
if (event.button == 29) {
brightness_down();
}
// up
if (event.button == 30) {
brightness_up();
}
}
#endif
// LED Keychain only buttons
#ifdef LED_KEYCHAIN
btn_brightness_up.poll(
/* Brightness UP pressed */
brightness_up,
/* Brightness UP held */
[]() {
if (g_brightness < 0xff) {
g_brightness++;
}
}
);
btn_brightness_down.poll(
/* Brightness DOWN pressed */
brightness_down,
/* Brightness DOWN held */
[]() {
if (g_brightness > 0) {
g_brightness--;
}
}
);
// Check for power off
if (!digitalRead(BUTTON1_PIN)) {
delay(10);
if (!digitalRead(BUTTON1_PIN)) {
leds_sleep();
}
}
#endif
uint8_t brightness = g_brightness;
if (pattern_timer.running()) {
if (pattern_timer.sinceStart() < 300) {
brightness = scale8(pattern_timer.sinceStart() * 256 / 300, g_brightness);
}
if (pattern_timer.untilDone() < 300) {
brightness = scale8(pattern_timer.untilDone() * 256 / 300, g_brightness);
}
}
FastLED.setBrightness(brightness);
uint32_t d = (patterns)[g_current_pattern]();
// add sparkle?
#ifdef HAS_RADIO
prune_sparkles(g_now - 1100);
for (int i=0; i<number_of_sparkles(); i++) {
if (random8() < 100) {
leds[ random16(NUM_LEDS) ] += CRGB::White;
}
}
#endif
FastLED.show();
if (d == 0) d = 1000/120;
FastLED.delay(d);
}