diff --git a/hoverserial.ino b/hoverserial.ino index e4b62dd..c9d18f4 100644 --- a/hoverserial.ino +++ b/hoverserial.ino @@ -4,7 +4,7 @@ #include #include -#define PIN PB5 // Pin where NeoPixels are connected +#define PIN PB5 // Pin where NeoPixels are connected #define NB_LED 12 // number of LEDs // Declare our NeoPixel strip object: @@ -71,17 +71,20 @@ SerialFeedback NewFeedback; int cmd1; // normalized input values. -1000 to 1000 int cmd2; + // ########################## colorWipe ########################## // Fill strip pixels one after another with a color. Strip is NOT cleared // first; anything there will be covered pixel by pixel. Pass in color // (as a single 'packed' 32-bit value, which you can get by calling // strip.Color(red, green, blue) as shown in the loop() function above), // and a delay time (in milliseconds) between pixels. -void colorWipe(uint32_t color, int wait) { - for(int i=0; i= 2 && idx < sizeof(SerialFeedback)) { // Save the new received data - *p++ = incomingByte; - idx++; + // Copy received data + if (bufStartFrame == START_FRAME) + { // Initialize if new data is detected + p = (byte *)&NewFeedback; + *p++ = incomingBytePrev; + *p++ = incomingByte; + idx = 2; + } + else if (idx >= 2 && idx < sizeof(SerialFeedback)) + { // Save the new received data + *p++ = incomingByte; + idx++; + } + // Check if we reached the end of the package + 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); + 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); + } + display.setCursor(0, 30); + display.setTextSize(1); // Draw 1X-scale text + display.setTextColor(SSD1306_WHITE); + display.print("V : "); + old_cursorX = display.getCursorX(); + old_cursorY = display.getCursorY(); + display.fillRect(old_cursorX, old_cursorY, (6 * 5), 8, SSD1306_BLACK); // erase previous display + display.setCursor(old_cursorX, old_cursorY); + display.print(Feedback.batVoltage); + display.display(); } - // Check if we reached the end of the package - 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); - 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); - } - display.setCursor(0, 30); - display.setTextSize(1); // Draw 1X-scale text - display.setTextColor(SSD1306_WHITE); - display.print("V : "); - old_cursorX = display.getCursorX(); - old_cursorY = display.getCursorY(); - display.fillRect(old_cursorX, old_cursorY, (6 * 5), 8, SSD1306_BLACK); // erase previous display - display.setCursor(old_cursorX, old_cursorY); - display.print(Feedback.batVoltage); - display.display(); - } 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) + else + { + if (Serial) + Serial.println("Non-valid data skipped"); } - // Update previous states - incomingBytePrev = incomingByte; + + idx = 0; // Reset the index (it prevents to enter in this if condition in the next cycle) + } + // Update previous states + incomingBytePrev = incomingByte; } // ########################## Nunchuk_control ##########################