/****************************************************************************
 Module
   BeamBreak.c

 Revision
   1.0.1

 Description
 Simple service with three states: Standby, Sampling, Debouncing 
   
 Notes

 Author
Sai Koppaka
****************************************************************************/
/*----------------------------- Include Files -----------------------------*/
/* include header files for this state machine as well as any machines at the
   next lower level in the hierarchy that are sub-machines to this machine
*/

//including header files for the framework 
#include "ES_Configure.h"
#include "ES_Framework.h"
#include "ES_DeferRecall.h"

// 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 "BITDEFS.H"

#include "LolaGameMaster.h"

/*----------------------------- Module Defines ----------------------------*/
#define BeamBreakPinHi  BIT4HI //Port B Pin 4
#define BeamBreakPinLo  BIT4LO
/*---------------------------- Module Functions ---------------------------*/
/* prototypes for private functions for this machine.They should be functions
   relevant to the behavior of this state machine
*/

/*---------------------------- Module Variables ---------------------------*/
// everybody needs a state variable, you may need others as well.
// type of state variable should match that of enum in header file

// with the introduction of Gen2, we need a module level Priority var as well

/*------------------------------ Module Code ------------------------------*/
/****************************************************************************
 Function
     InitBeamBreak

 Parameters
     uint8_t : the priorty of this service

 Returns
     bool, false if error in initialization, true otherwise

 Description
     Saves away the priority, sets up the initial transition and does any
     other required initialization for this state machine
 Notes

 Author
  sk
****************************************************************************/
void InitBeamBreak(void)
{
    
  //Initializes port B, pin 4 as an input to 
    HWREG(SYSCTL_RCGCGPIO) |= SYSCTL_RCGCGPIO_R1;   //enables the peripheral clock
  while ((HWREG(SYSCTL_PRGPIO) & SYSCTL_PRGPIO_R1) != SYSCTL_PRGPIO_R1)
  {
   ;                                                                     //waiting for the peripheral clock to be ready
  }
  HWREG(GPIO_PORTB_BASE + GPIO_O_DEN) |= (BeamBreakPinHi);  //assigning digital port B
  HWREG(GPIO_PORTB_BASE + GPIO_O_DIR) &= (BeamBreakPinLo); //setting data direction port B (reading)

}

/***************************************************************************
 Function
    HaveBall 

 Parameters
 No parameters, returns true if event posted

 Returns
   ES_Event, ES_NO_EVENT if no error ES_ERROR otherwise

 Description
   Getter function for current state of PB4 (if pin is low we have a ball, if high we do not)
 Notes

 Author
   sk
****************************************************************************/
bool HaveBall(void)
{
   bool      ReturnVal = false;
   uint8_t   CurrentBeamState ;
  
  //Sample the port pin and use it to initialize CurrentReloadBeamState
    CurrentBeamState = (HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA+ALL_BITS)) & (BeamBreakPinHi)); 

	//If the CurrentBeamState is low
   if (!CurrentBeamState)
  {
		//Return that we have a ball
    ReturnVal = true; 
    
  }
 
 // LastReloadBeamState = CurrentReloadBeamState;
  return ReturnVal;
}