summaryrefslogtreecommitdiff
path: root/src/ble.cpp
diff options
context:
space:
mode:
authoruvok2025-07-20 11:16:11 +0200
committeruvok2025-07-20 12:41:58 +0200
commit7791f43fb1b328e470ed14facc064f4252ce636e (patch)
tree4d6c3647e3d8c4933d53f8f1031aa8bcf30446e9 /src/ble.cpp
parentf5794c6f8b8a1f736ea7a524e72aed46baba4bd6 (diff)
Handle r/w/notify properly
signal back bluetooth selection
Diffstat (limited to 'src/ble.cpp')
-rw-r--r--src/ble.cpp39
1 files changed, 34 insertions, 5 deletions
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