Nunchuk added - reformat of code from IDE !

master
JbLb 6 years ago
parent fba4b39003
commit 0d462439e3

@ -41,6 +41,7 @@
#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>
@ -81,11 +82,15 @@ SerialFeedback NewFeedback;
// 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()
{ {
nchuk.begin();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x64) display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x64)
// Clear the buffer // Clear the buffer
display.clearDisplay(); display.clearDisplay();
@ -95,6 +100,9 @@ void setup()
display.setTextColor(SSD1306_WHITE); display.setTextColor(SSD1306_WHITE);
display.println("Hoverboard Serial"); display.println("Hoverboard Serial");
display.println("v1.0"); display.println("v1.0");
display.print("Nunchuk ");
nchuk.connect()? display.print("") : display.print("not ");
display.println("connected");
display.display(); display.display();
Serial1.begin(HOVER_SERIAL_BAUD); // RX, TX from arduino to TX RX on hoverboard board. ! be carreful 3v3 Serial1.begin(HOVER_SERIAL_BAUD); // RX, TX from arduino to TX RX on hoverboard board. ! be carreful 3v3
@ -113,7 +121,8 @@ void Send(int16_t uSteer, int16_t uSpeed)
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 // Write to Serial
Serial1.write((uint8_t *) & Command, sizeof(Command)); Serial1.write((uint8_t *) & Command, sizeof(Command));
@ -126,8 +135,7 @@ void Receive()
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;
} }
@ -147,36 +155,48 @@ void Receive()
*p++ = incomingByte; *p++ = incomingByte;
idx++; idx++;
} }
// Check if we reached the end of the package // Check if we reached the end of the package
if (idx == sizeof(SerialFeedback)) { if (idx == sizeof(SerialFeedback)) {
uint16_t checksum; uint16_t checksum;
checksum = (uint16_t)(NewFeedback.start ^ NewFeedback.cmd1 ^ NewFeedback.cmd2 ^ NewFeedback.speedR ^ NewFeedback.speedL checksum =
^ NewFeedback.speedR_meas ^ NewFeedback.speedL_meas ^ NewFeedback.batVoltage ^ NewFeedback.boardTemp); (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 // Check validity of the new data
if (NewFeedback.start == START_FRAME && checksum == NewFeedback.checksum) { if (NewFeedback.start == START_FRAME
&& checksum == NewFeedback.checksum) {
// Copy the new data // Copy the new data
memcpy(&Feedback, &NewFeedback, sizeof(SerialFeedback)); memcpy(&Feedback, &NewFeedback, sizeof(SerialFeedback));
// Print data to CDC Serial if available // Print data to CDC Serial if available
if (Serial) { if (Serial) {
Serial.print("1: "); Serial.print(Feedback.cmd1); Serial.print("1: ");
Serial.print(" 2: "); Serial.print(Feedback.cmd2); Serial.print(Feedback.cmd1);
Serial.print(" 3: "); Serial.print(Feedback.speedR); Serial.print(" 2: ");
Serial.print(" 4: "); Serial.print(Feedback.speedL); Serial.print(Feedback.cmd2);
Serial.print(" 5: "); Serial.print(Feedback.speedR_meas); Serial.print(" 3: ");
Serial.print(" 6: "); Serial.print(Feedback.speedL_meas); Serial.print(Feedback.speedR);
Serial.print(" 7: "); Serial.print(Feedback.batVoltage); Serial.print(" 4: ");
Serial.print(" 8: "); Serial.println(Feedback.boardTemp); 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 { } else {
if (Serial) 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) idx = 0; // Reset the index (it prevents to enter in this if condition in the next cycle)
} }
// Update previous states // Update previous states
incomingBytePrev = incomingByte; incomingBytePrev = incomingByte;
} }
@ -193,6 +213,27 @@ void loop(void)
// Check for new received data // Check for new received data
Receive(); Receive();
// Get new data from the controller
if (!nchuk.update()) {
Serial.println("Controller disconnected!");
nchuk.reconnect();
} else {
// Read a button (on/off, C and Z)
Serial.print("Z: ");
nchuk.buttonZ()? Serial.print("On ") : Serial.print("Off ");
Serial.print("C: ");
nchuk.buttonC()? Serial.print("On ") : Serial.print("Off ");
// Read joystick axis (0-255, X and Y)
Serial.print("The joystick's Y axis is at ");
Serial.print(nchuk.joyY());
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 // Send commands
if (iTimeSend > timeNow) if (iTimeSend > timeNow)

Loading…
Cancel
Save