State Diagram
Pseudocode
Pseudo-code for the fidget spinner module (a service that implements a state machine)
Data private to the module: CurrentState, LastSpinnerState
InitSpinner
Takes a priority number, returns True if no errors
Initialize the MyPriority variable with the passed in parameter
Call SpinnerInitialize
Set CurrentState to Waiting4TOT
Get the current state of the spinner
Post the initial transition event
End of InitSpinner
PostSpinner
Posts an event to this state machine's queue (false if the enqueue operation failed, true otherwise)
Return post given event with parameters MyPriority and ThisEvent
End of PostSpinner
RunSpinner
Takes an event ThisEvent with EventType that is one of ES_INIT, ES_TIMEOUT, START_POTATO, END_POTATO, or PULSE_DETECTED
Returns ES_NO_EVENT if no error ES_ERROR otherwise
If ThisEvent is not ES_TIMEOUT
Reinitialize the idle timer
End
Based on the state of the CurrentState variable choose one of the following blocks of code:
CurrentState is Waiting4TOT
If ThisEvent is START_POTATO
Change CurrentState to Waiting4Pulse
End
End Waiting4TOT block
CurrentState is Waiting4Pulse
If ThisEvent is PULSE_DETECTED
Initialize the spinning timer
Change CurrentState to MaybeSpinning
Else If ThisEvent is END_POTATO
Post SPINNER_STOP to all services
Change CurrentState to Waiting4TOT
End
End Waiting4Pulse block
CurrentState is MaybeSpinning
If ThisEvent is PULSE_DETECTED
Initialize the spinning timer
Post SPINNER_START to all services
Change CurrentState to Spinning
Else If ThisEvent is ES_TIMEOUT from the spinning timer
Change CurrentState to Waiting4Pulse
Else If ThisEvent is END_POTATO
Post SPINNING_STOP to all services
Change CurrentState to Waiting4TOT
End
End MaybeSpinning block
CurrentState is Spinning
If ThisEvent is PULSE_DETECTED
Initialize the spinning timer
Else If ThisEvent is ES_TIMEOUT from the spinning timer
Post SPINNER_STOP to all services
Change CurrentState to Waiting4Pulse
Else If ThisEvent is END_POTATO
Post SPINNER_STOP to all services
Change CurrentState to Waiting4TOT
End
End of RunSpinner
QuerySpinner
Return the CurrentState
End of QuerySpinner
SpinnerInitialize
Set PB1 as a digital input
End of SpinnerInitialize
GetSpinnerState
Checks the state of the Hall Effect sensor, returns 1 if high, and a 0 if low
If the HE sensor pin is high
Return 1
Else
Return 0
End
End of GetSpinnerState
CheckSpinnerEvents
Event checker for the fidget spinner
Get the current state of the HE sensor
If the spinner state has changed
Post PULSE_DETECTED to Spinner
Set ReturnVal to true
End
Change LastSpinnerState to CurrentSpinnerState
Return ReturnVal
End of CheckSpinnerEvents
Data private to the module: CurrentState, LastSpinnerState
InitSpinner
Takes a priority number, returns True if no errors
Initialize the MyPriority variable with the passed in parameter
Call SpinnerInitialize
Set CurrentState to Waiting4TOT
Get the current state of the spinner
Post the initial transition event
End of InitSpinner
PostSpinner
Posts an event to this state machine's queue (false if the enqueue operation failed, true otherwise)
Return post given event with parameters MyPriority and ThisEvent
End of PostSpinner
RunSpinner
Takes an event ThisEvent with EventType that is one of ES_INIT, ES_TIMEOUT, START_POTATO, END_POTATO, or PULSE_DETECTED
Returns ES_NO_EVENT if no error ES_ERROR otherwise
If ThisEvent is not ES_TIMEOUT
Reinitialize the idle timer
End
Based on the state of the CurrentState variable choose one of the following blocks of code:
CurrentState is Waiting4TOT
If ThisEvent is START_POTATO
Change CurrentState to Waiting4Pulse
End
End Waiting4TOT block
CurrentState is Waiting4Pulse
If ThisEvent is PULSE_DETECTED
Initialize the spinning timer
Change CurrentState to MaybeSpinning
Else If ThisEvent is END_POTATO
Post SPINNER_STOP to all services
Change CurrentState to Waiting4TOT
End
End Waiting4Pulse block
CurrentState is MaybeSpinning
If ThisEvent is PULSE_DETECTED
Initialize the spinning timer
Post SPINNER_START to all services
Change CurrentState to Spinning
Else If ThisEvent is ES_TIMEOUT from the spinning timer
Change CurrentState to Waiting4Pulse
Else If ThisEvent is END_POTATO
Post SPINNING_STOP to all services
Change CurrentState to Waiting4TOT
End
End MaybeSpinning block
CurrentState is Spinning
If ThisEvent is PULSE_DETECTED
Initialize the spinning timer
Else If ThisEvent is ES_TIMEOUT from the spinning timer
Post SPINNER_STOP to all services
Change CurrentState to Waiting4Pulse
Else If ThisEvent is END_POTATO
Post SPINNER_STOP to all services
Change CurrentState to Waiting4TOT
End
End of RunSpinner
QuerySpinner
Return the CurrentState
End of QuerySpinner
SpinnerInitialize
Set PB1 as a digital input
End of SpinnerInitialize
GetSpinnerState
Checks the state of the Hall Effect sensor, returns 1 if high, and a 0 if low
If the HE sensor pin is high
Return 1
Else
Return 0
End
End of GetSpinnerState
CheckSpinnerEvents
Event checker for the fidget spinner
Get the current state of the HE sensor
If the spinner state has changed
Post PULSE_DETECTED to Spinner
Set ReturnVal to true
End
Change LastSpinnerState to CurrentSpinnerState
Return ReturnVal
End of CheckSpinnerEvents
Header
ES_Configure.h
Source Code
Download
spinner.h | |
File Size: | 0 kb |
File Type: | h |
spinner.c | |
File Size: | 7 kb |
File Type: | c |