code cleanup and format

master
JbLb 6 years ago
parent f2bdcc8d76
commit 4e73f59bfd

@ -15,10 +15,10 @@
// by JbLb // by JbLb
// • Converted to platformio // • Converted to platformio
// • Adapted to Arduino leonardo to use CDC serial interface // • Adapted to Arduino leonardo to use CDC serial interface
// • Serial1 connected to hoverboard board // • Serial1 connected to hoverboard board
// • CDC used to display data on serial monitor // • CDC used to display data on serial monitor
// //
// CONFIGURATION on the hoverboard side in config.h: // CONFIGURATION on the hoverboard side in config.h:
// • Option 1: Serial on Left Sensor cable (long wired cable) // • Option 1: Serial on Left Sensor cable (long wired cable)
// #define CONTROL_SERIAL_USART2 // #define CONTROL_SERIAL_USART2
@ -34,7 +34,7 @@
#define HOVER_SERIAL_BAUD 38400 // [-] Baud rate for HoverSerial (used to communicate with the hoverboard) #define HOVER_SERIAL_BAUD 38400 // [-] Baud rate for HoverSerial (used to communicate with the hoverboard)
#define SERIAL_BAUD 115200 // [-] Baud rate for built-in Serial (used for the Serial Monitor) #define SERIAL_BAUD 115200 // [-] Baud rate for built-in Serial (used for the Serial Monitor)
#define START_FRAME 0xAAAA // [-] Start frme definition for reliable serial communication #define START_FRAME 0xAAAA // [-] Start frme definition for reliable serial communication
#define TIME_SEND 100 // [ms] Sending time interval #define TIME_SEND 1000 // [ms] Sending time interval
#define SPEED_MAX_TEST 300 // [-] Maximum speed for testing #define SPEED_MAX_TEST 300 // [-] Maximum speed for testing
//#define DEBUG_RX // [-] Debug received data. Prints all bytes to serial (comment-out to disable) //#define DEBUG_RX // [-] Debug received data. Prints all bytes to serial (comment-out to disable)
@ -80,11 +80,10 @@ SerialFeedback NewFeedback;
#define SCREEN_HEIGHT 64 // OLED display height, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// ########################## SETUP ########################## // ########################## SETUP ##########################
void setup() void setup()
{ {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x64) display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x64)
@ -98,11 +97,11 @@ void setup()
display.println("v1.0"); display.println("v1.0");
display.display(); display.display();
Serial1.begin(HOVER_SERIAL_BAUD); // RX, TX from arduino to TX RX on hoverboard board. ! be carreful 3v3 Serial1.begin(HOVER_SERIAL_BAUD); // RX, TX from arduino to TX RX on hoverboard board. ! be carreful 3v3
pinMode(LED_BUILTIN, OUTPUT); pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(SERIAL_BAUD); Serial.begin(SERIAL_BAUD);
if (Serial) { if (Serial) {
Serial.println("Hoverboard Serial v1.0"); Serial.println("Hoverboard Serial v1.0");
} }
} }
@ -110,14 +109,14 @@ void setup()
// ########################## SEND ########################## // ########################## SEND ##########################
void Send(int16_t uSteer, int16_t uSpeed) void Send(int16_t uSteer, int16_t uSpeed)
{ {
// Create command // Create command
Command.start = (uint16_t)START_FRAME; Command.start = (uint16_t)START_FRAME;
Command.steer = (int16_t)uSteer; Command.steer = (int16_t)uSteer;
Command.speed = (int16_t)uSpeed; Command.speed = (int16_t)uSpeed;
Command.checksum = (uint16_t)(Command.start ^ Command.steer ^ Command.speed); Command.checksum = (uint16_t)(Command.start ^ Command.steer ^ Command.speed);
// Write to Serial // Write to Serial
Serial1.write((uint8_t *) &Command, sizeof(Command)); Serial1.write((uint8_t *) &Command, sizeof(Command));
} }
// ########################## RECEIVE ########################## // ########################## RECEIVE ##########################
@ -126,7 +125,7 @@ void Receive()
// Check for new data availability in the Serial buffer // Check for new data availability in the Serial buffer
if (Serial1.available()) { if (Serial1.available()) {
incomingByte = Serial1.read(); // Read the incoming byte incomingByte = Serial1.read(); // Read the incoming byte
bufStartFrame = ((uint16_t)(incomingBytePrev) << 8) + incomingByte; // Construct the start frame bufStartFrame = ((uint16_t)(incomingBytePrev) << 8) + incomingByte; // Construct the start frame
} }
else { else {
return; return;
@ -136,30 +135,30 @@ void Receive()
#ifdef DEBUG_RX #ifdef DEBUG_RX
Serial.print(incomingByte); Serial.print(incomingByte);
return; return;
#endif #endif
// Copy received data // Copy received data
if (bufStartFrame == START_FRAME) { // Initialize if new data is detected if (bufStartFrame == START_FRAME) { // Initialize if new data is detected
p = (byte *)&NewFeedback; p = (byte *)&NewFeedback;
*p++ = incomingBytePrev; *p++ = incomingBytePrev;
*p++ = incomingByte; *p++ = incomingByte;
idx = 2; idx = 2;
} else if (idx >= 2 && idx < sizeof(SerialFeedback)) { // Save the new received data } else if (idx >= 2 && idx < sizeof(SerialFeedback)) { // Save the new received data
*p++ = incomingByte; *p++ = incomingByte;
idx++; idx++;
} }
// Check if we reached the end of the package // Check if we reached the end of the package
if (idx == sizeof(SerialFeedback)) { if (idx == sizeof(SerialFeedback)) {
uint16_t checksum; uint16_t checksum;
checksum = (uint16_t)(NewFeedback.start ^ NewFeedback.cmd1 ^ NewFeedback.cmd2 ^ NewFeedback.speedR ^ NewFeedback.speedL checksum = (uint16_t)(NewFeedback.start ^ NewFeedback.cmd1 ^ NewFeedback.cmd2 ^ NewFeedback.speedR ^ NewFeedback.speedL
^ NewFeedback.speedR_meas ^ NewFeedback.speedL_meas ^ NewFeedback.batVoltage ^ NewFeedback.boardTemp); ^ NewFeedback.speedR_meas ^ NewFeedback.speedL_meas ^ NewFeedback.batVoltage ^ NewFeedback.boardTemp);
// Check validity of the new data // Check validity of the new data
if (NewFeedback.start == START_FRAME && checksum == NewFeedback.checksum) { if (NewFeedback.start == START_FRAME && checksum == NewFeedback.checksum) {
// Copy the new data // Copy the new data
memcpy(&Feedback, &NewFeedback, sizeof(SerialFeedback)); memcpy(&Feedback, &NewFeedback, sizeof(SerialFeedback));
// Print data to CDC Serial if available // Print data to CDC Serial if available
if (Serial) { if (Serial) {
Serial.print("1: "); Serial.print(Feedback.cmd1); Serial.print("1: "); Serial.print(Feedback.cmd1);
@ -174,10 +173,10 @@ void Receive()
} else { } else {
if (Serial) Serial.println("Non-valid data skipped"); if (Serial) Serial.println("Non-valid data skipped");
} }
idx = 0; // Reset the index (it prevents to enter in this if condition in the next cycle) idx = 0; // Reset the index (it prevents to enter in this if condition in the next cycle)
} }
// Update previous states // Update previous states
incomingBytePrev = incomingByte; incomingBytePrev = incomingByte;
} }

Loading…
Cancel
Save