You are on page 1of 6

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

Header file for Button module and DDM module


based on the Gen2 Events and Services Framework
Author: Kacyn Fujii Date: 11/2/14
Final Editing: Dongao Yang 11/25/2014
****************************************************************************/
#ifndef Button_H
#define Button_H
// Event
#include
#include
#include

Definitions
"ES_Configure.h" /* gets us event definitions */
"ES_Types.h"
/* gets bool type for returns */
"ES_Framework.h"

#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include
#include
#include
#include
#include
#include
#include

"ES_Port.h"
"termio.h"
"inc/hw_memmap.h"
"inc/hw_types.h"
"inc/hw_gpio.h"
"inc/hw_sysctl.h"
"driverlib/gpio.h"

#define ALL_BITS (0xff<<2)


// typedefs for the states
// State definitions for use with the query function
typedef enum { Debouncing, Ready2Sample} ButtonState_t ;
// Public Function Prototypes
bool InitButton ( uint8_t Priority );
bool PostButtonDebounceFSM( ES_Event ThisEvent );
ES_Event RunButtonDebounceFSM( ES_Event ThisEvent );
// Snitch LEDs control
void SnitchLEDOn(void);
void SnitchLEDOff(void);
// DDM states control
void DDMOn(void);
void DDMOff(void);
#endif
/****************************************************************************
Module
Button.c

Description
Module to handle button debouncing
Module to change the IO out state for DDM state
Created by Kacyn Fujii on 11/2/14
Final Edit: Dongao Yang 11/25/14
****************************************************************************/
/*----------------------------- 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
*/
#include "ES_Configure.h"
#include "ES_Timers.h"
#include "ES_Framework.h"
#include "Button.h"
#include "Game.h"
#include "GeneralBase.h"
/*----------------------------- Module Defines ----------------------------*/
// define the IO port needed for the button
#define ButtonPort 'F'
// define the IO input pin for button
#define ButtonIO BIT1HI
// define the LED IO output pin for button
#define ButtonLEDIO BIT0HI
// define the port for DDM output
#define DDMPort 'D'
// define the IO output pin for DDM output
#define DDM BIT7HI
/*---------------------------- Module Functions ---------------------------*/
/* prototypes for private functions for this machine.They should be functions
relevant to the behavior of this state machine
*/
void SnitchLEDOn(void);
void SnitchLEDOff(void);
/*---------------------------- Module Variables ---------------------------*/
// everybody needs a state variable, you may need others as well.
// type of state variable should match htat of enum in header file
static ButtonState_t CurrentState;
// with the introduction of Gen2, we need a module level Priority var as well
static uint8_t MyPriority;
/*------------------------------ Module Code ------------------------------*/
/****************************************************************************
Function
InitButton
Parameters
uint8_t : the priorty of this service
Returns
bool, false if error in initialization, true otherwise
Description
Saves away the priority, does button debounce by 2 states

Final Edit: Dongao Yang 11/25/14


****************************************************************************/
bool InitButton ( uint8_t Priority )
{
//Initialize the priority for this SM
MyPriority = Priority;
//initialize port line for button
InitSystemClock(ButtonPort);
//initialize pins to IO for button
InitPinIO(ButtonPort, ButtonIO);
InitPinIO(ButtonPort, ButtonLEDIO);
//initialize pin to IO for DDM
InitPinIO(DDMPort, DDM);
//set button read pin to input
InitPinDir(ButtonPort, ButtonIO, 0);
//set button led pin to output
InitPinDir(ButtonPort, ButtonLEDIO, 1);
//set DDM output pin to output
InitPinDir(DDMPort, DDM, 1);
//initialize the state to debouncing state
CurrentState = Debouncing;
//start debounce timer, 20 ms
ES_Timer_InitTimer(BUTTON_TIMER, 20);
//Set DDM state to disarmed
DDMOn();
return true;
}
/****************************************************************************
Function
PostButtonDebounceFSM
Parameters
EF_Event ThisEvent , the event to post to the queue
Returns
boolean False if the Enqueue operation failed, True otherwise
Description
Posts an event to this state machine's queue
Notes
Author
J. Edward Carryer, 10/23/11, 19:25
Final Edit: Dongao Yang 11/25/14
****************************************************************************/
bool PostButtonDebounceFSM( ES_Event ThisEvent )
{
return ES_PostToService( MyPriority, ThisEvent);

}
/****************************************************************************
Function
RunButtonDebounceFSM
Parameters
ES_Event : the event to process
Returns
ES_Event, ES_NO_EVENT if no error ES_ERROR otherwise
Description
State machine to handle button debouncing
Final Edit: Dongao Yang 11/25/14
****************************************************************************/
ES_Event RunButtonDebounceFSM( ES_Event ThisEvent )
{
//Initialize the return event to ES_NO_EVENT
ES_Event ReturnEvent;
ReturnEvent.EventType = ES_NO_EVENT; // assume no errors
// The event to post to Game SM
ES_Event NewEvent;
// switch on current state
switch ( CurrentState )
{
//If current state is deboucing
case Debouncing :
//If event type is ES_TIMEOUT
if ( ThisEvent.EventType == ES_TIMEOUT )
{
//change state to ready2sample
CurrentState = Ready2Sample;
}
break;
//If state is ready to sample
case Ready2Sample:
//If event is button up
if(ThisEvent.EventType == BUTTON_UP)
{
//start debounce timer, 20 ms
ES_Timer_InitTimer(BUTTON_TIMER, 20);
//change state to deboucing
CurrentState = Debouncing;
}
//If state is button down
if(ThisEvent.EventType == BUTTON_DOWN) {
//start debounce timer, 20 ms
ES_Timer_InitTimer(BUTTON_TIMER, 20);
//change state to deboucing
CurrentState = Debouncing;
//post button press to game SM

NewEvent.EventType = ES_BUTTON_DOWN;
PostGameFSM(NewEvent);
}
break;
default :
;
}
// end switch on Current State
return ReturnEvent;
}
/***************************************************************************
private functions
***************************************************************************/
/****************************************************************************
Function
SnitchLEDOn
Parameters
nothing
Returns
nothing
Description
light LED on the button
Final Editing: Dongao Yang 11/25/2014
****************************************************************************/
void SnitchLEDOn(void)
{
//change button LED pin to high
write(ButtonPort, ButtonLEDIO, 1);
}
/****************************************************************************
Function
SnitchLEDOff
Parameters
nothing
Returns
nothing
Description
Shut down LED on the button
Final Editing: Dongao Yang 11/25/2014
****************************************************************************/
void SnitchLEDOff(void)
{
//change button LED pin to low

write(ButtonPort, ButtonLEDIO, 0);


}
/****************************************************************************
Function
DDMOn
Parameters
nothing
Returns
nothing
Description
change DDM state to armed, output 5V
Final Editing: Dongao Yang 11/25/2014
****************************************************************************/
void DDMOn(void)
{
// change DDM pin to high
write(DDMPort, DDM, 1);
}
/****************************************************************************
Function
DDMOn
Parameters
nothing
Returns
nothing
Description
change DDM state to disarmed, output 0V
Final Editing: Dongao Yang 11/25/2014
****************************************************************************/
void DDMOff(void)
{
// change DDM pin to low
write(DDMPort, DDM, 0);
}

You might also like