actions.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /**
  2. * @Author: 李建
  3. * @Date: 2025/3/18 11:34
  4. * Description: 定义了UI 与 逻辑的交互
  5. * Copyright: Copyright (©) 2025 永续绿建. All rights reserved.
  6. */
  7. #include <esp_log.h>
  8. #include <string.h>
  9. #include <nvs_flash.h>
  10. #include "misc/lv_types.h"
  11. #include "widgets/tabview/lv_tabview.h"
  12. #include "gui_guider.h"
  13. #include "main.h"
  14. #include "wifi.h"
  15. #include "actions.h"
  16. #include "lvgl_port.h"
  17. #include "modbus_master.h"
  18. #include "system/miscellaneous_interface.h"
  19. #include "heatpump_controller.h"
  20. LV_IMAGE_DECLARE(_orange_meter_RGB565A8_84x396);
  21. LV_IMAGE_DECLARE(_green_meter_RGB565A8_84x396);
  22. const static char *TAG = "actions";
  23. char mode_str[][8] = {"制冷", "制热", "除湿", "送风", "加湿"};
  24. char speed_str[][8] = {"一档", "二档", "三档", "四档", "五档"};
  25. static uint8_t addr = 1;
  26. /**
  27. * 点击管理密码登录按钮事件
  28. * @param e
  29. */
  30. void on_btn_check_manager_password(lv_event_t *e) {
  31. const char *password = lv_textarea_get_text(guider_ui.SettingPage_ta_1);
  32. if (strcmp(password, "0803") != 0) {
  33. // 显示密码错误标签
  34. lv_obj_remove_flag(guider_ui.SettingPage_lab_password_incorrect, LV_OBJ_FLAG_HIDDEN);
  35. } else {
  36. // 验证通过
  37. lv_obj_add_flag(guider_ui.SettingPage_lab_password_incorrect, LV_OBJ_FLAG_HIDDEN);
  38. // 隐藏密码输入 panel
  39. lv_obj_add_flag(guider_ui.SettingPage_cont_1, LV_OBJ_FLAG_HIDDEN);
  40. // 显示高级设置界面
  41. guider_ui.SettingPage_del = true;
  42. ui_load_scr_animation(&guider_ui, &guider_ui.FactorySettingPage, true,
  43. &guider_ui.SettingPage_del, setup_scr_FactorySettingPage,
  44. LV_SCR_LOAD_ANIM_MOVE_RIGHT, 200, 200, true, true);
  45. }
  46. }
  47. /**
  48. * 点击设置-网络-查找网络按钮事件
  49. * @param e
  50. */
  51. void on_btn_scan_wifi_click(lv_event_t *e) {
  52. lv_obj_clean(guider_ui.SettingPage_list_wifi);
  53. app_wifi_scan();
  54. }
  55. /**
  56. * 点击连接 wifi 按钮
  57. * @param e
  58. */
  59. void on_btn_connect_wifi(lv_event_t *e) {
  60. app_connect_wifi();
  61. }
  62. // 设置界面切换顶部tab按钮时的事件
  63. void on_setting_tabview_changed(lv_event_t *e) {
  64. // uint32_t index = lv_tabview_get_tab_active(guider_ui.SettingPage_tabview_1);
  65. // switch (index) {
  66. // case 0: // 参数设置
  67. // break;
  68. // case 1: // 网络设置
  69. // //app_wifi_scan();
  70. // break;
  71. // case 2:
  72. // default:
  73. // break;
  74. // }
  75. }
  76. /**
  77. * 高级配置-五恒模块选中事件
  78. * @param e
  79. */
  80. void on_set_five_const_module(lv_event_t *e) {
  81. lv_obj_t *cont = (lv_obj_t *) lv_event_get_current_target(e);
  82. lv_obj_t *act_cb = lv_event_get_target(e);
  83. if (act_cb == cont) return;
  84. lv_obj_add_state(act_cb, LV_STATE_CHECKED);
  85. uint32_t count = lv_obj_get_child_count(cont);
  86. // 遍历所有子控件
  87. for (int i = 0; i < count; i++) {
  88. lv_obj_t *child = lv_obj_get_child(cont, i);
  89. if (child != act_cb) {
  90. lv_obj_remove_state(child, LV_STATE_CHECKED);
  91. }
  92. }
  93. // TODO:修改本地配置
  94. }
  95. /**
  96. * 高级配置-设备风阀与分控对应关系
  97. * @param e
  98. * @param valve_index
  99. */
  100. void on_set_fan_valve(lv_event_t *e, uint8_t valve_index) {
  101. lv_obj_t *cont = (lv_obj_t *) lv_event_get_current_target(e);
  102. lv_obj_t *act_cb = lv_event_get_target(e);
  103. if (act_cb == cont) return;
  104. lv_obj_add_state(act_cb, LV_STATE_CHECKED);
  105. uint32_t count = lv_obj_get_child_count(cont);
  106. // 遍历所有子控件
  107. for (int i = 0; i < count; i++) {
  108. lv_obj_t *child = lv_obj_get_child(cont, i);
  109. if (child != act_cb) {
  110. lv_obj_remove_state(child, LV_STATE_CHECKED);
  111. }
  112. }
  113. // TODO: 修改风阀对应配置
  114. }
  115. /**
  116. * 高级配置-恢复出厂设置按钮事件
  117. * @param e
  118. */
  119. void on_reset_factory_setting(lv_event_t *e) {
  120. // 清空nvs
  121. nvs_flash_erase();
  122. // 重启设备
  123. esp_restart();
  124. }
  125. void on_set_inner_addr(uint8_t num) {
  126. if (num)
  127. addr += 1;
  128. else
  129. addr -= 1;
  130. if (addr + num > 255)
  131. addr = 254;
  132. if (addr + num < 1)
  133. addr = 1;
  134. lvgl_port_lock(0);
  135. lv_label_set_text_fmt(guider_ui.FactorySettingPage_lab_inner_addr, "%d", addr);
  136. lvgl_port_unlock();
  137. // 保存到 nvs
  138. system_setting.inner_addr = addr;
  139. save_inner_addr(addr);
  140. }
  141. void on_ac_page_set_power(lv_event_t *e, uint8_t power) {
  142. hp_controller.power = power;
  143. heatpump_set_power(true);
  144. }
  145. void on_set_beep_on_off(lv_event_t *e) {
  146. lv_obj_t *obj = lv_event_get_current_target(e);
  147. if (lv_obj_has_state(obj, LV_STATE_CHECKED)) {
  148. system_setting.sound_on_off = 1;
  149. } else {
  150. system_setting.sound_on_off = 0;
  151. }
  152. save_system_setting(&system_setting);
  153. }
  154. void on_set_allow_valve_on_off(lv_event_t *e) {
  155. lv_obj_t *obj = lv_event_get_current_target(e);
  156. if (lv_obj_has_state(obj, LV_STATE_CHECKED)) {
  157. //system_setting.allow_valve_onoff = 1;
  158. } else {
  159. //system_setting.allow_valve_onoff = 0;
  160. }
  161. save_system_setting(&system_setting);
  162. }
  163. void on_set_screen_auto_off(lv_event_t *e) {
  164. lv_obj_t *obj = lv_event_get_current_target(e);
  165. if (lv_obj_has_state(obj, LV_STATE_CHECKED)) {
  166. system_setting.screen_auto_off = 1;
  167. } else {
  168. system_setting.screen_auto_off = 0;
  169. }
  170. save_system_setting(&system_setting);
  171. }
  172. void on_set_beep_volume(lv_event_t *e) {
  173. const lv_obj_t *slider = lv_event_get_current_target(e);
  174. uint32_t value = lv_slider_get_value(slider);
  175. ESP_LOGD(TAG, "on_set_beep_volume;%d", (uint8_t) value);
  176. system_setting.sound_volume = (uint8_t) value;
  177. save_system_setting(&system_setting);
  178. }
  179. void on_set_screen_off_minute(lv_event_t *e) {
  180. lv_obj_t *spbox = lv_event_get_current_target(e);
  181. int32_t value = lv_spinbox_get_value(spbox);
  182. if (value <= 0) {
  183. value = 1;
  184. }
  185. ESP_LOGD(TAG, "on_set_screen_off_minute;%d", (uint8_t) value);
  186. system_setting.screen_off_minute = (uint8_t) value;
  187. save_system_setting(&system_setting);
  188. }
  189. void on_ota_failed_restart(lv_event_t *e) {
  190. esp_restart();
  191. }
  192. void on_restart_device(lv_event_t *e) {
  193. esp_restart();
  194. }
  195. void on_params_save(lv_event_t *e) {
  196. // 与分风箱同步内机地址 (改到同步任务中)
  197. // mb_param_request_t req = {
  198. // .reg_size = 1,
  199. // .reg_start = 0x58,
  200. // .command = 0x06,
  201. // .slave_addr = 0x01,
  202. // };
  203. // send_request(&req, &system_setting.inner_addr);
  204. // 保存参数
  205. int32_t temp_fix = lv_spinbox_get_value(guider_ui.FactorySettingPage_sp_temp_recoup);
  206. system_setting.fix_temp = (int8_t) temp_fix;
  207. int32_t hum_fix = lv_spinbox_get_value(guider_ui.FactorySettingPage_sp_hum_recoup);
  208. system_setting.fix_hum = (int8_t) hum_fix;
  209. save_system_setting(&system_setting);
  210. }
  211. void on_reset_wifi(lv_event_t *e) {
  212. yx_data_clear(NVS_SSID_KEY);
  213. yx_data_clear(NVS_PASSWORD_KEY);
  214. esp_restart();
  215. }
  216. void on_set_mode(lv_event_t *e, uint8_t mode) {
  217. ESP_LOGI(TAG, "on_set_mode:%d", mode);
  218. hp_controller.mode = mode;
  219. heatpump_set_mode(mode);
  220. if (mode == 1) {
  221. lvgl_port_lock(0);
  222. lv_label_set_text_static(guider_ui.screen_lab_mode, "制冷");
  223. lv_obj_set_style_arc_color(guider_ui.screen_arc_temp, lv_color_hex(0x00d9ff),
  224. LV_PART_INDICATOR | LV_STATE_DEFAULT);
  225. lv_label_set_text_fmt(guider_ui.screen_lab_ac_temp, "%d", hp_controller.cool_set_temp);
  226. lv_obj_add_state(guider_ui.screen_ib_cool, LV_STATE_CHECKED);
  227. lv_obj_remove_state(guider_ui.screen_ib_heat, LV_STATE_CHECKED);
  228. lv_arc_set_range(guider_ui.screen_arc_temp, 7 * 2, 22 * 2);
  229. lv_arc_set_value(guider_ui.screen_arc_temp, hp_controller.cool_set_temp * 2);
  230. lvgl_port_unlock();
  231. } else if (mode == 2) {
  232. lvgl_port_lock(0);
  233. lv_obj_set_style_arc_color(guider_ui.screen_arc_temp, lv_color_hex(0xfa890a),
  234. LV_PART_INDICATOR | LV_STATE_DEFAULT);
  235. lv_label_set_text_static(guider_ui.screen_lab_mode, "制热");
  236. lv_label_set_text_fmt(guider_ui.screen_lab_ac_temp, "%d", hp_controller.heat_set_temp);
  237. lv_obj_remove_state(guider_ui.screen_ib_cool, LV_STATE_CHECKED);
  238. lv_obj_add_state(guider_ui.screen_ib_heat, LV_STATE_CHECKED);
  239. lv_arc_set_range(guider_ui.screen_arc_temp, 30 * 2, 60 * 2);
  240. lv_arc_set_value(guider_ui.screen_arc_temp, hp_controller.heat_set_temp * 2);
  241. lvgl_port_unlock();
  242. }
  243. }
  244. void on_arc_temp_value_changed(lv_event_t *e) {
  245. lv_obj_t *arc = lv_event_get_current_target(e);
  246. int32_t value = lv_arc_get_value(arc);
  247. lvgl_port_lock(0);
  248. lv_label_set_text_fmt(guider_ui.screen_lab_ac_temp, "%d", (uint16_t) value / 2);
  249. lvgl_port_unlock();
  250. }
  251. void on_arc_temp_released(lv_event_t *e) {
  252. lv_obj_t *arc = lv_event_get_current_target(e);
  253. int32_t value = lv_arc_get_value(arc);
  254. if (hp_controller.mode == MODE_COOL) hp_controller.cool_set_temp = value / 2;
  255. if (hp_controller.mode == MODE_HEAT) hp_controller.heat_set_temp = value / 2;
  256. heatpump_set_temp(value / 2);
  257. }
  258. void on_screen_gesture_bottom(lv_event_t *e) {
  259. lv_obj_t *c_obj = lv_event_get_current_target_obj(e);
  260. lv_obj_t *obj = lv_event_get_target_obj(e);
  261. if(c_obj == guider_ui.screen_arc_temp)return;
  262. ui_load_scr_animation(&guider_ui, &guider_ui.SettingPage, guider_ui.SettingPage_del, &guider_ui.screen_del, setup_scr_SettingPage, LV_SCR_LOAD_ANIM_MOVE_BOTTOM, 100, 0, false, false);
  263. }