BluetoothDispatcher refactoring
This commit is contained in:
@@ -3,41 +3,53 @@
|
|||||||
static DeviceConnectedCb deviceConnectedCallback = nullptr;
|
static DeviceConnectedCb deviceConnectedCallback = nullptr;
|
||||||
static void deviceConnectedStaticCallback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param);
|
static void deviceConnectedStaticCallback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param);
|
||||||
|
|
||||||
BluetoothDispatcher::BluetoothDispatcher(BluetoothSerial *controller, const char *device_name, HardwareSerial *serial) {
|
BluetoothDispatcher::BluetoothDispatcher(BluetoothSerial *controller, const char *device_name) {
|
||||||
|
assert(controller != nullptr);
|
||||||
|
assert(device_name != nullptr);
|
||||||
|
|
||||||
_device_name = device_name;
|
_device_name = device_name;
|
||||||
_controller = controller;
|
_controller = controller;
|
||||||
_serial = serial;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BluetoothDispatcher::initialize() {
|
bool BluetoothDispatcher::initialize(bool loop_on_fail, int readTimeoutMS) {
|
||||||
_controller->enableSSP();
|
_controller->enableSSP();
|
||||||
_controller->onConfirmRequest([this](uint16_t pin) {
|
_controller->onConfirmRequest([this](uint16_t pin) {
|
||||||
_confirmRequestCallback(pin);
|
_onConfirmRequest(pin);
|
||||||
});
|
});
|
||||||
_controller->onAuthComplete([this](boolean success) {
|
_controller->onAuthComplete([this](boolean success) {
|
||||||
_authCompleteCallback(success);
|
_onAuthComplete(success);
|
||||||
});
|
});
|
||||||
_controller->register_callback(deviceConnectedStaticCallback);
|
_controller->register_callback(deviceConnectedStaticCallback);
|
||||||
deviceConnectedCallback = [this](esp_bd_addr_t bt_addr) {
|
deviceConnectedCallback = [this](BTAddress device) {
|
||||||
_deviceConnectedCallback(bt_addr);
|
_onDeviceConnected(device);
|
||||||
};
|
};
|
||||||
_controller->setTimeout(2);
|
_controller->setTimeout(2);
|
||||||
return _controller->begin(_device_name);
|
bool success = _controller->begin(_device_name, false);
|
||||||
|
if (!success) {
|
||||||
|
ESP_LOGI(_tag, "Failed to initialize Bluetooth controller!");
|
||||||
|
while (loop_on_fail)
|
||||||
|
;
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
ESP_LOGI(_tag, "Bluetooth host initialized");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void BluetoothDispatcher::tick() {
|
void BluetoothDispatcher::tick(NewPackageCallback newPackageReceivedCb) {
|
||||||
while (_controller->available() and _buffer.length() <= _buffer_size) {
|
/* Call the callback, if new package received */
|
||||||
_buffer += (char)_controller->read();
|
while (_controller->available() and _buffer.length() <= _buffer_size) { _buffer += (char)_controller->read(); }
|
||||||
}
|
|
||||||
if (_buffer.endsWith("\r")) {
|
if (_buffer.endsWith("\r")) {
|
||||||
char buffer[_buffer_size];
|
char buffer[_buffer_size];
|
||||||
_buffer.substring(0, _buffer.lastIndexOf('\r'), buffer);
|
_buffer.substring(0, _buffer.lastIndexOf('\r'), buffer);
|
||||||
_serial->println(buffer); // print the payload
|
ESP_LOGD(_tag, "Received new buffer %s", buffer);
|
||||||
|
if (newPackageReceivedCb) {
|
||||||
|
newPackageReceivedCb(buffer);
|
||||||
|
}
|
||||||
_buffer.clear();
|
_buffer.clear();
|
||||||
_controller->write((uint8_t *)"Hello, world!", strlen("Hello, world!"));
|
_controller->write((uint8_t *)"Hello, world!", strlen("Hello, world!"));
|
||||||
// TODO: add callback, that receive new state
|
|
||||||
}
|
}
|
||||||
if (_buffer.length() > _buffer_size) {
|
if (_buffer.length() > _available_buffer_size) {
|
||||||
_buffer.clear();
|
_buffer.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -47,30 +59,37 @@ BluetoothDispatcher::~BluetoothDispatcher() {
|
|||||||
delete _controller;
|
delete _controller;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BluetoothDispatcher::_confirmRequestCallback(uint16_t pin) {
|
void BluetoothDispatcher::_onConfirmRequest(uint16_t pin) {
|
||||||
_confirmRequestDone = true;
|
ESP_LOGI(_tag, "The Bluetooth PIN is: %06lu", (long unsigned int)pin);
|
||||||
_serial->print("The PIN is: ");
|
|
||||||
_serial->println(pin);
|
|
||||||
_controller->confirmReply(true);
|
_controller->confirmReply(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BluetoothDispatcher::_authCompleteCallback(boolean success) {
|
void BluetoothDispatcher::_onAuthComplete(boolean success) {
|
||||||
if (success) {
|
if (success) {
|
||||||
_confirmRequestDone = true;
|
ESP_LOGI(_tag, "Pairing success!");
|
||||||
_serial->println("Pairing success!");
|
|
||||||
} else {
|
} else {
|
||||||
_serial->println("Pairing failed, rejected by user!");
|
ESP_LOGI(_tag, "Pairing failed, rejected by user!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void BluetoothDispatcher::_deviceConnectedCallback(esp_bd_addr_t bt_addr) {
|
void BluetoothDispatcher::_onDeviceConnected(BTAddress device) {
|
||||||
_serial->print("New connection opened: ");
|
ESP_LOGI(_tag, "New device connected: %s", device.toString(true).c_str());
|
||||||
_serial->println(BTAddress(bt_addr).toString(true));
|
if (_deviceConnectedCallback) {
|
||||||
|
_deviceConnectedCallback(device);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void BluetoothDispatcher::onNewDeviceConnected(DeviceConnectedCb deviceConnectedCb) {
|
||||||
|
/* Callback called if device connected successfully. */
|
||||||
|
_deviceConnectedCallback = deviceConnectedCb;
|
||||||
|
}
|
||||||
|
|
||||||
|
void BluetoothDispatcher::sendPackage(const char *package, size_t size) {
|
||||||
|
_controller->write((uint8_t *)package, size);
|
||||||
|
}
|
||||||
|
|
||||||
static void deviceConnectedStaticCallback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) {
|
static void deviceConnectedStaticCallback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) {
|
||||||
if (event == ESP_SPP_SRV_OPEN_EVT and param->srv_open.status == ESP_SPP_SUCCESS and deviceConnectedCallback) {
|
if (event == ESP_SPP_SRV_OPEN_EVT and param->srv_open.status == ESP_SPP_SUCCESS and deviceConnectedCallback) {
|
||||||
deviceConnectedCallback(param->srv_open.rem_bda);
|
deviceConnectedCallback(BTAddress(param->srv_open.rem_bda));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
#include "BluetoothSerial.h"
|
#include "BluetoothSerial.h"
|
||||||
#include "HardwareSerial.h"
|
#include "HardwareSerial.h"
|
||||||
|
#include "esp_log.h"
|
||||||
#include "mString.h"
|
#include "mString.h"
|
||||||
|
|
||||||
/* Check the ESP configuration */
|
/* Check the ESP configuration */
|
||||||
@@ -18,24 +19,29 @@
|
|||||||
#endif
|
#endif
|
||||||
/* ************************* */
|
/* ************************* */
|
||||||
|
|
||||||
typedef std::function<void(esp_bd_addr_t bt_addr)> DeviceConnectedCb;
|
typedef std::function<void(BTAddress device)> DeviceConnectedCb;
|
||||||
|
typedef std::function<void(char *package)> NewPackageCallback;
|
||||||
|
|
||||||
class BluetoothDispatcher {
|
class BluetoothDispatcher {
|
||||||
public:
|
public:
|
||||||
BluetoothDispatcher(BluetoothSerial *controller, const char *device_name = "Helicopter", HardwareSerial *serial = &Serial);
|
BluetoothDispatcher(BluetoothSerial *controller, const char *device_name = "Helicopter");
|
||||||
bool initialize();
|
bool initialize(bool loop_on_fail = true, int readTimeoutMS = 2);
|
||||||
void tick();
|
void tick(NewPackageCallback newPackageReceivedCb = nullptr);
|
||||||
|
void onNewDeviceConnected(DeviceConnectedCb deviceConnectedCb);
|
||||||
|
void sendPackage(const char *package, size_t size);
|
||||||
~BluetoothDispatcher();
|
~BluetoothDispatcher();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void _confirmRequestCallback(uint16_t pin);
|
void _onConfirmRequest(uint16_t pin);
|
||||||
void _authCompleteCallback(boolean success);
|
void _onAuthComplete(boolean success);
|
||||||
void _deviceConnectedCallback(esp_bd_addr_t bt_addr);
|
void _onDeviceConnected(BTAddress device);
|
||||||
|
|
||||||
const char *_device_name = nullptr;
|
const char *_device_name = nullptr;
|
||||||
bool _confirmRequestDone = false;
|
|
||||||
BluetoothSerial *_controller = nullptr;
|
BluetoothSerial *_controller = nullptr;
|
||||||
HardwareSerial *_serial = nullptr;
|
DeviceConnectedCb _deviceConnectedCallback = nullptr;
|
||||||
static constexpr uint16_t _buffer_size = 256;
|
constexpr static const char *_tag = "BluetoothDispatcher";
|
||||||
|
|
||||||
|
static constexpr uint16_t _buffer_size = 512;
|
||||||
|
static constexpr uint16_t _available_buffer_size = 500;
|
||||||
mString<_buffer_size> _buffer;
|
mString<_buffer_size> _buffer;
|
||||||
};
|
};
|
||||||
2
main.cpp
2
main.cpp
@@ -10,6 +10,7 @@
|
|||||||
#include "board_pins.h"
|
#include "board_pins.h"
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
|
||||||
TwoWire i2c = TwoWire(0);
|
TwoWire i2c = TwoWire(0);
|
||||||
Barometer barometer(new GyverBME280(i2c));
|
Barometer barometer(new GyverBME280(i2c));
|
||||||
MPU mpu(new MPU6050(MPU6050_DEFAULT_ADDRESS, &i2c));
|
MPU mpu(new MPU6050(MPU6050_DEFAULT_ADDRESS, &i2c));
|
||||||
@@ -19,6 +20,7 @@ BluetoothDispatcher bluetoothDispatcher(new BluetoothSerial());
|
|||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
Serial.setDebugOutput(true);
|
||||||
|
|
||||||
Serial.print("Ininitialize I2C...");
|
Serial.print("Ininitialize I2C...");
|
||||||
Serial.println(i2c.begin(I2C_SDA_PIN, I2C_SCL_PIN, 400000));
|
Serial.println(i2c.begin(I2C_SDA_PIN, I2C_SCL_PIN, 400000));
|
||||||
|
|||||||
Reference in New Issue
Block a user