All modules and core of the app was created
This commit is contained in:
@@ -36,18 +36,23 @@ bool BluetoothDispatcher::initialize(bool loop_on_fail, int readTimeoutMS) {
|
||||
}
|
||||
}
|
||||
|
||||
void BluetoothDispatcher::tick(NewPackageCallback newPackageReceivedCb) {
|
||||
void BluetoothDispatcher::onNewPackageReceived(NewPackageCallback newPackageReceivedCb) {
|
||||
_newPackageReceivedCb = newPackageReceivedCb;
|
||||
}
|
||||
|
||||
void BluetoothDispatcher::tick() {
|
||||
/* Call the callback, if new package received */
|
||||
while (_controller->available() and _buffer.length() <= _buffer_size) { _buffer += (char)_controller->read(); }
|
||||
while (_controller->available() and _buffer.length() <= _buffer_size) {
|
||||
_buffer += (char)_controller->read();
|
||||
}
|
||||
if (_buffer.endsWith("\r")) {
|
||||
char buffer[_buffer_size];
|
||||
_buffer.substring(0, _buffer.lastIndexOf('\r'), buffer);
|
||||
ESP_LOGD(_tag, "Received new buffer %s", buffer);
|
||||
if (newPackageReceivedCb) {
|
||||
newPackageReceivedCb(buffer);
|
||||
if (_newPackageReceivedCb) {
|
||||
_newPackageReceivedCb(buffer);
|
||||
}
|
||||
_buffer.clear();
|
||||
_controller->write((uint8_t *)"Hello, world!", strlen("Hello, world!"));
|
||||
}
|
||||
if (_buffer.length() > _available_buffer_size) {
|
||||
_buffer.clear();
|
||||
@@ -64,7 +69,7 @@ void BluetoothDispatcher::_onConfirmRequest(uint16_t pin) {
|
||||
_controller->confirmReply(true);
|
||||
}
|
||||
|
||||
void BluetoothDispatcher::_onAuthComplete(boolean success) {
|
||||
void BluetoothDispatcher::_onAuthComplete(boolean success) const {
|
||||
if (success) {
|
||||
ESP_LOGI(_tag, "Pairing success!");
|
||||
} else {
|
||||
@@ -72,7 +77,7 @@ void BluetoothDispatcher::_onAuthComplete(boolean success) {
|
||||
}
|
||||
}
|
||||
|
||||
void BluetoothDispatcher::_onDeviceConnected(BTAddress device) {
|
||||
void BluetoothDispatcher::_onDeviceConnected(BTAddress device) const {
|
||||
ESP_LOGI(_tag, "New device connected: %s", device.toString(true).c_str());
|
||||
if (_deviceConnectedCallback) {
|
||||
_deviceConnectedCallback(device);
|
||||
@@ -88,8 +93,10 @@ 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) {
|
||||
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(BTAddress(param->srv_open.rem_bda));
|
||||
}
|
||||
}
|
||||
@@ -26,22 +26,24 @@ class BluetoothDispatcher {
|
||||
public:
|
||||
BluetoothDispatcher(BluetoothSerial *controller, const char *device_name = "Helicopter");
|
||||
bool initialize(bool loop_on_fail = true, int readTimeoutMS = 2);
|
||||
void tick(NewPackageCallback newPackageReceivedCb = nullptr);
|
||||
void tick();
|
||||
void onNewPackageReceived(NewPackageCallback newPackageReceivedCb);
|
||||
void onNewDeviceConnected(DeviceConnectedCb deviceConnectedCb);
|
||||
void sendPackage(const char *package, size_t size);
|
||||
~BluetoothDispatcher();
|
||||
|
||||
private:
|
||||
void _onConfirmRequest(uint16_t pin);
|
||||
void _onAuthComplete(boolean success);
|
||||
void _onDeviceConnected(BTAddress device);
|
||||
void _onAuthComplete(boolean success) const;
|
||||
void _onDeviceConnected(BTAddress device) const;
|
||||
|
||||
const char *_device_name = nullptr;
|
||||
BluetoothSerial *_controller = nullptr;
|
||||
DeviceConnectedCb _deviceConnectedCallback = nullptr;
|
||||
NewPackageCallback _newPackageReceivedCb = nullptr;
|
||||
constexpr static const char *_tag = "BluetoothDispatcher";
|
||||
|
||||
static constexpr uint16_t _buffer_size = 512;
|
||||
static constexpr uint16_t _available_buffer_size = 500;
|
||||
static constexpr uint16_t _buffer_size = 522;
|
||||
static constexpr uint16_t _available_buffer_size = 512;
|
||||
mString<_buffer_size> _buffer;
|
||||
};
|
||||
Reference in New Issue
Block a user