You are on page 1of 10

Pseudo-code for the DisplaySM module (a service that implements a

state machine)
Data private to the module: MyPriority, CurrentState, DisplayArray,
CursorArray, TargetArray, PeripheralArray, ScoreArray, CursorCenterX,
CursorCenterY, TargetCenterX, TargetCenterY, Score, CrashTimeLength

Functions private to the module: MoveDown, MoveUp, MoveLeft,


MoveRight, InitializeCursor, InitializeTarget, ResetTarget,
MergeArrays, InitializeWelcome, ClearCursor, ClearTarget,
ClearPeripheral, HapticOn, HapticOff, CheckTarget,
DisplayCrashWarning, DisplayCrashSuccess, DisplayCrashFailure,
ClearAll, IncrementScore, ClearScoreArray, DisplayGameEnding,
InitializeGameStartup

InitDisplaySM0
Takes a priority number, returns True.

Initialize the MyPriority variable with the passed in parameter.


Post Event ES_Init to this service
Setup 5 PWM channels
Set frequency on PWM timer
Assign PWM channels to timers
Map Servo output pin to PWM channel
Map Fan output pin to PWM channel
End of InitDisplaySM

RunDisplaySM0 (implements a state machine)


The EventType field of ThisEvent will be one of: ES_Init,
BUTTON_PRESS,MOVE_DX,MOVE_DY, SYSTEM_TIMEOUT, ES_GRAVITY, ES_TIMEOUT,
ES_SERVOTIMEREND, SYSTEM_TIMEOUT, LEVER_PULLED,
Returns ES_NO_EVENT

Based on the state of the CurrentState variable choose one of the


following blocks of code:

CurrentState is InitPState
If ThisEvent is ES_Init
Configure Digital Output for haptic motor
Call InitializeWelcome
Call MergeArrays
Set CurrentState equals to WelcomeMode
Endif
End InitPState block
CurrentState is WelcomeMode
ThisEvent is BUTTON_PRESS
Call InitializeCursor, InitializeGameStartup,
MergeArrays
Set CurrentState = GameStartup
Post ES_LEDWAIT event to LEDFSM and LED Handle SM
End WelcomeModeBlock

CurrentState is GameStartup
ThisEvent is MOVE_DX
If EventParam - Center > 0
Call ClearPeripheral,
InitializeCursor,InitializeTarget,MergeArrays
Set CrashTimeLength equal to random number
Start crash timer with crash time length
Post ES_SERVOSTART event to ServoTimeService
Set CurrentState equals PlayingMode
ThisEvent is MOVE_DY
If EventParam - Center > 0
Call ClearPeripheral,
InitializeCursor,InitializeTarget,MergeArrays
Set CrashTimeLength equal to random number
Start crash timer with crash time length
Post ES_SERVOSTART event to ServoTimeService
Set CurrentState equals PlayingMode
ThisEvent is SYSTEM_TIMEOUT
Call InitializeWelcome, MergeArrays
Set CurrentState = WelcomeMode
End GameStartup block

CurrentState is PlayingMode
ThisEvent is MOVE_DX
If EventParam - Center > 0
Call MoveRight, MergeArrays
Else if EventParam - Center < 0
Call MoveLeft, MergeArrays
ThisEvent is MOVE_DY
If EventParam - Center > 0
Call MoveUp, MergeArrays
Else if EventParam - Center < 0
Call MoveDown, MergeArrays
ThisEvent is BUTTON_PRESS
If CheckTarget returns true
Call ResetTarget, MergeArrays, IncrementScore
ThisEvent is ES_GRAVITY
Call MoveDown, MergeArrays
ThisEvent is ES_TIMEOUT
Call DisplayCrashWarning, MergeArrays, HapticOn
Set CurrentState equals CrashWarningMode
Start Crash timer
ThisEvent is ES_SERVOTIMEREND
Call DisplayGameEnding, MergeArrays, HapticOff
Set CurrentState equals GameEndingMode

Post ES_SERVOSTOP to ServoTimeService


Start Crash timer for five sec
ThisEvent is SYSTEM_TIMEOUT
Call InitializeWelcome, MergeArrays
Set CurrentState equals to WelcomeMode

End PlayingMode block

CurrentState is CrashWarningMode
ThisEvent is ES_TIMEOUT
Call DisplayCrashFailure, MergeArrays, HapticOff
Set CurrentState equals CrashFailureMode
Start Crash timer for 5 seconds
ThisEvent is LEVER_PULLED
Call DisplayCrashSuccess, MergeArrays,HapticOff
Set CurrentState equals CrashSuccessMode

Post ES_LEDWAIT to LEDHandle SM


Start Crash timer for one sec
ThisEvent is ES_SERVOTIMEREND
Call DisplayGameEnding, MergeArrays
Set Current State equal to GameEndingMode
Post ES_SERVOSTOP event to ServoTimeService
Set Crash timer for 5 sec

End CrashWarningMode block

CurrentState is CrashSuccessMode
ThisEvent is ES_TIMEOUT
Call ClearAll, InitializeCursor, ResetTarget,
MergeArrays, HapticOff
Set CurrentState = PlayingMode
Set CrashTimeLength equal to random number
Start Crash Timer with CrashTimeLength
ThisEvent is ES_SERVOTIMEREND
Call DisplayGameEnding, MergeArrays,
Set Current State equal to GameEndingMode
Set Crash timer for 5 sec

End CrashSuccessMode block

