Pseudocode for FARMERTXmitSM.c Transmits data packets from the controller (FARMER) to the hovercraft (DOG) via UART. InitFARMERTXmitSM Save the priority of this service Initialize into the first state, WaitingToSendFirstByte Post the initial transiti on event end of InitFARMERTXmitSM RunFARMERTXmitSM switch on the CurrentState case WaitingToSendFirstByte state if the event type is ES_BEGIN_TXMIT Save the event param that describes the packet type If constructing the packet of specified type is successful Reset the index of number of bytes sent Set CurrentState to SendingAPacket call helper function to begin transmitting else constructing packet failed, do nothing end if end if break case SendingAPacket state if the event type is ES_ALL_BYTES_SENT Set CurrentState to WaitingToSendFirstByte else if event type is ES_LOST_CONNECTION Set CurrentState to WaitingToSendFirstByte end if break end switch on Current State return ReturnEvent end RunFARMERTXmitSM FARMERTXmitISR (Only respond to the interrupt that we care about: TXMIS) If transmit interrupts are unmasked and it fired Clear UART TXmit interrupt by setting TXIC in UARTICR (interrupt clear register) if we have sent all of the bytes Disable UART TXmit interrupt by clearing interrupt mask bit in UART mask reg Post ES_ALL_BYTES_SENT to TXmitSM else (we are not done sending bytes yet) Write the new data to data register UARTDR Increment BytesSentIndex end if else (TXMIS is not set, meaning it's not a transmit interrupt, so take no action) end if end FARMERTXmitISR ConstructPacket Constructs the packet that will be sent by filling out the TxPacket array with appropriate data If PacketType is REQ_2_PAIR Set PacketSize accordingly Set FrameDataSize accordingly Set destination address to 'Broadcast', because it's a request to pair Create XBee header Fill out RF data of TxPacket, not encrypted. Get number of DOG to pair with from the FARMER's pair selector (DOG selector) mechanism SetXBeeChecksum Return true because packet was created successfully else if PacketType is ENCR_KEY Set PacketSize accordingly Set FrameDataSize accordingly Get destination address from RX service Create XBee header Create the encryption key so that you have something to send Using the encryption key generated, fill out the RF data of this packet Create an index variable to track RF data byte that's being set to an encr byte start the 'for' loop at RF data start +1 because of the PacketType bit that's first looping over the encryption key that was sent in the RF Data, save each to TxPacket SetXBeeChecksum Reset the encryption index because it's a new pairing Return true because packet was created successfully else if PacketType is CTRL Set PacketSize accordingly Set FrameDataSize accordingly Get destination address from RX service Create XBee header Get Fwd/Back data from TiltService Get Left/Right data from TiltService Get Peripheral status and Brake status Fill out RF data of TxPacket, WITH ENCRYPTION TxPacket @ header location equals PacketType XOR'ed with EncryptKey @ encryption index Increment encryption index TxPacket @ header location+1 equals FwdRevCtrl XOR'ed with EncryptKey @ encryption index Increment encryption index TxPacket @ header location+2 equals LeftRightCtrl XOR'ed with EncryptKey @ encryption index Increment encryption index TxPacket @ header location+3 equals DigitalCtrl XOR'ed with EncryptKey @ encryption index Increment encryption index SetXBeeChecksum Return true because packet was created successfully else bad packet type, packet was not created. return false end if end ConstructPacket CreateXBeeHeader Creates the header, which is all the bytes before the RF data TxPacket[0] equals 0x7E TxPacket[1] equals length of MSB which is 0x00 TxPacket[2] equals length of LSB TxPacket[3] equals API_ID for a transmit request TxPacket[4] equals FRAME_ID which is arbitrary, not specified in comm protocol TxPacket[5] equals Destination Address MSB TxPacket[6] equals Destination Address LSB TxPacket[7] equals OPTIONS, which in our case is 0x00 end CreateXBeeHeader SetXBeeChecksum Calculates checksum and puts that value in last byte of TxPacket[] for each byte in the framing data Add current TxPacket to the sum end for loop Calculate XBeeChecksum by subtracting sum from 0xff Set the last byte in the TxPacket array to be the XBeeCheckSum that was just calculated end SetXBeeChecksum StartFARMERTXmit Kicks off transmission of a packet and will cause a UART Tx interrupt, which will be handled in FARMERTXmitISR if there is room to transfer a byte (Txmit Fifo Empty = TXFE is set) Write the new data to data register UARTDR Enable UART TXmit interrupt by setting interrupt mask bit in UART mask reg so that when the byte is done sending, it fires an interrupt Increment BytesSentIndex (goes from 0 to 1 here) Return true because first byte was successfully transmitted else (there is not room to transmit a byte because the transmit fifo is full) return false end if end StartFARMERTXmit CreateEncryptionKey Creates the 32-byte encryption key by generating 32 bytes randomly initialize a for loop to go from 0 to 31 Generate a random number between 0 - 255 for each byte in the encryption key end for loop end CreateEncryptionKey