diff --git a/src/main.c b/src/main.c index deee5fd..c14ecef 100644 --- a/src/main.c +++ b/src/main.c @@ -44,7 +44,7 @@ void setup_gpio() DDRB = 0xff; PORTB = 0; DDRB &= ~(1 << IGNITION_PIN); - PORTB |= (1 << IGNITION_PIN); // enable pull-up + // PORTB |= (1 << IGNITION_PIN); // enable pull-up PORTB &= ~(1 << MOSI_PIN); PORTB &= ~(1 << MISO_PIN); PORTB &= ~(1 << SCK_PIN); @@ -80,19 +80,28 @@ void switch_power(enum power power) } } +void indicate_ignition(enum power ignition) +{ + if(ignition) { + PORTB &= ~(1 << MOSI_PIN); + PORTB &= ~(1 << MISO_PIN); + PORTB &= ~(1 << SCK_PIN); + } else { + PORTB |= (1 << MOSI_PIN); + PORTB |= (1 << MISO_PIN); + PORTB |= (1 << SCK_PIN); + } +} + void handle_state_init() { if(ignition_on()) { switch_power(on); - PORTB &= ~(1 << MOSI_PIN); - PORTB &= ~(1 << MISO_PIN); - PORTB &= ~(1 << SCK_PIN); + indicate_ignition(on); current_state = state_on; } else { shutdown_expire = tick + SHUTDOWN_TIME_MS; - PORTB |= (1 << MOSI_PIN); - PORTB |= (1 << MISO_PIN); - PORTB |= (1 << SCK_PIN); + indicate_ignition(off); current_state = state_shutdown; } } @@ -101,9 +110,7 @@ void handle_state_on() { if(!ignition_on()) { shutdown_expire = tick + SHUTDOWN_TIME_MS; - PORTB |= (1 << MOSI_PIN); - PORTB |= (1 << MISO_PIN); - PORTB |= (1 << SCK_PIN); + indicate_ignition(off); current_state = state_shutdown; } } @@ -131,9 +138,7 @@ void handle_state_guard() if(guard_expire < tick) { if(ignition_on()) { switch_power(on); - PORTB &= ~(1 << MOSI_PIN); - PORTB &= ~(1 << MISO_PIN); - PORTB &= ~(1 << SCK_PIN); + indicate_ignition(on); current_state = state_on; } else { current_state = state_off; @@ -148,10 +153,12 @@ void handle_state_off() { if(ignition_on()) { switch_power(on); - PORTB &= ~(1 << MOSI_PIN); - PORTB &= ~(1 << MISO_PIN); - PORTB &= ~(1 << SCK_PIN); + indicate_ignition(on); current_state = state_on; + } else { + sleep_enable(); + sleep_cpu(); + sleep_disable(); } } @@ -204,4 +211,3 @@ ISR(WDT_vect) { WDTCR |= (1 << WDIE); } -