mirror of
https://github.com/HASwitchPlate/openHASP.git
synced 2025-07-28 13:46:36 +00:00
Add ATTR_TEMPLATE for event_timer_clock
This commit is contained in:
parent
c02d039e18
commit
b4a973dda5
@ -1682,7 +1682,7 @@ static hasp_attribute_type_t attribute_common_json(lv_obj_t* obj, uint16_t attr_
|
|||||||
return HASP_ATTR_TYPE_METHOD_OK;
|
return HASP_ATTR_TYPE_METHOD_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
static hasp_attribute_type_t attribute_common_text(lv_obj_t* obj, const char* attr, const char* payload, char** text,
|
static hasp_attribute_type_t attribute_common_text(lv_obj_t* obj, uint16_t attr_hash, const char* payload, char** text,
|
||||||
bool update)
|
bool update)
|
||||||
{
|
{
|
||||||
uint8_t obj_type = obj_get_type(obj);
|
uint8_t obj_type = obj_get_type(obj);
|
||||||
@ -1690,6 +1690,7 @@ static hasp_attribute_type_t attribute_common_text(lv_obj_t* obj, const char* at
|
|||||||
hasp_attr_update_char_const_t list[] = {
|
hasp_attr_update_char_const_t list[] = {
|
||||||
{LV_HASP_BUTTON, ATTR_TEXT, my_btn_set_text, my_btn_get_text},
|
{LV_HASP_BUTTON, ATTR_TEXT, my_btn_set_text, my_btn_get_text},
|
||||||
{LV_HASP_LABEL, ATTR_TEXT, my_label_set_text, my_label_get_text},
|
{LV_HASP_LABEL, ATTR_TEXT, my_label_set_text, my_label_get_text},
|
||||||
|
{LV_HASP_LABEL, ATTR_TEMPLATE, my_obj_set_template, my_obj_get_template},
|
||||||
{LV_HASP_CHECKBOX, ATTR_TEXT, lv_checkbox_set_text, lv_checkbox_get_text},
|
{LV_HASP_CHECKBOX, ATTR_TEXT, lv_checkbox_set_text, lv_checkbox_get_text},
|
||||||
{LV_HASP_TABVIEW, ATTR_TEXT, my_tabview_set_text, my_tabview_get_text},
|
{LV_HASP_TABVIEW, ATTR_TEXT, my_tabview_set_text, my_tabview_get_text},
|
||||||
{LV_HASP_TEXTAREA, ATTR_TEXT, lv_textarea_set_text, lv_textarea_get_text},
|
{LV_HASP_TEXTAREA, ATTR_TEXT, lv_textarea_set_text, lv_textarea_get_text},
|
||||||
@ -1701,7 +1702,7 @@ static hasp_attribute_type_t attribute_common_text(lv_obj_t* obj, const char* at
|
|||||||
};
|
};
|
||||||
|
|
||||||
for(int i = 0; i < sizeof(list) / sizeof(list[0]); i++) {
|
for(int i = 0; i < sizeof(list) / sizeof(list[0]); i++) {
|
||||||
if(obj_type == list[i].obj_type) {
|
if(obj_type == list[i].obj_type && attr_hash == list[i].hash) {
|
||||||
if(update)
|
if(update)
|
||||||
list[i].set(obj, payload);
|
list[i].set(obj, payload);
|
||||||
else
|
else
|
||||||
@ -1711,6 +1712,7 @@ static hasp_attribute_type_t attribute_common_text(lv_obj_t* obj, const char* at
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(attr_hash == ATTR_TEXT || attr_hash == ATTR_TXT) {
|
||||||
// Special cases
|
// Special cases
|
||||||
// {LV_HASP_DROPDOWN, set_text_not_implemented, my_dropdown_get_text},
|
// {LV_HASP_DROPDOWN, set_text_not_implemented, my_dropdown_get_text},
|
||||||
// {LV_HASP_ROLLER, set_text_not_implemented, my_roller_get_text},
|
// {LV_HASP_ROLLER, set_text_not_implemented, my_roller_get_text},
|
||||||
@ -1730,6 +1732,7 @@ static hasp_attribute_type_t attribute_common_text(lv_obj_t* obj, const char* at
|
|||||||
default:
|
default:
|
||||||
return HASP_ATTR_TYPE_NOT_FOUND;
|
return HASP_ATTR_TYPE_NOT_FOUND;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return HASP_ATTR_TYPE_STR;
|
return HASP_ATTR_TYPE_STR;
|
||||||
}
|
}
|
||||||
@ -2622,7 +2625,8 @@ void hasp_process_obj_attribute(lv_obj_t* obj, const char* attribute, const char
|
|||||||
case ATTR_TXT: // TODO: remove
|
case ATTR_TXT: // TODO: remove
|
||||||
LOG_WARNING(TAG_HASP, F(D_ATTRIBUTE_OBSOLETE D_ATTRIBUTE_INSTEAD), attribute, "text");
|
LOG_WARNING(TAG_HASP, F(D_ATTRIBUTE_OBSOLETE D_ATTRIBUTE_INSTEAD), attribute, "text");
|
||||||
case ATTR_TEXT:
|
case ATTR_TEXT:
|
||||||
ret = attribute_common_text(obj, attribute, payload, &text, update);
|
case ATTR_TEMPLATE:
|
||||||
|
ret = attribute_common_text(obj, attr_hash, payload, &text, update);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ATTR_ALIGN:
|
case ATTR_ALIGN:
|
||||||
|
@ -417,6 +417,7 @@ _HASP_ATTRIBUTE(SCALE_END_LINE_WIDTH, scale_end_line_width, lv_style_int_t)
|
|||||||
#define ATTR_COLOR 58979
|
#define ATTR_COLOR 58979
|
||||||
#define ATTR_TXT 9328
|
#define ATTR_TXT 9328
|
||||||
#define ATTR_TEXT 53869
|
#define ATTR_TEXT 53869
|
||||||
|
#define ATTR_TEMPLATE 43290
|
||||||
#define ATTR_SRC 4964
|
#define ATTR_SRC 4964
|
||||||
#define ATTR_ID 6715
|
#define ATTR_ID 6715
|
||||||
#define ATTR_EXT_CLICK_H 46643
|
#define ATTR_EXT_CLICK_H 46643
|
||||||
|
@ -3,6 +3,61 @@
|
|||||||
|
|
||||||
#include "hasplib.h"
|
#include "hasplib.h"
|
||||||
|
|
||||||
|
lv_task_t* my_obj_get_task(const lv_obj_t* obj)
|
||||||
|
{
|
||||||
|
lv_task_t* task = lv_task_get_next(NULL);
|
||||||
|
while(task) {
|
||||||
|
if(task->user_data) {
|
||||||
|
hasp_task_user_data_t* data = (hasp_task_user_data_t*)task->user_data;
|
||||||
|
if(data->obj == obj) return task;
|
||||||
|
}
|
||||||
|
task = lv_task_get_next(task);
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void my_obj_del_task(const lv_obj_t* obj)
|
||||||
|
{
|
||||||
|
lv_task_t* task = my_obj_get_task(obj);
|
||||||
|
if(!task || !task->user_data) return;
|
||||||
|
|
||||||
|
hasp_task_user_data_t* data = (hasp_task_user_data_t*)task->user_data;
|
||||||
|
// hasp_free(data->templ);
|
||||||
|
// hasp_free(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* my_obj_get_template(const lv_obj_t* obj)
|
||||||
|
{
|
||||||
|
lv_task_t* task = my_obj_get_task(obj);
|
||||||
|
if(!task || !task->user_data) return NULL;
|
||||||
|
|
||||||
|
hasp_task_user_data_t* data = (hasp_task_user_data_t*)task->user_data;
|
||||||
|
return data->templ;
|
||||||
|
}
|
||||||
|
|
||||||
|
void my_obj_set_template(lv_obj_t* obj, const char* text)
|
||||||
|
{
|
||||||
|
lv_task_t* task = my_obj_get_task(obj);
|
||||||
|
if(!task || !task->user_data) {
|
||||||
|
// create new task
|
||||||
|
};
|
||||||
|
|
||||||
|
hasp_task_user_data_t* data = (hasp_task_user_data_t*)task->user_data;
|
||||||
|
if(data->templ != D_TIMESTAMP) hasp_free(data->templ);
|
||||||
|
data->templ = NULL;
|
||||||
|
if(!text) return;
|
||||||
|
|
||||||
|
size_t size = strlen(text);
|
||||||
|
if(size == 0) return;
|
||||||
|
|
||||||
|
size++; // terminating \0 character
|
||||||
|
data->templ = (char*)hasp_calloc(sizeof(char), size);
|
||||||
|
if(data->templ)
|
||||||
|
strncpy(data->templ, text, size);
|
||||||
|
else
|
||||||
|
LOG_WARNING(TAG_ATTR, "Failed to allocate memory!");
|
||||||
|
}
|
||||||
|
|
||||||
// the tag data is stored as SERIALIZED JSON data
|
// the tag data is stored as SERIALIZED JSON data
|
||||||
void my_obj_set_tag(lv_obj_t* obj, const char* tag)
|
void my_obj_set_tag(lv_obj_t* obj, const char* tag)
|
||||||
{
|
{
|
||||||
|
@ -135,34 +135,39 @@ void event_timer_calendar(lv_task_t* task)
|
|||||||
void event_timer_clock(lv_task_t* task)
|
void event_timer_clock(lv_task_t* task)
|
||||||
{
|
{
|
||||||
hasp_task_user_data_t* data = (hasp_task_user_data_t*)task->user_data;
|
hasp_task_user_data_t* data = (hasp_task_user_data_t*)task->user_data;
|
||||||
lv_obj_t* obj = NULL;
|
|
||||||
|
|
||||||
if(data) obj = hasp_find_obj_from_page_id(data->pageid, data->objid);
|
if(!data || !data->obj || !lv_debug_check_obj_valid(data->obj)) {
|
||||||
if(!obj || !data) {
|
if(data) {
|
||||||
if(data) lv_mem_free(data); // the object that the user_data points to is gone
|
if(data->templ != D_TIMESTAMP) hasp_free(data->templ);
|
||||||
|
lv_mem_free(data); // the object that the user_data points to is gone}
|
||||||
|
}
|
||||||
lv_task_del(task); // the calendar object for this task was deleted
|
lv_task_del(task); // the calendar object for this task was deleted
|
||||||
LOG_WARNING(TAG_EVENT, "event_timer_clock could not find the linked object");
|
LOG_WARNING(TAG_EVENT, "event_timer_clock could not find the linked object");
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
} else if(!data->templ) {
|
||||||
|
return; // nothing to do
|
||||||
}
|
}
|
||||||
|
|
||||||
timeval curTime;
|
timeval curTime;
|
||||||
int rslt = gettimeofday(&curTime, NULL);
|
int rslt = gettimeofday(&curTime, NULL);
|
||||||
time_t seconds = curTime.tv_sec;
|
|
||||||
tm* timeinfo = localtime(&seconds);
|
|
||||||
(void)rslt; // unused
|
(void)rslt; // unused
|
||||||
|
time_t seconds = curTime.tv_sec;
|
||||||
|
useconds_t tv_msec = curTime.tv_usec / 1000;
|
||||||
|
tm* timeinfo = localtime(&seconds);
|
||||||
|
lv_task_set_period(task, data->interval - tv_msec);
|
||||||
|
|
||||||
char buffer[24] = {0};
|
char buffer[128] = {0};
|
||||||
if(timeinfo->tm_year < 120) {
|
/* if(timeinfo->tm_year < 120) {
|
||||||
snprintf_P(buffer, sizeof(buffer), PSTR("%ld"), seconds);
|
snprintf_P(buffer, sizeof(buffer), PSTR("%ld"), seconds);
|
||||||
} else {
|
} else */
|
||||||
strftime(buffer, sizeof(buffer), D_TIMESTAMP, timeinfo); // Literal String
|
strftime(buffer, sizeof(buffer), data->templ, timeinfo);
|
||||||
}
|
|
||||||
|
|
||||||
// LOG_VERBOSE(TAG_EVENT, "event_timer_clock called with user %d:%d:%d", timeinfo->tm_hour, timeinfo->tm_min,
|
// LOG_VERBOSE(TAG_EVENT, "event_timer_clock called with user %d:%d:%d", timeinfo->tm_hour, timeinfo->tm_min,
|
||||||
// timeinfo->tm_sec);
|
// timeinfo->tm_sec);
|
||||||
|
|
||||||
lv_label_set_text(obj, buffer);
|
if(!strcmp(buffer, lv_label_get_text(data->obj))) return; // No change
|
||||||
lv_task_set_period(task, data->interval);
|
lv_label_set_text(data->obj, buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ============================== Timer Event ============================ */
|
/* ============================== Timer Event ============================ */
|
||||||
|
@ -111,7 +111,7 @@ void object_dispatch_state(uint8_t pageid, uint8_t btnid, const char* payload)
|
|||||||
{
|
{
|
||||||
char topic[64];
|
char topic[64];
|
||||||
char* pagename = haspPages.get_name(pageid);
|
char* pagename = haspPages.get_name(pageid);
|
||||||
if (pagename)
|
if(pagename)
|
||||||
snprintf_P(topic, sizeof(topic), PSTR("%s.b%u"), pagename, btnid);
|
snprintf_P(topic, sizeof(topic), PSTR("%s.b%u"), pagename, btnid);
|
||||||
else
|
else
|
||||||
snprintf_P(topic, sizeof(topic), PSTR(HASP_OBJECT_NOTATION), pageid, btnid);
|
snprintf_P(topic, sizeof(topic), PSTR(HASP_OBJECT_NOTATION), pageid, btnid);
|
||||||
@ -204,16 +204,18 @@ int hasp_parse_json_attributes(lv_obj_t* obj, const JsonObject& doc)
|
|||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void object_add_task(lv_obj_t* obj, uint8_t pageid, uint8_t objid, lv_task_cb_t task_xcb, uint16_t interval)
|
static void object_add_task(lv_obj_t* obj, lv_task_cb_t task_xcb, uint16_t interval)
|
||||||
{
|
{
|
||||||
hasp_task_user_data_t* user_data = (hasp_task_user_data_t*)lv_mem_alloc(sizeof(hasp_task_user_data_t));
|
hasp_task_user_data_t* user_data = (hasp_task_user_data_t*)lv_mem_alloc(sizeof(hasp_task_user_data_t));
|
||||||
if(!user_data) return;
|
if(!user_data) return;
|
||||||
|
|
||||||
user_data->pageid = pageid;
|
user_data->obj = obj;
|
||||||
user_data->objid = objid;
|
user_data->templ = (char*)D_TIMESTAMP;
|
||||||
user_data->interval = interval;
|
user_data->interval = interval;
|
||||||
lv_task_t* task = lv_task_create(task_xcb, 25, LV_TASK_PRIO_LOWEST, (void*)user_data);
|
lv_task_t* task = lv_task_create(task_xcb, interval, LV_TASK_PRIO_LOWEST, (void*)user_data);
|
||||||
(void)task; // unused
|
lv_task_set_repeat_count(task, -1); // Infinite
|
||||||
|
lv_task_ready(task); // trigger it
|
||||||
|
// (void)task; // unused
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -351,7 +353,7 @@ void hasp_new_object(const JsonObject& config, uint8_t& saved_page_id)
|
|||||||
lv_obj_set_event_cb(obj, generic_event_handler);
|
lv_obj_set_event_cb(obj, generic_event_handler);
|
||||||
obj->user_data.objid = LV_HASP_LABEL;
|
obj->user_data.objid = LV_HASP_LABEL;
|
||||||
|
|
||||||
// object_add_task(obj, pageid, id, event_timer_clock, 1000);
|
if(id == 200) object_add_task(obj, event_timer_clock, 1000);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -696,10 +698,9 @@ void hasp_new_object(const JsonObject& config, uint8_t& saved_page_id)
|
|||||||
/* id tag the object */
|
/* id tag the object */
|
||||||
obj->user_data.id = id;
|
obj->user_data.id = id;
|
||||||
|
|
||||||
|
#ifdef HASP_DEBUG
|
||||||
uint8_t temp; // needed for debug tests
|
uint8_t temp; // needed for debug tests
|
||||||
(void)temp;
|
(void)temp;
|
||||||
|
|
||||||
#ifdef HASP_DEBUG
|
|
||||||
/** testing start **/
|
/** testing start **/
|
||||||
if(!hasp_find_id_from_obj(obj, &pageid, &temp)) {
|
if(!hasp_find_id_from_obj(obj, &pageid, &temp)) {
|
||||||
LOG_ERROR(TAG_HASP, F(D_OBJECT_LOST));
|
LOG_ERROR(TAG_HASP, F(D_OBJECT_LOST));
|
||||||
|
@ -16,8 +16,8 @@ const char FP_GROUPID[] PROGMEM = "groupid";
|
|||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
uint8_t pageid;
|
lv_obj_t* obj;
|
||||||
uint8_t objid;
|
char* templ;
|
||||||
uint16_t interval;
|
uint16_t interval;
|
||||||
} hasp_task_user_data_t;
|
} hasp_task_user_data_t;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user