SPI.c

void debug_printActiveBeacons(void)

      prints ActivatedBeacons array

      prints curBeaconIndex

 

void SPI_skipCurTarget(void)

      increments the curBeaconIndex in ActivatedBeacons array

 

char SPI_getCurTarget(void)

      returns the current active target from the index in the ActivatedBeacons array

 

char SPI_isCurTargetHit(void)

      returns TRUE or FALSE from variable isCurBeaconHit (changed in function SPI_getStatus)

 

void SPI_requestTargetStatus(void)

      set requestedTargetStatus = TRUE

      set unreadTargetStatus = FALSE

     

void SPI_Init(void)

      Start MyTimer_Init

      Set Baud rate to 12.2kHz

      Initialize CPOL and CPHA bits in SPICR1

      Enable master

      Enable SPI

      Set MSB transfer first

      Set SS to SlaveSelectOutput with SSOE and MODFEN

     

      Set lastCmd = 0xAA

      Set receivedByte = 0xDE;

     

      Start LED blinking to flash between the red and green LED

 

      Set lastActiveBeaconIndex = 0

      Set curBeaconIndex = 0

      Set isCurBeaconHit to FALSE

 

unsigned char firstLetter(unsigned char inByte)

      Determine the firstLetter of the receivedByte by dividing by 16

 

unsigned char lastLetter(unsigned char inByte)

      Determine the lastLetter of the receivedByte by mod 16

 

void WriteByte(unsigned char myByte)

      If currently writing a good byte (curWriteGood = TRUE)

            Check the read flag

            Read the good received byte

            After 11ms

                  Check the write flag

                  Write out the good byte to SPI

                  Set curWriteGood to FALSE

      Else if currently writing a bad byte

            Check the read flag

            Read the bad byte into a temp variable

            After 11ms

                  Check the write flag

                  Write out bad byte to SPI

                  Set curWriteGood to TRUE

 

 

SPI_Status_t SPI_getStatus(void)

      Switch state:

            GAME OVER:

                  Return GAME OVER

            ERROR:

                  Return ERROR

            DELAY

                  Send 0xAA into WriteByte until response is not 0xDE

            READY

                  WriteByte (either 0xAA or 0xEx)

                  If receivedByte = 0xFB “busy”, do nothing

                  Else

                        Switch state:

                              C = active beacon

                                    If a new target is assigned

                                          Increment lastActiveBeaconIndex

                                          Add the new target number into ActivatedBeacons array

                                    Set state to READY

                              D = delay or game over

                                    If lastLetter = E, state is DELAY

                                    Else, state is GAME OVER

                              5 or 6 = beacon is active

                                    Set isCurBeaconHit to FALSE and not hit

                                    Set requestedTargetStatus to FALSE

                                    Set unreadTargetStatus to TRUE

                              7 or 8 = beacon has been hit

                                    Set requestedTargetStatus to FALSE

                                    Set unreadTargetStatus to TRUE

                                    Set isCurBeaconHit to TRUE and hit

                                    Increment curBeaconIndex to get the next activated beacon

                                    Set state to READY

                              F

                                    Set state to ERROR

                       

                        If requestedTargetStatus is TRUE (when set in Main after hitting the beacon)

                              Send 0xEx to TC

                        Else, send 0xAA to TC

      If state is READY and unreadTargetStatus is TRUE, return READY

      Else, return the state