typedef ret_t (*hmi_model_cmd_exec_t)(hmi_model_cmd_t* cmd, tk_object_t* obj, const char* args);
typedef bool_t (*hmi_model_cmd_can_exec_t)(hmi_model_cmd_t* cmd, tk_object_t* obj, const char* args);
命令对象一般定义为全局变量。
示例
static const hmi_model_cmd_t s_inc_temp_cmd = {
.name = "inc_temp",
.exec = inc_temp_exec,
.can_exec = inc_temp_can_exec,
};
调用函数 hmi_model_add_cmd 注册命令。
ret_t custom_cmds_init(void) {
tk_object_t* model = hmi_service_get_default_model();
hmi_model_add_cmd(model, &s_inc_temp_cmd);
return RET_OK;
}
下面的代码实现了一个命令 inc_temp,用于增加温度属性的值。温度的值小于 100 时,命令可以执行。
static ret_t inc_temp_exec(hmi_model_cmd_t* cmd, tk_object_t* obj, const char* args) {
int temp = tk_object_get_prop_int(obj, PROP_TEMP, 0);
tk_object_set_prop_int(obj, PROP_TEMP, temp + 1);
return RET_OBJECT_CHANGED;
}
static bool_t inc_temp_can_exec(hmi_model_cmd_t* cmd, tk_object_t* obj, const char* args) {
int temp = tk_object_get_prop_int(obj, PROP_TEMP, 0);
return temp < 100;
}
static const hmi_model_cmd_t s_inc_temp_cmd = {
.name = "inc_temp",
.exec = inc_temp_exec,
.can_exec = inc_temp_can_exec,
};
ret_t custom_cmds_init(void) {
tk_object_t* model = hmi_service_get_default_model();
hmi_model_add_cmd(model, &s_inc_temp_cmd);
return RET_OK;
}
更多往期文章,请点击“ 阅读原文 ”。