
// tenth second resolution timers

#include <Ticker.h>
#include "Timers_ADI.h"

Ticker tickerTenths;

void init_timers(void)
{

  // Interrupt .. Timers

  // ISR setup
  tickerTenths.attach(0.1, onehundredms); // every 0.1 second

  // obstruction sensor
  pinMode(OBSTRUCTIONPIN, INPUT_PULLUP);
  /*
    attachInterrupt(digitalPinToInterrupt(OBSTRUCTIONPIN), obstructionToggle, RISING);
  */

  // Setup timers the first time
  int it;
  for (it = 0; it < TIMERCOUNT; it++) // init all timers
    Timers[it] = -1;

  Timers[HEARTBEAT] = 1;
  Timers[COMMSREPLYTIMER] = 0;
  Timers[PINGTIMER] = 0;
  Timers[0] = 0;
  Timers[RELAYSERVICE] = 5;
  Timers[TENSECOND] = 1000;
  Timers[TIMER485] = 0;
  Timers[ONEMINUTE] = 0;   // Screen update timer
  Timers[WIFISERVICE] = 0; // turn it on
  Timers[NTP_TIMER] = 200000;
  Timers[AT200RX] = 0;
  Timers[OBSTRUCTION] = 1;
  Timers[DISPLAYSERVICETIMER] = 1;
  Timers[AT200DATA] = 1;
  Timers[OLEDHOLDTIMER] = 1;
}

void onehundredms(void)
{ // ISR once per onehundredms ticker
  onehundredmstick++;
}

void timer_update(void) // call every 0.1 sec from mainline
{
  int i;
  for (i = 0; i < TIMERCOUNT; i++) // update timers
    if (Timers[i] > -1)
      Timers[i]++;
}

void obstructionToggle(void) // interrupt on pin rising
{
  obstructionCounter++;
}
