widget support for Berry/MI32 dashboard (#22359)

This commit is contained in:
Christian Baars 2024-10-25 19:25:49 +02:00 committed by GitHub
parent 0f2b3b1898
commit b5a487a595
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 67 additions and 31 deletions

View File

@ -1,6 +1,6 @@
/******************************************************************** /********************************************************************
* Tasmota lib * Tasmota lib
* *
* To use: `import MI32` * To use: `import MI32`
*******************************************************************/ *******************************************************************/
#include "be_constobj.h" #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); extern void be_MI32_set_temp(int slot, int temp_val);
BE_FUNC_CTYPE_DECLARE(be_MI32_set_temp, "", "ii"); 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" #include "be_fixed_MI32.h"
/* @const_object_info_begin /* @const_object_info_begin
@ -35,12 +38,13 @@ module MI32 (scope: global) {
set_bat, ctype_func(be_MI32_set_bat) set_bat, ctype_func(be_MI32_set_bat)
set_hum, ctype_func(be_MI32_set_hum) set_hum, ctype_func(be_MI32_set_hum)
set_temp, ctype_func(be_MI32_set_temp) set_temp, ctype_func(be_MI32_set_temp)
widget, ctype_func(be_MI32_widget)
} }
@const_object_info_end */ @const_object_info_end */
/******************************************************************** /********************************************************************
* Tasmota lib * Tasmota lib
* *
* To use: `import BLE` * To use: `import BLE`
*******************************************************************/ *******************************************************************/

View File

@ -28,14 +28,14 @@
/*********************************************************************************************\ /*********************************************************************************************\
* Native functions mapped to Berry functions * Native functions mapped to Berry functions
* *
* *
\*********************************************************************************************/ \*********************************************************************************************/
extern "C" { extern "C" {
/******************************************************************** /********************************************************************
** MI32 - sensor specific functions ** MI32 - sensor specific functions
********************************************************************/ ********************************************************************/
extern uint32_t MI32numberOfDevices(); extern uint32_t MI32numberOfDevices();
extern char * MI32getDeviceName(uint32_t slot); extern char * MI32getDeviceName(uint32_t slot);
@ -44,15 +44,21 @@ extern "C" {
extern void MI32setTemperatureForSlot(uint32_t slot, float value); extern void MI32setTemperatureForSlot(uint32_t slot, float value);
extern uint8_t * MI32getDeviceMAC(uint32_t slot); 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) { int be_MI32_devices(void) {
return MI32numberOfDevices(); 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); MI32setBatteryForSlot(slot,bat_val);
} }
const char* be_MI32_get_name(int slot){ const char* be_MI32_get_name(int slot){
return MI32getDeviceName(slot); return MI32getDeviceName(slot);
} }
@ -65,14 +71,26 @@ extern "C" {
return buffer; 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); 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); 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 ** 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){ void be_BLE_reg_conn_cb(void* function, uint8_t *buffer){
MI32setBerryConnCB(function,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){ void be_BLE_reg_server_cb(void* function, uint8_t *buffer){
MI32setBerryServerCB(function,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){ void be_BLE_set_service(struct bvm *vm, const char *Svc, bbool discoverAttributes){
bool _discoverAttributes = false; bool _discoverAttributes = false;
if(discoverAttributes){ if(discoverAttributes){
_discoverAttributes = 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);
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; if (MI32setBerryCtxChr(Chr)) return;
be_raisef(vm, "ble_error", "BLE: could not set characteristic"); 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);
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 int32_t argc = be_top(vm); // Get the number of arguments
bool _response = false; bool _response = false;
if(response){ if(response){
_response = 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){ void be_BLE_adv_block(struct bvm *vm, uint8_t *buf, size_t size, uint8_t type){
if(!be_BLE_MAC_size(vm, size)){ if(!be_BLE_MAC_size(vm, size)){
return; return;
} }
@ -190,12 +208,12 @@ extern "C" {
_type = type; _type = type;
} }
if(MI32addMACtoBlockList(buf, _type)) return; if(MI32addMACtoBlockList(buf, _type)) return;
be_raisef(vm, "ble_error", "BLE: could not block MAC"); 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){ void be_BLE_adv_watch(struct bvm *vm, uint8_t *buf, size_t size, uint8_t type){
if(!be_BLE_MAC_size(vm, size)){ if(!be_BLE_MAC_size(vm, size)){
return; return;
} }
@ -331,7 +349,7 @@ __commands
201 add/set advertisement 201 add/set advertisement
202 add/set scan response 202 add/set scan response
211 add/set characteristic 211 add/set characteristic
__response __response
221 onRead 221 onRead
@ -358,5 +376,6 @@ MI32.get_MAC(slot)
MI32.set_bat(slot,int) MI32.set_bat(slot,int)
MI32.set_hum(slot,float) MI32.set_hum(slot,float)
MI32.set_temp(slot,float) MI32.set_temp(slot,float)
MI32.widget(string[,cb])
*/ */

View File

@ -686,7 +686,7 @@ void MI32Init(void) {
if (MI32.mode.init) { return; } if (MI32.mode.init) { return; }
if (TasmotaGlobal.global_state.wifi_down && TasmotaGlobal.global_state.eth_down) { 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) { if (!TasmotaGlobal.global_state.wifi_down) {
@ -919,6 +919,14 @@ extern "C" {
return _name; 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" } //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 * @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 * 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 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 * ... 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 * @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 * 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: 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 byte: properties in a bitfield
* ... next characteristic * ... next characteristic
* *
* @param pSvc * @param pSvc
*/ */
void MI32ConnectionGetCharacteristics(NimBLERemoteService* pSvc); void MI32ConnectionGetCharacteristics(NimBLERemoteService* pSvc);
void MI32ConnectionGetCharacteristics(NimBLERemoteService* pSvc){ void MI32ConnectionGetCharacteristics(NimBLERemoteService* pSvc){
@ -1478,7 +1486,7 @@ bool MI32StartServerTask(){
void MI32ServerSetAdv(NimBLEServer *pServer, std::vector<NimBLEService*>& servicesToStart, bool &shallStartServices); void MI32ServerSetAdv(NimBLEServer *pServer, std::vector<NimBLEService*>& servicesToStart, bool &shallStartServices);
/** /**
* @brief Sets the advertisement message from the data of the context, could be regular advertisement or scan response * @brief Sets the advertisement message from the data of the context, could be regular advertisement or scan response
* *
* @param pServer - our server instance * @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 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 * @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<NimBLEService*>& servic
void MI32ServerSetCharacteristic(NimBLEServer *pServer, std::vector<NimBLEService*>& servicesToStart, bool &shallStartServices); void MI32ServerSetCharacteristic(NimBLEServer *pServer, std::vector<NimBLEService*>& servicesToStart, bool &shallStartServices);
/** /**
* @brief Create a characteristic or modify its value with data of the context * @brief Create a characteristic or modify its value with data of the context
* *
* @param pServer - our server instance * @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 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 * @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]) { if(data[13] == addr[3] && data[14] == addr[4] && data[15] == addr[5]) {
MIBLEsensors[idx].lastTime = Rtc.local_time; MIBLEsensors[idx].lastTime = Rtc.local_time;
return idx; return idx;
} }
} }
return 0xff; return 0xff;
} }
@ -2120,7 +2128,7 @@ void MI32HandleEveryDevice(NimBLEAdvertisedDevice* advertisedDevice, uint8_t add
if(MI32.option.directBridgeMode == 1){ if(MI32.option.directBridgeMode == 1){
MI32.mode.shallTriggerTele = 1; MI32.mode.shallTriggerTele = 1;
_sensor.shallSendMQTT = 1; _sensor.shallSendMQTT = 1;
} }
} }
} }
@ -2156,7 +2164,7 @@ void MI32BLELoop()
if(MI32.mode.connected == 1 && BLERingBufferQueue != nullptr && MI32.mode.triggerBerryConnCB == 0) { if(MI32.mode.connected == 1 && BLERingBufferQueue != nullptr && MI32.mode.triggerBerryConnCB == 0) {
size_t size; size_t size;
BLERingBufferItem_t *q = (BLERingBufferItem_t *)xRingbufferReceive(BLERingBufferQueue, &size, pdMS_TO_TICKS(1)); BLERingBufferItem_t *q = (BLERingBufferItem_t *)xRingbufferReceive(BLERingBufferQueue, &size, pdMS_TO_TICKS(1));
if(q != nullptr){ if(q != nullptr){
if(q->length != 0){ if(q->length != 0){
memcpy(MI32.conCtx->buffer,&q->length,q->length + 1); memcpy(MI32.conCtx->buffer,&q->length,q->length + 1);
@ -2184,7 +2192,7 @@ void MI32BLELoop()
if(MI32.mode.connected == 0 && BLERingBufferQueue != nullptr){ if(MI32.mode.connected == 0 && BLERingBufferQueue != nullptr){
size_t size; size_t size;
BLERingBufferItem_t *q = (BLERingBufferItem_t *)xRingbufferReceive(BLERingBufferQueue, &size, pdMS_TO_TICKS(1)); BLERingBufferItem_t *q = (BLERingBufferItem_t *)xRingbufferReceive(BLERingBufferQueue, &size, pdMS_TO_TICKS(1));
if(q != nullptr){ if(q != nullptr){
if(q->length != 0){ if(q->length != 0){
memcpy(MI32.conCtx->buffer,&q->length,q->length + 1); memcpy(MI32.conCtx->buffer,&q->length,q->length + 1);
@ -2361,10 +2369,13 @@ void CmndMi32Option(void){
\*********************************************************************************************/ \*********************************************************************************************/
#ifdef USE_MI_EXT_GUI #ifdef USE_MI_EXT_GUI
bool MI32HandleWebGUIResponse(void){ bool MI32HandleWebGUIResponse(void){
if(be_MI32Widget.callback != nullptr){
((void(*)())be_MI32Widget.callback)();
}
char tmp[16]; char tmp[16];
WebGetArg(PSTR("wi"), tmp, sizeof(tmp)); WebGetArg(PSTR("wi"), tmp, sizeof(tmp));
if (strlen(tmp)) { if (strlen(tmp)) {
WSContentBegin(200, CT_PLAIN); WSContentBegin(200, CT_PLAIN);
if(MI32.widgetSlot!=0){ if(MI32.widgetSlot!=0){
for(uint32_t i=0;i<32;i++){ for(uint32_t i=0;i<32;i++){
if(bitRead(MI32.widgetSlot,i)){ if(bitRead(MI32.widgetSlot,i)){
@ -2373,6 +2384,8 @@ bool MI32HandleWebGUIResponse(void){
break; break;
} }
} }
} else {
MI32sendBerryWidget();
} }
WSContentEnd(); WSContentEnd();
return true; return true;
@ -2585,7 +2598,7 @@ void MI32InitGUI(void){
#endif //USE_MI_ESP32_ENERGY #endif //USE_MI_ESP32_ENERGY
#ifdef USE_WEBCAM #ifdef USE_WEBCAM
MI32sendCamWidget(); MI32sendCamWidget();
#endif //USE_WEBCAM #endif //USE_WEBCAM
WSContentSend_P(PSTR("</div>")); WSContentSend_P(PSTR("</div>"));
WSContentSpaceButton(BUTTON_MAIN); WSContentSpaceButton(BUTTON_MAIN);
WSContentStop(); WSContentStop();