From 4e73f59bfd7c20063b4de4d83631e7e58c18e3f1 Mon Sep 17 00:00:00 2001 From: JbLb Date: Fri, 3 Jan 2020 23:17:41 +0100 Subject: [PATCH] code cleanup and format --- src/hoverserial.ino | 59 ++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/src/hoverserial.ino b/src/hoverserial.ino index 88f8203..d4851e8 100644 --- a/src/hoverserial.ino +++ b/src/hoverserial.ino @@ -15,10 +15,10 @@ // by JbLb // • Converted to platformio // • 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 -// +// // CONFIGURATION on the hoverboard side in config.h: // • Option 1: Serial on Left Sensor cable (long wired cable) // #define CONTROL_SERIAL_USART2 @@ -34,7 +34,7 @@ #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 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 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 // 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, OLED_RESET); +Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire); // ########################## SETUP ########################## -void setup() +void setup() { 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.display(); - Serial1.begin(HOVER_SERIAL_BAUD); // RX, TX from arduino to TX RX on hoverboard board. ! be carreful 3v3 - pinMode(LED_BUILTIN, OUTPUT); + Serial1.begin(HOVER_SERIAL_BAUD); // RX, TX from arduino to TX RX on hoverboard board. ! be carreful 3v3 + pinMode(LED_BUILTIN, OUTPUT); Serial.begin(SERIAL_BAUD); - if (Serial) { + if (Serial) { Serial.println("Hoverboard Serial v1.0"); } } @@ -110,14 +109,14 @@ void setup() // ########################## SEND ########################## void Send(int16_t uSteer, int16_t uSpeed) { - // Create command - Command.start = (uint16_t)START_FRAME; - Command.steer = (int16_t)uSteer; - Command.speed = (int16_t)uSpeed; - Command.checksum = (uint16_t)(Command.start ^ Command.steer ^ Command.speed); - - // Write to Serial - Serial1.write((uint8_t *) &Command, sizeof(Command)); + // Create command + Command.start = (uint16_t)START_FRAME; + Command.steer = (int16_t)uSteer; + Command.speed = (int16_t)uSpeed; + Command.checksum = (uint16_t)(Command.start ^ Command.steer ^ Command.speed); + + // Write to Serial + Serial1.write((uint8_t *) &Command, sizeof(Command)); } // ########################## RECEIVE ########################## @@ -126,7 +125,7 @@ void Receive() // Check for new data availability in the Serial buffer if (Serial1.available()) { 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 { return; @@ -136,30 +135,30 @@ void Receive() #ifdef DEBUG_RX Serial.print(incomingByte); return; - #endif - + #endif + // Copy received data if (bufStartFrame == START_FRAME) { // Initialize if new data is detected p = (byte *)&NewFeedback; *p++ = incomingBytePrev; - *p++ = incomingByte; - idx = 2; + *p++ = incomingByte; + idx = 2; } else if (idx >= 2 && idx < sizeof(SerialFeedback)) { // Save the new received data - *p++ = incomingByte; + *p++ = incomingByte; idx++; - } - + } + // Check if we reached the end of the package - if (idx == sizeof(SerialFeedback)) { + if (idx == sizeof(SerialFeedback)) { uint16_t checksum; checksum = (uint16_t)(NewFeedback.start ^ NewFeedback.cmd1 ^ NewFeedback.cmd2 ^ NewFeedback.speedR ^ NewFeedback.speedL ^ NewFeedback.speedR_meas ^ NewFeedback.speedL_meas ^ NewFeedback.batVoltage ^ NewFeedback.boardTemp); - + // Check validity of the new data if (NewFeedback.start == START_FRAME && checksum == NewFeedback.checksum) { // Copy the new data memcpy(&Feedback, &NewFeedback, sizeof(SerialFeedback)); - + // Print data to CDC Serial if available if (Serial) { Serial.print("1: "); Serial.print(Feedback.cmd1); @@ -174,10 +173,10 @@ void Receive() } else { 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) } - + // Update previous states incomingBytePrev = incomingByte; }