Offensive Coordinator Pseudocode
Offensive Coordinator State Machine Function: OC_AddCommand Parameters: unsigned char Command, unsigned char Priority Returns: unsigned char 1 on successful add, 0 on failure If priority is 0 (default) If queue is not full Add command to end of queue Increment queue length variable If priority is not 0 Insert command into queue at spot Priority Increment queue length variable, if appropriate Function: OC_WriteCommand Parameter: unsigned char Command Read SPISR register to make sure last byte was written Write command to SPISR register Function: StartOffensiveCoordinatorSM Initialize SPI interface Initialize output compare for timing intervals between commands - don't enable interrupt yet Function: RunOffensiveCoordinatorSM State Ready: Run during function for Ready Event: New Command Pull command from queue Write new command Decrement queue length variable Transition to InProgress state State InProgress: Run during function for InProgress Event: New read from OC Set flag indicating we have a new read Event: OC timer expired Write a dummy byte to the OC, as required Transition to Pending state State Pending: Run during function for Pending Event: New read from OC Set flag indicating we have a new read Event: OC timer expired Response: LastRequestPending Write "query status of last request" Transition to InProgress state Response: DispenserQueryPending Rewrite last command, as required Transition to InProgress state Response: Anything else Set flag to indicate we have a response from the field Store command and response for external access Transition to Ready state Event: OC counter expired (we have tried a command too many times) Set flag to indicate we have a response Store command and most recent OC response for external access Transition to Ready state Function: DuringReady (dummy function) Function: DuringInProgress Entry: Set command interval timer Enable interrupt for timer Exit: Disable interrupt for timer Function: DuringPending Entry: Set command interval timer Enable interrupt for timer Exit: Disable interrupt for timer Interrupt: OC_Timer when output compare triggered, Set timer expired flag variable Clear interrupt flag Interrupt: SPI_Interrupts check for new read Read SPIDR register and store it Set flag indicating new read