diff --git a/platformio.ini b/platformio.ini index b48a97e..acbc971 100644 --- a/platformio.ini +++ b/platformio.ini @@ -12,4 +12,4 @@ board = leonardo framework = arduino platform = atmelavr - +upload_port = /dev/ttyACM* diff --git a/src/hoverserial.ino b/src/hoverserial.ino index 494ee11..7f8f64d 100644 --- a/src/hoverserial.ino +++ b/src/hoverserial.ino @@ -14,7 +14,9 @@ // // by JbLb // • Converted to platformio -// • Adapted to Arduino leonardo to use CDC serail interface +// • Adapted to Arduino leonardo to use CDC serial interface +// • Serial1 connected to hoverboard board +// • CDC used to display data on serial monitor // // CONFIGURATION on the hoverboard side in config.h: @@ -36,13 +38,11 @@ #define SPEED_MAX_TEST 300 // [-] Maximum speed for testing //#define DEBUG_RX // [-] Debug received data. Prints all bytes to serial (comment-out to disable) -#ifndef AVR_LEONARDO -#include -SoftwareSerial HoverSerial(2,3); // RX, TX -#endif +// #include +// SoftwareSerial HoverSerial(2,3); // RX, TX // Global variables -uint8_t idx = 0; // Index for new data pointer +uint8_t 1 idx = 0; // Index for new data pointer uint16_t bufStartFrame; // Buffer Start Frame byte *p; // Pointer declaration for the new received data byte incomingByte; @@ -75,9 +75,11 @@ SerialFeedback NewFeedback; void setup() { Serial.begin(SERIAL_BAUD); - Serial.println("Hoverboard Serial v1.0"); + if (Serial) { + Serial.println("Hoverboard Serial v1.0"); + } - HoverSerial.begin(HOVER_SERIAL_BAUD); + Serial1.begin(HOVER_SERIAL_BAUD); pinMode(LED_BUILTIN, OUTPUT); } @@ -91,15 +93,15 @@ void Send(int16_t uSteer, int16_t uSpeed) Command.checksum = (uint16_t)(Command.start ^ Command.steer ^ Command.speed); // Write to Serial - HoverSerial.write((uint8_t *) &Command, sizeof(Command)); + Serial1.write((uint8_t *) &Command, sizeof(Command)); } // ########################## RECEIVE ########################## void Receive() { // Check for new data availability in the Serial buffer - if (HoverSerial.available()) { - incomingByte = HoverSerial.read(); // Read the incoming byte + if (Serial1.available()) { + incomingByte = Serial1.read(); // Read the incoming byte bufStartFrame = ((uint16_t)(incomingBytePrev) << 8) + incomingByte; // Construct the start frame } else { @@ -110,7 +112,7 @@ void Receive() #ifdef DEBUG_RX Serial.print(incomingByte); return; - #endif + #endif // Copy received data if (bufStartFrame == START_FRAME) { // Initialize if new data is detected @@ -134,18 +136,21 @@ void Receive() // Copy the new data memcpy(&Feedback, &NewFeedback, sizeof(SerialFeedback)); - // Print data to built-in Serial - Serial.print("1: "); Serial.print(Feedback.cmd1); - Serial.print(" 2: "); Serial.print(Feedback.cmd2); - Serial.print(" 3: "); Serial.print(Feedback.speedR); - Serial.print(" 4: "); Serial.print(Feedback.speedL); - Serial.print(" 5: "); Serial.print(Feedback.speedR_meas); - Serial.print(" 6: "); Serial.print(Feedback.speedL_meas); - Serial.print(" 7: "); Serial.print(Feedback.batVoltage); - Serial.print(" 8: "); Serial.println(Feedback.boardTemp); + // Print data to CDC Serial if available + if (Serial) { + Serial.print("1: "); Serial.print(Feedback.cmd1); + Serial.print(" 2: "); Serial.print(Feedback.cmd2); + Serial.print(" 3: "); Serial.print(Feedback.speedR); + Serial.print(" 4: "); Serial.print(Feedback.speedL); + Serial.print(" 5: "); Serial.print(Feedback.speedR_meas); + Serial.print(" 6: "); Serial.print(Feedback.speedL_meas); + Serial.print(" 7: "); Serial.print(Feedback.batVoltage); + Serial.print(" 8: "); Serial.println(Feedback.boardTemp); + } } else { - 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) }