You are on page 1of 2

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

state machine)
Data private to the module: MyPriority, CurrentState, DeferralQueue

InitLEDMatrix
Takes a priority number, returns True.

Initialize the MyPriority variable with the passed in parameter.


Set CurrentState equal to ReadyToSend

Call BasicConfig for SPI_SPI1


Call ClockIdleState on SPI1 with 1
Call SetActiveEdge on SPI1 with 0
Call SetTransferWidth on SPI1 with 16Bit
Cal SetEnhancedBuffer on SPI1 with 1
Call SetLeader on SPI1 with mid sampling
Call MapSSOutput on SPI1 with RPA0
Call MapSDOutput on SPI1 with RPA1
Call SetBitTime with 1000 ms
Call EnableSPI

While DM_TakeDisplayUpdateStep is false


Call DM_TakeInitDisplayStep
EndWhile

Post Event ES_Init to this service


End of InitLEDMatrix

RunLEDMatrix (implements a state machine)


The EventType field of ThisEvent will be one of: ES_Init,
ES_UPDATE_COMPLETE, MATRIX_UPDATE, ES_UPDATE_ROW

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


following blocks of code:

CurrentState is ReadyToSend
ThisEvent is MATRIX_UPDATE
Post ES_UPDATE_ROW to this service
Set CurrentState = Writing
ThisEvent is ES_UPDATE_COMPLETE
Post ES_UPDATE_ROW event to this service
Set CurrentState equal to Writing
End ReadyToSend Block
CurrentState is Writing
ThisEvent is ES_UPDATE_ROW
If DM_TakeDisplayUpdateStep returns false
Set CurrentState equals to ReadyToSend
Post ES_UPDATE_COMPLETE event to this service
Else
Set CurrentState to WritingLastRow
Post ES_UPDATE_COMPLETE event to this service
EndIf
Default case
Defer event
End Writing Block

CurrentState is WritingLastRow
ThisEvent is ES_UPDATE_COMPLETE
Set CurrentState = ReadyToSend
Recall deferred events
Default case
Defer event
End WritingLastRow Block

End of RunLEDMatrixSM

You might also like