-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDigisparkTorch.ino
449 lines (360 loc) · 13.5 KB
/
DigisparkTorch.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
// https://github.com/FastLED/FastLED/issues/413 Making FastLED 3.1.3 work with ATTinyCore
#define ATTINY_CORE 1
#include <FastLED.h>
#include <util/delay.h>
#include <avr/sleep.h>
#include <avr/power.h>
//#include <avr/wdt.h>
#include <EEPROM.h>
#define DATA_PIN PIN_PB3
//#define CLK_PIN 4
#define LED_TYPE SK6812
#define COLOR_ORDER GRB
//#define COLOR_ORDER RGB
#define NUM_LEDS 8
CRGB leds[NUM_LEDS];
uint8_t gHue = 0; // rotating "base color" used by many of the patterns
CRGBPalette16 gPal; // Fire2012WithPalette
// turn off RGB and on-board leds and put MCU to sleep after defined time
// note that the RGB leds typically still draw power even when turned "off" and are set to black
#define SLEEP_TIMEOUT_MILLIS 14400000 // approximately 4 hours
//#define LED_BUILTIN_DEFAULT_STATE 0 // on-board led normally turned off
#define LED_BUILTIN_DEFAULT_STATE 1 // use more power with on-board led turned on: can help to reach the low power cutoff of many powerbanks
#define BRIGHTNESS 70
#define MIN_BRIGHTNESS 25
#define MAX_BRIGHTNESS 255
#define FRAMES_PER_SECOND 120
#define BUTTON_PRESSED_SHORT 1
#define BUTTON_PRESSED_DOUBLE 2
#define BUTTON_PRESSED_LONG 3
#define PATTERN_RAINBOW 0
#define PATTERN_RAINBOW_PAUSED 1
#define PATTERN_RAINBOW_GLITTER 2
#define PATTERN_RAINBOW_GLITTER_PAUSED 3
#define PATTERN_TORCH 4
#define LAST_PATTERN PATTERN_TORCH
uint8_t current_pattern = 0;
bool cycle_rainbow_hue_active = 1;
volatile byte button_pressed_state = 0;
byte button_pin = PIN_PB0; // PCINT0 -> Pin Change Interrupt
uint32_t wakeup_time_millis = 0;
bool change_brightness_active = 0;
bool brightness_change_direction = 0;
void setup(){
// 9.3.2 GIMSK – General Interrupt Mask Register
GIMSK = 1<<PCIE; // Pin Change Interrupt Enable
// 9.3.4 PCMSK – Pin Change Mask Register
PCMSK = 1<<button_pin;
// tell FastLED about the LED strip configuration
FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip).setRgbw();
// set master brightness control
FastLED.setBrightness(BRIGHTNESS);
gPal = HeatColors_p; // Fire2012WithPalette
pinMode(LED_BUILTIN, OUTPUT);
pinMode(button_pin, INPUT_PULLUP);
digitalWrite(LED_BUILTIN, LED_BUILTIN_DEFAULT_STATE);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
load_config_eeprom();
}
// button state handling detects short, double or long press
// perposfully blocks to ignore button bounce phase
ISR (PCINT0_vect){
handle_button_press_blocking();
// 9.3.3 GIFR – General Interrupt Flag Register
GIFR &= 1<<PCIF; // clear pin change interrupt flag
if (button_pressed_state){
// disable pin change interrupt, reenable after processing button_pressed_state in loop
GIMSK &= ~(1<<PCIE);
}
}
#define EEPROM_ADDRESS_CURRENT_PATTERN 0
#define EEPROM_ADDRESS_BRIGHTNESS 1
#define EEPROM_ADDRESS_GHUE 2
void load_config_eeprom(){
uint8_t current_pattern_eeprom = EEPROM.read(EEPROM_ADDRESS_CURRENT_PATTERN);
if (current_pattern_eeprom != 0xFF){
current_pattern = current_pattern_eeprom;
setup_pattern(current_pattern);
FastLED.setBrightness(EEPROM.read(EEPROM_ADDRESS_BRIGHTNESS));
gHue = EEPROM.read(EEPROM_ADDRESS_GHUE);
}
}
void update_config_eeprom(){
EEPROM.update(EEPROM_ADDRESS_CURRENT_PATTERN, current_pattern);
EEPROM.update(EEPROM_ADDRESS_BRIGHTNESS, FastLED.getBrightness());
EEPROM.update(EEPROM_ADDRESS_GHUE, gHue);
}
void check_sleep_timeout(){
if (button_pressed_state || !button_state()){
// don't sleep when there's button activity
return;
}
if (millis() - wakeup_time_millis >= SLEEP_TIMEOUT_MILLIS){
turn_off_all_leds();
go_to_sleep();
// wake up invoked by button press: ignore button_pressed_state that was evaluated in handle_button_press_blocking()
// handle_button_press_blocking() also disables pin change interrupt, enable again:
reset_button_pressed_state();
digitalWrite(LED_BUILTIN, LED_BUILTIN_DEFAULT_STATE);
wakeup_time_millis = millis();
}
}
void turn_off_all_leds(){
digitalWrite(LED_BUILTIN, LOW);
fadeToBlackBy(leds, NUM_LEDS, 255);
FastLED.show();
}
void go_to_sleep(){
ADCSRA &= ~(1<<ADEN); // turn off ADC
power_all_disable(); // power off ADC, Timer 0 and 1, serial interface
GIMSK = 1<<PCIE; // pin change interrupt must be enabled for wakeup
sleep_mode(); // sleep
// watchdog not used at the moment
//
// from the ATTinyCore docs:
// When using the WDT as a reset source and NOT using a bootloader remember that after reset the WDT will be enabled with minimum timeout.
// The very first thing your application must do upon restart is reset the WDT (wdt_reset()), clear WDRF flag in MCUSR (MCUSR&=~(1<<WDRF))
// and then turn off or configure the WDT for your desired settings. If using the Optiboot bootloader, this is already done for you by the bootloader.
//
//wdt_reset();
//MCUSR&=~(1<<WDRF);
//wdt_disable();
// awake again
power_all_enable(); // power everything back on
}
void handle_button_press_blocking(){
// debounce delay
_delay_ms(5);
// only interested when initial state is low: button pressed
if (button_state()){
return;
}
if (button_down_time(250) >= 250){
button_pressed_state = BUTTON_PRESSED_LONG;
} else {
if (button_up_time(175) < 175){
button_pressed_state = BUTTON_PRESSED_DOUBLE;
} else {
button_pressed_state = BUTTON_PRESSED_SHORT;
}
}
}
uint16_t button_down_time(uint16_t button_timeout_ms){
for (uint16_t ms = 0; ms <= button_timeout_ms; ms++){
_delay_ms(1);
if (button_state()){
return ms;
}
}
return button_timeout_ms;
}
uint16_t button_up_time(uint16_t button_timeout_ms){
for (uint16_t ms = 0; ms <= button_timeout_ms; ms++){
_delay_ms(1);
if (!button_state()){
return ms;
}
}
return button_timeout_ms;
}
bool button_state(){
return PINB & 1<<button_pin;
}
void handle_button_pressed_state(){
if (button_pressed_state == BUTTON_PRESSED_SHORT){
blink_led_once(50);
cycle_next_pattern();
} else if (button_pressed_state == BUTTON_PRESSED_DOUBLE){
// store current pattern and brightness in persistent memory
update_config_eeprom();
blink_led_multiple(200, 2);
} else if (button_pressed_state == BUTTON_PRESSED_LONG){
// toggle brightness_change_direction
brightness_change_direction = !brightness_change_direction;
change_brightness_active = 1;
blink_led_once(100);
}
reset_button_pressed_state();
}
void reset_button_pressed_state(){
// 9.3.2 GIMSK – General Interrupt Mask Register
GIMSK = 1<<PCIE; // Pin Change Interrupt Enable
button_pressed_state = 0;
}
void blink_led_multiple (uint16_t blink_duration_ms, uint8_t blink_count){
for (uint8_t i = 0; i < blink_count ; i++){
if (i > 0){
// subsequent blinks need leading delay to be visible
delay(blink_duration_ms);
}
blink_led_once(blink_duration_ms);
}
}
void blink_led_once(uint16_t blink_duration_ms){
digitalWrite(LED_BUILTIN, !LED_BUILTIN_DEFAULT_STATE);
delay(blink_duration_ms);
digitalWrite(LED_BUILTIN, LED_BUILTIN_DEFAULT_STATE);
}
void paint_current_pattern(){
if (current_pattern == PATTERN_RAINBOW || current_pattern == PATTERN_RAINBOW_PAUSED){
rainbow();
} else if (current_pattern == PATTERN_RAINBOW_GLITTER || current_pattern == PATTERN_RAINBOW_GLITTER_PAUSED){
rainbowWithGlitter();
} else if (current_pattern == PATTERN_TORCH){
// Add entropy to random number generator; we use a lot of it.
random16_add_entropy( random());
// Fourth, the most sophisticated: this one sets up a new palette every
// time through the loop, based on a hue that changes every time.
// The palette is a gradient from black, to a dark color based on the hue,
// to a light color based on the hue, to white.
//
// static uint8_t hue = 0;
// hue++;
// CRGB darkcolor = CHSV(hue,255,192); // pure hue, three-quarters brightness
// CRGB lightcolor = CHSV(hue,128,255); // half 'whitened', full brightness
// gPal = CRGBPalette16( CRGB::Black, darkcolor, lightcolor, CRGB::White);
Fire2012WithPalette(); // run simulation frame, using palette colors
}
}
void cycle_next_pattern(){
current_pattern++;
if (current_pattern > LAST_PATTERN){
current_pattern = 0;
}
setup_pattern(current_pattern);
}
void setup_pattern(uint8_t pattern){
// turn off cycling of rainbow hue for the paused rainbow patterns
if (pattern == PATTERN_RAINBOW){
cycle_rainbow_hue_active = 1;
} else if (pattern == PATTERN_RAINBOW_PAUSED){
cycle_rainbow_hue_active = 0;
} else if (pattern == PATTERN_RAINBOW_GLITTER){
cycle_rainbow_hue_active = 1;
} else if (pattern == PATTERN_RAINBOW_GLITTER_PAUSED){
cycle_rainbow_hue_active = 0;
}
}
void rainbow(){
// FastLED's built-in rainbow generator
fill_rainbow( leds, NUM_LEDS, gHue, 7);
}
void rainbowWithGlitter(){
// built-in FastLED rainbow, plus some random sparkly glitter
rainbow();
addGlitter(5);
}
void addGlitter(fract8 chanceOfGlitter){
if (random8() < chanceOfGlitter){
leds[ random16(NUM_LEDS) ] += CRGB::White;
}
}
void cycle_rainbow_hue(){
if (cycle_rainbow_hue_active){
gHue++;
}
}
void change_brightness(){
if (change_brightness_active){
if (!button_state()){
// button still pressed
// new_brightness initially copy of current brightness
int16_t new_brightness = FastLED.getBrightness();
int8_t brightness_change_rate = 1;
if (new_brightness >= 80){
brightness_change_rate = map(new_brightness, 80, 255, 1, 10);
}
if (!brightness_change_direction){
brightness_change_rate *= -1; // invert
}
new_brightness += brightness_change_rate;
new_brightness = constrain(new_brightness, MIN_BRIGHTNESS, MAX_BRIGHTNESS);
if (new_brightness != FastLED.getBrightness()){
blink_led_once(20);
FastLED.setBrightness(new_brightness);
}
} else {
// button no longer pressed
change_brightness_active = 0;
}
}
}
// Fire2012 by Mark Kriegsman, July 2012
// as part of "Five Elements" shown here: http://youtu.be/knWiGsmgycY
////
// This basic one-dimensional 'fire' simulation works roughly as follows:
// There's a underlying array of 'heat' cells, that model the temperature
// at each point along the line. Every cycle through the simulation,
// four steps are performed:
// 1) All cells cool down a little bit, losing heat to the air
// 2) The heat from each cell drifts 'up' and diffuses a little
// 3) Sometimes randomly new 'sparks' of heat are added at the bottom
// 4) The heat from each cell is rendered as a color into the leds array
// The heat-to-color mapping uses a black-body radiation approximation.
//
// Temperature is in arbitrary units from 0 (cold black) to 255 (white hot).
//
// This simulation scales it self a bit depending on NUM_LEDS; it should look
// "OK" on anywhere from 20 to 100 LEDs without too much tweaking.
//
// I recommend running this simulation at anywhere from 30-100 frames per second,
// meaning an interframe delay of about 10-35 milliseconds.
//
// Looks best on a high-density LED setup (60+ pixels/meter).
//
//
// There are two main parameters you can play with to control the look and
// feel of your fire: COOLING (used in step 1 above), and SPARKING (used
// in step 3 above).
//
// COOLING: How much does the air cool as it rises?
// Less cooling = taller flames. More cooling = shorter flames.
// Default 55, suggested range 20-100
//#define COOLING 55
// SPARKING: What chance (out of 255) is there that a new spark will be lit?
// Higher chance = more roaring fire. Lower chance = more flickery fire.
// Default 120, suggested range 50-200.
#define SPARKING 15
void Fire2012WithPalette()
{
// Array of temperature readings at each simulation cell
static uint8_t heat[NUM_LEDS];
// Step 1. Cool down every cell a little
for( int i = 0; i < NUM_LEDS; i++) {
heat[i] = qsub8( heat[i], random8(0, 3));
}
// Step 2. Heat from each cell drifts 'up' and diffuses a little
for( int k= NUM_LEDS - 1; k >= 2; k--) {
heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
}
// Step 3. Randomly ignite new 'sparks' of heat near the bottom
if( random8() < SPARKING ) {
int y = random8(4);
heat[y] = qadd8( heat[y], random8(160,255) );
//heat[y] = qadd8( heat[y], random8(160,220) );
}
// Step 4. Map from heat cells to LED colors
for( int j = 0; j < NUM_LEDS; j++) {
// Scale the heat value from 0-255 down to 0-240
// for best results with color palettes.
uint8_t colorindex = scale8( heat[j], 100);
CRGB color = ColorFromPalette( gPal, colorindex);
leds[j] = color;
}
}
void loop(){
// do some periodic updates
EVERY_N_MILLISECONDS(40){
cycle_rainbow_hue(); // slowly cycle the "base color" through the rainbow
change_brightness();
}
check_sleep_timeout();
if (button_pressed_state){
handle_button_pressed_state();
}
paint_current_pattern();
// send the 'leds' array out to the actual LED strip
FastLED.show();
// insert a delay to keep the framerate modest
FastLED.delay(1000/FRAMES_PER_SECOND);
}