
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;}

??技術交流群
更多往期文章,請點擊“?閱讀原文?”。






