]> www.fi.muni.cz Git - bike-lights.git/blob - firmware/main.c
buttons, wakeup rework
[bike-lights.git] / firmware / main.c
1 #include <avr/io.h>
2 #include <util/delay.h>
3 #include <avr/sleep.h>
4 #include <avr/interrupt.h>
5 #include <avr/power.h>
6 #include <avr/wdt.h>
7
8 #include "lights.h"
9
10 static void hw_setup()
11 {
12         wdt_enable(WDTO_1S);
13
14         init_battery();
15         init_pwm();
16         init_adc();
17         init_tmr();
18         init_buttons();
19
20         pwmled_init();
21         gpio_init();
22         ambient_init();
23         pattern_init();
24
25         set_sleep_mode(SLEEP_MODE_IDLE);
26 }
27
28 static void hw_suspend()
29 {
30         susp_pwm();
31         susp_adc();
32         susp_tmr();
33         susp_gpio();
34         susp_buttons();
35
36         wdt_disable();
37 }
38
39 void power_down()
40 {
41         hw_suspend();
42
43         do {
44                 // G'night
45                 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
46                 sleep_enable();
47                 sleep_bod_disable();
48                 sei();
49                 sleep_cpu();
50
51                 // G'morning
52                 cli();
53                 sleep_disable();
54
55                 // allow wakeup by long button-press only
56         } while (!buttons_wait_for_release());
57
58         // ok, so I will wake up
59         hw_setup();
60 }
61
62 int main(void)
63 {
64         log_init();
65
66         power_usi_disable(); // Once for lifetime
67         ACSRA |= _BV(ACD);   // disable analog comparator
68
69         log_set_state(3);
70
71         hw_setup();
72         power_down();
73
74         sei();
75 #if 1
76         while (1) {
77                 wdt_reset();
78                 sleep_mode();
79         }
80 #endif
81
82 #if 0
83         DDRB |= _BV(PB2);
84         while (1) {
85                 PORTB |=  _BV( PB2 );
86                 _delay_ms(200);
87                 PORTB &=~ _BV( PB2 );
88                 _delay_ms(200);
89         }
90 #endif
91 }