You are on page 1of 2

#include

#include
#include
#include

"ES_Configure.h"
"ES_Framework.h"
"ES_DeferRecall.h"
"ES_ShortTimer.h"

#include
#include
#include
#include
#include
#include
#include

"inc/hw_memmap.h"
"inc/hw_types.h"
"inc/hw_gpio.h"
"inc/hw_sysctl.h"
"driverlib/sysctl.h"
"driverlib/pin_map.h"
"driverlib/gpio.h"

#include "ButtonDebounce.h"
#include "SceneSM.h"
#ifndef ALL_BITS
#define ALL_BITS (0xff<<2)
#endif
static uint8_t MyPriority;
static DBState_t CurrentState;
static uint16_t debounce_time=500;
////////////////////////////////////////////////////////////////////////////////
/////
// Initialize the Button Debounce Service
// Takes: uint8_t Priority: service priority number
// Returns: bool: true if ES_Init event is posted, false otherwise
////////////////////////////////////////////////////////////////////////////////
/////
bool InitButtonDebounce(uint8_t Priority) {
// Set service priority
MyPriority=Priority;
// Initialize the current state machine state
CurrentState=Debouncing;
// Initialize the debouncing timer
ES_Timer_InitTimer(DEBOUNCE_TIMER,debounce_time);
// End Initialization
ES_Event ThisEvent;
ThisEvent.EventType=ES_INIT;
return (ES_PostToService(MyPriority, ThisEvent));
}
////////////////////////////////////////////////////////////////////////////////
/////
// Implement a Post Function for Button Debounce Service
// Takes: ES_Event ThisEvent: event to be posted to the queue
// Returns: bool: true if event posts, false if an error occurs
////////////////////////////////////////////////////////////////////////////////
/////
bool PostButtonDebounce(ES_Event ThisEvent) {
return ES_PostToService(MyPriority, ThisEvent);
}
////////////////////////////////////////////////////////////////////////////////
/////
// State machine for Button Debounce Service
// Takes: ES_Event ThisEvent: must be of type ButtonUp, ButtonDown, or
ES_TIMEOUT
// Returns: ES_Event ES_NO_EVENT: only if no errors occur
////////////////////////////////////////////////////////////////////////////////
/////
ES_Event RunButtonDebounce(ES_Event ThisEvent) {

//
Set up return event assuming no errors
ES_Event ReturnEvent;
ReturnEvent.EventType=ES_NO_EVENT;
switch (CurrentState) {
//
if current state is debouncing
case Debouncing:
//
if event is debounce timeout
if
((ThisEvent.EventType==ES_TIMEOUT)&&(ThisEvent.EventParam==DEBOUNCE_TIMER)) {
//
current state is ready to sample
CurrentState=Ready2Sample;
}
break;
//
else if state is ready to sample
case Ready2Sample:
//
if event is button up
if (ThisEvent.EventType==ES_BUTTON_UP) {
//
set debounce timer
ES_Timer_InitTimer(DEBOUNCE_TIMER,debounce_time);
//
state is debouncing
CurrentState=Debouncing;
}
// else if event is button down
else if (ThisEvent.EventType==ES_BUTTON_DOWN) {
// set debounce timer
ES_Timer_InitTimer(DEBOUNCE_TIMER,debounce_time);
// state is debouncing
CurrentState=Debouncing;
//
post button down event
ES_Event Event2Post;
Event2Post.EventType=ES_BUTTON_DOWN;
PostSceneSM(Event2Post);
}
break;
}
return ReturnEvent;
}

You might also like