/****************************************************************************
 Module
   TapeChecker.c

 Revision
   1.0.1

 Description
   This module contains init and checker for the Tape Sensor

 Notes

 History
 When           Who     What/Why
 -------------- ---     --------
 02/03/18 10:25 edzc    first pass

****************************************************************************/
// the common headers for C99 types
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>

// the headers to access the GPIO subsystem
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "inc/hw_PWM.h"
#include "inc/hw_timer.h"
#include "inc/hw_nvic.h"
#include "inc/hw_sysctl.h"

// the headers to access the TivaWare Library
#include "driverlib/sysctl.h"
#include "driverlib/pin_map.h"
#include "driverlib/gpio.h"
#include "driverlib/timer.h"
#include "driverlib/interrupt.h"

#include "ES_Configure.h"
#include "ES_Framework.h"
#include "ES_DeferRecall.h"
#include "ES_Timers.h"
#include "ES_Events.h"

#include "BITDEFS.H"

#include "ADMulti.h"
#include "LolaGameMaster.h"

// **********************************************************************
// PROTOTYPES

// READABILITY DEFINES
#define BLACK_TAPE 2000   // value for the limit that we have to be above to have seen tape

// MODULE LEVEL VARIABLES
static uint32_t CurrentRawTape[4];
static uint32_t FrontSensorVal;
static uint32_t LeftSensorVal;
static uint32_t RightSensorVal;
static uint32_t BackSensorVal;
static uint8_t LastPost=100;

// **********************************************************************
// PUBLIC FUNCTIONS //
// **********************************************************************

// Initialize the necessary TIVA ports to gather data about the sensors in main
void InitTapeChecker(void)
{
  // TAPE
  // Initialize the A/D Multi Reading for the Tape Sensor
  ADC_MultiInit(4); // PE0, PE1, PE2, PE2
}

// Event Checker to determine if we have seen the Tape on the floor
void CheckTapeSensor(void)
{
  // Sample the port line
  ADC_MultiRead(CurrentRawTape);
  
  // Save the center, back, left, and right readings
  FrontSensorVal=CurrentRawTape[0];
  LeftSensorVal = CurrentRawTape[1];
  RightSensorVal = CurrentRawTape[2];
  BackSensorVal = CurrentRawTape[3];

   if((FrontSensorVal >= BLACK_TAPE) && (LeftSensorVal<BLACK_TAPE)&& (RightSensorVal<BLACK_TAPE) && (BackSensorVal<BLACK_TAPE) && (LastPost !=0))//only front sensor viewing tape
   { 
     // Post to LolaGameMaster
     LastPost=0;
     ES_Event_t Event2Post;
     Event2Post.EventType = TAPE_DETECTED;
     Event2Post.EventParam = 0;
     PostLolaGameMasterSM(Event2Post);      
   }
   
   else if ((LeftSensorVal >= BLACK_TAPE) && (FrontSensorVal<BLACK_TAPE) && (RightSensorVal < BLACK_TAPE)  && (BackSensorVal < BLACK_TAPE) && (LastPost !=1)) // only left sensor sees black tape
   {
     // Post to LolaGameMaster
     LastPost=1;      
     ES_Event_t Event2Post;
     Event2Post.EventType = TAPE_DETECTED;
     Event2Post.EventParam = 1;
     PostLolaGameMasterSM(Event2Post);      
   }
  
   else if ((RightSensorVal >= BLACK_TAPE) && (LeftSensorVal < BLACK_TAPE) && (FrontSensorVal < BLACK_TAPE) && (BackSensorVal < BLACK_TAPE) && (LastPost !=2)) // only right sensor sees black tape
   {
     // Post to LolaGameMaster
     LastPost=2;  
     ES_Event_t Event2Post;
     Event2Post.EventType = TAPE_DETECTED;
     Event2Post.EventParam = 2;
     PostLolaGameMasterSM(Event2Post);      
   }  

	    else if ((BackSensorVal >= BLACK_TAPE) && (FrontSensorVal < BLACK_TAPE) && (RightSensorVal < BLACK_TAPE) && (LeftSensorVal < BLACK_TAPE) && (LastPost !=3)) //only back sensor sees black tape
   {
     // Post to LolaGameMaster
     LastPost=3;  
     ES_Event_t Event2Post;
     Event2Post.EventType = TAPE_DETECTED;
     Event2Post.EventParam = 3;
     PostLolaGameMasterSM(Event2Post);      
   }  
	 
	 else if ((FrontSensorVal >=BLACK_TAPE) && (BackSensorVal>=BLACK_TAPE) && (LeftSensorVal < BLACK_TAPE) && (RightSensorVal < BLACK_TAPE) && LastPost!=4)//front and back only see tape
	 {
		 //Post to LolaGameMaster
		LastPost=4;  
     ES_Event_t Event2Post;
     Event2Post.EventType = TAPE_DETECTED;
     Event2Post.EventParam = 4;
     PostLolaGameMasterSM(Event2Post);
	 }

	    else if ((LeftSensorVal >= BLACK_TAPE) && (LastPost !=5)) // left sensor sees black tape
   {
     // Post to LolaGameMaster
     LastPost=5;      
     ES_Event_t Event2Post;
     Event2Post.EventType = TAPE_DETECTED;
     Event2Post.EventParam = 5;
     PostLolaGameMasterSM(Event2Post);      
   }
  
   else if ((RightSensorVal >= BLACK_TAPE) && (LastPost !=6)) // right sensor sees black tape
   {
     // Post to LolaGameMaste
     LastPost=6;  
     ES_Event_t Event2Post;
     Event2Post.EventType = TAPE_DETECTED;
     Event2Post.EventParam = 6;
     PostLolaGameMasterSM(Event2Post);      
   }  
	 
	    else if ((FrontSensorVal >= BLACK_TAPE) && (LastPost !=7)) // front sensor sees black tape
   {
     // Post to LolaGameMaste
     LastPost=7;  
     ES_Event_t Event2Post;
     Event2Post.EventType = TAPE_DETECTED;
     Event2Post.EventParam = 7;
     PostLolaGameMasterSM(Event2Post);      
   }  
}

// **********************************************************************
// PRIVATE FUNCTIONS //
// **********************************************************************