From 7791f43fb1b328e470ed14facc064f4252ce636e Mon Sep 17 00:00:00 2001 From: uvok Date: Sun, 20 Jul 2025 11:16:11 +0200 Subject: Handle r/w/notify properly signal back bluetooth selection --- src/ble.cpp | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) (limited to 'src/ble.cpp') diff --git a/src/ble.cpp b/src/ble.cpp index 1e0b252..7388b80 100644 --- a/src/ble.cpp +++ b/src/ble.cpp @@ -9,6 +9,12 @@ static NimBLEServer *server; static NimBLEAdvertising *pAdvertising; static NimBLECharacteristic *selectorCharacteristic; +static volatile struct +{ + bool changed; + uint8_t value; +} value_changed; + #define IMAGE_DATA(_, display) display const char *templates[] = { @@ -35,24 +41,28 @@ class BadgeSelectorCallbacks : public NimBLECharacteristicCallbacks if (pCharacteristic == selectorCharacteristic) { + LOG_F("Write!"); const char *val = pCharacteristic->getValue().c_str(); char *end = NULL; long newVal = strtol(val, &end, 10); + String defVal("0"); if (end == NULL || end == val) { LOG_F("Error parsing value\n"); - pCharacteristic->setValue("0"); - pCharacteristic->notify(BLE_HS_CONN_HANDLE_NONE); + pCharacteristic->setValue(defVal); + pCharacteristic->notify(defVal, BLE_HS_CONN_HANDLE_NONE); } else if (newVal >= ARRAY_SIZE(templates)) { LOG_F("Value out of range: %d\n", newVal); - pCharacteristic->setValue("0"); - pCharacteristic->notify(BLE_HS_CONN_HANDLE_NONE); + pCharacteristic->setValue(defVal); + pCharacteristic->notify(defVal, BLE_HS_CONN_HANDLE_NONE); } else { LOG_F("Value set to %d\n", newVal); + value_changed.value = newVal; + value_changed.changed = true; } } } @@ -75,7 +85,7 @@ void de::uvok::badge::ble_init(void) // get pictures auto call = service->createCharacteristic("ca260002-b4bb-46b2-bd06-b7b7a61ea990", NIMBLE_PROPERTY::READ); - // needs to be on heap + // needs to be on heap??? String *s = new String(); for (int i = 0; i < ARRAY_SIZE(templates); i++) { @@ -101,3 +111,22 @@ void de::uvok::badge::ble_advertise(void) pAdvertising->start(10000); } } + +de::uvok::badge::ble_poll_result_t de::uvok::badge::ble_poll(void) +{ + if (value_changed.changed) + { + const uint8_t val = value_changed.value; + value_changed.changed = false; + return (ble_poll_result_t){.has_data = true, .new_template = val}; + } + return (ble_poll_result_t){.has_data = false}; +} + +void de::uvok::badge::ble_set_image(uint8_t image) +{ + LOG_F("Notify BLE: set image to %d\n", image); + String s(image); + selectorCharacteristic->setValue(s); + selectorCharacteristic->notify(s, BLE_HS_CONN_HANDLE_NONE); +} \ No newline at end of file -- cgit v1.2.3