/***************************************************************************

LEDMap.c

****************************************************************************/

#include "LEDMap.h"

 

/*-------------------------- Module Constants -----------------------------*/

const static int cal_waitTime_ms = 3000;        //time delay between transitions between LEDs of map

const static int cal_blinkOn_ms = 308;          //time LED stays on for each blink

const static int cal_blinkOff_ms = 708;         //time LED is off for each blink

 

const static int alertGain = 2;   //When the alarm has been tripped, make guard move this many times

//faster

 

/*------------------------- Module Definitions ----------------------------*/

 

/*-------------------------- Module Variables -----------------------------*/

static char curLED;               //Indicates which LED is currently lit

static char curBlinkState;        //Indicates whether current LED is currently on or off when

                                  //blinking

static LEDMap_Status_t myState;   //Internal state representation

 

static unsigned long lastUpdateTime;     //timing variable for LED transitions

static unsigned long lastBlinkTime;      //timing variable for blinking LEDs

 

static int waitTime_ms;    //time delay between transitions between LEDs of map

static int blinkOn_ms;     //time LED stays on for each blink

static int blinkOff_ms;    //time LED is off for each blink

 

/*-------------------------- Module Functions -----------------------------*/

static void updateOutputs(void);  //handles blinking of the current LED

 

/*---------------------------- Module Code ------------------------------*/

void initLEDMap(void) {

  myState = LEDMap_OFF;

  curLED = 0;                     //There are only 15 LEDs; decoder input zero (0) is not tied to an

                                  //LED so the map appears to be off

  PTT = (15 & PTT) | 0;           //set the ouput to the decoder to zero

  waitTime_ms = cal_waitTime_ms;  //Set wait times to the normal/calibrated times

  blinkOn_ms = cal_blinkOn_ms;

  blinkOff_ms = cal_blinkOff_ms;

}

 

void LEDMapGuardAlerted(void) {

  waitTime_ms = cal_waitTime_ms / alertGain;    //Update all times to be faster

  blinkOn_ms = cal_blinkOn_ms / alertGain;

  blinkOff_ms = cal_blinkOff_ms / alertGain;

}

 

LEDMap_Status_t checkLEDMapEvents(void){

  unsigned long myTime1 = 0;

  myTime1 = TMRS12_GetTime();

 

  switch (myState) {

    case LEDMap_OFF:

        //do nothing

    break;

   

    case LEDMap_RUNNING:

        //update which LED is active if enough time has passed

        if ((myTime1 - lastUpdateTime) > waitTime_ms) {

          lastUpdateTime = myTime1;         //update timing variable

          if (curLED < 15) {

            curLED++;         

   }

          else {

            //if curLED count has counted beyond 15, map has reached the end

            myState = LEDMap_GUARD_ARRIVED; 

          }

        }

    break;

   

    case LEDMap_GUARD_ARRIVED:

        //do nothing

    break;

  }

  updateOutputs();                //always update the outputs (both which LED is lit and the blinking of it)

  return myState;

}

 

//

void startLEDMap(void){

  myState = LEDMap_RUNNING;

  curLED = 1;                           //Turn on the first LED on map

  curBlinkState = 1;                    //Set the blink state to be on initially

  lastUpdateTime = TMRS12_GetTime();    //Update the timing variables to the current time

  lastBlinkTime = lastUpdateTime;

  updateOutputs();                      //Update the outputs

}

 

static void updateOutputs(void) {

  unsigned long myTime = 0;

  myTime = TMRS12_GetTime();

 

  //update blinking

  if (curBlinkState) {

    if ((myTime - lastBlinkTime) > blinkOn_ms) {

      curBlinkState = 0;

      lastBlinkTime = myTime;

    }

  }

  else {

    if ((myTime - lastBlinkTime) > blinkOff_ms) {

      curBlinkState = 1;

      lastBlinkTime = myTime;

    }

  }

 

  //update decoder to blink current LED

  if (curBlinkState)

    PTT = (15 & PTT) | (curLED*16);

  else

    PTT = (15 & PTT) | 0;

}