Nunchuk added - reformat of code from IDE !

master
JbLb 6 years ago
parent fba4b39003
commit 0d462439e3

@ -33,152 +33,172 @@
// ########################## DEFINES ########################## // ########################## DEFINES ##########################
#define HOVER_SERIAL_BAUD 38400 // [-] Baud rate for HoverSerial (used to communicate with the hoverboard) #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 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 START_FRAME 0xAAAA // [-] Start frme definition for reliable serial communication
#define TIME_SEND 1000 // [ms] Sending time interval #define TIME_SEND 1000 // [ms] Sending time interval
#define SPEED_MAX_TEST 300 // [-] Maximum speed for testing #define SPEED_MAX_TEST 300 // [-] Maximum speed for testing
//#define DEBUG_RX // [-] Debug received data. Prints all bytes to serial (comment-out to disable) //#define DEBUG_RX // [-] Debug received data. Prints all bytes to serial (comment-out to disable)
#include <Wire.h> #include <Wire.h>
#include <Adafruit_GFX.h> #include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h> #include <Adafruit_SSD1306.h>
#include <NintendoExtensionCtrl.h>
// #include <SoftwareSerial.h> // #include <SoftwareSerial.h>
// SoftwareSerial HoverSerial(2,3); // RX, TX // SoftwareSerial HoverSerial(2,3); // RX, TX
// Global variables // Global variables
uint8_t idx = 0; // Index for new data pointer uint8_t idx = 0; // Index for new data pointer
uint16_t bufStartFrame; // Buffer Start Frame uint16_t bufStartFrame; // Buffer Start Frame
byte *p; // Pointer declaration for the new received data byte *p; // Pointer declaration for the new received data
byte incomingByte; byte incomingByte;
byte incomingBytePrev; byte incomingBytePrev;
typedef struct{ typedef struct {
uint16_t start; uint16_t start;
int16_t steer; int16_t steer;
int16_t speed; int16_t speed;
uint16_t checksum; uint16_t checksum;
} SerialCommand; } SerialCommand;
SerialCommand Command; SerialCommand Command;
typedef struct{ typedef struct {
uint16_t start; uint16_t start;
int16_t cmd1; int16_t cmd1;
int16_t cmd2; int16_t cmd2;
int16_t speedR; int16_t speedR;
int16_t speedL; int16_t speedL;
int16_t speedR_meas; int16_t speedR_meas;
int16_t speedL_meas; int16_t speedL_meas;
int16_t batVoltage; int16_t batVoltage;
int16_t boardTemp; int16_t boardTemp;
int16_t checksum; int16_t checksum;
} SerialFeedback; } SerialFeedback;
SerialFeedback Feedback; SerialFeedback Feedback;
SerialFeedback NewFeedback; SerialFeedback NewFeedback;
#define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire); Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);
// Connect to a Nunchuk
Nunchuk nchuk;
// ########################## SETUP ########################## // ########################## SETUP ##########################
void setup() void setup()
{ {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x64) nchuk.begin();
// Clear the buffer
display.clearDisplay(); display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x64)
display.display(); // Clear the buffer
display.setCursor(0, 0); display.clearDisplay();
display.setTextSize(1); // Draw 1X-scale text display.display();
display.setTextColor(SSD1306_WHITE); display.setCursor(0, 0);
display.println("Hoverboard Serial"); display.setTextSize(1); // Draw 1X-scale text
display.println("v1.0"); display.setTextColor(SSD1306_WHITE);
display.display(); display.println("Hoverboard Serial");
display.println("v1.0");
Serial1.begin(HOVER_SERIAL_BAUD); // RX, TX from arduino to TX RX on hoverboard board. ! be carreful 3v3 display.print("Nunchuk ");
pinMode(LED_BUILTIN, OUTPUT); nchuk.connect()? display.print("") : display.print("not ");
display.println("connected");
Serial.begin(SERIAL_BAUD); display.display();
if (Serial) {
Serial.println("Hoverboard Serial v1.0"); 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) {
Serial.println("Hoverboard Serial v1.0");
}
} }
// ########################## SEND ########################## // ########################## SEND ##########################
void Send(int16_t uSteer, int16_t uSpeed) void Send(int16_t uSteer, int16_t uSpeed)
{ {
// Create command // Create command
Command.start = (uint16_t)START_FRAME; Command.start = (uint16_t) START_FRAME;
Command.steer = (int16_t)uSteer; Command.steer = (int16_t) uSteer;
Command.speed = (int16_t)uSpeed; Command.speed = (int16_t) uSpeed;
Command.checksum = (uint16_t)(Command.start ^ Command.steer ^ Command.speed); Command.checksum =
(uint16_t) (Command.start ^ Command.steer ^ Command.speed);
// Write to Serial
Serial1.write((uint8_t *) &Command, sizeof(Command)); // Write to Serial
Serial1.write((uint8_t *) & Command, sizeof(Command));
} }
// ########################## RECEIVE ########################## // ########################## RECEIVE ##########################
void Receive() void Receive()
{ {
// 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 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 {
else { return;
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) { // Initialize if new data is detected p = (byte *) & NewFeedback;
p = (byte *)&NewFeedback; *p++ = incomingBytePrev;
*p++ = incomingBytePrev; *p++ = incomingByte;
*p++ = incomingByte; idx = 2;
idx = 2; } else if (idx >= 2 && idx < sizeof(SerialFeedback)) { // Save the new received data
} else if (idx >= 2 && idx < sizeof(SerialFeedback)) { // Save the new received data *p++ = incomingByte;
*p++ = incomingByte; idx++;
idx++; }
} // Check if we reached the end of the package
if (idx == sizeof(SerialFeedback)) {
// Check if we reached the end of the package uint16_t checksum;
if (idx == sizeof(SerialFeedback)) { checksum =
uint16_t checksum; (uint16_t) (NewFeedback.start ^ NewFeedback.
checksum = (uint16_t)(NewFeedback.start ^ NewFeedback.cmd1 ^ NewFeedback.cmd2 ^ NewFeedback.speedR ^ NewFeedback.speedL cmd1 ^ NewFeedback.cmd2 ^ NewFeedback.
^ NewFeedback.speedR_meas ^ NewFeedback.speedL_meas ^ NewFeedback.batVoltage ^ NewFeedback.boardTemp); speedR ^ NewFeedback.speedL ^ NewFeedback.
speedR_meas ^ NewFeedback.speedL_meas ^
// Check validity of the new data NewFeedback.batVoltage ^ NewFeedback.boardTemp);
if (NewFeedback.start == START_FRAME && checksum == NewFeedback.checksum) {
// Copy the new data // Check validity of the new data
memcpy(&Feedback, &NewFeedback, sizeof(SerialFeedback)); if (NewFeedback.start == START_FRAME
&& checksum == NewFeedback.checksum) {
// Print data to CDC Serial if available // Copy the new data
if (Serial) { memcpy(&Feedback, &NewFeedback, sizeof(SerialFeedback));
Serial.print("1: "); Serial.print(Feedback.cmd1);
Serial.print(" 2: "); Serial.print(Feedback.cmd2); // Print data to CDC Serial if available
Serial.print(" 3: "); Serial.print(Feedback.speedR); if (Serial) {
Serial.print(" 4: "); Serial.print(Feedback.speedL); Serial.print("1: ");
Serial.print(" 5: "); Serial.print(Feedback.speedR_meas); Serial.print(Feedback.cmd1);
Serial.print(" 6: "); Serial.print(Feedback.speedL_meas); Serial.print(" 2: ");
Serial.print(" 7: "); Serial.print(Feedback.batVoltage); Serial.print(Feedback.cmd2);
Serial.print(" 8: "); Serial.println(Feedback.boardTemp); Serial.print(" 3: ");
} Serial.print(Feedback.speedR);
} else { Serial.print(" 4: ");
if (Serial) Serial.println("Non-valid data skipped"); Serial.print(Feedback.speedL);
} Serial.print(" 5: ");
Serial.print(Feedback.speedR_meas);
idx = 0; // Reset the index (it prevents to enter in this if condition in the next cycle) Serial.print(" 6: ");
} Serial.print(Feedback.speedL_meas);
Serial.print(" 7: ");
// Update previous states Serial.print(Feedback.batVoltage);
incomingBytePrev = incomingByte; Serial.print(" 8: ");
Serial.println(Feedback.boardTemp);
}
} 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;
} }
// ########################## LOOP ########################## // ########################## LOOP ##########################
@ -189,34 +209,55 @@ int iTest = 0;
void loop(void) void loop(void)
{ {
unsigned long timeNow = millis(); unsigned long timeNow = millis();
// Check for new received data // Check for new received data
Receive(); Receive();
// Get new data from the controller
// Send commands if (!nchuk.update()) {
if (iTimeSend > timeNow) Serial.println("Controller disconnected!");
return; nchuk.reconnect();
iTimeSend = timeNow + TIME_SEND; } else {
Send(0, abs(iTest)); // Read a button (on/off, C and Z)
Serial.print("Z: ");
display.setCursor(0, 30); nchuk.buttonZ()? Serial.print("On ") : Serial.print("Off ");
display.setTextSize(1); // Draw 1X-scale text Serial.print("C: ");
display.setTextColor(SSD1306_WHITE); nchuk.buttonC()? Serial.print("On ") : Serial.print("Off ");
display.print("Speed : ");
display.setCursor(45, 30); // Read joystick axis (0-255, X and Y)
display.print(" "); Serial.print("The joystick's Y axis is at ");
display.display(); Serial.print(nchuk.joyY());
display.setCursor(45, 30); Serial.print(" and X axis is at ");
Serial.print(nchuk.joyX());
// Read an accelerometer and print values (0-1023, X, Y, and Z)
Serial.print(" - The accelerometer's X-axis is at ");
Serial.println(nchuk.accelX());
}
// Send commands
if (iTimeSend > timeNow)
return;
iTimeSend = timeNow + TIME_SEND;
Send(0, abs(iTest));
display.setCursor(0, 30);
display.setTextSize(1); // Draw 1X-scale text
display.setTextColor(SSD1306_WHITE);
display.print("Speed : ");
display.setCursor(45, 30);
display.print(" ");
display.display();
display.setCursor(45, 30);
display.print(iTest); display.print(iTest);
display.display(); display.display();
// Calculate test command signal // Calculate test command signal
iTest += 10; iTest += 10;
if (iTest > iTestMax) if (iTest > iTestMax)
iTest = -iTestMax; iTest = -iTestMax;
// Blink the LED // Blink the LED
digitalWrite(LED_BUILTIN, (timeNow % 2000) < 1000); digitalWrite(LED_BUILTIN, (timeNow % 2000) < 1000);
} }
// ########################## END ########################## // ########################## END ##########################

Loading…
Cancel
Save