diff --git a/splash-h.h b/splash-h.h new file mode 100644 index 0000000..b3288c1 --- /dev/null +++ b/splash-h.h @@ -0,0 +1,58 @@ + +#define my_splash_width 128 +#define my_splash_height 52 + +const uint8_t PROGMEM my_splash_data[] = { + B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000, + B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000, + B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000, + B00000000,B00000000,B00000000,B00000000,B00000000,B00000111,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000, + B00000000,B00000000,B00000000,B00000000,B00000000,B00000111,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000, + B00000000,B00000000,B00000000,B00000000,B00000000,B00000111,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000, + B00000000,B00000000,B00000000,B00000000,B00000000,B00000111,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000, + B00000000,B00000000,B00000000,B00000000,B00000000,B00000111,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000, + B00000000,B00000000,B00000000,B00000000,B00000000,B00000111,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000, + B00000000,B00000000,B00000000,B00000000,B00000000,B00000111,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000, + B00000000,B00000000,B00000000,B00000000,B00000000,B00000111,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000, + B00000000,B00000000,B00000000,B00000000,B00000000,B00000111,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000, + B00000011,B11111000,B00000001,B11111100,B01111111,B11111111,B11111111,B11110011,B11111000,B00000001,B11111000,B00001111,B11111000,B00000000,B01111111,B11000000, + B00000011,B11111000,B00000001,B11111100,B01111111,B11111111,B11111111,B11110011,B11111000,B00000001,B11111000,B00001111,B11111000,B00000000,B11111111,B11000000, + B00000011,B11111000,B00000001,B11111100,B01111111,B11111111,B11111111,B11110011,B11111000,B00000001,B11111000,B00001111,B11111100,B00000000,B11111111,B11000000, + B00000011,B11111000,B00000001,B11111100,B01111111,B11111111,B11111111,B11110011,B11111000,B00000001,B11111000,B00001111,B11111100,B00000000,B11111111,B11000000, + B00000011,B11111000,B00000001,B11111100,B00000000,B00001111,B10000000,B00000011,B11111000,B00000001,B11111000,B00001111,B11111100,B00000001,B11111111,B11000000, + B00000011,B11111000,B00000001,B11111100,B00000000,B00011111,B11000000,B00000011,B11111000,B00000001,B11111000,B00001111,B11111110,B00000001,B11111111,B11000000, + B00000011,B11111000,B00000001,B11111100,B00000000,B00011111,B11000000,B00000011,B11111000,B00000001,B11111000,B00001111,B11111110,B00000011,B11111111,B11000000, + B00000011,B11111000,B00000001,B11111100,B00000000,B00111111,B11100000,B00000011,B11111000,B00000001,B11111000,B00001111,B11011111,B10000011,B11111111,B11000000, + B00000011,B11111000,B00000001,B11111100,B00000000,B00111111,B11100000,B00000011,B11111000,B00000001,B11111000,B00001111,B11011111,B10000111,B11011111,B11000000, + B00000011,B11111000,B00000001,B11111100,B00000000,B01111101,B11110000,B00000011,B11111000,B00000001,B11111000,B00001111,B11001111,B11000111,B11011111,B11000000, + B00000011,B11111111,B11111111,B11111100,B00000000,B01111000,B11110000,B00000011,B11111000,B00000001,B11111000,B00001111,B11001111,B11000111,B11011111,B11000000, + B00000011,B11111111,B11111111,B11111100,B00000000,B11111000,B11111000,B00000011,B11111000,B00000001,B11111000,B00001111,B11001111,B11001111,B10011111,B11000000, + B00000011,B11111111,B11111111,B11111100,B00000001,B11111000,B01111000,B00000011,B11111000,B00000001,B11111000,B00001111,B11000111,B11101111,B10011111,B11000000, + B00000011,B11111111,B11111111,B11111100,B00000001,B11111000,B01111000,B00000011,B11111000,B00000001,B11111000,B00001111,B11000111,B11101111,B10011111,B11000000, + B00000011,B11111111,B11111111,B11111100,B00000001,B11110000,B01111100,B00000011,B11111000,B00000001,B11111000,B00001111,B11000111,B11111111,B00011111,B11000000, + B00000011,B11111111,B11111111,B11111100,B00000011,B11110000,B00111100,B00000011,B11111000,B00000001,B11111000,B00001111,B11000011,B11111111,B00011111,B11000000, + B00000011,B11111000,B00000001,B11111100,B00000011,B11100000,B00111110,B00000011,B11111000,B00000001,B11111000,B00001111,B11000011,B11111110,B00011111,B11000000, + B00000011,B11111000,B00000001,B11111100,B00000111,B11100000,B00011110,B00000001,B11111000,B00000001,B11111000,B00001111,B11000001,B11111110,B00011111,B11000000, + B00000011,B11111000,B00000001,B11111100,B00000111,B11000000,B00011110,B00000001,B11111000,B00000011,B11111000,B00001111,B11000001,B11111110,B00011111,B11000000, + B00000011,B11111000,B00000001,B11111100,B00001111,B11000000,B00011111,B10000001,B11111000,B00000011,B11111000,B00001111,B11000001,B11111100,B00011111,B11000000, + B00000011,B11111000,B00000001,B11111100,B00001111,B10000000,B00001111,B10000001,B11111100,B00000011,B11111000,B00001111,B11000000,B01111100,B00011111,B11000000, + B00000011,B11111000,B00000001,B11111100,B00011111,B10000000,B00001111,B11000001,B11111110,B00000111,B11110000,B00001111,B11000000,B00000000,B00011111,B11000000, + B00000011,B11111000,B00000001,B11111100,B00011111,B11111111,B11111111,B11000000,B11111111,B11111111,B11110000,B00001111,B11000000,B00000000,B00011111,B11000000, + B00000011,B11111000,B00000001,B11111100,B00111111,B11111111,B11111111,B11100000,B01111111,B11111111,B11100000,B00001111,B11000000,B00000000,B00011111,B11000000, + B00000011,B11111000,B00000001,B11111100,B00111111,B11111111,B11111111,B11100000,B00111111,B11111111,B11000000,B00001111,B11000000,B00000000,B00011111,B11000000, + B00000011,B11111000,B00000001,B11111100,B00111111,B11111111,B11111111,B11100000,B00011111,B11111111,B00000000,B00001111,B11000000,B00000000,B00011111,B11000000, + B00000000,B00000000,B00000000,B00000000,B00000000,B00000111,B00000000,B00000000,B00000001,B11110000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000, + B00000000,B00000000,B00000000,B00000000,B00000000,B00000111,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000, + B00000000,B00000000,B00000000,B00000000,B00000000,B00000111,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000, + B00000000,B00000000,B00000000,B00000000,B00000000,B00000111,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000, + B00000000,B00000000,B00000000,B00000000,B00000000,B00000111,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000, + B00000000,B00000000,B00000000,B00000000,B00000000,B00000111,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000, + B00000000,B00000000,B00000000,B00000000,B00000000,B00000111,B00000000,B00000000,B00000000,B00000000,B10001000,B00000000,B00000000,B00000000,B00000000,B00000000, + B00000000,B00000000,B00000000,B00000000,B00000000,B00000111,B00000000,B00000000,B00000000,B00000000,B10001000,B00000000,B00000000,B00000000,B00000000,B00000000, + B00000000,B00000000,B00000000,B00000000,B00000000,B00000111,B00000000,B00000000,B00000000,B00000000,B10001001,B10001000,B10110100,B00000001,B11001011,B00011100, + B00000000,B00000000,B00000000,B00000000,B00000000,B00000111,B00000000,B00000000,B00000000,B00000000,B11111000,B01001000,B10101010,B00000010,B00101100,B10100110, + B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B10001001,B11001000,B10101010,B00000010,B00101000,B00011010, + B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B10001010,B01001001,B10101010,B00110010,B00101000,B00000010, + B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B10001001,B11100110,B10101010,B00110001,B11001000,B00011100, + B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000, +}; diff --git a/test_f103.ino b/test_f103.ino index 2a2413e..7128ddf 100644 --- a/test_f103.ino +++ b/test_f103.ino @@ -1,5 +1,5 @@ #include -#include +#include "splash-h.h" #include #include @@ -8,25 +8,59 @@ #include // second I2C on PB_11 (SDA) PB_10 (SCL) TwoWire Wire2(PB_11, PB_10); -// TwoWire Wire(PB_9, PB_8); +// Connect to a Nunchuk +Nunchuk nchuk(Wire2); +// ########################## DEFINES ########################## +#define LED_BUILTIN PC13 +#define TIME_SEND 20 // [ms] poolling time interval +#define HOVER_SERIAL_BAUD 38400 // [-] Baud rate for HoverSerial (used to communicate with the hoverboard) +#define START_FRAME 0xAAAA // [-] Start frme definition for reliable serial communication #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels + // Declaration for an SSD1306 display connected to I2C2 (SDA, SCL pins) Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); -// Connect to a Nunchuk -Nunchuk nchuk(Wire); - -int cmd1; // normalized input values. -1000 to 1000 +// Global variables +uint8_t idx = 0; // Index for new data pointer +uint16_t bufStartFrame; // Buffer Start Frame +byte *p; // Pointer declaration for the new received data +byte incomingByte; +byte incomingBytePrev; + +typedef struct +{ + uint16_t start; + int16_t steer; + int16_t speed; + uint16_t checksum; +} SerialCommand; +SerialCommand Command; + +typedef struct +{ + uint16_t start; + int16_t cmd1; + int16_t cmd2; + int16_t speedR; + int16_t speedL; + int16_t speedR_meas; + int16_t speedL_meas; + int16_t batVoltage; + int16_t boardTemp; + int16_t checksum; +} SerialFeedback; +SerialFeedback Feedback; +SerialFeedback NewFeedback; + +int cmd1; // normalized input values. -1000 to 1000 int cmd2; - -#define LED_BUILTIN PC13 - // Serial1 PA_9 TX PA_10 RX - -void setup() { +// ########################## SETUP ########################## +void setup() +{ Wire.setSCL(PB_8); Wire.setSDA(PB_9); Wire2.setSCL(PB_10); @@ -34,34 +68,107 @@ 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) + display.clearDisplay(); + display.drawBitmap((SCREEN_WIDTH - my_splash_width) / 2, (SCREEN_HEIGHT - my_splash_height) / 2, + my_splash_data, my_splash_width, my_splash_height, 1); + + /* + display.setTextSize(1); // Draw 1X-scale text + display.setTextColor(SSD1306_WHITE); + display.setCursor(int((display.width() - (strlen("g")*6))), 50); + display.println("g"); + */ display.display(); Serial.begin(115200); - Serial1.begin(115200); + Serial1.begin(HOVER_SERIAL_BAUD); - while (!Serial) ; + while (!Serial) + ; pinMode(LED_BUILTIN, OUTPUT); - Serial.println("STM32 blink test"); + Serial.println("STM32 hoverboard control"); + display.clearDisplay(); + display.setTextSize(1); // Draw 1X-scale text + display.setTextColor(SSD1306_WHITE); + display.setCursor(int((display.width() - + (strlen("Hoverboard Control") * 6)) / + 2), + 0); + display.println("Hoverboard Control"); + display.setCursor(int((display.width() - (strlen("v STM32 0.1") * 6)) / 2), 8); + display.println("v STM32 0.1"); +} +// ########################## SEND ########################## +void Send(int16_t uSteer, int16_t uSpeed) +{ + // Create command + Command.start = (uint16_t)START_FRAME; + Command.steer = (int16_t)uSteer; + Command.speed = (int16_t)uSpeed; + Command.checksum = + (uint16_t)(Command.start ^ Command.steer ^ Command.speed); + + // Write to Serial + Serial1.write((uint8_t *)&Command, sizeof(Command)); } -void loop() { - unsigned long timeNow = millis(); - // Blink the LED - digitalWrite(LED_BUILTIN, (timeNow % 2000) < 1000); - if (!nchuk.update()) { +// ########################## Nunchuk_control ########################## + +void Nunchuk_control() +{ + int old_cursorX, old_cursorY; + + if (!nchuk.update()) + { Serial.println("Controller Disconnected!"); nchuk.reconnect(); - } else { + } + else + { + cmd1 = map(nchuk.joyX(), 0, 256, 150, -150); // x - axis. Nunchuck joystick readings range 30 - 230 cmd2 = map(nchuk.joyY(), 0, 256, -150, 150); // y - axis + /* + Serial.print("X : "); + Serial.print(nchuk.joyX()); + Serial.print(" Y : "); + Serial.println(nchuk.joyY()); +*/ + Send(cmd1, cmd2); + } + display.setCursor(0, 40); + display.setTextSize(1); // Draw 1X-scale text + display.setTextColor(SSD1306_WHITE); + display.print("X : "); + 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(cmd1); + display.setCursor(0, 50); + display.setTextSize(1); // Draw 1X-scale text + display.setTextColor(SSD1306_WHITE); + display.print("y : "); + 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(cmd2); + display.display(); +} - Serial.print("X : "); Serial.println(nchuk.joyX()); - Serial.print("Y : "); Serial.println(nchuk.joyY()); +// ########################## LOOP ########################## - } - if ((timeNow % 2000) < 1000) { - Serial.println(millis() / 1000); - } +unsigned long iTimeSend = 0; +void loop() +{ + unsigned long timeNow = millis(); + // Blink the LED + digitalWrite(LED_BUILTIN, (timeNow % 2000) < 1000); + // if (iTimeSend > timeNow) return; + iTimeSend = timeNow + TIME_SEND; + Nunchuk_control(); + Serial.println(timeNow); }