From 6a3feb983590dc17a77584fe5c0bb8ff6f764df2 Mon Sep 17 00:00:00 2001 From: Rene Arnhold Date: Tue, 28 Jul 2020 16:42:12 +0200 Subject: [PATCH] first relyable --- platformio.ini | 1 + src/main.cpp | 89 ++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 66 insertions(+), 24 deletions(-) diff --git a/platformio.ini b/platformio.ini index 931397b..e6686f8 100644 --- a/platformio.ini +++ b/platformio.ini @@ -12,5 +12,6 @@ platform = espressif32 board = odroid_esp32 framework = arduino +upload_speed = 460800 lib_deps = ESP32 BLE Arduino \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 16d7192..09a2c55 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -39,24 +39,32 @@ uint8_t txValue = 0; #define BUTTON_A_PIN 32 #define BUTTON_B_PIN 33 +#define BUTTON_MENU 13 +#define BUTTON_SELECT 27 +#define BUTTON_VOLUME 0 +#define BUTTON_START 39 +#define BUTTON_JOY_Y 35 +#define BUTTON_JOY_X 34 -uint8_t params[] = { 0x66, 0x11, 0x20, 0x00, 0x64, 0x30, 0x3C, 0x2A, 0x14, 0xD9, 0x00, 0x32, 0x00, 0x64, 0x00, 0x2D, 0x00, 0xCC, 0x10, 0x64, 0x32, 0xFC, 0x53, 0x98, 0x08, 0x64, 0x00, 0x41, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x78 }; -byte params_request[] = {0x66, 0x11, 0x00, 0x77}; +#define BUTTNO_PAUSE 100 + +uint8_t params[] = { 0x66, 0x11, 0x20, 0x09, 0x64, 0x30, 0x3C, 0x2A, 0x14, 0xD9, 0x00, 0x32, 0x00, 0x64, 0x00, 0x2D, 0x00, 0xCC, 0x10, 0x64, 0x32, 0xFC, 0x53, 0x98, 0x08, 0x64, 0x00, 0x41, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x78 }; +byte params_request[] = { 0x66, 0x11, 0x00, 0x77 }; bool send_params = false; -// uint8_t status[] = { 0x66, 0x41, 0x05, 0x32, 0x32, 0x00, 0x00, 0x02 , 0x02}; -uint8_t status[] = { 0x66, 0x41, 0x05, 0x05, 0x00, 0xFF, 0xFF, 0x00, 0xAF, 0xFC }; -byte status_request[] = {0x66, 0x41, 0x02}; +uint8_t status[] = { 0x66, 0x41, 0x05, 0x05, 0x00, 0x00, 0xFF, 0x01, 0x00 }; +byte status_request[] = { 0x66, 0x41, 0x02 }; bool send_status = false; +int btn_pressed = millis(); + void write_status_checksum() { int suma = 0; for (int i = 0; i < sizeof(status) - 1; i++) { suma += status[i]; } int tmp_suma = (suma / 256) * 256; - status[8] = suma - tmp_suma; - status[8] = 0; + status[sizeof(status) - 1] = suma - tmp_suma; } class MyServerCallbacks: public BLEServerCallbacks { @@ -96,12 +104,12 @@ class MyCallbacks: public BLECharacteristicCallbacks { } }; - void setup() { Serial.begin(9600); pinMode(BUTTON_A_PIN, INPUT_PULLUP); pinMode(BUTTON_B_PIN, INPUT_PULLUP); + pinMode(BUTTON_START, INPUT_PULLUP); // Create the BLE Device BLEDevice::init("RAYVOLT DUMMY"); @@ -138,46 +146,79 @@ void setup() { } void loop() { - if (!digitalRead(BUTTON_A_PIN)) { - Serial.println("Button A"); - if (status[3] == 0xFF) { + if ( (!digitalRead(BUTTON_A_PIN)) && (millis() - btn_pressed > BUTTNO_PAUSE) ) { + if ( status[7] == 0x00 ) { + status[7] = 0x01; + } + else if ( status[7] == 0x01 ) { + status[7] = 0x02; + } + else if ( status[7] == 0x02 ) { + status[7] = 0x04; + } + else if ( status[7] == 0x04 ) { + status[7] = 0x08; + } + else if ( status[7] == 0x08 ) { + status[7] = 0x00; + } + btn_pressed = millis(); + } + + if ( (!digitalRead(BUTTON_B_PIN)) && (millis() - btn_pressed > BUTTNO_PAUSE) ) { + if ( status[5] == 0xFF ) { + status[5] = 0x00; + } + else { + status[5] += 1; + } + Serial.print("RMP1: "); + Serial.print(status[5]); + Serial.println(""); + btn_pressed = millis(); + } + + if ( (!digitalRead(BUTTON_START)) && (millis() - btn_pressed > BUTTNO_PAUSE) ) { + Serial.println("Start pressed"); + if ( status[3] == 0x05 ) { status[3] = 0x00; } else { status[3] += 1; } + btn_pressed = millis(); } - if (!digitalRead(BUTTON_B_PIN)) { - Serial.println("Button B"); - if (status[3] == 0x00) { - status[3] = 0xFF; - } - else { - status[3] -= 1; - } - } + if (deviceConnected) { if (send_params) { - Serial.print("sending: "); + Serial.println("sending params"); + Serial.print(sizeof(params), DEC); +/* for (int i = 0; i < sizeof(params); i++) { Serial.print(params[i], HEX); + Serial.print(" "); pRTxCharacteristic->setValue(¶ms[i], 1); pRTxCharacteristic->notify(); } +*/ + pRTxCharacteristic->setValue(params, sizeof(params)); + pRTxCharacteristic->notify(); send_params = false; - Serial.println(""); } else if (send_status) { write_status_checksum(); - Serial.print("sending: "); + Serial.println("sending status"); +/* for (int i = 0; i < sizeof(status); i++) { Serial.print(status[i], HEX); Serial.print(" "); pRTxCharacteristic->setValue(&status[i], 1); pRTxCharacteristic->notify(); } +*/ + pRTxCharacteristic->setValue(status, sizeof(status)); + pRTxCharacteristic->notify(); send_status = false; - Serial.println(""); } delay(10); // bluetooth stack will go into congestion, if too many packets are sent }