Answered step by step
Verified Expert Solution
Question
1 Approved Answer
1. If a properly formatted packet with a 4 byte payload is received, AND the rst byte of the payload is L (0x4C in ASCII
1. If a properly formatted packet with a 4 byte payload is received, AND the rst byte of the payload is L (0x4C in ASCII code), then assign payload[1], payload[2], and payload[3] to the Red, Green, and Blue channels of the LED respectively. For example, the following packet should shine the LED in full red, with no Green or Blue light present. 0xAA 0x07 0x4C 0xFF 0x00 0x00 1E Do not send a response packet in this case. 1. If a properly formatted packet with a single byte payload is received, AND the byte is P (0x50 in ASCII code), then read the potentiometer value and scale to a single byte (analogRead in Arduino will return the value in the range [0-1024], so just divide it by 4). Once you have read and scaled the value, send a properly formatted response packet in the following form... 0xAA 0x05 0x50 [pot value] [checksum] Demonstrate your program with CuteCom using the test cases provided by the lab instructors | |
/>. | |
// | |
/*********************************************************************************************************************** | |
* @FILE serial_communication_variable.ino | |
* @BRIEF An example Arduino sketch showing USB-serial communications with the Teensy microcontroller | |
* | |
* This program provides an example of USB-serial communications with the Teensy 3.1/3.2 microcontroller. The communication | |
* is based on variable width byte packets containing an error checksum. The packet structure is defined as follows: | |
* | |
* packet[0] = PACKET_START_BYTE (0xAA) | |
* packet[1] = PACKET_SIZE (total number of bytes including overhead and payload) | |
* packet[n+2] = payload byte n -> [0, PAYLOAD_SIZE - 1] | |
* packet[PACKET_SIZE - 1] = packet checksum | |
* | |
* The checksum is computed as the XOR chain of each byte in the packet before the checksum: | |
* packet[0] XOR packet[1] XOR ... XOR packet[PACKET_SIZE - 2] | |
* | |
* @AUTHOR Christopher D. McMurrough | |
**********************************************************************************************************************/ | |
// define GPIO pins | |
const int LED_PIN = 13; | |
const int LED_ON = HIGH; | |
const int LED_OFF = LOW; | |
// define serial communication parameters | |
const unsigned long BAUD_RATE = 9600; | |
// define packet parameters | |
const byte PACKET_START_BYTE = 0xAA; | |
const unsigned int PACKET_OVERHEAD_BYTES = 3; | |
const unsigned int PACKET_MIN_BYTES = PACKET_OVERHEAD_BYTES + 1; | |
const unsigned int PACKET_MAX_BYTES = 255; | |
/*********************************************************************************************************************** | |
* @BRIEF perform initial setup of the microcontroller | |
* @AUTHOR Christoper D. McMurrough | |
**********************************************************************************************************************/ | |
void setup() | |
{ | |
// initialize the IO pins | |
pinMode(LED_PIN, OUTPUT); | |
// initialize the serial port | |
Serial.begin(BAUD_RATE); | |
// flash the LED state | |
for(int i = 0; i < 25; i++) | |
{ | |
digitalWrite(LED_PIN, LED_ON); | |
delay(50); | |
digitalWrite(LED_PIN, LED_OFF); | |
delay(50); | |
} | |
} | |
/*********************************************************************************************************************** | |
* @BRIEF assembles and transmits a serial packet containing the given payload | |
* @PARAM[in] payloadSize the size of the given payload in bytes | |
* @PARAM[in] payload pointer to the data payload array | |
* @RETURN true if the packet was transmitted successfully | |
* @AUTHOR Christoper D. McMurrough | |
**********************************************************************************************************************/ | |
boolean sendPacket(unsigned int payloadSize, byte *payload) | |
{ | |
// check for max payload size | |
unsigned int packetSize = payloadSize + PACKET_OVERHEAD_BYTES; | |
if(packetSize > PACKET_MAX_BYTES) | |
{ | |
return false; | |
} | |
// create the serial packet transmit buffer | |
static byte packet[PACKET_MAX_BYTES]; | |
// populate the overhead fields | |
packet[0] = PACKET_START_BYTE; | |
packet[1] = packetSize; | |
byte checkSum = packet[0] ^ packet[1]; | |
// populate the packet payload while computing the checksum | |
for(int i = 0; i < payloadSize; i++) | |
{ | |
packet[i + 2] = payload[i]; | |
checkSum = checkSum ^ packet[i + 2]; | |
} | |
// store the checksum | |
packet[packetSize - 1] = checkSum; | |
// send the packet | |
Serial.write(packet, packetSize); | |
Serial.flush(); | |
return true; | |
} | |
/*********************************************************************************************************************** | |
* @BRIEF checks to see if the given packet is complete and valid | |
* @PARAM[in] packetSize the size of the given packet buffer in bytes | |
* @PARAM[in] packet pointer to the packet buffer | |
* @RETURN true if the packet is valid | |
* @AUTHOR Christoper D. McMurrough | |
**********************************************************************************************************************/ | |
boolean validatePacket(unsigned int packetSize, byte *packet) | |
{ | |
// check the packet size | |
if(packetSize < PACKET_MIN_BYTES || packetSize > PACKET_MAX_BYTES) | |
{ | |
return false; | |
} | |
// check the start byte | |
if(packet[0] != PACKET_START_BYTE) | |
{ | |
return false; | |
} | |
// check the length byte | |
if(packet[1] != packetSize) | |
{ | |
return false; | |
} | |
// compute the checksum | |
byte checksum = 0x00; | |
for(int i = 0; i < packetSize - 1; i++) | |
{ | |
checksum = checksum ^ packet[i]; | |
} | |
// check to see if the computed checksum and packet checksum are equal | |
if(packet[packetSize - 1] != checksum) | |
{ | |
return false; | |
} | |
// all validation checks passed, the packet is valid | |
return true; | |
} | |
/*********************************************************************************************************************** | |
* @BRIEF main program loop | |
* @AUTHOR Christoper D. McMurrough | |
**********************************************************************************************************************/ | |
void loop() | |
{ | |
// define control variables | |
boolean isRunning = true; | |
boolean ledState = false; | |
// create the serial packet receive buffer | |
static byte buffer[PACKET_MAX_BYTES]; | |
int count = 0; | |
int packetSize = PACKET_MIN_BYTES; | |
// continuously check for received packets | |
while(isRunning) | |
{ | |
// check to see if serial byte is available | |
if(Serial.available()) | |
{ | |
// get the byte | |
byte b = Serial.read(); | |
// handle the byte according to the current count | |
if(count == 0 && b == PACKET_START_BYTE) | |
{ | |
// this byte signals the beginning of a new packet | |
buffer[count] = b; | |
count++; | |
continue; | |
} | |
else if(count == 0) | |
{ | |
// the first byte is not valid, ignore it and continue | |
continue; | |
} | |
else if(count == 1) | |
{ | |
// this byte contains the overall packet length | |
buffer[count] = b; | |
// reset the count if the packet length is not in range | |
if(packetSize < PACKET_MIN_BYTES || packetSize > PACKET_MAX_BYTES) | |
{ | |
count = 0; | |
} | |
else | |
{ | |
packetSize = b; | |
count++; | |
} | |
continue; | |
} | |
else if(count < packetSize) | |
{ | |
// store the byte | |
buffer[count] = b; | |
count++; | |
} | |
// check to see if we have acquired enough bytes for a full packet | |
if(count >= packetSize) | |
{ | |
// validate the packet | |
if(validatePacket(packetSize, buffer)) | |
{ | |
// change the LED state if the packet is valid | |
ledState = !ledState; | |
digitalWrite(LED_PIN, ledState); | |
// echo back the received packet payload | |
sendPacket(packetSize - PACKET_OVERHEAD_BYTES, buffer + 2); | |
} | |
// reset the count | |
count = 0; | |
} | |
} | |
} | |
}
|
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started