Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

LED Control FSM Pseudo Code Using the Gen2.

x Event Framework
*************************************************************************************
Pseudo-code for the LED Control FSM module (a service that
implements a state machine)
Data private to the module: CurrentState, MyPriority, LED_Val,
FlashTime

InitLEDFSM
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 LEDFSM queue (this service)
End of InitLEDFSM

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

RunLEDFSM
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 13 of port B as a digital output
Set port B latch bit 13 to 0
Endif
End LED_Init block

CurrentState is LED_Waiting:
If ThisEvent is ES_LEDDEACTIVATE:
Set CurrentState to LED_Inactive
Configure pin 13 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 bit 13
Endif
If ThisEvent is ES_LEDON:
Set LED_Val to 1
Write LED_Val to port B latch bit 13
Endif

If ThisEvent is ES_LEDOFF:
Set LED_Val to 0
Write LED_Val to port B latch bit 13
Endif
End LED_Waiting block

CurrentState is LED_Inactive:
If ThisEvent is ES_LEDWAIT:
Set CurrentState to LED_Waiting
Configure pin 13 of port B as a digital output
Endif

If ThisEvent is ES_LEDFLASH:
Set CurrentState to LED_Flashing
Configure pin 13 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 bit 13
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 pin 13 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 bit 13
Else:
Set LED_Val to 1
Write LED_Val to port B latch bit 13
Endif
Initialize LED_FLASH_TIMER to FLASHTIME
Endif
End LED_Flashing block

Return ReturnEvent
End of RunLEDFSM

QueryLEDFSM
Takes nothing, returns LEDState_t
Return CurrentState
End of QueryLEDFSM

You might also like