// this will pull in the symbolic definitions for events, which we will want
// to post in response to detecting events
#include "ES_Configure.h"
// this will get us the structure definition for events, which we will need
// in order to post events in response to detecting events
#include "ES_Events.h"
// if you want to use distribution lists then you need those function 
// definitions too.
#include "ES_PostList.h"
// This include will pull in all of the headers from the service modules
// providing the prototypes for all of the post functions
#include "ES_ServiceHeaders.h"
// this test harness for the framework references the serial routines that
// are defined in ES_Port.c
#include "ES_Port.h"
// include our own prototypes to insure consistency between header & 
// actual functionsdefinition
#include "EventCheckers.h"

//HEADER FILES FOR SERVICE EVENT CHECKERS - ALSO NEED FUNCTION PROTOTYPES IN h FILE
#include "Spigot.h"
#include "Flame.h"

//neeeded for GPIO reads
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "inc/hw_sysctl.h"
#include "driverlib/sysctl.h"
#include "driverlib/pin_map.h"	// Define PART_TM4C123GH6PM in project
#include "driverlib/gpio.h"
#include "driverlib/timer.h"
#include "driverlib/interrupt.h"
#include "BITDEFS.H"

//The input value when the reset button is pressed
#define RESET_INPUT_HI 1
// always access all 8 bits at a time
#define ALL_BITS (0xff<<2)

static uint8_t LastButtonState;

// Event checker used to move through the game with keyboard strokes
bool Check4Keystroke(void)
{
  if ( IsNewKeyReady() ) // new key waiting?
  {
    ES_Event ThisEvent;
		ThisEvent.EventType = ES_NEW_KEY;			
    ThisEvent.EventParam = GetNewKey();
    // if the key is 's' and the event is game start
    if ( ThisEvent.EventParam == 's'){
			ES_Event ThisEvent;
			ThisEvent.EventType = ES_GAME_START;
      // post the event to servo, spigot, flame, and water reservoir
      PostServo( ThisEvent );
			PostSpigot( ThisEvent );
			PostFlame( ThisEvent );
			PostWaterReservoir( ThisEvent );
      // write event post notice to console
			printf ("\r\n Posted ES_GAME_START \r\n");
    // else if the key is 'r' and the event is game reset
    } else if ( ThisEvent.EventParam == 'r') {
			ES_Event ThisEvent;
			ThisEvent.EventType = ES_GAME_RESET;
      // post to servo, flame, spigot, watergun, and water reservoir
      PostServo( ThisEvent );
			PostFlame( ThisEvent );
			PostSpigot( ThisEvent );
			PostWaterGun(ThisEvent);
			PostWaterReservoir( ThisEvent );
      // write event post notice to console
			printf ("\r\n Posted ES_GAME_RESET \r\n");
    // else if the key is 'o' and the event is the game timeout
		} else if ( ThisEvent.EventParam == 'o') {
			ES_Event ThisEvent;
			ThisEvent.EventType = ES_GAME_TIMER_OUT;
      // post to servo, flame, and water gun
      PostServo( ThisEvent );
			PostFlame( ThisEvent );
			PostWaterGun(ThisEvent);
      // write event post notice to console
			printf ("\r\n Posted ES_TIMER_OUT\r\n");
    // else if the key is 'h' and the event is flame hit
		} else if ( ThisEvent.EventParam == 'h'){
			ES_Event ThisEvent;
			ThisEvent.EventType = ES_FLAME_HIT;
      // post to the flame service
      PostFlame( ThisEvent );
      // write event post notice to console
			printf ("\r\n Posted ES_FLAME_HIT \r\n");
    // else if the key is 'a' and the event is all flame off
		} else if ( ThisEvent.EventParam == 'a'){
			ES_Event ThisEvent;
			ThisEvent.EventType = ES_ALL_FLAME_OFF;
      // post to servo service
      PostServo( ThisEvent );
      // write event post notice to console
			printf ("\r\n Posted ES_ALL_FLAME_OFF \r\n");
		} else
    return true;
  }
  return false;
} // End Check4Keystroke


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


// Event checker for Start Button service
bool CheckResetButton (void)
{
	bool ReturnVal = false;
  uint8_t CurrentButtonState;
	
//Set CurrentButtonState to state read from port pin
	CurrentButtonState = (HWREG(GPIO_PORTA_BASE+(GPIO_O_DATA + ALL_BITS))) & BIT2HI;
//If the CurrentButtonState is different from the LastButtonState and it is HI
	if ((CurrentButtonState != LastButtonState) &&
			(CurrentButtonState > 0))
	{
	// PostEvent ES_BUTTON_HI to Reset Button service for debouncing
		ES_Event ThisEvent;
		ThisEvent.EventType = ES_BUTTON_HI; 
		PostResetButton(ThisEvent);
		ReturnVal = true;			
	} // Endif
//Set LastButtonState to the CurrentButtonState
	LastButtonState = CurrentButtonState;
//Return ReturnVal
	return ReturnVal;
} //End of CheckButtonEvents

