From b5a487a5952d1b655b40bc27180176031c92dc83 Mon Sep 17 00:00:00 2001 From: Christian Baars Date: Fri, 25 Oct 2024 19:25:49 +0200 Subject: [PATCH] widget support for Berry/MI32 dashboard (#22359) --- lib/libesp32/berry_tasmota/src/be_MI32_lib.c | 8 ++- .../xdrv_52_3_berry_MI32.ino | 51 +++++++++++++------ .../tasmota_xsns_sensor/xsns_62_esp32_mi.ino | 39 +++++++++----- 3 files changed, 67 insertions(+), 31 deletions(-) diff --git a/lib/libesp32/berry_tasmota/src/be_MI32_lib.c b/lib/libesp32/berry_tasmota/src/be_MI32_lib.c index 5078de941..3babcf3c4 100644 --- a/lib/libesp32/berry_tasmota/src/be_MI32_lib.c +++ b/lib/libesp32/berry_tasmota/src/be_MI32_lib.c @@ -1,6 +1,6 @@ /******************************************************************** * Tasmota lib - * + * * To use: `import MI32` *******************************************************************/ #include "be_constobj.h" @@ -25,6 +25,9 @@ BE_FUNC_CTYPE_DECLARE(be_MI32_set_hum, "", "ii"); extern void be_MI32_set_temp(int slot, int temp_val); BE_FUNC_CTYPE_DECLARE(be_MI32_set_temp, "", "ii"); +extern bbool be_MI32_widget(const char *sbuf, void* function); +BE_FUNC_CTYPE_DECLARE(be_MI32_widget, "b", "s[c]"); + #include "be_fixed_MI32.h" /* @const_object_info_begin @@ -35,12 +38,13 @@ module MI32 (scope: global) { set_bat, ctype_func(be_MI32_set_bat) set_hum, ctype_func(be_MI32_set_hum) set_temp, ctype_func(be_MI32_set_temp) + widget, ctype_func(be_MI32_widget) } @const_object_info_end */ /******************************************************************** * Tasmota lib - * + * * To use: `import BLE` *******************************************************************/ diff --git a/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_MI32.ino b/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_MI32.ino index 6db4ec644..2f506828a 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_MI32.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_MI32.ino @@ -28,14 +28,14 @@ /*********************************************************************************************\ * Native functions mapped to Berry functions - * - * + * + * \*********************************************************************************************/ extern "C" { /******************************************************************** ** MI32 - sensor specific functions -********************************************************************/ +********************************************************************/ extern uint32_t MI32numberOfDevices(); extern char * MI32getDeviceName(uint32_t slot); @@ -44,15 +44,21 @@ extern "C" { extern void MI32setTemperatureForSlot(uint32_t slot, float value); extern uint8_t * MI32getDeviceMAC(uint32_t slot); + struct { + const char * data = nullptr; + size_t size = 0; + void* callback = nullptr; + } be_MI32Widget; + int be_MI32_devices(void) { return MI32numberOfDevices(); } - void be_MI32_set_bat(int slot, int bat_val){ + void be_MI32_set_bat(int slot, int bat_val){ MI32setBatteryForSlot(slot,bat_val); } - const char* be_MI32_get_name(int slot){ + const char* be_MI32_get_name(int slot){ return MI32getDeviceName(slot); } @@ -65,14 +71,26 @@ extern "C" { return buffer; } - void be_MI32_set_hum(int slot, int hum_val){ + void be_MI32_set_hum(int slot, int hum_val){ MI32setHumidityForSlot(slot,hum_val); } - void be_MI32_set_temp(int slot, int temp_val){ + void be_MI32_set_temp(int slot, int temp_val){ MI32setTemperatureForSlot(slot,temp_val); } + bool be_MI32_widget(const char* sbuf, void* function){ + if (function){ + be_MI32Widget.callback = function; + } + if(be_MI32Widget.size == 0){ + be_MI32Widget.data = sbuf; + be_MI32Widget.size = strlen(sbuf); + return true; + } + return false; + } + /******************************************************************** ** BLE - generic BLE functions @@ -103,12 +121,12 @@ extern "C" { } void be_BLE_reg_conn_cb(void* function, uint8_t *buffer); - void be_BLE_reg_conn_cb(void* function, uint8_t *buffer){ + void be_BLE_reg_conn_cb(void* function, uint8_t *buffer){ MI32setBerryConnCB(function,buffer); } void be_BLE_reg_server_cb(void* function, uint8_t *buffer); - void be_BLE_reg_server_cb(void* function, uint8_t *buffer){ + void be_BLE_reg_server_cb(void* function, uint8_t *buffer){ MI32setBerryServerCB(function,buffer); } @@ -145,7 +163,7 @@ extern "C" { } void be_BLE_set_service(struct bvm *vm, const char *Svc, bbool discoverAttributes); - void be_BLE_set_service(struct bvm *vm, const char *Svc, bbool discoverAttributes){ + void be_BLE_set_service(struct bvm *vm, const char *Svc, bbool discoverAttributes){ bool _discoverAttributes = false; if(discoverAttributes){ _discoverAttributes = discoverAttributes ; @@ -157,7 +175,7 @@ extern "C" { void be_BLE_set_characteristic(struct bvm *vm, const char *Chr); void be_BLE_set_characteristic(struct bvm *vm, const char *Chr){ - + if (MI32setBerryCtxChr(Chr)) return; be_raisef(vm, "ble_error", "BLE: could not set characteristic"); @@ -166,7 +184,7 @@ extern "C" { void be_BLE_run(struct bvm *vm, uint8_t operation, bbool response, int32_t arg1); void be_BLE_run(struct bvm *vm, uint8_t operation, bbool response, int32_t arg1){ int32_t argc = be_top(vm); // Get the number of arguments - bool _response = false; + bool _response = false; if(response){ _response = response; } @@ -181,7 +199,7 @@ extern "C" { } void be_BLE_adv_block(struct bvm *vm, uint8_t *buf, size_t size, uint8_t type); - void be_BLE_adv_block(struct bvm *vm, uint8_t *buf, size_t size, uint8_t type){ + void be_BLE_adv_block(struct bvm *vm, uint8_t *buf, size_t size, uint8_t type){ if(!be_BLE_MAC_size(vm, size)){ return; } @@ -190,12 +208,12 @@ extern "C" { _type = type; } if(MI32addMACtoBlockList(buf, _type)) return; - + be_raisef(vm, "ble_error", "BLE: could not block MAC"); } void be_BLE_adv_watch(struct bvm *vm, uint8_t *buf, size_t size, uint8_t type); - void be_BLE_adv_watch(struct bvm *vm, uint8_t *buf, size_t size, uint8_t type){ + void be_BLE_adv_watch(struct bvm *vm, uint8_t *buf, size_t size, uint8_t type){ if(!be_BLE_MAC_size(vm, size)){ return; } @@ -331,7 +349,7 @@ __commands 201 add/set advertisement 202 add/set scan response -211 add/set characteristic +211 add/set characteristic __response 221 onRead @@ -358,5 +376,6 @@ MI32.get_MAC(slot) MI32.set_bat(slot,int) MI32.set_hum(slot,float) MI32.set_temp(slot,float) +MI32.widget(string[,cb]) */ \ No newline at end of file diff --git a/tasmota/tasmota_xsns_sensor/xsns_62_esp32_mi.ino b/tasmota/tasmota_xsns_sensor/xsns_62_esp32_mi.ino index b336ec17c..f0d816744 100644 --- a/tasmota/tasmota_xsns_sensor/xsns_62_esp32_mi.ino +++ b/tasmota/tasmota_xsns_sensor/xsns_62_esp32_mi.ino @@ -686,7 +686,7 @@ void MI32Init(void) { if (MI32.mode.init) { return; } if (TasmotaGlobal.global_state.wifi_down && TasmotaGlobal.global_state.eth_down) { - if (!(WIFI_MANAGER == Wifi.config_type || WIFI_MANAGER_RESET_ONLY == Wifi.config_type)) return; + if (!(WIFI_MANAGER == Wifi.config_type || WIFI_MANAGER_RESET_ONLY == Wifi.config_type)) return; } if (!TasmotaGlobal.global_state.wifi_down) { @@ -919,6 +919,14 @@ extern "C" { return _name; } + void MI32sendBerryWidget() { + if(be_MI32Widget.size != 0) { + WSContentSend(be_MI32Widget.data, be_MI32Widget.size); + be_MI32Widget.data = nullptr; + be_MI32Widget.size = 0; + } + } + } //extern "C" /*********************************************************************************************\ @@ -1187,7 +1195,7 @@ bool MI32ConnectActiveSensor(){ // only use inside a task !! /** * @brief Retrieves all services of the connected BLE device and stores the result into the transfer buffer of Berry's BLE module -* buffer format: + * buffer format: * first byte: number of services * next byte: format of the UUID in bits, next N bytes: the UUID as 16-bit-uint or uint8_t buffer of 16 bytes * ... next service @@ -1212,13 +1220,13 @@ void MI32ConnectionGetServices(){ /** * @brief Retrieves all characteristics of the given service and stores the result into the transfer buffer of Berry's BLE module - * buffer format: + * buffer format: * first byte: number of characteristics * next byte: format of the UUID in bits, next N bytes: the UUID as 16-bit-uint or uint8_t buffer of 16 bytes * next byte: properties in a bitfield * ... next characteristic - * - * @param pSvc + * + * @param pSvc */ void MI32ConnectionGetCharacteristics(NimBLERemoteService* pSvc); void MI32ConnectionGetCharacteristics(NimBLERemoteService* pSvc){ @@ -1478,7 +1486,7 @@ bool MI32StartServerTask(){ void MI32ServerSetAdv(NimBLEServer *pServer, std::vector& servicesToStart, bool &shallStartServices); /** * @brief Sets the advertisement message from the data of the context, could be regular advertisement or scan response - * + * * @param pServer - our server instance * @param servicesToStart - for the first run, this vector holds all our services, would not be used for later modifications of the advertisement message * @param shallStartServices - true only for the first call, which will finish the construction of the server by starting all services @@ -1570,7 +1578,7 @@ void MI32ServerSetAdv(NimBLEServer *pServer, std::vector& servic void MI32ServerSetCharacteristic(NimBLEServer *pServer, std::vector& servicesToStart, bool &shallStartServices); /** * @brief Create a characteristic or modify its value with data of the context - * + * * @param pServer - our server instance * @param servicesToStart - before the finish of the server construction, a characteristic and maybe the holding service will be created and added to this vector * @param shallStartServices - true, if the server construction is not finished by first setting of advertisement data @@ -2083,7 +2091,7 @@ uint16_t MI32checkRPA(uint8_t *addr) { if(data[13] == addr[3] && data[14] == addr[4] && data[15] == addr[5]) { MIBLEsensors[idx].lastTime = Rtc.local_time; return idx; - } + } } return 0xff; } @@ -2120,7 +2128,7 @@ void MI32HandleEveryDevice(NimBLEAdvertisedDevice* advertisedDevice, uint8_t add if(MI32.option.directBridgeMode == 1){ MI32.mode.shallTriggerTele = 1; _sensor.shallSendMQTT = 1; - } + } } } @@ -2156,7 +2164,7 @@ void MI32BLELoop() if(MI32.mode.connected == 1 && BLERingBufferQueue != nullptr && MI32.mode.triggerBerryConnCB == 0) { size_t size; BLERingBufferItem_t *q = (BLERingBufferItem_t *)xRingbufferReceive(BLERingBufferQueue, &size, pdMS_TO_TICKS(1)); - + if(q != nullptr){ if(q->length != 0){ memcpy(MI32.conCtx->buffer,&q->length,q->length + 1); @@ -2184,7 +2192,7 @@ void MI32BLELoop() if(MI32.mode.connected == 0 && BLERingBufferQueue != nullptr){ size_t size; BLERingBufferItem_t *q = (BLERingBufferItem_t *)xRingbufferReceive(BLERingBufferQueue, &size, pdMS_TO_TICKS(1)); - + if(q != nullptr){ if(q->length != 0){ memcpy(MI32.conCtx->buffer,&q->length,q->length + 1); @@ -2361,10 +2369,13 @@ void CmndMi32Option(void){ \*********************************************************************************************/ #ifdef USE_MI_EXT_GUI bool MI32HandleWebGUIResponse(void){ + if(be_MI32Widget.callback != nullptr){ + ((void(*)())be_MI32Widget.callback)(); + } char tmp[16]; WebGetArg(PSTR("wi"), tmp, sizeof(tmp)); if (strlen(tmp)) { - WSContentBegin(200, CT_PLAIN); + WSContentBegin(200, CT_PLAIN); if(MI32.widgetSlot!=0){ for(uint32_t i=0;i<32;i++){ if(bitRead(MI32.widgetSlot,i)){ @@ -2373,6 +2384,8 @@ bool MI32HandleWebGUIResponse(void){ break; } } + } else { + MI32sendBerryWidget(); } WSContentEnd(); return true; @@ -2585,7 +2598,7 @@ void MI32InitGUI(void){ #endif //USE_MI_ESP32_ENERGY #ifdef USE_WEBCAM MI32sendCamWidget(); -#endif //USE_WEBCAM +#endif //USE_WEBCAM WSContentSend_P(PSTR("")); WSContentSpaceButton(BUTTON_MAIN); WSContentStop();