todo: ble notify length, speed

This commit is contained in:
Rene Arnhold 2020-08-02 13:46:42 +02:00
parent 6a3feb9835
commit 55c048eeb1
2 changed files with 166 additions and 112 deletions

View File

@ -12,6 +12,8 @@
platform = espressif32
board = odroid_esp32
framework = arduino
build_flags = -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG
monitor_speed = 115200
upload_speed = 460800
lib_deps =
ESP32 BLE Arduino
ODROID-GO

View File

@ -24,6 +24,10 @@
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include <esp32-hal-log.h>
#include <odroid_go.h>
#define CORE_DEBUG_LEVEL 5
BLEServer *pServer = NULL;
BLECharacteristic * pRTxCharacteristic;
@ -34,29 +38,26 @@ uint8_t txValue = 0;
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
#define BLE_MTU 517
#define SERVICE_UUID "0000ffe0-0000-1000-8000-00805f9b34fb" // UART service UUID
#define CHARACTERISTIC_UUID_RTX "0000ffe1-0000-1000-8000-00805f9b34fb"
#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
#define WHEEL_DIAMETER 27
#define MAX_SPEED 27
#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 };
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 };
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, 0x05, 0x00, 0x00, 0xFF, 0x01, 0x00 };
uint8_t status[] = { 0x66, 0x41, 0x05, 0x05, 0x00, 0x00, 0xFF, 0x00, 0x00 };
byte status_request[] = { 0x66, 0x41, 0x02 };
bool send_status = false;
int btn_pressed = millis();
int speed = 0;
uint8_t pas_ratio = 0;
void write_status_checksum() {
int suma = 0;
@ -67,6 +68,70 @@ void write_status_checksum() {
status[sizeof(status) - 1] = suma - tmp_suma;
}
uint8_t bytes[sizeof(float)];
void gen_speed() {
//int rpm = (int)((( speed / 60 ) * 3.141592653589793 ) / (WHEEL_DIAMETER / 39.37) * 1000 * 60 * 1000);
int rpm = 10;
// *(float*)(bytes) = ((WHEEL_DIAMETER / 39.37) * 3.141592653589793) * ((speed / 60) * 1000);
Serial.println(((WHEEL_DIAMETER / 39.37) * 3.141592653589793) * ((speed / 60) * 1000));
*(float*)(bytes) = 359.084758475;
Serial.print("bytes: ");
Serial.print(bytes[0], HEX);
Serial.print(" ");
Serial.print(bytes[1], HEX);
Serial.print(" ");
Serial.print(bytes[2], HEX);
Serial.print(" ");
Serial.print(bytes[3], HEX);
Serial.println();
Serial.print("### Speed: ");
Serial.print(speed);
Serial.print(" RPM: ");
Serial.print(rpm);
Serial.print(" | ");
// status[5] = (byte) (rpm & 0xFF);
// status[6] = (byte) ( (rpm >> 8) & 0xFF);
status[5] = bytes[2];
status[6] = bytes[3];
Serial.print(status[5], HEX);
Serial.print(" ");
Serial.print(status[6], HEX);
Serial.println("");
}
void write_battery() {
GO.lcd.setCursor(10, 20);
GO.lcd.print("Battery: ");
GO.lcd.print(status[3]);
}
void write_fault() {
GO.lcd.setCursor(10, 50);
GO.lcd.print("ERROR: ");
GO.lcd.print(status[7]);
}
void write_speed() {
GO.lcd.setCursor(10, 80);
GO.lcd.print("SPEED: ");
GO.lcd.print(speed);
}
void write_pas() {
GO.lcd.setCursor(10, 130);
GO.lcd.print("Fahrstufe: ");
GO.lcd.print(pas_ratio);
}
void update_display() {
GO.lcd.clearDisplay();
write_battery();
write_fault();
write_speed();
write_pas();
}
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
@ -85,35 +150,38 @@ class MyCallbacks: public BLECharacteristicCallbacks {
send_params = true;
}
else if( (rxValue.length() == (sizeof(status_request) + 3)) && (rxValue[0] == status_request[0]) && (rxValue[1] == status_request[1]) && (rxValue[2] == status_request[2]) ) {
if (pas_ratio != rxValue[3]) {
pas_ratio = rxValue[3];
update_display();
}
send_status = true;
}
Serial.print(rxValue.length());
Serial.print(" ");
Serial.print(sizeof(status_request));
Serial.print(" ");
Serial.print(sizeof(status));
Serial.println();
Serial.print("Received Value: ");
for (int i = 0; i < rxValue.length(); i++) {
Serial.print(rxValue[i], HEX);
Serial.print(" ");
}
Serial.println();
Serial.println("*********");
}
}
};
void setup() {
Serial.begin(9600);
void reset_ble() {
delay(500); // give the bluetooth stack the chance to get things ready
pServer->startAdvertising(); // restart advertising
oldDeviceConnected = deviceConnected;
}
pinMode(BUTTON_A_PIN, INPUT_PULLUP);
pinMode(BUTTON_B_PIN, INPUT_PULLUP);
pinMode(BUTTON_START, INPUT_PULLUP);
void setup() {
//esp_log_level_set("*", ESP_LOG_VERBOSE);
GO.begin();
GO.lcd.setTextSize(3);
GO.lcd.clear();
GO.Speaker.mute();
write_battery();
write_fault();
write_speed();
// Create the BLE Device
BLEDevice::init("RAYVOLT DUMMY");
ESP_LOGI("RayvoltDummy", "setMTU");
BLEDevice::setMTU(BLE_MTU);
// Create the BLE Server
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
@ -142,97 +210,81 @@ void setup() {
// Start advertising
pServer->getAdvertising()->start();
Serial.println("Waiting a client connection to notify...");
reset_ble();
ESP_LOGI("RayvoltDummy", "localMTU: %d", BLEDevice::getMTU());
}
void loop() {
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();
GO.update();
if (GO.JOY_Y.isAxisPressed() == 2) {
if (speed != MAX_SPEED) {
speed += 1;
}
update_display();
}
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 (GO.JOY_Y.isAxisPressed() == 1) {
if (speed != 0) {
speed -= 1;
}
update_display();
}
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 ( GO.BtnA.isPressed() == 1 ) {
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;
}
update_display();
btn_pressed = millis();
}
if (deviceConnected) {
if (send_params) {
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(&params[i], 1);
pRTxCharacteristic->notify();
}
*/
pRTxCharacteristic->setValue(params, sizeof(params));
pRTxCharacteristic->notify();
send_params = false;
}
else if (send_status) {
write_status_checksum();
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;
}
delay(10); // bluetooth stack will go into congestion, if too many packets are sent
}
if ( GO.BtnStart.isPressed() == 1 ) {
if ( status[3] == 0x05 ) {
status[3] = 0x00;
}
else {
status[3] += 1;
}
update_display();
btn_pressed = millis();
}
// disconnecting
if (!deviceConnected && oldDeviceConnected) {
delay(500); // give the bluetooth stack the chance to get things ready
pServer->startAdvertising(); // restart advertising
Serial.println("start advertising");
oldDeviceConnected = deviceConnected;
if (deviceConnected) {
if (send_params) {
pRTxCharacteristic->setValue("Hallo BLE World ich hab Probleme, mit langen Daten");
ESP_LOGI("RayvoltDummy", "localMTU: %d", BLEDevice::getMTU());
pRTxCharacteristic->notify();
send_params = false;
}
// connecting
if (deviceConnected && !oldDeviceConnected) {
// do stuff here on connecting
oldDeviceConnected = deviceConnected;
else if (send_status) {
gen_speed();
write_status_checksum();
pRTxCharacteristic->setValue(status, sizeof(status));
pRTxCharacteristic->notify();
send_status = false;
}
delay(10); // bluetooth stack will go into congestion, if too many packets are sent
}
// disconnecting
if (!deviceConnected && oldDeviceConnected) {
reset_ble();
}
// connecting
if (deviceConnected && !oldDeviceConnected) {
// do stuff here on connecting
oldDeviceConnected = deviceConnected;
}
}