CurrentState is CrashFailureMode
ThisEvent is ES_TIMEOUT
Call HapticOff, DisplayGameEnding, MergeArrays
Set CurrentState = GameEndingMode
Start Crash Timer with 5 seconds
End CrashFailureMode block

CurrentState is GameEndingMode
ThisEvent is ES_TIMEOUT
Call InitializeWelcome, MergeArrays
Set CurrentState = WelcomeMode
Post ES_SERVORESET event to ServoTimeService
End CrashFailureMode block

Return ES_NO_EVENT
End of RunDisplaySM

MoveUp
Takes no parameters, returns nothing.

Local variables: wallCrash equal to false

If max row of cursor array is greater than 0


Set wallCrash equals True
Call HapticOn
Else
Call HapticOff
For every row of CursorArray
Set CursorArray rows equal to the value of one row less
EndFor
Set bottom row of CursorArray equal 0.
EndIf

End of MoveUp

MoveDown
Takes no parameters, returns nothing.

Local variables: wallCrash equal to false

If min row of cursor array is greater than 0


Set wallCrash equals True
Call HapticOn
Else
Call HapticOff
For every row of CursorArray
Set CursorArray rows equal to the value of one row greater
EndFor
Set top row of CursorArray equal 0.
EndIf

End of MoveDown

MoveLeft
Takes no parameters, returns nothing.

Local variables: wallCrash equal to false

For every row of CursorArray


If leftmost bit is greater than 0
Set wallCrash = True
Call HapticOn
EndIf
EndFor
If WallCrash is false
Call HapticOff
For every row in CursorArray
Bit shift one to the left
EndFor
EndIf

End of MoveLeft

MoveRight
Takes no parameters, returns nothing.

Local variables: wallCrash equal to false

For every row of CursorArray


If rightmost bit is greater than 0
Set wallCrash = True
Call HapticOn
EndIf
EndFor
If WallCrash is false
Call HapticOff
For every row in CursorArray
Bit shift one to the right
EndFor
EndIf

End of MoveRight

InitializeCursor
Takes no parameters, returns nothing.

Call ClearAll
Call ClearCursor
Add Cursor graphic to CursorArray

End of InitializeCursor

InitializeGameStartup
Takes no parameters, returns nothing.

Call ClearPeripheral

Add startup graphic to PeripheralArray

End of InitializeGameStartup

InitializeTarget
Takes no parameters, returns nothing.

Seed random number generator with GetTime


Set X and Y positions of target

Add target graphic to TargetArray to row Y, in rightmost position


Bit shift target by (xMax - X position)

End of InitializeTarget

ResetTarget
Takes no parameters, returns nothing.
Call ClearTarget

Set X and Y positions of target with random number generator


Add target graphic to TargetArray to row Y, in rightmost position
Bit shift target by (xMax - X position)

End of ResetTarget

MergeArrays
Takes no parameters, returns nothing.

For all rows in DisplayArray


Set DisplayArray equal to “or” of Cursor, Target, Peripheral
and Score Arrays
EndFor

Post MATRIX_UPDATE event to LEDMatrixSM1

End of MergeArrays

InitializeWelcome
Takes no parameters, returns nothing.

Call ClearAll

Add welcome graphic to PeripheralArray

Post ES_LEDFLASH event to LEDFSM and LEDHandleControlSM

Call ClearScoreArray

End of InitializeWelcome

ClearCursor
Takes no parameters, returns nothing.

For all rows in CursorArray


Set CursorArray row to 0

End of ClearCursor

ClearTarget
Takes no parameters, returns nothing.
For all rows in TargetArray
Set TargetArray row to 0

End of ClearTarget

ClearPeripheral
Takes no parameters, returns nothing.

For all rows in PeripheralArray


Set PeripheralArray row to 0

End of ClearPeripheral

ClearAll
Takes no parameters, returns nothing.

Call ClearCursor, ClearTarget, ClearPeripheral

End of ClearAll

HapticOn
Takes no parameters, returns nothing.

Post HAPTIC_ON event to Vibration service

End of HapticOn

HapticOff
Takes no parameters, returns nothing.

Post HAPTIC_OFF event to Vibration service

End of HapticOff

CheckTarget
Takes no parameters, returns TargetHit.

Local variables: TargetHit equal to false

For all rows in CursorArray


If “and” of CursorArray row and TargetArray row is greater than
0
Set TargetHit equals true
EndIf
EndFor
Return TargetHit

End of CheckTarget

DisplayCrashWarning
Takes no parameters, returns nothing.

Call ClearPeripheral

Add crash warning graphic to PeripheralArray

Post ES_LEDFLASH to LEDHandleControl Service

End of DisplayCrashWarning

DisplayCrashSuccess
Takes no parameters, returns nothing.

Call ClearAll

Add crash success graphic to PeripheralArray

End of DisplayCrashSuccess

DisplayCrashFailure
Takes no parameters, returns nothing.

Call ClearAll

Add crash failure graphic to PeripheralArray

End of DisplayCrashFailure

IncrementScore
Takes no parameters, returns nothing.

Set Score = Score + 1

If Score is less than 23


For all rows in array less than score
Set rightmost bit high
EndFor
Else
Post SERVOTIMEREND to this service
EndIf

End of IncrementScore

ClearScoreArray
Takes no parameters, returns nothing.

Set Score equal to 0


For all rows in ScoreArray
Set row equal to 0
EndFor

End of ClearScoreArray

DisplayGameEnding
Takes no parameters, returns nothing.

Call ClearAll

Add game ending graphic to PeripheralArray

End of DisplayGameEnding

You might also like