Bug fix in clearpage and code cleanup

This commit is contained in:
fvanroie 2021-01-30 15:39:13 +01:00
parent 7676fa3d0f
commit 377fa31a9b
6 changed files with 70 additions and 69 deletions

View File

@ -100,14 +100,6 @@ lv_font_t * hasp_get_font(uint8_t fontid)
} }
} }
/**
* WakeUp the display using a command instead of touch
*/
void hasp_wakeup()
{
lv_disp_trig_activity(NULL);
}
/** /**
* Check if sleep state needs to be updated * Check if sleep state needs to be updated
*/ */
@ -529,7 +521,7 @@ void haspGetVersion(char * version, size_t len)
void haspClearPage(uint16_t pageid) void haspClearPage(uint16_t pageid)
{ {
lv_obj_t * page = get_page_obj(pageid); lv_obj_t * page = get_page_obj(pageid);
if(!page || pageid > (pageid > sizeof pages / sizeof *pages)) { if(!page || (pageid > HASP_NUM_PAGES)) {
Log.warning(TAG_HASP, F("Invalid page %u"), pageid); Log.warning(TAG_HASP, F("Invalid page %u"), pageid);
} else if(page == lv_layer_sys() /*|| page == lv_layer_top()*/) { } else if(page == lv_layer_sys() /*|| page == lv_layer_top()*/) {
Log.warning(TAG_HASP, F("Cannot clear system layer")); Log.warning(TAG_HASP, F("Cannot clear system layer"));

View File

@ -73,7 +73,6 @@ lv_font_t * hasp_get_font(uint8_t fontid);
bool IRAM_ATTR hasp_update_sleep_state(); bool IRAM_ATTR hasp_update_sleep_state();
void hasp_get_sleep_time(uint16_t & short_time, uint16_t & long_time); void hasp_get_sleep_time(uint16_t & short_time, uint16_t & long_time);
void hasp_set_sleep_time(uint16_t short_time, uint16_t long_time); void hasp_set_sleep_time(uint16_t short_time, uint16_t long_time);
void hasp_wakeup(void);
/********************** /**********************
* MACROS * MACROS

View File

@ -32,6 +32,7 @@
#endif #endif
extern unsigned long debugLastMillis; // UpdateStatus timer extern unsigned long debugLastMillis; // UpdateStatus timer
extern uint8_t hasp_sleep_state;
uint8_t nCommands = 0; uint8_t nCommands = 0;
haspCommand_t commands[16]; haspCommand_t commands[16];
@ -81,39 +82,48 @@ inline void dispatch_process_button_attribute(String strTopic, const char * payl
{ {
// Log.verbose(TAG_MSGR,F("BTN ATTR: %s = %s"), strTopic.c_str(), payload); // Log.verbose(TAG_MSGR,F("BTN ATTR: %s = %s"), strTopic.c_str(), payload);
String strPageId((char *)0); unsigned int pageid, objid;
String strTemp((char *)0); const char * topic_p = strTopic.c_str();
strPageId = strTopic.substring(2, strTopic.indexOf("]")); if(sscanf(topic_p, "p%ub%u.", &pageid, &objid) == 2) { // Literal String
strTemp = strTopic.substring(strTopic.indexOf("]") + 1, strTopic.length());
if(strTemp.startsWith(".b[")) { // OK, continue below
String strObjId((char *)0);
String strAttr((char *)0);
strObjId = strTemp.substring(3, strTemp.indexOf("]")); } else if(sscanf(topic_p, "p[%u]b[%u].", &pageid, &objid) == 2) { // Literal String
strAttr = strTemp.substring(strTemp.indexOf("]") + 1, strTemp.length());
// debugPrintln(strPageId + " && " + strObjId + " && " + strAttr);
int pageid = strPageId.toInt(); // TODO: obsolete old syntax p[x].b[]y
int objid = strObjId.toInt(); // OK, continue below
if(pageid >= 0 && pageid <= 255 && objid >= 0 && objid <= 255) {
hasp_process_attribute((uint8_t)pageid, (uint8_t)objid, strAttr.c_str(), payload);
} // valid page
} else { } else {
return;
unsigned int pageid, objid;
const char * topic_p = strTopic.c_str();
if(sscanf(topic_p, "p%ub%u.", &pageid, &objid) == 2) { // Literal String
while(*topic_p++ != '.') {
// strip to '.' character
}
hasp_process_attribute((uint8_t)pageid, (uint8_t)objid, topic_p, payload);
}
} }
while(*topic_p++ != '.') {
// strip to '.' character
}
hasp_process_attribute((uint8_t)pageid, (uint8_t)objid, topic_p, payload);
// String strPageId((char *)0);
// String strTemp((char *)0);
// strPageId = strTopic.substring(2, strTopic.indexOf("]"));
// strTemp = strTopic.substring(strTopic.indexOf("]") + 1, strTopic.length());
// if(strTemp.startsWith(".b[")) {
// String strObjId((char *)0);
// String strAttr((char *)0);
// strObjId = strTemp.substring(3, strTemp.indexOf("]"));
// strAttr = strTemp.substring(strTemp.indexOf("]") + 1, strTemp.length());
// // debugPrintln(strPageId + " && " + strObjId + " && " + strAttr);
// pageid = strPageId.toInt();
// objid = strObjId.toInt();
// if(pageid >= 0 && pageid <= 255 && objid >= 0 && objid <= 255) {
// hasp_process_attribute(pageid, objid, strAttr.c_str(), payload);
// } // valid page
// }
} }
// objectattribute=value // objectattribute=value
@ -398,11 +408,11 @@ bool dispatch_get_event_state(uint8_t eventid)
case HASP_EVENT_LONG: case HASP_EVENT_LONG:
case HASP_EVENT_HOLD: case HASP_EVENT_HOLD:
return true; return true;
case HASP_EVENT_OFF: // case HASP_EVENT_OFF:
case HASP_EVENT_UP: // case HASP_EVENT_UP:
case HASP_EVENT_SHORT: // case HASP_EVENT_SHORT:
case HASP_EVENT_DOUBLE: // case HASP_EVENT_DOUBLE:
case HASP_EVENT_LOST: // case HASP_EVENT_LOST:
default: default:
return false; return false;
} }
@ -742,6 +752,13 @@ void dispatch_reboot(bool saveConfig)
halRestartMcu(); halRestartMcu();
} }
void dispatch_current_state()
{
dispatch_output_current_page();
dispatch_output_statusupdate(NULL, NULL);
dispatch_output_idle_state(hasp_sleep_state);
}
/******************************************* Command Wrapper Functions *********************************/ /******************************************* Command Wrapper Functions *********************************/
// Periodically publish a JSON string indicating system status // Periodically publish a JSON string indicating system status
@ -791,14 +808,15 @@ void dispatch_output_statusupdate(const char *, const char *)
#endif #endif
} }
void dispatch_calibrate(const char *, const char *) void dispatch_calibrate(const char * topic = NULL, const char * payload = NULL)
{ {
guiCalibrate(); guiCalibrate();
} }
void dispatch_wakeup(const char *, const char *) void dispatch_wakeup(const char *, const char *)
{ {
hasp_wakeup(); dispatch_calibrate();
lv_disp_trig_activity(NULL);
} }
void dispatch_reboot(const char *, const char *) void dispatch_reboot(const char *, const char *)
@ -855,7 +873,7 @@ void dispatchSetup()
/* WARNING: remember to expand the commands array when adding new commands */ /* WARNING: remember to expand the commands array when adding new commands */
} }
void IRAM_ATTR dispatchLoop() void dispatchLoop()
{ {
// Not used // Not used
} }

