/*******************************************************************************************************************
* This function gets the entire Weather Struct from the slave weather station.
* Needs:
* The buffer pointer to put the incoming data in (WeatherDataStruct).
* void* ptr
*
* Returns:
* A code which tells whether of not the data packet was recieved correctly.
* Return Code:
* GOOD_PACKET = 0
* BAD_PACKET = 1
* PACKET_NOT_FOUND = 2
* BAD_CRC = 3
* BAD_ACK = 4
* BAD_COMMAND = 5
* BAD_PREFIX_CHAR = 6
* BAD_SUFFIX_CHAR = 7
* BAD_DATA_SIZE = 8
*******************************************************************************************************************/
packetStatus getDataFromSlave( void *inputDataPointer)
{
uint8_t CRC1, CRC2;
uint16_t inputCRC;
uint8_t i, j;
digitalWrite(13, HIGH); // This turns on the LED.
#if defined (DEBUG17)
Serial.print(F("\nThis is the getDataFromSlave Sub. This is the size of the data Returned From Master: "));
Serial.println(numberOfBytesFromDMAReceive);
Serial.println(F("Printing the data in the receiveBuffer Buffer..."));
printBuffer(receiveBuffer, RxBUFSIZE);
#endif
i = 0; // Start with the Pointer to the input data array at zero.
j = 0; // Start with the pointer to the receiveBuffer array at zero.
#if defined (DEBUG17)
Serial.println(F("Begin transferring bytes from transmit buffer to local buffer..."));
Serial.print(F("This is the value of j: "));
Serial.println(j, DEC);
Serial.println(F("Read the prefix character from the transmit Buffer OK..."));
Serial.print(F("This is the value of the prefix character: "));
Serial.println( ( receiveBuffer[j] ), DEC);
#endif
// Receive the data from the slave.
// The recieved packet is in the receiveBuffer, (Stx), (Command), (Size of Data Packet), (Data...), (CRC1),(CRC2), (Etx).
// It needs to be parsed for a command byte and good data.
if( ( receiveBuffer[j] ) != (byte)prefixChar ) // Check for the correct Prefix Character.
{
// Prefix character is not found
#if defined (DEBUG17)
Serial.println(F("Prefix character is not found!\n"));
#endif
digitalWrite(13, LOW); // This turns off the LED.
return BAD_PACKET;
}
else
{
j++; // increment the pointer to the receiveBuffer array.
#if defined (DEBUG17)
Serial.println(F("Reading the receive Command from the transmit Buffer..."));
Serial.print(F("This is the value of the Command Code: "));
Serial.println( ( receiveBuffer[j] ), DEC);
Serial.print(F("This is the value of j: "));
Serial.println(j, DEC);
#endif
receiveCommand = (commandTypes)receiveBuffer[j]; // This is the number of the command recieved from the master to execute.
if ( !(receiveCommand >= 0 ) && ( receiveCommand < maxCommandNumber ) )
{
#if defined (DEBUG17)
Serial.println(F("Receive Command is bad!\n"));
#endif
digitalWrite(13, LOW); // This turns off the LED.
return BAD_COMMAND;
}
j++; // increment the pointer to the receiveBuffer array.
#if defined (DEBUG17)
Serial.println(F("Read the receive Command from the transmit Buffer OK..."));
Serial.print(F("This is the value of j: "));
Serial.println(j, DEC);
Serial.print(F("This is the size of the Command Data Packet: "));
Serial.println( ( receiveBuffer[j] ), DEC);
#endif
receivedDataSize = receiveBuffer[j]; // This is the size of the command data packet (<= 127 bytes).
if ( receivedDataSize > maxRxDataBufferSize )
{
#if defined (DEBUG17)
Serial.println(F("Receive Data Size is more than the buffer size!\n"));
#endif
digitalWrite(13, LOW); // This turns off the LED.
return BAD_DATA_SIZE;
}
j++; // increment the pointer to the receiveBuffer array.
uint8_t data[receivedDataSize]; // Create a tmp char array of the data from the Uart.
#if defined (DEBUG17)
Serial.println(F("Read the receive Data Size from the transmit Buffer OK..."));
Serial.print(F("This is the value of receivedDataSize: "));
Serial.println(receivedDataSize, DEC);
Serial.print(F("This is the value of j: "));
Serial.println(j, DEC);
#endif
for( i = 0; i < ( receivedDataSize ); i++)
{
data[i] = (uint8_t)receiveBuffer[j]; // Copy the data from the receive Buffer to the temp byte array.
j++; // increment the pointer to the receiveBuffer array.
}
// Need to get the 2 byte CRC from the next two bytes in the receiveBuffer (DMA data from Uart2).
/// Need to do the CRC check on these two characters.
#if defined (DEBUG17)
Serial.println(F("Transfered the data from the transmit buffer to the temp array..."));
Serial.print(F("This is the data in the buffer: "));
printBuffer(data, receivedDataSize);
Serial.print(F("\nThis is the value of j: "));
Serial.println(j, DEC);
#endif
/// Need to move the pointer to the first CRC byte all the way past the 121 byte data packet.
j = j + ( maxRxDataBufferSize - receivedDataSize );
#if defined (DEBUG17)
Serial.println(F("Moved the j pointer to the end of the data packet..."));
Serial.print(F("This is the value of j: "));
Serial.println(j, DEC);
#endif
CRC1 = (uint8_t)receiveBuffer[j];
j++; // increment the pointer to the receiveBuffer array.
#if defined (DEBUG17)
Serial.print(F("This is the CRC1 byte from the receiveBuffer: "));
Serial.println(CRC1, HEX);
Serial.print(F("This is the value of j: "));
Serial.println(j, DEC);
#endif
CRC2 = (uint8_t)receiveBuffer[j];
#if defined (DEBUG22)
Serial.print(F("This is the CRC2 byte from the receiveBuffer: "));
Serial.println(CRC2, HEX);
#endif
/// Convert two 8 bit bytes to a 16 bit word.
inputCRC = 0; // Start out with zero
inputCRC = ( ( uint16_t)CRC2 << 8); // This is the high byte, shift left 8 places and put into the 16 bit inputCRC variable.
inputCRC = (inputCRC | ( uint16_t)CRC1);
#if defined (DEBUG17)
Serial.print(F("This is the returned CRC word: "));
Serial.println(inputCRC, HEX);
#endif
// bool crcCheck(void * inputBuffer, uint16_t bufferSize , uint16_t inputCRC)
if ( crcCheck(data, receivedDataSize, inputCRC) == false )
{
// The CRC recieved was bad.
#if defined (DEBUG17)
Serial.println(F("The CRC recieved was bad!\n"));
#endif
digitalWrite(13, LOW); // This turns off the LED.
return BAD_CRC;
}
else
{
j++; // increment the pointer to the receiveBuffer array.
#if defined (DEBUG17)
Serial.print(F("This is the value of j: "));
Serial.println(j, DEC);
#endif
if( receiveBuffer[j] != suffixChar)
{
// Suffix character is not found
#if defined (DEBUG17)
Serial.println(F("Suffix character is Bad!\n"));
#endif
digitalWrite(13, LOW); // This turns off the LED.
return BAD_SUFFIX_CHAR;
}
else
{
#if defined (DEBUG17)
Serial.println(F("Coping the bytes into the struct passed in!"));
#endif
memcpy(inputDataPointer, data, receivedDataSize); // Copy the bytes from the receiveBuffer (DMA data from Uart2) into the dataReturnedFromMaster Struct.
#if defined (DEBUG17)
Serial.println(F("Good Packet found!\n"));
#endif
digitalWrite(13, LOW); // This turns off the LED.
return GOOD_PACKET;
}
}
}
}