Professional Documents
Culture Documents
Spi Leader Pseudocode
Spi Leader Pseudocode
Enum spi_comms
InitSPILeaderService
Takes a priority number, returns True.
Sets Mypriority
Enable Interrupts
Turn SPI on
End of InitSPILeaderService
PostSPILeaderService
Takes an ES_Event_t, returns boolean outcome of ES_PostToService
End of PostSPILeaderService
PIC_to_Send_to
Given a PIC pin, return nothing
if FOLLOWER_PIN
lower the follower SS pin
Set peripheral_request to false
else
lower the peripheral pin
Set peripheral_request to true
RunSPILeaderService
The EventType field of ThisEvent will be one of: ES_INIT,
GAME_STARTED, ES_ALIGN_QUERY, ES_FIRE, ES_INDEX_OPEN, ES_INDEX_CLOSE,
ES_FIRE_QUERY, ES_LIMIT_QUERY, GE_FOLLOWER_RESPONSE,
GET_PERIPHERAL_RESPONSE, ES_LAUNCH_OFF, ES_LAUNCH_20, ES_LAUNCH_40,
ES_LAUNCH_60, ES_LAUNCH_80, ES_LAUNCH_100, GAME_ENDED
CurrentState is:
InitPState
ThisEvent is ES_INIT
Set CurrentState to Idle
Idle
ThisEvent is GAME_STARTED
Set PIC_to_Send_to to PERIPHERAL_PIN
Write GAME_START to SPI1BUF
ThisEvent is ES_ALIGN_QUERY
Set PIC_to_Send_to to FOLLOWER_PIN
Set query_sent to true
Write ALIGN_QUERY to SPI1BUF
ThisEvent is ES_FIRE
Set PIC_to_Send_to to PERIPHERAL_PIN
Write FIRE to SPI1BUF
ThisEvent is ES_INDEX_OPEN
Set PIC_to_Send_to to PERIPHERAL_PIN
Write INDEX_OPEN to SPI1BUF
ThisEvent is ES_INDEX_CLOSE
Set PIC_to_Send_to to PERIPHERAL_PIN
Write INDEX_CLOSE to SPI1BUF
ThisEvent is ES_FIRE_QUERY:
Set PIC_to_Send_to to FOLLOWER_PIN
Set query_sent to true
Write FIRE_QUERY to SPI1BUF
ThisEvent is ES_LIMIT_QUERY:
Set PIC_to_Send_to to PERIPHERAL_PIN
Set query_sent to true
Write LIMIT_QUERY to SPI1BUF
ThisEvent is GET_FOLLOWER_RESPONSE:
Set PIC_to_Send_to to FOLLOWER_PIN
Write CLOCK_BITS to SPI1BUF
ThisEvent is GET_PERIPHERAL_RESPONSE:
Set PIC_to_Send_to to PERIPHERAL_PIN
Write CLOCK_BITS to SPI1BUF
ThisEvent is ES_LAUNCH_OFF:
Set PIC_to_Send_to to FOLLOWER_PIN
Write LAUNCH_OFF to SPI1BUF
ThisEvent is ES_LAUNCH_20:
Set PIC_to_Send_to to FOLLOWER_PIN
Write LAUNCH_20 to SPI1BUF
ThisEvent is ES_LAUNCH_40:
Set PIC_to_Send_to to FOLLOWER_PIN
Write LAUNCH_40 to SPI1BUF
ThisEvent is ES_LAUNCH_60:
Set PIC_to_Send_to to FOLLOWER_PIN
Write LAUNCH_60 to SPI1BUF
ThisEvent is ES_LAUNCH_80:
Set PIC_to_Send_to to FOLLOWER_PIN
Write LAUNCH_80 to SPI1BUF
ThisEvent is ES_LAUNCH_100:
Set PIC_to_Send_to to FOLLOWER_PIN
Write LAUNCH_100 to SPI1BUF
ThisEvent is GAME_ENDED:
Set PIC_to_Send_to to PERIPHERAL_PIN
Write GAME_END to SPI1BUF
Return ES_NO_EVENT
End of RunSPILeaderService
if query_requested
if not a peripheral_request and the response is that robot is
aligned
Set NewEvent to AR1_ALIGN
Post AR1_ALIGN to LineFollowingSM
else if not peripheral_request
Set NewEvent to NOT_ALIGNED
Post NOT_ALIGNED to LineFollowingSM
else if peripheral_request and the response is that limit
switch is pressed
Set NewEvent to LIMIT_CHANGE
Set NewEvent.EventParam to 1
else if peripheral_request and the response is that limit
switch is unpressed
Set NewEvent to LIMIT_CHANGE
Set NewEvent.EventParam to 0
End of SPIReceiveInterrupt