code format

bluepill
JbLb 6 years ago
parent fe9377cc28
commit 588212519f

@ -4,7 +4,7 @@
#include <Adafruit_SSD1306.h> #include <Adafruit_SSD1306.h>
#include <Adafruit_NeoPixel.h> #include <Adafruit_NeoPixel.h>
#define PIN PB5 // Pin where NeoPixels are connected #define PIN PB5 // Pin where NeoPixels are connected
#define NB_LED 12 // number of LEDs #define NB_LED 12 // number of LEDs
// Declare our NeoPixel strip object: // Declare our NeoPixel strip object:
@ -71,17 +71,20 @@ SerialFeedback NewFeedback;
int cmd1; // normalized input values. -1000 to 1000 int cmd1; // normalized input values. -1000 to 1000
int cmd2; int cmd2;
// ########################## colorWipe ########################## // ########################## colorWipe ##########################
// Fill strip pixels one after another with a color. Strip is NOT cleared // 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 // 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 // (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), // strip.Color(red, green, blue) as shown in the loop() function above),
// and a delay time (in milliseconds) between pixels. // and a delay time (in milliseconds) between pixels.
void colorWipe(uint32_t color, int wait) { void colorWipe(uint32_t color, int wait)
for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip... {
strip.setPixelColor(i, color); // Set pixel's color (in RAM) for (int i = 0; i < strip.numPixels(); i++)
strip.show(); // Update strip to match { // For each pixel in strip...
delay(wait); // Pause for a moment strip.setPixelColor(i, color); // Set pixel's color (in RAM)
strip.show(); // Update strip to match
delay(wait); // Pause for a moment
} }
} }
@ -126,11 +129,10 @@ void setup()
// Fill along the length of the strip in various colors... // Fill along the length of the strip in various colors...
colorWipe(strip.Color(255, 0, 0) , 20); // Red colorWipe(strip.Color(255, 0, 0), 20); // Red
colorWipe(strip.Color( 0, 255, 0) , 20); // Green colorWipe(strip.Color(0, 255, 0), 20); // Green
colorWipe(strip.Color( 0, 0, 255) , 20); // Blue colorWipe(strip.Color(0, 0, 255), 20); // Blue
colorWipe(strip.Color( 0, 0, 0) , 20); // Black colorWipe(strip.Color(0, 0, 0), 20); // Black
} }
// ########################## SEND ########################## // ########################## SEND ##########################
void Send(int16_t uSteer, int16_t uSpeed) void Send(int16_t uSteer, int16_t uSpeed)
@ -149,85 +151,91 @@ void Send(int16_t uSteer, int16_t uSpeed)
void Receive() void Receive()
{ {
int old_cursorX, old_cursorY; int old_cursorX, old_cursorY;
// 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 {
bufStartFrame = ((uint16_t) (incomingBytePrev) << 8) + incomingByte; // Construct the start frame incomingByte = Serial1.read(); // Read the incoming byte
} else { bufStartFrame = ((uint16_t)(incomingBytePrev) << 8) + incomingByte; // Construct the start frame
return; }
} else
{
return;
}
// If DEBUG_RX is defined print all incoming bytes // If DEBUG_RX is defined print all incoming bytes
#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)
p = (byte *) & NewFeedback; { // Initialize if new data is detected
*p++ = incomingBytePrev; p = (byte *)&NewFeedback;
*p++ = incomingByte; *p++ = incomingBytePrev;
idx = 2; *p++ = incomingByte;
} else if (idx >= 2 && idx < sizeof(SerialFeedback)) { // Save the new received data idx = 2;
*p++ = incomingByte; }
idx++; 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 else
if (idx == sizeof(SerialFeedback)) { {
uint16_t checksum; if (Serial)
checksum = Serial.println("Non-valid data skipped");
(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)
} }
// 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 ########################## // ########################## Nunchuk_control ##########################

Loading…
Cancel
Save