Anda di halaman 1dari 6

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

Module
EventCheckers.h
Description
header file for the event checking functions
Final Editing: Dongao Yang 11/25/2014
*****************************************************************************
/
#ifndef EventCheckers_H
#define EventCheckers_H
// prototypes for event checkers
bool CheckTiltEvents(void);
bool CheckIR(void);
bool CheckButton(void);
#endif /* EventCheckers_H */
/****************************************************************************
Module
EventCheckers.c
Description
This is the event checker module contains the function to check the tilt
signal
from accelerometer, button states, and the IR beam breaker
Final Editing: Dongao Yang 11/25/2014
****************************************************************************/
/*----------------------------- Include Files -----------------------------*/
/* include header files for this module
*/
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include "ES_Port.h"
#include "termio.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "inc/hw_sysctl.h"
#include "driverlib/gpio.h"
#define ALL_BITS (0xff<<2)
// 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"
#include "ADCSWTrigger.h"
#include "PWMTiva.h"
#include "GeneralBase.h"
#include "Button.h"
#include "Game.h"
//static variable to monitor the state of tilt range
static int previous;
/****************************************************************************
Function
CheckTiltEvents
Parameters
None
Returns
bool: true if a new event was detected
Description
Event checker for tilt input
Final Editing: Dongao Yang 11/25/2014
****************************************************************************/
bool CheckTiltEvents(void)
{
// initialize the last broom position, only once
static uint32_t LastBroomPosition = 0;
// variable to store current broom position
uint32_t CurrentBroomPosition;
// the threshold for a tilt event happen
uint32_t threshold = 40;
// set return value to false
bool ReturnVal = false;
// read tilt signal from ADC pin
CurrentBroomPosition = ADC0_InSeq3();
//if the broom position change is larger than threshold
if( ((LastBroomPosition - CurrentBroomPosition) > threshold) ||
((CurrentBroomPosition - LastBroomPosition) > threshold))
{
// Event to post to Broom SM
ES_Event BroomEvent;
// Set BroomEvent to TILT_DETECTED
BroomEvent.EventType = TILT_DETECTED;
// Store CurrentBroomPosition to EventParam
BroomEvent.EventParam = CurrentBroomPosition;
// if this position is in left range and last state is not left
if((CurrentBroomPosition < 1700) && previous != 0)
{

// Event to post to Game SM


ES_Event GameEvent;
// Set this event to LEFT_TILT_DETECTED
GameEvent.EventType = LEFT_TILT_DETECTED;
// Set current range to left
previous = 0;
// Post this event to Game SM
PostGameFSM(GameEvent);
}
//else if this position is in mid range and last state is not mid
else if ((CurrentBroomPosition >= 1700) && (CurrentBroomPosition
< 2500) && previous != 1)
{
// Event to post to Game SM
ES_Event GameEvent;
// Set current range to mid
previous = 1;
// Set this event to CENTER_TILT_DETECTED
GameEvent.EventType = CENTER_TILT_DETECTED;
// Post this event to Game SM
PostGameFSM(GameEvent);
}
//else if this position is in right range and last state is not
right
else if((CurrentBroomPosition >= 2500)&& previous != 2 )
{
// Event to post to Game SM
ES_Event GameEvent;
// Set current range to right
previous = 2;
// Set this event to RIGHT_TILT_DETECTED
GameEvent.EventType = RIGHT_TILT_DETECTED;
// Post this event to Game SM
PostGameFSM(GameEvent);
}
//post broom event information to broom SM
PostBroomFSM( BroomEvent );
//Set return value to true
ReturnVal = true;
}
//store current state as last input state
LastBroomPosition = CurrentBroomPosition;
// Return the return value
return ReturnVal;
}
/****************************************************************************
Function
CheckButtonEvents
Parameters
None
Returns
bool: true if a new event was detected
Description
Event checker for button input
Final Editing: Dongao Yang 11/25/2014
****************************************************************************/

bool CheckButton(void)
{
// Initialize the last button state
static uint32_t LastButtonState = 1;
// Set the return value to false
bool ReturnVal=false;
// Variable to store current button state
uint8_t CurrentButtonState;
// Read current button state from the IO pin, it is set to be PF1
CurrentButtonState = read('F', BIT1HI);
// If Current Button State is changed
if (CurrentButtonState!=LastButtonState)
{
// Set return value to true
ReturnVal=true;
// If button pin read low
if (CurrentButtonState==0)
{
// Post BUTTON_DOWN event to Button Debounce SM
ES_Event ThisEvent;
ThisEvent.EventType = BUTTON_DOWN;
PostButtonDebounceFSM(ThisEvent);
}
else
{
// Post BUTTON_UP event to Button Debounce SM
ES_Event ThisEvent;
ThisEvent.EventType = BUTTON_UP;
PostButtonDebounceFSM(ThisEvent);
}
}
// Store Current state into last state
LastButtonState=CurrentButtonState;
// return the return value
return ReturnVal;
}
/****************************************************************************
Function
CheckIR
Parameters
None
Returns
bool: true if a new event was detected
Description
Event checker for IR beam breaker
Final Editing: Dongao Yang 11/25/2014
****************************************************************************/
bool CheckIR(void)
{
// Set the return value to false
bool ReturnVal = false;
// Set all last IR pin value to high
static uint8_t LastIRLeft = 1;

static uint8_t LastIRCenter = 1;


static uint8_t LastIRRight = 1;
// variables to store current IR pins states
uint8_t CurrentIRLeft;
uint8_t CurrentIRCenter;
uint8_t CurrentIRRight;
//read IR states from pins F2-4
CurrentIRLeft = read('F', BIT2HI);
CurrentIRCenter = read('F', BIT3HI);
CurrentIRRight = read('F', BIT4HI);
//Event to post to game SM
ES_Event ThisEvent;
// If left IR state changes
if (CurrentIRLeft!=LastIRLeft)
{
// If left IR change from high to low
if (CurrentIRLeft==0)
{
//Post LEFT_BALL_DETECTED to game SM
ThisEvent.EventType = LEFT_BALL_DETECTED;
PostGameFSM(ThisEvent);
}
//Set return value to be true
ReturnVal = true;
}
// If center IR changes
if (CurrentIRCenter!=LastIRCenter)
{
//If center IR changes from high to low
if (CurrentIRCenter==0)
{
//Post CENTER_BALL_DETECTED to game SM
ThisEvent.EventType = CENTER_BALL_DETECTED;
PostGameFSM(ThisEvent);
}
//Set return value to be true
ReturnVal = true;
}
// If right IR state changes
if (CurrentIRRight!=LastIRRight) // CurrentIR state is different (IR
beam broken)
{
//If right IR change from high to low
if (CurrentIRRight==0)
{
//Post RIGHT_BALL_DETECTED to game SM
ThisEvent.EventType = RIGHT_BALL_DETECTED;
PostGameFSM(ThisEvent);
}
//Set return value to be true
ReturnVal = true;
}
// Store all led current states into last state
LastIRLeft = CurrentIRLeft;
LastIRCenter = CurrentIRCenter;
LastIRRight = CurrentIRRight;

// return return value


return ReturnVal;
}

Anda mungkin juga menyukai