View File

@ -22,7 +22,7 @@ enum hasp_event_t { // even = released, odd = pressed
/* ===== Default Event Processors ===== */ /* ===== Default Event Processors ===== */
void dispatchSetup(void); void dispatchSetup(void);
void IRAM_ATTR dispatchLoop(void); void dispatchLoop(void);
void dispatchEverySecond(void); void dispatchEverySecond(void);
void dispatchStart(void); void dispatchStart(void);
void dispatchStop(void); void dispatchStop(void);
@ -45,7 +45,7 @@ void dispatch_reboot(bool saveConfig);
void dispatch_output_idle_state(uint8_t state); void dispatch_output_idle_state(uint8_t state);
void dispatch_output_statusupdate(const char *, const char *); void dispatch_output_statusupdate(const char *, const char *);
void dispatch_output_current_page(); void dispatch_current_state();
void dispatch_gpio_input_event(uint8_t pin, uint8_t group, uint8_t eventid); void dispatch_gpio_input_event(uint8_t pin, uint8_t group, uint8_t eventid);
void dispatch_object_event(lv_obj_t * obj, uint8_t eventid); void dispatch_object_event(lv_obj_t * obj, uint8_t eventid);

View File

@ -6,6 +6,7 @@
#include "PubSubClient.h" #include "PubSubClient.h"
#include "hasp/hasp.h"
#include "hasp_mqtt.h" #include "hasp_mqtt.h"
#include "hasp_mqtt_ha.h" #include "hasp_mqtt_ha.h"
@ -38,8 +39,6 @@ EthernetClient mqttNetworkClient;
#include "hasp_config.h" #include "hasp_config.h"
#include "../hasp/hasp_dispatch.h" #include "../hasp/hasp_dispatch.h"
#include "../hasp/hasp.h" // for hasp_sleep_state
extern uint8_t hasp_sleep_state;
#ifdef USE_CONFIG_OVERRIDE #ifdef USE_CONFIG_OVERRIDE
#include "user_config_override.h" #include "user_config_override.h"
@ -115,13 +114,6 @@ static bool mqttPublish(const char * topic, const char * payload, bool retain =
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
// Send changed values OUT // Send changed values OUT
void mqtt_send_current_states()
{
dispatch_output_current_page();
dispatch_output_statusupdate(NULL, NULL);
dispatch_output_idle_state(hasp_sleep_state);
}
bool IRAM_ATTR mqttIsConnected() bool IRAM_ATTR mqttIsConnected()
{ {
return mqttEnabled && mqttClient.connected(); return mqttEnabled && mqttClient.connected();
@ -179,7 +171,7 @@ static void mqtt_message_cb(char * topic, byte * payload, unsigned int length)
} else if(topic == strstr_P(topic, PSTR("homeassistant/status"))) { // HA discovery topic } else if(topic == strstr_P(topic, PSTR("homeassistant/status"))) { // HA discovery topic
if(mqttHAautodiscover && !strcasecmp_P((char *)payload, PSTR("online"))) { if(mqttHAautodiscover && !strcasecmp_P((char *)payload, PSTR("online"))) {
mqtt_send_current_states(); dispatch_current_state();
mqtt_ha_send_backlight(); mqtt_ha_send_backlight();
} }
return; return;
@ -253,13 +245,13 @@ void mqttStart()
switch(mqttClient.state()) { switch(mqttClient.state()) {
case MQTT_CONNECTION_TIMEOUT: case MQTT_CONNECTION_TIMEOUT:
snprintf_P(buffer, sizeof(buffer), PSTR("Network connection timeout")); snprintf_P(buffer, sizeof(buffer), PSTR("Connection timeout"));
break; break;
case MQTT_CONNECTION_LOST: case MQTT_CONNECTION_LOST:
snprintf_P(buffer, sizeof(buffer), PSTR("Network connection lost")); snprintf_P(buffer, sizeof(buffer), PSTR("Connection lost"));
break; break;
case MQTT_CONNECT_FAILED: case MQTT_CONNECT_FAILED:
snprintf_P(buffer, sizeof(buffer), PSTR("Network connection failed")); snprintf_P(buffer, sizeof(buffer), PSTR("Connection failed"));
break; break;
case MQTT_DISCONNECTED: case MQTT_DISCONNECTED:
snprintf_P(buffer, sizeof(buffer), PSTR("Disconnected")); snprintf_P(buffer, sizeof(buffer), PSTR("Disconnected"));
@ -267,10 +259,10 @@ void mqttStart()
case MQTT_CONNECTED: case MQTT_CONNECTED:
break; break;
case MQTT_CONNECT_BAD_PROTOCOL: case MQTT_CONNECT_BAD_PROTOCOL:
snprintf_P(buffer, sizeof(buffer), PSTR("Server doesn't support the requested version of MQTT")); snprintf_P(buffer, sizeof(buffer), PSTR("MQTT version not suported"));
break; break;
case MQTT_CONNECT_BAD_CLIENT_ID: case MQTT_CONNECT_BAD_CLIENT_ID:
snprintf_P(buffer, sizeof(buffer), PSTR("Server rejected the client ID")); snprintf_P(buffer, sizeof(buffer), PSTR("Client ID rejected"));
break; break;
case MQTT_CONNECT_UNAVAILABLE: case MQTT_CONNECT_UNAVAILABLE:
snprintf_P(buffer, sizeof(buffer), PSTR("Server unavailable")); snprintf_P(buffer, sizeof(buffer), PSTR("Server unavailable"));
@ -319,7 +311,7 @@ void mqttStart()
haspReconnect(); haspReconnect();
haspProgressVal(255); haspProgressVal(255);
mqtt_send_current_states(); dispatch_current_state();
} }
void mqttSetup() void mqttSetup()

View File

@ -159,7 +159,7 @@ void mqtt_ha_register_backlight()
// doc[F("brightness")] = true; // doc[F("brightness")] = true;
doc[F("bri_scl")] = 100; doc[F("bri_scl")] = 100;
snprintf_P(buffer, sizeof(buffer), PSTR("hasp_%s-backlight"), halGetMacAddress(0, "").c_str(), mqttNodeName); snprintf_P(buffer, sizeof(buffer), PSTR("hasp_%s-backlight"), halGetMacAddress(0, "").c_str());
doc[F("uniq_id")] = buffer; doc[F("uniq_id")] = buffer;
snprintf_P(buffer, sizeof(buffer), PSTR("%s/light/%s/backlight/config"), discovery_prefix, mqttNodeName); snprintf_P(buffer, sizeof(buffer), PSTR("%s/light/%s/backlight/config"), discovery_prefix, mqttNodeName);
@ -184,11 +184,11 @@ void mqtt_ha_register_moodlight()
doc[F("avty_t")] = F("~LWT"); doc[F("avty_t")] = F("~LWT");
doc[F("bri_stat_t")] = F("~state/moodlight/dim"); doc[F("bri_stat_t")] = F("~state/moodlight/dim");
doc[F("bri_cmd_t")] = F("~command/moodlight/dim"); doc[F("bri_cmd_t")] = F("~command/moodlight/dim");
//doc[F("rgb")] = true; // doc[F("rgb")] = true;
doc[F("bri_scl")] = 100; doc[F("bri_scl")] = 100;
doc[F("rgb_stat_t")] = F("~state/moodlight/rgb"); doc[F("rgb_stat_t")] = F("~state/moodlight/rgb");
doc[F("rgb_cmd_t")] = F("~command/moodlight/rgb"); doc[F("rgb_cmd_t")] = F("~command/moodlight/rgb");
// doc[F("state_value_template")] = F("~command/moodlight/light"); // doc[F("state_value_template")] = F("~command/moodlight/light");
// doc[F("brightness_value_template")] = F("{{ value_json.brightness }}"); // doc[F("brightness_value_template")] = F("{{ value_json.brightness }}");
// doc[F("rgb_command_template")] = F("{{ '%02x%02x%02x0000'| format(red, green, blue) }}"); // doc[F("rgb_command_template")] = F("{{ '%02x%02x%02x0000'| format(red, green, blue) }}");
@ -215,7 +215,7 @@ void mqtt_ha_register_idle()
doc[F("avty_t")] = F("~LWT"); doc[F("avty_t")] = F("~LWT");
doc[F("json_attr_t")] = F("~state/statusupdate"); doc[F("json_attr_t")] = F("~state/statusupdate");
snprintf_P(buffer, sizeof(buffer), PSTR("hasp_%s-idlestate"), halGetMacAddress(0, "").c_str(), mqttNodeName); snprintf_P(buffer, sizeof(buffer), PSTR("hasp_%s-idlestate"), halGetMacAddress(0, "").c_str());
doc[F("uniq_id")] = buffer; doc[F("uniq_id")] = buffer;
// "value_template" : "{{ value | capitalize }}", // "value_template" : "{{ value | capitalize }}",