From 8f2126e76859ea3c986c0c85637b564d4c9b73f7 Mon Sep 17 00:00:00 2001 From: cvanelteren Date: Wed, 16 Feb 2022 19:16:18 +0100 Subject: [PATCH] Initial implementation of ESP now replacement --- platformio.ini | 12 ++++++------ src/config.hpp | 2 +- src/keyboard.cpp | 1 + src/main.cpp | 3 +-- src/mesh.cpp | 36 ++++++++++++------------------------ src/mesh.hpp | 17 +++++++++-------- 6 files changed, 30 insertions(+), 41 deletions(-) diff --git a/platformio.ini b/platformio.ini index 32f51ec..505ecfa 100644 --- a/platformio.ini +++ b/platformio.ini @@ -14,11 +14,11 @@ default_envs = ttgo-lora32-v2 [env:ttgo-lora32-v2] ; build_unflags = -std=gnu++11 build_flags = - -I src/ESP32-BLE-Keyboard - -I src/NimBLE-Arduino/src - -I src/FastLED/ + ; -I ESP32-BLE-Keyboard + ; -I NimBLE-Arduino/src + ; -I src/FastLED/ -D USE_NIMBLE - ; -std=c++17 + -std=c++11 -Wfatal-errors platform = espressif32 @@ -29,8 +29,8 @@ lib_deps = FastLED ; nkolban/ESP32 BLE Arduino @ ^1.0.4 ; nkolban/ESP32 BLE Arduino - ; t-vk/ESP32 BLE Keyboard - ; NimBLE-Arduino + t-vk/ESP32 BLE Keyboard + NimBLE-Arduino monitor_speed = 115200 board_build.partitions = part.csv diff --git a/src/config.hpp b/src/config.hpp index a0ec1c8..d9d9e9c 100644 --- a/src/config.hpp +++ b/src/config.hpp @@ -106,8 +106,8 @@ class Config { // see constructor in cpp file uint8_t rot_encoder_steps = 4; uint8_t rot_accel = 250; // change this for your device - // uint8_t serv_add[6] = {0x80, 0x7D, 0x3A, 0xD4, 0x2C, 0x9C}; uint8_t serv_add[6] = {0x80, 0x7D, 0x3A, 0xD4, 0x2C, 0x9E}; + // uint8_t serv_add[6] = {0x80, 0x7D, 0x3A, 0xD4, 0x2C, 0x9E}; // uint8_t serv_add[6] = {0x7C, 0x9E, 0xBD, 0xFB, 0xD9, 0x7A}; }; #endif diff --git a/src/keyboard.cpp b/src/keyboard.cpp index 3c542b0..678a772 100644 --- a/src/keyboard.cpp +++ b/src/keyboard.cpp @@ -504,6 +504,7 @@ void Keyboard::update() { printf("Bluetooth disconnected\n"); ble_connected = 0; } + led->update(); } void Keyboard::wakeup() { diff --git a/src/main.cpp b/src/main.cpp index bb94959..1c28c5a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -188,7 +188,7 @@ void setup() { // mesh.begin(); keyboard.begin(); - // keyboard.led->active_keys = &(keyboard.matrix->active_keys); + keyboard.led->active_keys = &(keyboard.matrix->active_keys); printf("------------SPLITBOARD------------\n"); // printf("layers size %d\n", keyboard.layers.size()); @@ -196,7 +196,6 @@ void setup() { static bool is_connected; void loop() { - // keyboard.mesh->update(); keyboard.update(); // while (!keyboard.mesh->is_connected()) { diff --git a/src/mesh.cpp b/src/mesh.cpp index a54ed02..34a42d9 100644 --- a/src/mesh.cpp +++ b/src/mesh.cpp @@ -43,6 +43,7 @@ Mesh::Mesh(Config *config) { printf("Setting up mesh connection\n"); this->config = config; has_connection = false; + host_dev = NULL; } void Mesh::begin() { @@ -143,21 +144,6 @@ void Mesh::end() { BLEDevice::deinit(false); } void Mesh::wakeup() { begin(); } -// void Mesh::send(const std::vector &data, -// BLECharacteristic *characteristic) { -// if (data.size()) { -// characteristic->setValue((uint8_t *)&data, sizeof(data[0]) * -// data.size()); characteristic->notify(true); -// } -// } -// void Mesh::send(const std::vector &data, -// BLECharacteristic *characteristic) { -// if (data.size()) { -// characteristic->setValue((uint8_t *)&data, sizeof(data[0]) * -// data.size()); characteristic->notify(true); -// } -// } - std::vector Mesh::get_buffer() { /** * @brief Returns non-empty mesh keys @@ -290,7 +276,7 @@ BLEClient *Mesh::create_client(BLEAdvertisedDevice *host_dev) { bool Mesh::connectServer() { NimBLEClient *client = nullptr; - printf("Attempting connection"); + printf("Attempting connection\n"); if (host_dev == nullptr) { printf("no host dev found\n"); return false; @@ -316,7 +302,9 @@ bool Mesh::connectServer() { return false; } + printf("%d", host_dev == NULL); printf("Creating client\n"); + printf("%s \n", host_dev->getAddress().toString().c_str()); client = BLEDevice::createClient(host_dev->getAddress()); printf("New client created\n"); @@ -347,8 +335,6 @@ bool Mesh::connectServer() { printf("Warning connection is not secure\n"); } - printf("Testing here"); - printf("Connected to: %s\n", client->getPeerAddress().toString().c_str()); printf("SSRI: %d \n", client->getRssi()); @@ -456,12 +442,15 @@ void Mesh::retrieve_events(BLERemoteCharacteristic *remoteCharacteristic, void Mesh::update() { static size_t last_time; - if ((is_hub == false)) - if (is_connected() == false) { - if (!connectServer()) { - scan(); + if ((last_time - millis()) > 1000) { + last_time = millis(); + if ((is_hub == false)) + if (is_connected() == false) { + if (!connectServer()) { + scan(); + } } - } + } } void Mesh::onConnect(BLEClient *client) { @@ -503,7 +492,6 @@ bool subscribe_to(std::string characteristic_uuid, remote_service->getCharacteristic(characteristic_uuid); // subscribe to remote characteristic if (remote_characteristic->canNotify()) { - printf("REMOTE SERVICE CAN NOTIFY\n"); if (!remote_characteristic->subscribe(true, *cb)) { printf("Cannot subscribe to characteristic \n"); client->disconnect(); diff --git a/src/mesh.hpp b/src/mesh.hpp index a0ab444..266a390 100644 --- a/src/mesh.hpp +++ b/src/mesh.hpp @@ -7,6 +7,7 @@ #include #if defined(USE_NIMBLE) + #include "NimBLECharacteristic.h" #include "NimBLEHIDDevice.h" #include @@ -14,14 +15,14 @@ #include #include -#define BLEDevice NimBLEDevice -#define BLEServerCallbacks NimBLEServerCallbacks -#define BLECharacteristicCallbacks NimBLECharacteristicCallbacks -#define BLEHIDDevice NimBLEHIDDevice -#define BLECharacteristic NimBLECharacteristic -#define BLEAdvertising NimBLEAdvertising -#define BLEServer NimBLEServer -#define BLAdvertisedDeviceCallbacks NimBLEAdvertisedDeviceCallbacks +// #define BLEDevice NimBLEDevice +// #define BLEServerCallbacks NimBLEServerCallbacks +// #define BLECharacteristicCallbacks NimBLECharacteristicCallbacks +// #define BLEHIDDevice NimBLEHIDDevice +// #define BLECharacteristic NimBLECharacteristic +// #define BLEAdvertising NimBLEAdvertising +// #define BLEServer NimBLEServer +// #define BLAdvertisedDeviceCallbacks NimBLEAdvertisedDeviceCallbacks #define BLE_MAX_CONNECTIONS NIMBLE_MAX_CONNECTIONS