X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=blobdiff_plain;f=firmware%2Fmain.c;h=ea3bb69d1384dd5840c5e6a90066dda0807a229c;hb=28656ba8d230fc14bab6445439fb5c1b966dddbe;hp=5db08b17d7335ed07b370b775de44416819978e8;hpb=33c451a9ba18c59b405d735b2420bc7b549643a4;p=bike-lights.git diff --git a/firmware/main.c b/firmware/main.c index 5db08b1..ea3bb69 100644 --- a/firmware/main.c +++ b/firmware/main.c @@ -7,9 +7,10 @@ #include "lights.h" -void hw_setup() +static void hw_setup() { wdt_enable(WDTO_1S); + init_battery(); init_pwm(); init_adc(); init_tmr(); @@ -23,7 +24,7 @@ void hw_setup() set_sleep_mode(SLEEP_MODE_IDLE); } -void hw_suspend() +static void hw_suspend() { susp_pwm(); susp_adc(); @@ -35,6 +36,11 @@ void hw_suspend() void power_down() { + uint16_t wake_count = 0; + unsigned char btn; + + hw_suspend(); +sleep_again: // enable PCINT14, so that user can wake up later GIMSK |= _BV(PCIE1); PCMSK1 |= _BV(PCINT14); @@ -55,6 +61,20 @@ void power_down() GIMSK &= ~_BV(PCIE1); PCMSK1 &= ~_BV(PCINT14); + // allow wakeup by long button-press only + for (btn = 0; btn < 5; btn++) { + if ((PINB & _BV(PB6)) != 0) { + wake_count++; + goto sleep_again;; + } + _delay_ms(100); + } + + // ok, so I will wake up + log_byte(0xb1); + log_word(wake_count); + log_flush(); + hw_setup(); } @@ -68,12 +88,13 @@ ISR(PCINT_vect) int main(void) { log_init(); + power_usi_disable(); // Once for lifetime + ACSRA |= _BV(ACD); // disable analog comparator log_set_state(3); hw_setup(); - hw_suspend(); power_down(); sei();