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

Flicker.c

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

#include "Flicker.h"

 

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

 

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

 

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

static char startCmd;             //flicker start input status (button debouncing)      

static char activeOn;             //keeps track of whether or not the active LEDs are on

static char failOn;               //keeps track of whether or not the fail LEDs are on

 

static unsigned long lastButtonTime;            //timing variable for button debouncing

static unsigned long lastBlinkFailTime;         //timing variable for blinking fail LEDs

static unsigned long lastBlinkActiveTime;       //timing variable for blinking active LEDs

 

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

static void wait(unsigned int);   //blocking code that waits for a specified number of ms

 

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

void initFlicker(void) {

  turnOnActive(0);                       //At startup, turn off Active and Fail LEDs

  turnOnFail(0);

  lastButtonTime = TMRS12_GetTime();     //Update all timer variables with the current time

  lastBlinkActiveTime = lastButtonTime;

  lastBlinkFailTime = lastButtonTime;

  startCmd = !(PTT & BIT0HI);            //read FLICKER start input (high = off, low = on)

}

 

FLICKER_EVENT_t checkFlickerEvents(void){

  unsigned long myTime = 0;

  myTime = TMRS12_GetTime();

 

  //update start input if 5 ms has passed (button debouncing)

  if ((myTime - lastButtonTime) > 5) {

    lastButtonTime = myTime;

    startCmd = !(PTT & BIT0HI);

  }

 

  if (startCmd) {

    return FLICKER_START;

  }

  else {

    return FLICKER_NO_EVENT;

  }

}

 

//Activate next FLICKER by sending 12mA for 50ms - 100ms

void startNextFlicker(void){       

  PTT = PTT | BIT1HI;      //set output hi

  wait(75);               

  PTT = PTT & BIT1LO;

}

 

//Control Active LEDs

void turnOnActive(char myActiveOn) {

  activeOn = myActiveOn;

  if (myActiveOn)

    PTM = PTM | BIT3HI;             //turn Active LEDs on

  else

    PTM = PTM & BIT3LO;             //turn Active LEDs off

}

 

//Blink Active LEDs

void blinkActive(int timeToWait) {   

  unsigned int myTime = TMRS12_GetTime();

  if ((myTime - lastBlinkActiveTime) > timeToWait) {

    lastBlinkActiveTime = myTime;        //update blink timing variable

    turnOnActive(!activeOn);             //turn on LEDs if they were off and vise versa

  }

}

 

//Control Fail LEDs

void turnOnFail(char myFailOn) {

  failOn = myFailOn;

  if (myFailOn)

    PTM = PTM | BIT2HI;           //turn Fail LEDs on

  else

    PTM = PTM & BIT2LO;           //turn Fail LEDs off

}

 

//Blink Fail LEDs during failure

void blinkFail(int timeToWait) {

  unsigned int myTime = TMRS12_GetTime();

  if ((myTime - lastBlinkFailTime) > timeToWait) {

    lastBlinkFailTime = myTime;   //update blink timing variable

    if (failOn)                   //turn on LEDs if they were off and turn them off if they were on

      turnOnFail(0);

    else

      turnOnFail(1);

  }

}

 

//delay for a specified number of ms

static void wait(unsigned int ms) {

  unsigned int myTime = 0;

  myTime = TMRS12_GetTime();

  //wait until the current time minus the start time is greater than the wait time

  while ((TMRS12_GetTime() - myTime) < ms) {};

}