rgb-led-string: Christmas tree mod after real-world testing
[tinyboard.git] / projects / step-up / 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         power_all_disable();
13
14         init_battery();
15         init_pwm();
16         init_adc();
17         init_wdt();
18
19         init_buttons();
20
21         init_pwmled();
22         init_pattern();
23         init_control();
24
25         set_sleep_mode(SLEEP_MODE_IDLE);
26 }
27
28 static void hw_suspend()
29 {
30         susp_pwm();
31         susp_adc();
32         susp_wdt();
33
34         susp_buttons();
35
36         power_all_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         init_log();
65
66         log_set_state(3);
67
68         hw_setup();
69         power_down();
70
71         sei();
72 #if 1
73         while (1) {
74                 cli();
75                 if (pwm_enabled) {
76                         set_sleep_mode(SLEEP_MODE_IDLE);
77                 } else if (adc_enabled) {
78                         set_sleep_mode(SLEEP_MODE_ADC);
79                 } else {
80                         set_sleep_mode(SLEEP_MODE_PWR_DOWN);
81                 }
82
83                 sleep_enable();
84                 // keep BOD active, no sleep_bod_disable();
85                 sei();
86                 sleep_cpu();
87                 sleep_disable();
88         }
89 #endif
90
91 #if 0
92         DDRB |= _BV(PB2);
93         while (1) {
94                 PORTB |=  _BV( PB2 );
95                 _delay_ms(200);
96                 PORTB &=~ _BV( PB2 );
97                 _delay_ms(200);
98         }
99 #endif
100 }