diff options
author | uvok | 2025-07-20 10:38:45 +0200 |
---|---|---|
committer | uvok | 2025-07-20 10:38:45 +0200 |
commit | f5794c6f8b8a1f736ea7a524e72aed46baba4bd6 (patch) | |
tree | 3cc86e83f5d45346402fa59b7b71307930e30a75 /src | |
parent | ad7f1107328fdd69aa7b808c904df2b052807bbf (diff) |
Implement BLE callbacks
also, extract image config
Diffstat (limited to 'src')
-rw-r--r-- | src/ble.cpp | 73 | ||||
-rw-r--r-- | src/display.cpp | 10 | ||||
-rw-r--r-- | src/images.cfg | 6 |
3 files changed, 81 insertions, 8 deletions
diff --git a/src/ble.cpp b/src/ble.cpp index 4f98679..1e0b252 100644 --- a/src/ble.cpp +++ b/src/ble.cpp @@ -3,22 +3,87 @@ #include <NimBLEDevice.h> #include "badge/config.h" +#include "badge/log.h" static NimBLEServer *server; static NimBLEAdvertising *pAdvertising; +static NimBLECharacteristic *selectorCharacteristic; -void de::uvok::badge::ble_init(void) +#define IMAGE_DATA(_, display) display + +const char *templates[] = { +#include "./images.cfg" +}; + +class BadgeServerCallbacks : public NimBLEServerCallbacks +{ + void onConnect(NimBLEServer *pServer, NimBLEConnInfo &connInfo) override + { + NimBLEServerCallbacks::onConnect(pServer, connInfo); + } + void onDisconnect(NimBLEServer *pServer, NimBLEConnInfo &connInfo, int reason) override + { + NimBLEServerCallbacks::onDisconnect(pServer, connInfo, reason); + } +} badgeServerCallbacks; + +class BadgeSelectorCallbacks : public NimBLECharacteristicCallbacks { + void onWrite(NimBLECharacteristic *pCharacteristic, NimBLEConnInfo &connInfo) override + { + NimBLECharacteristicCallbacks::onWrite(pCharacteristic, connInfo); + + if (pCharacteristic == selectorCharacteristic) + { + const char *val = pCharacteristic->getValue().c_str(); + char *end = NULL; + long newVal = strtol(val, &end, 10); + if (end == NULL || end == val) + { + LOG_F("Error parsing value\n"); + pCharacteristic->setValue("0"); + pCharacteristic->notify(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); + } + else + { + LOG_F("Value set to %d\n", newVal); + } + } + } +} badgeSelectorCallbacks; +void de::uvok::badge::ble_init(void) +{ NimBLEDevice::init("Espadge"); server = NimBLEDevice::createServer(); + server->setCallbacks(&badgeServerCallbacks); + NimBLEService *service = new NimBLEService("ca260000-b4bb-46b2-bd06-b7b7a61ea990"); // read/write current - auto c = service->createCharacteristic("ca260001-b4bb-46b2-bd06-b7b7a61ea990", - NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE || NIMBLE_PROPERTY::NOTIFY); + selectorCharacteristic = + service->createCharacteristic("ca260001-b4bb-46b2-bd06-b7b7a61ea990", + NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::NOTIFY); + selectorCharacteristic->setValue("0"); + selectorCharacteristic->setCallbacks(&badgeSelectorCallbacks); // get pictures - service->createCharacteristic("ca260002-b4bb-46b2-bd06-b7b7a61ea990", NIMBLE_PROPERTY::READ); + auto call = service->createCharacteristic("ca260002-b4bb-46b2-bd06-b7b7a61ea990", NIMBLE_PROPERTY::READ); + + // needs to be on heap + String *s = new String(); + for (int i = 0; i < ARRAY_SIZE(templates); i++) + { + char tmp[32]; + snprintf(tmp, sizeof(tmp), "%d-%s;", i, templates[i]); + s->concat(tmp); + } + call->setValue(s->c_str()); service->start(); diff --git a/src/display.cpp b/src/display.cpp index f115fd1..bc4c8dc 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -6,6 +6,7 @@ #include <GxEPD2_BW.h> #include "badge/config.h" +#include "badge/util.h" #if UVOK_EPAP_DISPLAY == DISPLAY_WAVESHARE_219_YBW // HINT: Update the library code, set budy timeout to 30 or 60 seconds! @@ -85,8 +86,7 @@ void de::uvok::badge::display_demo(void) #undef TEXT_BORDER } -#define IMAGE_DATA_T(name, text) {name##_bits, name##_width, name##_height, text} -#define IMAGE_DATA(name) {name##_bits, name##_width, name##_height, #name} +#define IMAGE_DATA(name, text) {name##_bits, name##_width, name##_height, text} const struct { @@ -95,9 +95,11 @@ const struct const int width; const int height; const char *text; -} imgs[] = {IMAGE_DATA_T(chleepy, "sleepy"), IMAGE_DATA(hungry), IMAGE_DATA_T(hug, "cuddly"), IMAGE_DATA(uvok)}; +} imgs[] = { + #include "./images.cfg" +}; -const int de::uvok::badge::image_count = (sizeof(imgs) / sizeof(imgs[0])); +const int de::uvok::badge::image_count = (ARRAY_LENGTH(imgs)); typedef enum { DISPLAY_PREVIEW, DISPLAY_FULL} display_mode_t; static void displayDo(display_mode_t preview) diff --git a/src/images.cfg b/src/images.cfg new file mode 100644 index 0000000..daae6ae --- /dev/null +++ b/src/images.cfg @@ -0,0 +1,6 @@ +// includes, define macro. +// first is variable vasename, second is text to be displayed +IMAGE_DATA(chleepy, "sleepy"), +IMAGE_DATA(hungry, "hungry"), +IMAGE_DATA(hug, "cuddly"), +IMAGE_DATA(uvok, "uvok") |