From 0282d0741e81dff6299ddb83120b27f219954de7 Mon Sep 17 00:00:00 2001 From: "torok.istvan" Date: Fri, 19 Sep 2025 13:13:52 +0200 Subject: [PATCH] Add new bluetooth lib code --- .vscode/settings.json | 3 +- {src => lib}/BluetoothSerial/BTAddress.cpp | 2 +- {src => lib}/BluetoothSerial/BTAddress.h | 9 +- .../BluetoothSerial/BTAdvertisedDevice.h | 0 .../BluetoothSerial/BTAdvertisedDeviceSet.cpp | 2 +- {src => lib}/BluetoothSerial/BTScan.h | 0 .../BluetoothSerial/BTScanResultsSet.cpp | 2 +- .../BluetoothSerial/BluetoothSerial.cpp | 29 +-- .../BluetoothSerial/BluetoothSerial.h | 3 +- platformio.ini | 3 + src/bluetoothSelect.h | 174 ++++++++++++++++++ src/main.cpp | 12 +- src/mybluetooth.h | 76 -------- 13 files changed, 202 insertions(+), 113 deletions(-) rename {src => lib}/BluetoothSerial/BTAddress.cpp (96%) rename {src => lib}/BluetoothSerial/BTAddress.h (80%) rename {src => lib}/BluetoothSerial/BTAdvertisedDevice.h (100%) rename {src => lib}/BluetoothSerial/BTAdvertisedDeviceSet.cpp (96%) rename {src => lib}/BluetoothSerial/BTScan.h (100%) rename {src => lib}/BluetoothSerial/BTScanResultsSet.cpp (97%) rename {src => lib}/BluetoothSerial/BluetoothSerial.cpp (96%) rename {src => lib}/BluetoothSerial/BluetoothSerial.h (96%) create mode 100644 src/bluetoothSelect.h delete mode 100644 src/mybluetooth.h diff --git a/.vscode/settings.json b/.vscode/settings.json index 97bd8ae..f79feda 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -9,5 +9,6 @@ "outline.showNamespaces": false, "outline.showProperties": false, "outline.showTypeParameters": false, - "outline.showVariables": false + "outline.showVariables": false, + "C_Cpp.errorSquiggles": "disabled" } \ No newline at end of file diff --git a/src/BluetoothSerial/BTAddress.cpp b/lib/BluetoothSerial/BTAddress.cpp similarity index 96% rename from src/BluetoothSerial/BTAddress.cpp rename to lib/BluetoothSerial/BTAddress.cpp index 836617e..417cf42 100644 --- a/src/BluetoothSerial/BTAddress.cpp +++ b/lib/BluetoothSerial/BTAddress.cpp @@ -1,5 +1,5 @@ #include "sdkconfig.h" -#if defined(CONFIG_BT_ENABLED) +#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BLUEDROID_ENABLED) #include "BTAddress.h" #include diff --git a/src/BluetoothSerial/BTAddress.h b/lib/BluetoothSerial/BTAddress.h similarity index 80% rename from src/BluetoothSerial/BTAddress.h rename to lib/BluetoothSerial/BTAddress.h index dbab3bb..86301ec 100644 --- a/src/BluetoothSerial/BTAddress.h +++ b/lib/BluetoothSerial/BTAddress.h @@ -1,14 +1,15 @@ #ifndef COMPONENTS_CPP_UTILS_BTADDRESS_H_ #define COMPONENTS_CPP_UTILS_BTADDRESS_H_ #include "sdkconfig.h" -#if defined(CONFIG_BT_ENABLED) +#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BLUEDROID_ENABLED) #include // ESP32 BT #include + /** - * @brief A %!BT device address - * - * Every %BT device has a unique address which can be used to identify it and from connection + * @brief A %BT device address. + * + * Every %BT device has a unique address which can be used to identify it and form connections. */ class BTAddress { public: diff --git a/src/BluetoothSerial/BTAdvertisedDevice.h b/lib/BluetoothSerial/BTAdvertisedDevice.h similarity index 100% rename from src/BluetoothSerial/BTAdvertisedDevice.h rename to lib/BluetoothSerial/BTAdvertisedDevice.h diff --git a/src/BluetoothSerial/BTAdvertisedDeviceSet.cpp b/lib/BluetoothSerial/BTAdvertisedDeviceSet.cpp similarity index 96% rename from src/BluetoothSerial/BTAdvertisedDeviceSet.cpp rename to lib/BluetoothSerial/BTAdvertisedDeviceSet.cpp index c4b5888..5760367 100644 --- a/src/BluetoothSerial/BTAdvertisedDeviceSet.cpp +++ b/lib/BluetoothSerial/BTAdvertisedDeviceSet.cpp @@ -1,5 +1,5 @@ #include "sdkconfig.h" -#if defined(CONFIG_BT_ENABLED) +#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BLUEDROID_ENABLED) //#include diff --git a/src/BluetoothSerial/BTScan.h b/lib/BluetoothSerial/BTScan.h similarity index 100% rename from src/BluetoothSerial/BTScan.h rename to lib/BluetoothSerial/BTScan.h diff --git a/src/BluetoothSerial/BTScanResultsSet.cpp b/lib/BluetoothSerial/BTScanResultsSet.cpp similarity index 97% rename from src/BluetoothSerial/BTScanResultsSet.cpp rename to lib/BluetoothSerial/BTScanResultsSet.cpp index 6ab1a9d..6e42ab6 100644 --- a/src/BluetoothSerial/BTScanResultsSet.cpp +++ b/lib/BluetoothSerial/BTScanResultsSet.cpp @@ -1,5 +1,5 @@ #include "sdkconfig.h" -#if defined(CONFIG_BT_ENABLED) +#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BLUEDROID_ENABLED) #include diff --git a/src/BluetoothSerial/BluetoothSerial.cpp b/lib/BluetoothSerial/BluetoothSerial.cpp similarity index 96% rename from src/BluetoothSerial/BluetoothSerial.cpp rename to lib/BluetoothSerial/BluetoothSerial.cpp index 63652b6..c25bc8b 100644 --- a/src/BluetoothSerial/BluetoothSerial.cpp +++ b/lib/BluetoothSerial/BluetoothSerial.cpp @@ -27,11 +27,9 @@ const char * _spp_server_name = "ESP32SPP"; //Now passed in during begin() -//#define RX_QUEUE_SIZE (512 * 4) //Increase to facilitate larger NTRIP transfers -//#define TX_QUEUE_SIZE 512 //Increase to facilitate high transmission rates - //#define RX_QUEUE_SIZE 512 //Original //#define TX_QUEUE_SIZE 32 + #define SPP_TX_QUEUE_TIMEOUT 1000 #define SPP_TX_DONE_TIMEOUT 1000 #define SPP_CONGESTED_TIMEOUT 1000 @@ -153,18 +151,7 @@ static esp_err_t _spp_queue_packet(uint8_t *data, size_t len){ return ESP_OK; } -//SPP_TX_MAX seems to default to a lower amount (330) until there is congestion it then -//tries to catch up but at 330, it cannot and the heap begins to take the hit. -//Increasing the max to 2048 we see normal verbose xfers of 512 until congestion -//when it increases briefly to 2048 then returns to 512 with no heap hit. -//The rate at which we congest is dependant on how much we are attempting to TX and -//how much is coming in RX from the phone. -//At ~25kBps heap lowest point is 100k and stable -//At ~50kBps heap lowest point is ~78k and stable -//Above 50kbps it becomes unstable - -//const uint16_t SPP_TX_MAX = 330; //Original -const uint16_t SPP_TX_MAX = 1024*4; //Should match the SERIAL_SIZE_RX buffer size in RTK_Surveyor.ino +const uint16_t SPP_TX_MAX = 330; static uint8_t _spp_tx_buffer[SPP_TX_MAX]; static uint16_t _spp_tx_buffer_len = 0; @@ -326,7 +313,7 @@ static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) } else if (_spp_rx_queue != NULL){ for (int i = 0; i < param->data_ind.len; i++){ if(xQueueSend(_spp_rx_queue, param->data_ind.data + i, (TickType_t)0) != pdTRUE){ - log_e("RX Full! Discarding %u bytes", param->data_ind.len - i); + Serial.printf("Bluetooth RX buffer full! Discarding %u bytes. Consider increasing SPP RX buffer size.\r\n", param->data_ind.len - i); break; } } @@ -528,6 +515,7 @@ static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *pa } } +//static bool _init_bt(const char *deviceName) static bool _init_bt(const char *deviceName, uint16_t rxQueueSize, uint16_t txQueueSize) { if(!_bt_event_group){ @@ -549,6 +537,7 @@ static bool _init_bt(const char *deviceName, uint16_t rxQueueSize, uint16_t txQu xEventGroupSetBits(_spp_event_group, SPP_DISCONNECTED); } if (_spp_rx_queue == NULL){ + //_spp_rx_queue = xQueueCreate(RX_QUEUE_SIZE, sizeof(uint8_t)); //initialize the queue _spp_rx_queue = xQueueCreate(rxQueueSize, sizeof(uint8_t)); //initialize the queue if (_spp_rx_queue == NULL){ log_e("RX Queue Create Failed"); @@ -556,6 +545,7 @@ static bool _init_bt(const char *deviceName, uint16_t rxQueueSize, uint16_t txQu } } if (_spp_tx_queue == NULL){ + //_spp_tx_queue = xQueueCreate(TX_QUEUE_SIZE, sizeof(spp_packet_t*)); //initialize the queue _spp_tx_queue = xQueueCreate(txQueueSize, sizeof(spp_packet_t*)); //initialize the queue if (_spp_tx_queue == NULL){ log_e("TX Queue Create Failed"); @@ -714,12 +704,14 @@ BluetoothSerial::~BluetoothSerial(void) _stop_bt(); } +//bool BluetoothSerial::begin(String localName, bool isMaster) bool BluetoothSerial::begin(String localName, bool isMaster, uint16_t rxQueueSize, uint16_t txQueueSize) { _isMaster = isMaster; if (localName.length()){ local_name = localName; } + //return _init_bt(local_name.c_str()); return _init_bt(local_name.c_str(), rxQueueSize, txQueueSize); } @@ -1009,9 +1001,4 @@ BluetoothSerial::operator bool() const { return true; } - -bool BluetoothSerial::isCongested(){ - return(!(xEventGroupGetBits(_spp_event_group) & SPP_CONGESTED)); -} - #endif \ No newline at end of file diff --git a/src/BluetoothSerial/BluetoothSerial.h b/lib/BluetoothSerial/BluetoothSerial.h similarity index 96% rename from src/BluetoothSerial/BluetoothSerial.h rename to lib/BluetoothSerial/BluetoothSerial.h index 3bf3b20..7194ca6 100644 --- a/src/BluetoothSerial/BluetoothSerial.h +++ b/lib/BluetoothSerial/BluetoothSerial.h @@ -24,7 +24,7 @@ class BluetoothSerial: public Stream BluetoothSerial(void); ~BluetoothSerial(void); - bool begin(String localName=String(), bool isMaster=false, uint16_t rxQueueSize = 512 * 4, uint16_t txQueueSize = 512); + bool begin(String localName=String(), bool isMaster=false, uint16_t rxQueueSize = 512 * 2, uint16_t txQueueSize = 512); bool begin(unsigned long baud){//compatibility return begin(); } @@ -65,7 +65,6 @@ class BluetoothSerial: public Stream operator bool() const; - bool isCongested(); private: String local_name; diff --git a/platformio.ini b/platformio.ini index c07d713..903e188 100644 --- a/platformio.ini +++ b/platformio.ini @@ -20,3 +20,6 @@ lib_deps = greiman/SdFat@^2.2.0 jchristensen/JC_Button@^2.1.2 fbiego/ESP32Time@^2.0.0 + avinabmalla/ESP32_BleSerial@1.0.4 +build_flags= + -I BluetoothSerial diff --git a/src/bluetoothSelect.h b/src/bluetoothSelect.h new file mode 100644 index 0000000..21cac95 --- /dev/null +++ b/src/bluetoothSelect.h @@ -0,0 +1,174 @@ +#ifdef COMPILE_BT + +#include "BluetoothSerial.h" +#include + +class BTSerialInterface +{ + public: + virtual bool begin(String deviceName, bool isMaster, uint16_t rxQueueSize, uint16_t txQueueSize) = 0; + virtual void disconnect() = 0; + virtual void end() = 0; + virtual esp_err_t register_callback(esp_spp_cb_t * callback) = 0; + virtual void setTimeout(unsigned long timeout) = 0; + + virtual int available() = 0; + // virtual bool hasClient() = 0; + virtual size_t readBytes(uint8_t *buffer, size_t bufferSize) = 0; + virtual int read() = 0; + + //virtual bool isCongested() = 0; + virtual size_t write(const uint8_t *buffer, size_t size) = 0; + virtual size_t write(uint8_t value) = 0; + virtual void flush() = 0; +}; + + +class BTClassicSerial : public virtual BTSerialInterface, public BluetoothSerial +{ + // Everything is already implemented in BluetoothSerial since the code was + // originally written using that class + public: + bool begin(String deviceName, bool isMaster, uint16_t rxQueueSize, uint16_t txQueueSize) + { + return BluetoothSerial::begin(deviceName, isMaster, rxQueueSize, txQueueSize); + } + + void disconnect() + { + BluetoothSerial::disconnect(); + } + + void end() + { + BluetoothSerial::end(); + } + + esp_err_t register_callback(esp_spp_cb_t * callback) + { + return BluetoothSerial::register_callback(callback); + } + + void setTimeout(unsigned long timeout) + { + BluetoothSerial::setTimeout(timeout); + } + + int available() + { + return BluetoothSerial::available(); + } + + // bool hasClient() + // { + // return BluetoothSerial::hasClient(); + // } + + size_t readBytes(uint8_t *buffer, size_t bufferSize) + { + return BluetoothSerial::readBytes(buffer, bufferSize); + } + + int read() + { + return BluetoothSerial::read(); + } + + size_t write(const uint8_t *buffer, size_t size) + { + return BluetoothSerial::write(buffer, size); + } + + size_t write(uint8_t value) + { + return BluetoothSerial::write(value); + } + + void flush() + { + BluetoothSerial::flush(); + } +}; + +class BTLESerial : public virtual BTSerialInterface, public BleSerial +{ + public: + // Missing from BleSerial + bool begin(String deviceName, bool isMaster, uint16_t rxQueueSize, uint16_t txQueueSize) + { + BleSerial::begin(deviceName.c_str()); + return true; + } + + void disconnect() + { + Server->disconnect(Server->getConnId()); + } + + void end() + { + BleSerial::end(); + } + + esp_err_t register_callback(esp_spp_cb_t *callback) + { + connectionCallback = callback; + return ESP_OK; + } + + void setTimeout(unsigned long timeout) + { + BleSerial::setTimeout(timeout); + } + + int available() + { + return BleSerial::available(); + } + + size_t readBytes(uint8_t *buffer, size_t bufferSize) + { + return BleSerial::readBytes(buffer, bufferSize); + } + + int read() + { + return BleSerial::read(); + } + + size_t write(const uint8_t *buffer, size_t size) + { + return BleSerial::write(buffer, size); + } + + size_t write(uint8_t value) + { + return BleSerial::write(value); + } + + void flush() + { + BleSerial::flush(); + } + + // override BLEServerCallbacks + void onConnect(BLEServer *pServer) + { + // bleConnected = true; Removed until PR is accepted + if(connectionCallback){ + connectionCallback(ESP_SPP_SRV_OPEN_EVT, nullptr); + } + } + + void onDisconnect(BLEServer *pServer) + { + // bleConnected = false; Removed until PR is accepted + connectionCallback(ESP_SPP_CLOSE_EVT, nullptr); + Server->startAdvertising(); + } + + private: + esp_spp_cb_t *connectionCallback; +}; + +#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index e0bd026..df8756c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -292,7 +292,7 @@ bool zedUartPassed = false; //Goes true during testing if ESP can communicate wi volatile int counter = 0; long lastTime = 0; -#include +#include "bluetoothSelect.h" // BluetoothSerial SerialBT; BTSerialInterface *bluetoothSerial; @@ -965,11 +965,11 @@ void loop() oled.setCursor(0,11); oled.printf("%d bytes",fileSize); - if(bluetoothSerial->hasClient()) - { - oled.setCursor(0,21); - oled.println("BT connected"); - } + // if(bluetoothSerial->hasClient()) + // { + // oled.setCursor(0,21); + // oled.println("BT connected"); + // } oled.display(); } diff --git a/src/mybluetooth.h b/src/mybluetooth.h deleted file mode 100644 index 13bbcbe..0000000 --- a/src/mybluetooth.h +++ /dev/null @@ -1,76 +0,0 @@ -#include "BluetoothSerial.h" - -class BTSerialInterface -{ - public: - virtual bool begin(String deviceName) = 0; - virtual void disconnect() = 0; - virtual void end() = 0; - virtual esp_err_t register_callback(esp_spp_cb_t * callback) = 0; - virtual void setTimeout(unsigned long timeout) = 0; - - virtual int available() = 0; - virtual bool hasClient() = 0; - virtual size_t readBytes(uint8_t *buffer, size_t bufferSize) = 0; - - //virtual bool isCongested() = 0; - virtual size_t write(const uint8_t *buffer, size_t size) = 0; - virtual void flush() = 0; -}; - - -class BTClassicSerial : public virtual BTSerialInterface, public BluetoothSerial -{ - // Everything is already implemented in BluetoothSerial since the code was - // originally written using that class - public: - bool begin(String deviceName) - { - return BluetoothSerial::begin(deviceName); - } - - void disconnect() - { - BluetoothSerial::disconnect(); - } - - void end() - { - BluetoothSerial::end(); - } - - esp_err_t register_callback(esp_spp_cb_t * callback) - { - return BluetoothSerial::register_callback(callback); - } - - void setTimeout(unsigned long timeout) - { - BluetoothSerial::setTimeout(timeout); - } - - int available() - { - return BluetoothSerial::available(); - } - - bool hasClient() - { - return BluetoothSerial::hasClient(); - } - - size_t readBytes(uint8_t *buffer, size_t bufferSize) - { - return BluetoothSerial::readBytes(buffer, bufferSize); - } - - size_t write(const uint8_t *buffer, size_t size) - { - return BluetoothSerial::write(buffer, size); - } - - void flush() - { - BluetoothSerial::flush(); - } -};