You are on page 1of 3

BUTTON MODULE

Debounce code for both buttons


MODULE VARIABLES
MyPriority
LastButtonStateStart
LastButtonStateWeather
CurrentState

ES_Framework prioritu
Last state of start button
Last state of weather button
Current state of either button

InitializeStartButton
Parameters:
priority number
Returns:
success/failure
Local Variables: Pseudo-code:
FOR BOTH StartButton AND WeatherButton
Initialize the port and pins
Make the pins digital inputs
Sample the pins and save to LastButtonStates
Set CurrentState to ButtonInit
Start the debounce timer
Post ES_INIT event to this service
END FOR
PostStartButton
Parameters:
Returns:
Local Variables: Pseudo-code:
ES_PostToService
Return success/failure
RunStartButtonSM
Parameters:
ThisEvent
Event we are handling
Returns:
ReturnEvent ES_NO_EVENT if no errors
Local Variables: ReturnEvent ES_Event to return (ES_NO_EVENT if no errors)
Pseudo-code:
Switch CurrentState
CurrentState is ButtonInit
IF ThisEvent is ES_TIMEOUT
Set CurrentState to WaitingForStart
END IF
CurrentState is WaitingForStart
IF ThisEvent is ButtonDownStart
Start start button timer for debounce time
Set CurrentState to DebouncingStart
Post BTN_START to tree service
ELSE IF ThisEvent is ButtonUpStart
Start start button timer for debounce time
Set CurrentState to DebouncingStart

END IF
CurrentState is DebouncingStart
IF ThisEvent is ES_TIMEOUT
Set CurrentState to WaitingToReset
END IF
CurrentState is WaitingToReset
IF ThisEvent is START
Set CurrentState to WaitingForWeather
END IF
CurrentState is WaitingForWeather
IF ThisEvent is ButtonDownWeather
Start start button timer for debounce time
Set CurrentState to DebouncingWeather
Post BTN_WEATHER event to tree service
ELSE If ThisEvent is ButtonUpWeather
Start start button timer for debounce time
Set CurrentState to DebouncingWeather
ELSE IF ThisEvent is WAITING
Set CurrentState to WaitingForStart
END IF
CurrentState is DebouncingWeather
IF ThisEvent is ES_TIMEOUT
Set CurrentState to WaitingForWeather
ELSE IF ThisEvent is WAITING
Set CurrentState to WaitingForStart
END IF
END Switch
CheckButtonEvents
Parameters:
Returns:
bool Whether a new event was detected
Local Variables: CurrentButtonStateStart
Current state, start button
CurrentButtonStateWeather
Current state, weather button
ReturnVal
Value to return
Pseudo-code:
Set the return value to false
Get the current button states for CurrentButtonStates
IF state of start button changed and CurrentState is WaitingForStart
Set ReturnVal to true
IF the CurrentButtonStateStart is down
Post ButtonDownStart event to this service
ELSE
Post ButtonUpStart event to this service
END IF
END IF
IF state of weather button changed and CurrentState is WaitingForWeather
Set ReturnVal to true
IF the CurrentButtonStateWeather is down
Post ButtonDownWeather event to this service

ELSE
Post ButtonUpWeather event to this service
END IF
END IF
Set LastButtonStates to the CurrentStates
Return ReturnVal

You might also like