Download this file
#include "leffakone_timer.h"
#include "state_machine.h"

void StateMachine::input(uint32_t value) {
  if (state == IDLE && value > 0) {
    reset_timer->disable();
    wakeup_timer->get_off_in(value);
    blinker->start(BLINKER_COUNTDOWN_RATE);
    state = WAITING_WAKEUP;
  }
  else if ((state == WAITING_WAKEUP || state == WAITING_RESET) && value == 0) {
    wakeup_timer->disable();
    reset_timer->disable();
    blinker->stop();
    state = IDLE;
  }
  Serial.println(value, DEC);
}

void StateMachine::wakeup() {
  if (state == WAITING_WAKEUP) {
    wakeup_timer->disable();
    reset_timer->disable();
    digitalWrite(POWER_BUTTON_PIN, HIGH);
    delay(500);
    digitalWrite(POWER_BUTTON_PIN, LOW);
#ifdef ENABLE_RESET
    reset_timer->get_off_in(RESET_AFTER);
    blinker->start(BLINKER_WATCHDOG_RATE);
    state = WAITING_RESET;
#else
    blinker->stop();
    state = IDLE;
#endif
  }
}

#ifdef ENABLE_RESET
void StateMachine::reset() {
  if (state == WAITING_RESET) {
    reset_timer->disable();
    digitalWrite(RESET_BUTTON_PIN, HIGH);
    delay(500);
    digitalWrite(RESET_BUTTON_PIN, LOW);
    blinker->stop();
    state = IDLE;
  }
}
#endif

// vim:ft=arduino