FACComm
Structure – coord
Has x, y, r, active, sector (all ints)
Global variables-
CommState_t CurrentState
Char msg (Current message being sent)
Coord coordinates[40] (Data structure to keep track of the coordinates of each ship/bot)
Int blueShips, redShips
Int updateAll, active
Boolean blueDisable, redDisable
StartCommSM
Takes ES_Event CurrentEvent and char sendMsg (sendMsg is the message we want to send and get information back from)
If active is 0 (active is 1 if the comm state is currently going)
Set msg to sendMsg, and set timer 0 to 2 ms
Set CurrentState to Sending, and active to 1
RunCommSM with ES_ENTRY
RunCommSM
State machine –
State Sending
On Entry, set active to 1
If it is Timer 0 timing out,
Pull slave select line low
QueryCurrentCommand
If SPIF is set
Set recMsg to readMessage’s last 8 bits
Change states to WaitFirst
State WaitFirst
On Entry, QueryForResponse
If SPIF is set
Set recMsg to readMessage’s last 8 bits
Change states to WaitSecond
State WaitSecond
On Entry, QueryForResponse
If SPIF is set
Set recMsg to readMessage’s last 8 bits
If we are querying a ship or a bot and not the game status
Set coordinates of last 8 bits and with 0x3F’s x coordinate to recMsg
Change states to WaitThird
State WaitThird
On Entry, QueryForResponse
If SPIF is set
Set recMsg to readMessage’s last 8 bits
If we are querying a ship or a bot and not the game status
Set coordinates of last 8 bits and with 0x3F’y coordinate to recMsg
Change states to FACEnd
State FACEnd
On Entry, QueryForResponse
If SPIF is set
Set recMsg to readMessage’s last 8 bits
If we are querying the game status
Set redDisable and blueDisable depending on recMsg’s BIT6 and BIT7
Take the last 4 bits and if the last 4 bits are:
0x0A – if currState != 1 and MasterSM’s current state is WaitForStart,
Send MasterSM GameStart, and set currState = 1
0x0B or 0x0C – if currState != 2, send MasterSM GameEnd
Else, for the number ship we are querying
Set the ship’s r to recMsg
If the ship’s x, y, and r are all 0, set active to 0, else set active to 1
Place in the proper sector –
4, 5, and 6 are past x = 190 for blue, before x = 64 for red
1 is the middle triangle, 2 is the right, 3 is the left, 4, 5, and 6 follow the same pattern
Call UpdateCounts
If updateAll is 1 and we were querying the game status
Start the updateAll by setting msg to 0xC3, set timer 0 for 2 ms
Change state to Sending
Else if updateAll is 1 and we are in the middle of the process (msg is not 0x3F)
Increment msg, set timer 0 for 2ms
Change state to Sending
Else if updateAll is 1 and we went through all of them (msg is now 0xE7)
Post UpdateFinish to MasterSM, and set updateAll to 0
Pull up slave select line and set active to 0 for the next message
QueryCurrentCommand
If SPTEF flag is set, set SPIDR to msg
QueryForResponse
If SPTEF flag is set, set SPIDR to 0x00
readMessage
if SPIF flag is set, return inputMessage after setting it to SPIDR
updateCounts
Set redShipCount and blueShipCount to 0
Loop through ships 20-29 and increment redShipCount for each active ship
Loop through ships 30-39 and increment blueShipCount for each active ship
Set blueShips/redShips to blueShipCount, redShipCount
queryShip
Takes int i
Return coordinates[i]
queryRedShips
return redShips
queryBlueShips
return blueShips
queryActive
Takes int i
return FALSE if coordinates[i].active is 0
else return TRUE
updateFAC
Set updateAll to 1
If not active (active == 0)
Set msg to 0xC3
Set CurrentState to Sending, Timer 0 to 2 ms
RunCommSM with ES_ENTRY
getBlueDisable
return blueDisable
getRedDisable
return redDisable
Has x, y, r, active, sector (all ints)
Global variables-
CommState_t CurrentState
Char msg (Current message being sent)
Coord coordinates[40] (Data structure to keep track of the coordinates of each ship/bot)
Int blueShips, redShips
Int updateAll, active
Boolean blueDisable, redDisable
StartCommSM
Takes ES_Event CurrentEvent and char sendMsg (sendMsg is the message we want to send and get information back from)
If active is 0 (active is 1 if the comm state is currently going)
Set msg to sendMsg, and set timer 0 to 2 ms
Set CurrentState to Sending, and active to 1
RunCommSM with ES_ENTRY
RunCommSM
State machine –
State Sending
On Entry, set active to 1
If it is Timer 0 timing out,
Pull slave select line low
QueryCurrentCommand
If SPIF is set
Set recMsg to readMessage’s last 8 bits
Change states to WaitFirst
State WaitFirst
On Entry, QueryForResponse
If SPIF is set
Set recMsg to readMessage’s last 8 bits
Change states to WaitSecond
State WaitSecond
On Entry, QueryForResponse
If SPIF is set
Set recMsg to readMessage’s last 8 bits
If we are querying a ship or a bot and not the game status
Set coordinates of last 8 bits and with 0x3F’s x coordinate to recMsg
Change states to WaitThird
State WaitThird
On Entry, QueryForResponse
If SPIF is set
Set recMsg to readMessage’s last 8 bits
If we are querying a ship or a bot and not the game status
Set coordinates of last 8 bits and with 0x3F’y coordinate to recMsg
Change states to FACEnd
State FACEnd
On Entry, QueryForResponse
If SPIF is set
Set recMsg to readMessage’s last 8 bits
If we are querying the game status
Set redDisable and blueDisable depending on recMsg’s BIT6 and BIT7
Take the last 4 bits and if the last 4 bits are:
0x0A – if currState != 1 and MasterSM’s current state is WaitForStart,
Send MasterSM GameStart, and set currState = 1
0x0B or 0x0C – if currState != 2, send MasterSM GameEnd
Else, for the number ship we are querying
Set the ship’s r to recMsg
If the ship’s x, y, and r are all 0, set active to 0, else set active to 1
Place in the proper sector –
4, 5, and 6 are past x = 190 for blue, before x = 64 for red
1 is the middle triangle, 2 is the right, 3 is the left, 4, 5, and 6 follow the same pattern
Call UpdateCounts
If updateAll is 1 and we were querying the game status
Start the updateAll by setting msg to 0xC3, set timer 0 for 2 ms
Change state to Sending
Else if updateAll is 1 and we are in the middle of the process (msg is not 0x3F)
Increment msg, set timer 0 for 2ms
Change state to Sending
Else if updateAll is 1 and we went through all of them (msg is now 0xE7)
Post UpdateFinish to MasterSM, and set updateAll to 0
Pull up slave select line and set active to 0 for the next message
QueryCurrentCommand
If SPTEF flag is set, set SPIDR to msg
QueryForResponse
If SPTEF flag is set, set SPIDR to 0x00
readMessage
if SPIF flag is set, return inputMessage after setting it to SPIDR
updateCounts
Set redShipCount and blueShipCount to 0
Loop through ships 20-29 and increment redShipCount for each active ship
Loop through ships 30-39 and increment blueShipCount for each active ship
Set blueShips/redShips to blueShipCount, redShipCount
queryShip
Takes int i
Return coordinates[i]
queryRedShips
return redShips
queryBlueShips
return blueShips
queryActive
Takes int i
return FALSE if coordinates[i].active is 0
else return TRUE
updateFAC
Set updateAll to 1
If not active (active == 0)
Set msg to 0xC3
Set CurrentState to Sending, Timer 0 to 2 ms
RunCommSM with ES_ENTRY
getBlueDisable
return blueDisable
getRedDisable
return redDisable