Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add feature : object alias #689

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/lv_conf_v7.h
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,10 @@ typedef struct {
// uint8_t swipeid:4;
void* ext;
// char* action;
#if USE_OBJ_ALIAS > 0
uint16_t aliashash;
char *alias;
#endif // #if USE_OBJ_ALIAS > 0
} lv_obj_user_data_t;

/*1: enable `lv_obj_realaign()` based on `lv_obj_align()` parameters*/
Expand Down
1 change: 1 addition & 0 deletions include/user_config_override-template.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,6 @@
//#define HASP_DEBUG_OBJ_TREE // Output all objects to the log on page changes
//#define HASP_LOG_LEVEL LOG_LEVEL_VERBOSE // LOG_LEVEL_* can be DEBUG, VERBOSE, TRACE, INFO, WARNING, ERROR, CRITICAL, ALERT, FATAL, SILENT
//#define HASP_LOG_TASKS // Also log the Taskname and watermark of ESP32 tasks
#define USE_OBJ_ALIAS 1 // Enable store object alias und accept commands for objects with alias1

#endif // HASP_USER_CONFIG_OVERRIDE_H
55 changes: 41 additions & 14 deletions src/hasp/hasp_attribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1794,6 +1794,16 @@ static hasp_attribute_type_t attribute_common_text(lv_obj_t* obj, uint16_t attr_
}
}

#if USE_OBJ_ALIAS > 0
if(attr_hash == ATTR_ALIAS) {
if(update) {
my_obj_set_alias(obj, payload);
} else {
*text = (char*)my_obj_get_alias(obj);
}
}
#endif // #if USE_OBJ_ALIAS > 0

return HASP_ATTR_TYPE_STR;
}

Expand Down Expand Up @@ -2183,6 +2193,15 @@ static hasp_attribute_type_t attribute_common_val(lv_obj_t* obj, int32_t& val, b
val = lv_tabview_get_tab_act(obj);
break;

case LV_HASP_CPICKER:
if(update) {
lv_cpicker_set_color(obj, lv_color_hex(val));
} else {
lv_color_t color = lv_cpicker_get_color(obj);
val = lv_color_to32(color);
}
break;

default:
return HASP_ATTR_TYPE_NOT_FOUND; // not found
}
Expand All @@ -2195,23 +2214,28 @@ bool attribute_set_normalized_value(lv_obj_t* obj, hasp_update_value_t& value)
if(value.min == value.max) return false; // would cause divide by zero error

int32_t val;
int32_t min;
int32_t max;
if(!my_obj_get_range(obj, min, max)) return false; // range could not be determined

