/*
 * timer.c
 *
 *  Created on: May 31, 2017
 *      Author: wn
 */

#include "hmi.h"
#include "timer.h"
#include "alarm.h"
#include <PontCoopScheduler.h>



extern tDisplay display;

void startTimer() {
  display.timerState = STARTED;
}

void stopTimer() {
  display.timerState = IDLE;
}

void secondTick(void *handle) {
  tDisplay *lDisplay = (tDisplay*) handle;

  switch (lDisplay->timerState) {
  case STARTED:
    lDisplay->currentTime = lDisplay->targetTime;
    lDisplay->overrunTime = 0;
    lDisplay->timerState = RUNNING;
    break;
  case RUNNING:
    lDisplay->currentTime -= 1;
    if (lDisplay->currentTime == 0) {
      enableAlarm(TIME_ALARM);
      lDisplay->timerState = OVERRUN;
    }
    break;
  case OVERRUN:
    lDisplay->overrunTime += 1;
    break;
  case IDLE:
    ;
    break;
  default:
    ;
  }
}

void timerInit() {
  schAdd(secondTick, &display, 0, 1000);
}
