You are on page 1of 3

Button LED SM Pseudo Code Using the Gen2.

x Event Framework
*************************************************************************************
Pseudo-code for the Button LED SM module (a service that implements
a state machine)
Data private to the module: CurrentState, MyPriority, LatchState,
LastButtonState, DebounceTime

InitDebounceFSM
Takes a priority number, returns True.
Initialize the MyPriority variable with the passed in parameter.
Set CurrentState to be DebounceInit
Post Event ES_Init to ButtonLEDSM queue (this service)
End of InitDebounceFSM

PostDebounceFSM
Takes an event, returns a Boolean value
Calls ES_PostToService on the passed in event with MyPriority
Returns the result of ES_PostToService
End of PostDebounceFSM

RunDebounceFSM
Takes an event (ThisEvent), returns an event
Initialize ReturnEvent as ES_NO_EVENT
Based on CurrentState choose one of the following blocks of code:
CurrentState is DebounceInit:
If ThisEvent is ES_Init:
Set CurrentState to DebounceWaiting
Configure pin 13 of port B as a digital output
Set port B latch bit 13 to 0
Configure pin 12 of port B as a digital input
Read bit 12 of port B into LastButtonState
Endif
End DebounceInit block

CurrentState is DebounceWaiting:
If ThisEvent is ES_ButtonRiseDetected:
Set CurrentState to DebounceHigh
Initialize timer SERVICE1_TIMER to DEBOUNCETIME
Endif

If ThisEvent is ES_ButtonFallDetected:
Set CurrentState to DebounceLow
Initialize timer SERVICE1_TIMER to DEBOUNCETIME
Endif
If ThisEvent is ES_ButtonPressed:
Set port B latch bit 13 to 1
Call PostList00 on new BUTTON_PRESS event
Endif

If ThisEvent is ES_ButtonUnpressed:
Set port B latch bit 13 to 0
Endif
End DebounceWaiting block

If CurrentState is DebounceHigh:
If ThisEvent is ES_ButtonFallDetected:
Set CurrentState to DebounceWaiting
Endif

If ThisEvent is ES_TIMEOUT:
Set CurrentState to DebounceWaiting
Post ES_ButtonUnpressed event to this service
Endif
End DebounceHigh block

If CurrentState is DebounceLow:
If ThisEvent is ES_ButtonRiseDetected:
Set CurrentState to DebounceWaiting
Endif

If ThisEvent is ES_TIMEOUT:
Set CurrentState to DebounceWaiting
Post ES_ButtonPressed event to this service
Endif
End DebounceLow block

Return ReturnEvent
End of RunDebounceFSM

QueryDebounceFSM
Takes nothing, returns DebounceState_t
Return CurrentState
End of QueryDebounceFSM

CheckButtonEvents
Takes nothing, returns a Boolean
Initialize Boolean ReturnVal as false
Initialize unsigned int CurrentButtonState
Read bit 12 of port B into CurrentButtonState
If CurrentButtonState is not equal to LastButtonState:
Set ReturnVal to true
If CurrentButtonState is greater than LastButtonState:
Post new ES_ButtonRiseDetected event to this service
Else:
Post new ES_ButtonFallDetected event to this service
Endif
Endif
Set LastButtonState equal to CurrentButtonState
Return ReturnVal
End of CheckButtonEvents

You might also like