// Limit the value between min and max, adjust if power = 0
if(value.power == 0 || value.val <= value.min) {
val = value.min;
} else if(value.val >= value.max) {
val = value.max;
} else {
val = value.val;
}
if (obj_get_type(obj) != LV_HASP_CPICKER) {
int32_t min;
int32_t max;
if(!my_obj_get_range(obj, min, max)) return false; // range could not be determined

if(min == 0 && max == 1) {
val = val != value.min; // Toggles are set to 0 when val = min, otherwise 1
// Limit the value between min and max, adjust if power = 0
if(value.power == 0 || value.val <= value.min) {
val = value.min;
} else if(value.val >= value.max) {
val = value.max;
} else {
val = value.val;
}

if(min == 0 && max == 1) {
val = val != value.min; // Toggles are set to 0 when val = min, otherwise 1
} else {
val = map(val, value.min, value.max, min, max);
}
} else {
val = map(val, value.min, value.max, min, max);
val = value.val;
}

attribute_common_val(obj, val, true);
Expand Down Expand Up @@ -2698,6 +2722,9 @@ void hasp_process_obj_attribute(lv_obj_t* obj, const char* attribute, const char
// LOG_WARNING(TAG_HASP, F(D_ATTRIBUTE_OBSOLETE D_ATTRIBUTE_INSTEAD), attribute, "text");
case ATTR_TEXT:
case ATTR_TEMPLATE:
#if USE_OBJ_ALIAS > 0
case ATTR_ALIAS:
#endif // #if USE_OBJ_ALIAS > 0
ret = attribute_common_text(obj, attr_hash, payload, &text, update);
break;

Expand Down
3 changes: 3 additions & 0 deletions src/hasp/hasp_attribute.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ void my_obj_set_value_str_text(lv_obj_t* obj, uint8_t part, lv_state_t state, co
void my_obj_set_tag(lv_obj_t* obj, const char* tag);
void my_obj_set_action(lv_obj_t* obj, const char* tag);
void my_obj_set_swipe(lv_obj_t* obj, const char* tag);
void my_obj_set_alias(lv_obj_t* obj, const char* text);
const char* my_obj_get_tag(lv_obj_t* obj);
const char* my_obj_get_action(lv_obj_t* obj);
const char* my_obj_get_swipe(lv_obj_t* obj);
const char* my_obj_get_alias(lv_obj_t* obj);
void my_btnmatrix_map_clear(lv_obj_t* obj);
void my_msgbox_map_clear(lv_obj_t* obj);
void my_line_clear_points(lv_obj_t* obj);
Expand Down Expand Up @@ -501,6 +503,7 @@ _HASP_ATTRIBUTE(SCALE_END_LINE_WIDTH, scale_end_line_width, lv_style_int_t)
#define ATTR_GROUPID 48986
#define ATTR_OBJID 41010
#define ATTR_OBJ 53623
#define ATTR_ALIAS 33840

#define ATTR_TEXT_MAC 38107
#define ATTR_TEXT_IP 41785
Expand Down
52 changes: 52 additions & 0 deletions src/hasp/hasp_attribute_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,58 @@ static uint16_t my_btnmatrix_get_count(const lv_obj_t* btnm)
return ext->btn_cnt;
}

#if USE_OBJ_ALIAS > 0
/**
* Get the text of alias
* @param obj pointer to perent object
* @return alias text
*/
const char* my_obj_get_alias(lv_obj_t* obj)
{
if(!obj) return NULL;

return obj->user_data.alias;
}

/**
* Set the alias of an object
* @param obj pointer to object
* @param text alias text
*/
void my_obj_set_alias(lv_obj_t* obj, const char* text)
{
// If exist old alias string, free up memory
if(obj->user_data.alias) {
hasp_free(obj->user_data.alias);
obj->user_data.aliashash = 0;
obj->user_data.alias = NULL;
}

// new alias is blank
if(text == NULL || text[0] == '\0') {
obj->user_data.aliashash = 0;
obj->user_data.alias = NULL;
return;
}

// calculate hash
uint16_t hash = Parser::get_sdbm(text, UINT16_MAX, true);

// store alias hash in object
obj->user_data.aliashash = hash;

// allocate mem for store alias text and save the pointer in object
const size_t size = strlen(text);
if(char* str = (char*)hasp_malloc(size + 1)) {
strncpy(str, text, size + 1); // copy include 0 termination
str[size] = '\0'; // safety 0 termination
obj->user_data.alias = str;
}

return;
}
#endif // USE_OBJ_ALIAS

#if 0
static bool attribute_lookup_lv_property(uint16_t hash, uint8_t * prop)
{
Expand Down
87 changes: 82 additions & 5 deletions src/hasp/hasp_dispatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ void dispatch_state_eventid(const char* topic, hasp_event_t eventid)
char payload[32];
char eventname[8];

LOG_INFO(TAG_MQTT, "dispatch_state_eventid topic[%s] eid[%d]", topic, (uint16_t)eventid);

Parser::get_event_name(eventid, eventname, sizeof(eventname));
if(eventid == HASP_EVENT_ON || eventid == HASP_EVENT_OFF) {
snprintf_P(payload, sizeof(payload), PSTR("{\"state\":\"%s\"}"), eventname);
Expand Down Expand Up @@ -184,6 +186,59 @@ static inline bool dispatch_parse_button_attribute(const char* topic_p, const ch
return true;
}

#if USE_OBJ_ALIAS > 0
static inline bool dispatch_parse_alias_attribute(const char* topic_p, const char* payload, bool update)
{
bool result = false;

if(*topic_p != '@') return false; // obligated @

topic_p++;

const char *pSeperator = strchr(topic_p, '.');
uint16_t aliaslen = (uint16_t)(pSeperator-topic_p);

uint16_t aliashash = Parser::get_sdbm(topic_p, aliaslen, true);
topic_p = pSeperator;

if(*topic_p != '.') return false; // obligated separator

topic_p++;

LOG_DEBUG(TAG_MSGR, "parse alias attribute : obj alias hash[%d] command[%s] payload[%s]", aliashash, topic_p, payload);

lv_obj_t *top = lv_layer_top();

/* search object on page 0 */
hasp_cmd_process_data_t data = {.topic_p = topic_p, .payload = payload, .update = update};
LOG_DEBUG(TAG_MSGR, "parse alias attribute : search page 0 childs[%d]", lv_obj_count_children(top));
if (hasp_find_obj_from_alias(top, aliashash, false, &data)) {
result = true;
}

/* search object on currently visible page first include subpages (tabview) */
uint8_t current_page = haspPages.get();
LOG_DEBUG(TAG_MSGR, "parse alias attribute : search page %d childs[%d]", current_page, lv_obj_count_children(haspPages.get_obj(current_page)));
if (hasp_find_obj_from_alias(haspPages.get_obj(current_page), aliashash, false, &data)) {
result = true;
}

/* search object on all other pages include subpages (tabview) */
uint8_t page = HASP_START_PAGE;
while (page <= HASP_NUM_PAGES) {
if (page != current_page) {
LOG_DEBUG(TAG_MSGR, "parse alias attribute : search page %d childs[%d]", page, lv_obj_count_children(haspPages.get_obj(page)));
if (hasp_find_obj_from_alias(haspPages.get_obj(page), aliashash, false, &data)) {
result = true;
}
}
page++;
}

return result;
}
#endif // #if USE_OBJ_ALIAS > 0

static void dispatch_input(const char* topic, const char* payload)
{
#if HASP_USE_GPIO > 0
Expand Down Expand Up @@ -310,7 +365,17 @@ static void dispatch_command(const char* topic, const char* payload, bool update
{
/* ================================= Standard payload commands ======================================= */

if(dispatch_parse_button_attribute(topic, payload, update)) return; // matched pxby.attr, first for speed
if(dispatch_parse_button_attribute(topic, payload, update)) {
LOG_DEBUG(TAG_MSGR, "dispatch object matched pxby.attr");
return; // matched pxby.attr, first for speed
}

#if USE_OBJ_ALIAS > 0
if(dispatch_parse_alias_attribute(topic, payload, update)) {
LOG_DEBUG(TAG_MSGR, "dispatch object matched alias.attr");
return; // matched alias.attr
}
#endif // #if USE_OBJ_ALIAS > 0

// check and execute commands from commands array
for(int i = 0; i < nCommands; i++) {
Expand Down Expand Up @@ -611,16 +676,28 @@ void dispatch_config(const char* topic, const char* payload, uint8_t source)

void dispatch_normalized_group_values(hasp_update_value_t& value)
{
if(value.group == 0) return;
// if(value.group == 0) return;

#if HASP_USE_GPIO > 0
gpio_set_normalized_group_values(value); // Update GPIO states first
if(value.group) {
gpio_set_normalized_group_values(value); // Update GPIO states first
}
#endif

#if USE_OBJ_ALIAS == 0
if(value.group)
#else
if(value.group || value.alias)
#endif
object_set_normalized_group_values(value); // Update onsreen objects except originating obj
{
object_set_normalized_group_values(value); // Update onsreen objects except originating obj
}

LOG_VERBOSE(TAG_MSGR, F("GROUP %d value %d (%d-%d)"), value.group, value.val, value.min, value.max);
#if HASP_USE_GPIO > 0
gpio_output_group_values(value.group); // Output new gpio values
if(value.group) {
gpio_output_group_values(value.group); // Output new gpio values
}
#endif
}

Expand Down
Loading