mirror of
https://github.com/daveallie/crosspoint-reader.git
synced 2026-02-04 22:57:50 +03:00
port to nimble
This commit is contained in:
parent
26b2dc27fa
commit
e45780674d
@ -48,6 +48,7 @@ lib_deps =
|
|||||||
bblanchon/ArduinoJson @ 7.4.2
|
bblanchon/ArduinoJson @ 7.4.2
|
||||||
ricmoo/QRCode @ 0.0.1
|
ricmoo/QRCode @ 0.0.1
|
||||||
links2004/WebSockets @ 2.7.3
|
links2004/WebSockets @ 2.7.3
|
||||||
|
h2zero/NimBLE-Arduino @ 2.3.7
|
||||||
|
|
||||||
[env:default]
|
[env:default]
|
||||||
extends = base
|
extends = base
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
#include "BluetoothActivity.h"
|
#include "BluetoothActivity.h"
|
||||||
|
|
||||||
#include <GfxRenderer.h>
|
#include <GfxRenderer.h>
|
||||||
#include <BLE2902.h>
|
|
||||||
|
|
||||||
#include "MappedInputManager.h"
|
#include "MappedInputManager.h"
|
||||||
#include "fontIds.h"
|
#include "fontIds.h"
|
||||||
@ -29,37 +28,35 @@ void BluetoothActivity::taskTrampoline(void* param) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void BluetoothActivity::startAdvertising() {
|
void BluetoothActivity::startAdvertising() {
|
||||||
BLEDevice::startAdvertising();
|
NimBLEDevice::startAdvertising();
|
||||||
}
|
}
|
||||||
|
|
||||||
void BluetoothActivity::stopAdvertising() {
|
void BluetoothActivity::stopAdvertising() {
|
||||||
BLEDevice::stopAdvertising();
|
NimBLEDevice::stopAdvertising();
|
||||||
}
|
}
|
||||||
|
|
||||||
void BluetoothActivity::onEnter() {
|
void BluetoothActivity::onEnter() {
|
||||||
Activity::onEnter();
|
Activity::onEnter();
|
||||||
|
|
||||||
BLEDevice::init(DEVICE_NAME);
|
NimBLEDevice::init(DEVICE_NAME);
|
||||||
pServer = BLEDevice::createServer();
|
pServer = NimBLEDevice::createServer();
|
||||||
pServer->setCallbacks(&serverCallbacks);
|
pServer->setCallbacks(&serverCallbacks, false);
|
||||||
pService = pServer->createService(SERVICE_UUID);
|
pService = pServer->createService(SERVICE_UUID);
|
||||||
pRequestChar = pService->createCharacteristic(
|
pRequestChar = pService->createCharacteristic(
|
||||||
REQUEST_CHARACTERISTIC_UUID,
|
REQUEST_CHARACTERISTIC_UUID,
|
||||||
BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_WRITE_NR
|
NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::WRITE_NR
|
||||||
);
|
);
|
||||||
pRequestChar->setCallbacks(&requestCallbacks);
|
pRequestChar->setCallbacks(&requestCallbacks);
|
||||||
pResponseChar = pService->createCharacteristic(
|
pResponseChar = pService->createCharacteristic(
|
||||||
RESPONSE_CHARACTERISTIC_UUID,
|
RESPONSE_CHARACTERISTIC_UUID,
|
||||||
BLECharacteristic::PROPERTY_INDICATE
|
NIMBLE_PROPERTY::INDICATE
|
||||||
);
|
);
|
||||||
pResponseChar->addDescriptor(new BLE2902());
|
|
||||||
pService->start();
|
pService->start();
|
||||||
|
|
||||||
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
|
NimBLEAdvertising *pAdvertising = NimBLEDevice::getAdvertising();
|
||||||
pAdvertising->addServiceUUID(SERVICE_UUID);
|
pAdvertising->setName(DEVICE_NAME);
|
||||||
pAdvertising->setScanResponse(true);
|
pAdvertising->addServiceUUID(pService->getUUID());
|
||||||
pAdvertising->setMinPreferred(0x06);
|
pAdvertising->enableScanResponse(true);
|
||||||
pAdvertising->setMinPreferred(0x12);
|
|
||||||
|
|
||||||
renderingMutex = xSemaphoreCreateMutex();
|
renderingMutex = xSemaphoreCreateMutex();
|
||||||
|
|
||||||
@ -94,10 +91,9 @@ void BluetoothActivity::intoState(State newState) {
|
|||||||
{
|
{
|
||||||
// caller sets errorMessage
|
// caller sets errorMessage
|
||||||
file.close();
|
file.close();
|
||||||
auto connId = pServer->getConnId();
|
if (pServer->getConnectedCount() > 0) {
|
||||||
if (connId != ESP_GATT_IF_NONE) {
|
|
||||||
// TODO: send back a response over BLE?
|
// TODO: send back a response over BLE?
|
||||||
pServer->disconnect(connId);
|
pServer->disconnect(pServer->getPeerInfo(0));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -114,8 +110,7 @@ void BluetoothActivity::onExit() {
|
|||||||
|
|
||||||
stopAdvertising();
|
stopAdvertising();
|
||||||
|
|
||||||
pService->stop();
|
NimBLEDevice::deinit(true);
|
||||||
BLEDevice::deinit();
|
|
||||||
|
|
||||||
// Wait until not rendering to delete task
|
// Wait until not rendering to delete task
|
||||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||||
@ -219,12 +214,12 @@ void BluetoothActivity::render() const {
|
|||||||
renderer.displayBuffer();
|
renderer.displayBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
void BluetoothActivity::ServerCallbacks::onConnect(BLEServer* pServer) {
|
void BluetoothActivity::ServerCallbacks::onConnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo) {
|
||||||
Serial.printf("BLE connected\n");
|
Serial.printf("BLE connected\n");
|
||||||
activity->onConnected(true);
|
activity->onConnected(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BluetoothActivity::ServerCallbacks::onDisconnect(BLEServer* pServer) {
|
void BluetoothActivity::ServerCallbacks::onDisconnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo, int reason) {
|
||||||
Serial.printf("BLE disconnected\n");
|
Serial.printf("BLE disconnected\n");
|
||||||
activity->onConnected(false);
|
activity->onConnected(false);
|
||||||
}
|
}
|
||||||
@ -314,8 +309,8 @@ void BluetoothActivity::onRequest(lfbt_message* msg, size_t msg_len) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void BluetoothActivity::RequestCallbacks::onWrite(BLECharacteristic* pCharacteristic, esp_ble_gatts_cb_param_t* param) {
|
void BluetoothActivity::RequestCallbacks::onWrite(NimBLECharacteristic* pCharacteristic, NimBLEConnInfo& connInfo) {
|
||||||
lfbt_message *msg = (lfbt_message*) param->write.value;
|
lfbt_message *msg = (lfbt_message*) pCharacteristic->getValue().data();
|
||||||
Serial.printf("Received BLE message of type %u, txnId %x, length %d\n", msg->type, msg->txnId, param->write.len);
|
Serial.printf("Received BLE message of type %u, txnId %x, length %d\n", msg->type, msg->txnId, pCharacteristic->getValue().length());
|
||||||
activity->onRequest(msg, param->write.len);
|
activity->onRequest(msg, pCharacteristic->getValue().length());
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,9 +3,9 @@
|
|||||||
#include <freertos/semphr.h>
|
#include <freertos/semphr.h>
|
||||||
#include <freertos/task.h>
|
#include <freertos/task.h>
|
||||||
|
|
||||||
#include <BLEDevice.h>
|
#include <NimBLEDevice.h>
|
||||||
#include <BLEUtils.h>
|
#include <NimBLEUtils.h>
|
||||||
#include <BLEServer.h>
|
#include <NimBLEServer.h>
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
|
||||||
@ -61,12 +61,12 @@ class BluetoothActivity final : public Activity {
|
|||||||
void onConnected(bool isConnected);
|
void onConnected(bool isConnected);
|
||||||
void onRequest(lfbt_message *msg, size_t msg_len);
|
void onRequest(lfbt_message *msg, size_t msg_len);
|
||||||
|
|
||||||
class ServerCallbacks : public BLEServerCallbacks {
|
class ServerCallbacks : public NimBLEServerCallbacks {
|
||||||
friend class BluetoothActivity;
|
friend class BluetoothActivity;
|
||||||
BluetoothActivity *activity;
|
BluetoothActivity *activity;
|
||||||
|
|
||||||
void onConnect(BLEServer* pServer);
|
void onConnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo);
|
||||||
void onDisconnect(BLEServer* pServer);
|
void onDisconnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo, int reason);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
explicit ServerCallbacks(BluetoothActivity *activity) : activity(activity) {}
|
explicit ServerCallbacks(BluetoothActivity *activity) : activity(activity) {}
|
||||||
@ -74,11 +74,11 @@ class BluetoothActivity final : public Activity {
|
|||||||
|
|
||||||
ServerCallbacks serverCallbacks;
|
ServerCallbacks serverCallbacks;
|
||||||
|
|
||||||
class RequestCallbacks : public BLECharacteristicCallbacks {
|
class RequestCallbacks : public NimBLECharacteristicCallbacks {
|
||||||
friend class BluetoothActivity;
|
friend class BluetoothActivity;
|
||||||
BluetoothActivity *activity;
|
BluetoothActivity *activity;
|
||||||
|
|
||||||
void onWrite(BLECharacteristic* pCharacteristic, esp_ble_gatts_cb_param_t* param);
|
void onWrite(NimBLECharacteristic* pCharacteristic, NimBLEConnInfo& connInfo);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
explicit RequestCallbacks(BluetoothActivity *activity) : activity(activity) {}
|
explicit RequestCallbacks(BluetoothActivity *activity) : activity(activity) {}
|
||||||
@ -86,10 +86,10 @@ class BluetoothActivity final : public Activity {
|
|||||||
|
|
||||||
RequestCallbacks requestCallbacks;
|
RequestCallbacks requestCallbacks;
|
||||||
|
|
||||||
BLEServer *pServer;
|
NimBLEServer *pServer;
|
||||||
BLEService *pService;
|
NimBLEService *pService;
|
||||||
BLECharacteristic *pRequestChar, *pResponseChar;
|
NimBLECharacteristic *pRequestChar, *pResponseChar;
|
||||||
BLEAdvertising *pAdvertising;
|
NimBLEAdvertising *pAdvertising;
|
||||||
void startAdvertising();
|
void startAdvertising();
|
||||||
void stopAdvertising();
|
void stopAdvertising();
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user