Add new bluetooth lib code

This commit is contained in:
torok.istvan 2025-09-19 13:13:52 +02:00
parent ca65cb47a8
commit 0282d0741e
13 changed files with 202 additions and 113 deletions

View File

@ -9,5 +9,6 @@
"outline.showNamespaces": false,
"outline.showProperties": false,
"outline.showTypeParameters": false,
"outline.showVariables": false
"outline.showVariables": false,
"C_Cpp.errorSquiggles": "disabled"
}

View File

@ -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 <string>

View File

@ -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 <esp_gap_bt_api.h> // ESP32 BT
#include <string>
/**
* @brief A %!BT device address
* @brief A %BT device address.
*
* Every %BT device has a unique address which can be used to identify it and from connection
* Every %BT device has a unique address which can be used to identify it and form connections.
*/
class BTAddress {
public:

View File

@ -1,5 +1,5 @@
#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED)
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BLUEDROID_ENABLED)
//#include <map>

View File

@ -1,5 +1,5 @@
#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED)
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BLUEDROID_ENABLED)
#include <esp_err.h>

View File

@ -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

View File

@ -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;

View File

@ -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

174
src/bluetoothSelect.h Normal file
View File

@ -0,0 +1,174 @@
#ifdef COMPILE_BT
#include "BluetoothSerial.h"
#include <BleSerial.h>
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

View File

@ -292,7 +292,7 @@ bool zedUartPassed = false; //Goes true during testing if ESP can communicate wi
volatile int counter = 0;
long lastTime = 0;
#include <mybluetooth.h>
#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();
}

View File

@ -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();
}
};