Function InitLEDService(Priority):
Set MyPriority to Priority
Clear the screen
Set CurrentState to InitLState
Initialize the DeferralQueue with the required size
Configure SPI module (SPI_1):
- Set basic SPI configuration
- Set leader mode, clock period, transfer width, etc.
- Enable SPI module
If SPI configuration succeeds:
Print "PIC 32 Initialized"
Initialize the display
Transition CurrentState to WAITING
Print "Display Initialized"
Else:
Print "Failed to initialize"
Post an ES_INIT event to the service queue
If the event posting succeeds:
Return true
Else:
Return false
Function PostLEDService(Event):
Post Event to the queue with MyPriority
Return success or failure
Function RunLEDService(Event):
Initialize ReturnEvent as ES_NO_EVENT (default: no errors)
Switch(CurrentState):
Case InitLState:
If Event.Type is ES_INIT:
Print "ES_INIT received in LED Service"
Case WAITING:
If Event.Type is ES_MESSAGE:
Switch(Event.Parameter):
Case ES_DISPLAY_SCORE:
Clear the display buffer
Add "Score: 00" to the display buffer
Post an ES_UPDATING event
Transition to UPDATING
Case ES_UPDATING_SCORE:
Increment score
Clear the display buffer
Add "Score:" followed by updated score to the display buffer
Post an ES_UPDATING event
Transition to UPDATING
Case ES_MESSAGE:
Print "Game Timed Out"
Post an ES_PRINT event with "Game Timed Out"
Case ES_INSERT2:
Print "Insert 2 Coins..."
Post an ES_PRINT event with "Insert 2 Coins..."
Case ES_INSERT1:
Print "Insert 1 Coin..."
Post an ES_PRINT event with "Insert 1 Coin..."
Case ES_CONGRATS:
Clear the display buffer
Print "Great job!!!"
Post an ES_PRINT event with "Great job!!!"
Case ES_DEFEAT:
Clear the display buffer
Print "I've seen better..."
Post an ES_PRINT event with "I've seen better..."
Default:
Scroll the display buffer by 4 columns
Add the Event.Parameter to the display buffer
Post an ES_UPDATING event
Transition to UPDATING
Case UPDATING:
If Event.Type is ES_UPDATING:
If the display update step is incomplete:
Post another ES_UPDATING event
Else:
Transition to WAITING
Default:
Do nothing (no action for undefined states)
Return ReturnEvent
Function QueryLEDService():
Return CurrentState