Buttondb
Buttondb
Module
ButtonDB.c
Revision
1.0.1
Description
This implements the button debouncing state machine
Notes
History
// with the introduction of Gen2, we need a module level Priority var as well
static uint8_t MyPriority;
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 initializes the
* button ports
Notes
Author
J. Edward Carryer, 10/23/11, 18:55
****************************************************************************/
bool InitButtonDB(uint8_t Priority)
{
ES_Event_t ThisEvent;
MyPriority = Priority;
// put us into the Initial PseudoState
CurrentState = InitButtonState;
// post the initial transition event
ThisEvent.EventType = ES_INIT;
Parameters
EF_Event_t 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
C. Paullin
****************************************************************************/
bool PostButtonDB(ES_Event_t ThisEvent)
{
return ES_PostToService(MyPriority, ThisEvent);
}
/****************************************************************************
Function
RunButtonDB
Parameters
ES_Event_t : One of InitButtonState, Waiting, Falling_edge,
Rising_edge
Returns
ES_Event_t, ES_NO_EVENT if no error ES_ERROR otherwise
Description
debounces a button press
Notes
uses nested switch/case to implement the machine.
Author
C. Paullin
****************************************************************************/
ES_Event_t RunButtonDB(ES_Event_t ThisEvent)
{
ES_Event_t ReturnEvent;
ReturnEvent.EventType = ES_NO_EVENT; // assume no errors
switch (CurrentState)
{
case InitButtonState: // If current state is init Button State
{
if (ThisEvent.EventType == ES_INIT) // only respond to ES_Init
{
LastButtonState = Button_Port;
CurrentState = Waiting;
}
}
break;
/****************************************************************************
Function
CheckButton
Parameters
none
*
Returns
bool true if changed state, false if not
Description
checks if any change in button port
Notes
Author
C. Paullin
*/
bool CheckButton(void){
bool ReturnVal = false;
uint8_t CurrentButtonState;
CurrentButtonState= Button_Port;
ES_Event_t Event2Post;
if(CurrentButtonState!=LastButtonState){
ReturnVal = true;
LastButtonState = CurrentButtonState;
if(CurrentButtonState == Down){
Event2Post.EventType = ES_DB_FALLING_EDGE;
ES_PostToService(MyPriority, Event2Post);
}
if(CurrentButtonState == Up){
Event2Post.EventType = ES_DB_RISING_EDGE;
ES_PostToService(MyPriority, Event2Post);
}
}
return ReturnVal;
}
/***************************************************************************
private functions
***************************************************************************/