BluetoothDispatcher refactoring

This commit is contained in:
2024-01-06 12:11:06 +07:00
parent b86af180ab
commit 3c2889b8f7
3 changed files with 64 additions and 37 deletions

View File

@@ -1,5 +1,6 @@
#include "BluetoothSerial.h"
#include "HardwareSerial.h"
#include "esp_log.h"
#include "mString.h"
/* Check the ESP configuration */
@@ -18,24 +19,29 @@
#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 {
public:
BluetoothDispatcher(BluetoothSerial *controller, const char *device_name = "Helicopter", HardwareSerial *serial = &Serial);
bool initialize();
void tick();
BluetoothDispatcher(BluetoothSerial *controller, const char *device_name = "Helicopter");
bool initialize(bool loop_on_fail = true, int readTimeoutMS = 2);
void tick(NewPackageCallback newPackageReceivedCb = nullptr);
void onNewDeviceConnected(DeviceConnectedCb deviceConnectedCb);
void sendPackage(const char *package, size_t size);
~BluetoothDispatcher();
private:
void _confirmRequestCallback(uint16_t pin);
void _authCompleteCallback(boolean success);
void _deviceConnectedCallback(esp_bd_addr_t bt_addr);
void _onConfirmRequest(uint16_t pin);
void _onAuthComplete(boolean success);
void _onDeviceConnected(BTAddress device);
const char *_device_name = nullptr;
bool _confirmRequestDone = false;
BluetoothSerial *_controller = nullptr;
HardwareSerial *_serial = nullptr;
static constexpr uint16_t _buffer_size = 256;
DeviceConnectedCb _deviceConnectedCallback = nullptr;
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;
};