You are on page 1of 3

Pseudo-code for the Handle LED Control SM module (a service that

implements a state machine)


Data private to the module: CurrentState, MyPriority, LED_Val,
FlashTime

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

PostLEDHandleControlSM
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 PostLEDHandleControlSM

RunLEDHandleControlSM
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 LED_Init:
If ThisEvent is ES_Init:
Set CurrentState to LED_Waiting
Set LED_Val to 0
Configure pin 4 and 5 of port B as a digital output
Set port B latch pins 4 and 5 to 0
Endif
End LED_Init block

CurrentState is LED_Waiting:
If ThisEvent is ES_LEDDEACTIVATE:
Set CurrentState to LED_Inactive
Configure pins 4 and 5 of port B as a digital input
Endif
If ThisEvent is ES_LEDFLASH:
Set CurrentState to LED_Flashing
Initialize LED_FLASH_TIMER to FLASHTIME
Set LED_Val to 1
Write LED_Val to port B latch pins 4 and 5
Endif
If ThisEvent is ES_LEDON:
Set LED_Val to 1
Write LED_Val to port B latch pins 4 and 5
Endif

If ThisEvent is ES_LEDOFF:
Set LED_Val to 0
Write LED_Val to port B latch pins 4 and 5
Endif
End LED_Waiting block

CurrentState is LED_Inactive:
If ThisEvent is ES_LEDWAIT:
Set CurrentState to LED_Waiting
Configure pins 4 and 5 of port B as a digital output
Endif

If ThisEvent is ES_LEDFLASH:
Set CurrentState to LED_Flashing
Configure pins 4 and 5 of port B as a digital output
Initialize LED_FLASH_TIMER to FLASHTIME
Set LED_Val to 1
Write LED_Val to port B latch pins 4 and 5
Endif
End LED_Inactive block

CurrentState is LED_Flashing:
If ThisEvent is ES_LEDWAIT:
Set CurrentState to LED_Waiting
Endif

If ThisEvent is ES_LEDDEACTIVATE:
Set CurrentState to LED_Inactive
Configure pins 4 and 5 of port B as a digital input
Endif

If ThisEvent is ES_TIMEOUT:
If LED_Val is greater than 0:
Set LED_Val to 0
Write LED_Val to port B latch pins 4 and 5
Else:
Set LED_Val to 1
Write LED_Val to port B latch pins 4 and 5
Endif
Initialize LED_FLASH_TIMER to FLASHTIME
Endif
End LED_Flashing block
Return ReturnEvent
End of RunLEDHandleControlSM

QueryLEDHandleControlSM
Takes nothing, returns LEDHandleState_t
Return CurrentState
End of QueryLEDHandleControlSM

You might also like