code format

bluepill
JbLb 6 years ago
parent fe9377cc28
commit 588212519f

@ -4,7 +4,7 @@
#include <Adafruit_SSD1306.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
// 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<strip.numPixels(); i++) { // For each pixel in strip...
strip.setPixelColor(i, color); // Set pixel's color (in RAM)
strip.show(); // Update strip to match
delay(wait); // Pause for a moment
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)
strip.show(); // Update strip to match
delay(wait); // Pause for a moment
}
}
@ -123,14 +126,13 @@ void setup()
display.println("Hoverboard Control");
display.setCursor(int((display.width() - (strlen("v STM32 0.1") * 6)) / 2), 8);
display.println("v STM32 0.1");
// Fill along the length of the strip in various colors...
colorWipe(strip.Color(255, 0, 0) , 20); // Red
colorWipe(strip.Color( 0, 255, 0) , 20); // Green
colorWipe(strip.Color( 0, 0, 255) , 20); // Blue
colorWipe(strip.Color( 0, 0, 0) , 20); // Black
colorWipe(strip.Color(255, 0, 0), 20); // Red
colorWipe(strip.Color(0, 255, 0), 20); // Green
colorWipe(strip.Color(0, 0, 255), 20); // Blue
colorWipe(strip.Color(0, 0, 0), 20); // Black
}
// ########################## SEND ##########################
void Send(int16_t uSteer, int16_t uSpeed)
@ -149,85 +151,91 @@ void Send(int16_t uSteer, int16_t uSpeed)
void Receive()
{
int old_cursorX, old_cursorY;
// 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
} else {
return;
}
int old_cursorX, old_cursorY;
// 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
}
else
{
return;
}
// If DEBUG_RX is defined print all incoming bytes
// If DEBUG_RX is defined print all incoming bytes
#ifdef DEBUG_RX
Serial.print(incomingByte);
return;
Serial.print(incomingByte);
return;
#endif
// 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++;
// 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 ##########################

Loading…
Cancel
Save