-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidle.c
45 lines (41 loc) · 1.09 KB
/
idle.c
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
#include <avr/interrupt.h>
#include <util/delay.h>
#include "psu.h"
#include "idle.h"
#include "ws2812b.h"
#define IDLE_TIME 16 // in seconds
#define WAITS (((int)(IDLE_TIME/4.2))+1) // timer overflows after ~4.2s, see init
#if numleds > 550
void pretty_shutdown() {
// find last led with data
uint16_t last = numleds*3;
while(!leds[last] && !leds[last-1] && !leds[last-2])
last-=3;
while(last > 3 && idle_counter >= WAITS) {
if(idle_counter <= WAITS) return; // shutdown aborted?
// shift array down by three
for(uint16_t i=3; i < last-3; i++) {
leds[i-3] = leds[i];
}
leds[last] = 0;
leds[last-1] = 0;
leds[last-2] = 0;
last -= 3;
ws2812b_show();
_delay_ms(3);
}
}
ISR(TIMER1_OVF_vect) {
if(++idle_counter >= WAITS ) {
pretty_shutdown();
if(idle_counter >= WAITS) {
psu_off();
}
}
}
void init_timer1() {
TCCR1B |= (1 << CS12) | (1 << CS10); // prescale F_CPU/1024
reset_idle();
TIMSK1 |= (1 << TOIE2); // Enable overflow interrupt
}
#endif