actions.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 <esp_app_desc.h>
  11. #include "misc/lv_types.h"
  12. #include "widgets/tabview/lv_tabview.h"
  13. #include "gui_guider.h"
  14. #include "main.h"
  15. #include "wifi.h"
  16. #include "actions.h"
  17. #include "fp_controller.h"
  18. #include "lvgl_port.h"
  19. #include "modbus_master.h"
  20. #include "system/miscellaneous_interface.h"
  21. LV_IMAGE_DECLARE(_orange_meter_RGB565A8_84x396);
  22. LV_IMAGE_DECLARE(_green_meter_RGB565A8_84x396);
  23. LV_IMAGE_DECLARE(_power_open_RGB565A8_36x36);
  24. LV_IMAGE_DECLARE(_nxh_off_RGB565A8_80x80);
  25. LV_IMAGE_DECLARE(_js_on_RGB565A8_80x80);
  26. LV_IMAGE_DECLARE(_auto_on_RGB565A8_80x80);
  27. LV_IMAGE_DECLARE(_xf_on_RGB565A8_80x80);
  28. const static char *TAG = "actions";
  29. extern void set_ac_mode(lv_ui *ui, uint16_t mode);
  30. //
  31. /**
  32. * 点击管理密码登录按钮事件
  33. * @param e
  34. */
  35. void on_btn_check_manager_password(lv_event_t *e) {
  36. const char *password = lv_textarea_get_text(guider_ui.SettingPage_ta_1);
  37. if (strcmp(password, "0803") != 0) {
  38. // 显示密码错误标签
  39. lv_obj_remove_flag(guider_ui.SettingPage_lab_password_incorrect, LV_OBJ_FLAG_HIDDEN);
  40. } else {
  41. // 验证通过
  42. lv_obj_add_flag(guider_ui.SettingPage_lab_password_incorrect, LV_OBJ_FLAG_HIDDEN);
  43. // 隐藏密码输入 panel
  44. lv_obj_add_flag(guider_ui.SettingPage_cont_1, LV_OBJ_FLAG_HIDDEN);
  45. // 显示高级设置界面
  46. guider_ui.SettingPage_del = true;
  47. ui_load_scr_animation(&guider_ui, &guider_ui.FactorySettingPage, true,
  48. &guider_ui.SettingPage_del, setup_scr_FactorySettingPage,
  49. LV_SCR_LOAD_ANIM_MOVE_RIGHT, 200, 200, true, true);
  50. }
  51. }
  52. /**
  53. * 点击设置-开关机按钮事件
  54. * @param e
  55. */
  56. void on_ac_page_set_power(lv_event_t *e, uint8_t power) {
  57. ac_status.power = power;
  58. ac_set_power(true);
  59. }
  60. /**
  61. * 点击设置-开关机按钮事件
  62. * @param e
  63. */
  64. void on_btn_power_click(lv_event_t *e) {
  65. lv_obj_t *obj = lv_event_get_current_target(e);
  66. if (lv_obj_has_state(obj, LV_STATE_CHECKED)) {
  67. ac_status.power = 1;
  68. } else {
  69. ac_status.power = 0;
  70. }
  71. ac_set_power(true);
  72. }
  73. /**
  74. * 点击设置-网络-查找网络按钮事件
  75. * @param e
  76. */
  77. void on_btn_scan_wifi_click(lv_event_t *e) {
  78. lv_obj_clean(guider_ui.SettingPage_list_wifi);
  79. app_wifi_scan();
  80. }
  81. /**
  82. * 点击连接 wifi 按钮
  83. * @param e
  84. */
  85. void on_btn_connect_wifi(lv_event_t *e) {
  86. app_connect_wifi();
  87. }
  88. /**
  89. * 切换至主页面
  90. * @param e
  91. */
  92. void switch_screen_main_set(lv_event_t *e) {
  93. lv_ui * ui = lv_event_get_user_data(e);
  94. // 设置温度及滑动和风速状态
  95. lvgl_port_lock(-1);
  96. // lv_label_set_text_fmt(ui->screen_main_label_humSet_vul, "%d%%", ac_status.set_max_hum);
  97. // lv_slider_set_value(ui->screen_main_slider_speedSet_sign, ac_status.fan_speed*20, LV_ANIM_ON);
  98. // lv_label_set_text_fmt(ui->screen_main_label_spedSet_vul, "%d档",ac_status.fan_speed);
  99. // lv_slider_set_value(ui->screen_main_slider_humSet_sign, ac_status.set_max_hum, LV_ANIM_ON);
  100. set_ac_mode(ui, ac_status.mode);
  101. ac_set_power(true);
  102. // 设置电源状态
  103. lv_obj_add_state(ui->screen_main_imgbtn_power, ac_status.power == 1 ? LV_STATE_CHECKED : LV_STATE_DEFAULT);
  104. lvgl_port_unlock();
  105. }
  106. /**
  107. * 切换至设置页面
  108. * @param e
  109. */
  110. void switch_setting_page_set(lv_event_t *e) {
  111. lv_ui * ui = lv_event_get_user_data(e);
  112. lvgl_port_lock(-1);
  113. if(system_setting.sound_on_off)
  114. lv_obj_add_state(ui->SettingPage_sw_sound, LV_STATE_CHECKED);
  115. else
  116. lv_obj_remove_state(ui->SettingPage_sw_sound, LV_STATE_CHECKED);
  117. if(system_setting.screen_auto_off)
  118. lv_obj_add_state(ui->SettingPage_sw_1, LV_STATE_CHECKED);
  119. else
  120. lv_obj_remove_state(ui->SettingPage_sw_1, LV_STATE_CHECKED);
  121. lv_slider_set_value(ui->SettingPage_slider_sound_level, system_setting.sound_volume, false);
  122. lv_spinbox_set_value(ui->SettingPage_spinbox_2,system_setting.screen_off_minute);
  123. lvgl_port_unlock();
  124. }
  125. //刷新ui
  126. void refresh_ui_error(lv_event_t *e)
  127. {
  128. lv_ui * ui = lv_event_get_user_data(e);
  129. char str[320]={0};
  130. int index=0;
  131. lvgl_port_lock(-1);
  132. if(ac_status.error_code==0)
  133. {
  134. lv_label_set_text_static(ui->FactorySettingPage_label_detail_error, "NONE");
  135. } else{
  136. index=0;
  137. for(int i=0;i<8;i++)
  138. {
  139. if(ac_status.error_code&(1<<i))
  140. {
  141. //sprintf(str,"%s:%s\n",xf_fault[i].code,xf_fault[i].description);
  142. str[index++]='E';
  143. str[index++]='0'+i+1;
  144. str[index++]=':';
  145. for(int j=0;j<strlen((const char*)xf_fault[i].description);j++)
  146. {
  147. str[index++]=xf_fault[i].description[j];
  148. }
  149. str[index++]='\n';
  150. }
  151. }
  152. str[index++]=0;
  153. lv_label_set_text(ui->FactorySettingPage_label_detail_error, str);
  154. }
  155. lvgl_port_unlock();
  156. }
  157. /**
  158. * 切换至高级设置页面
  159. * @param e
  160. */
  161. void switch_factory_setting_page_set(lv_event_t *e) {
  162. lv_ui * ui = lv_event_get_user_data(e);
  163. lvgl_port_lock(-1);
  164. lv_spinbox_set_value(guider_ui.FactorySettingPage_lab_inner_addr, system_setting.inner_addr);
  165. lvgl_port_unlock();
  166. //refresh_ui_error(e);
  167. }
  168. /**
  169. * 重置滤网剩余寿命
  170. * @param e
  171. */
  172. void on_btn_filter_life_remain_clear(lv_event_t *e) {
  173. ac_status.filter_used_time=0;
  174. ac_status.filter_life_remaining=100;
  175. xf_set_filter_life_remain(true);
  176. }
  177. void on_setting_tabview_changed(lv_event_t *e) {
  178. }
  179. void on_fan_vol_refresh(lv_event_t *e) {
  180. lv_label_set_text_fmt(guider_ui.FactorySettingPage_lab_inner_addr,"%d",system_setting.inner_addr);
  181. lv_spinbox_set_value(guider_ui.FactorySettingPage_sp_temp_recoup,system_setting.fix_temp);
  182. lv_spinbox_set_value(guider_ui.FactorySettingPage_sp_hum_recoup,system_setting.fix_hum);
  183. }
  184. void on_fan_vol_save(lv_event_t *e) {
  185. lv_ui * ui = lv_event_get_user_data(e);
  186. int32_t temp_fix = lv_spinbox_get_value(guider_ui.FactorySettingPage_sp_temp_recoup);
  187. if ((temp_fix>=0)&&(temp_fix<99)) {
  188. system_setting.fix_temp = (int8_t) temp_fix;
  189. }
  190. int32_t hum_fix = lv_spinbox_get_value(guider_ui.FactorySettingPage_sp_hum_recoup);
  191. if ((hum_fix>=0)&&(hum_fix<99)) {
  192. system_setting.fix_hum = (int8_t) hum_fix;
  193. }
  194. int32_t inner_addr=lv_spinbox_get_value(guider_ui.FactorySettingPage_lab_inner_addr);
  195. if ((inner_addr>0)&&(inner_addr<255)) {
  196. system_setting.inner_addr=inner_addr;
  197. }
  198. save_system_setting(&system_setting);
  199. }
  200. void on_reset_factory_setting(lv_event_t *e) {
  201. // 清空nvs
  202. nvs_flash_erase();
  203. // 重启设备
  204. esp_restart();
  205. }
  206. void on_set_xf_mode(lv_event_t *e) {
  207. lv_obj_t *cont = (lv_obj_t *) lv_event_get_current_target(e);
  208. lv_obj_t *act_cb = lv_event_get_target(e);
  209. if (act_cb == cont) return;
  210. lvgl_port_lock(-1);
  211. lv_obj_add_state(act_cb, LV_STATE_CHECKED);
  212. uint32_t count = lv_obj_get_child_count(cont);
  213. // 遍历所有子控件
  214. for (int i = 0; i < count; i++) {
  215. lv_obj_t *child = lv_obj_get_child(cont, i);
  216. if (child != act_cb) {
  217. lv_obj_remove_state(child, LV_STATE_CHECKED);
  218. }
  219. }
  220. lvgl_port_unlock();
  221. if (act_cb == guider_ui.screen_main_imgbtn_cold) {
  222. ac_status.mode = COLD;
  223. lv_obj_set_style_arc_color(guider_ui.screen_main_arc_temp, lv_color_hex(0x2cbccc), LV_PART_INDICATOR|LV_STATE_DEFAULT);
  224. lv_obj_set_style_border_color(guider_ui.screen_main_arc_temp, lv_color_hex(0x2cbccc), LV_PART_KNOB|LV_STATE_DEFAULT);
  225. }
  226. else if (act_cb == guider_ui.screen_main_imgbtn_heat) {
  227. lv_obj_set_style_arc_color(guider_ui.screen_main_arc_temp, lv_color_hex(0xff4200), LV_PART_INDICATOR|LV_STATE_DEFAULT);
  228. lv_obj_set_style_border_color(guider_ui.screen_main_arc_temp, lv_color_hex(0xff4200), LV_PART_KNOB|LV_STATE_DEFAULT);
  229. ac_status.mode =HEAT ;
  230. } else if (act_cb == guider_ui.screen_main_imgbtn_tf) {
  231. lv_obj_set_style_arc_color(guider_ui.screen_main_arc_temp, lv_color_hex(0x00912c), LV_PART_INDICATOR|LV_STATE_DEFAULT);
  232. lv_obj_set_style_border_color(guider_ui.screen_main_arc_temp, lv_color_hex(0x00912c), LV_PART_KNOB|LV_STATE_DEFAULT);
  233. ac_status.mode = TF;
  234. }
  235. xf_set_mode(true);
  236. }
  237. static uint8_t addr = 1;
  238. void on_set_inner_addr(uint8_t num) {
  239. if (num)
  240. addr += 1;
  241. else
  242. addr -= 1;
  243. if (addr + num > 255)
  244. addr = 254;
  245. if (addr + num < 1)
  246. addr = 1;
  247. lvgl_port_lock(0);
  248. lv_label_set_text_fmt(guider_ui.FactorySettingPage_lab_inner_addr, "%d", addr);
  249. lvgl_port_unlock();
  250. // 保存到 nvs
  251. system_setting.inner_addr = addr;
  252. save_inner_addr(addr);
  253. }
  254. void on_set_beep_on_off(lv_event_t * e) {
  255. lv_obj_t *obj = lv_event_get_current_target(e);
  256. if (lv_obj_has_state(obj, LV_STATE_CHECKED)) {
  257. system_setting.sound_on_off = 1;
  258. } else {
  259. system_setting.sound_on_off = 0;
  260. }
  261. save_system_setting(&system_setting);
  262. }
  263. void on_set_screen_auto_off(lv_event_t * e) {
  264. lv_obj_t *obj = lv_event_get_current_target(e);
  265. if (lv_obj_has_state(obj, LV_STATE_CHECKED)) {
  266. system_setting.screen_auto_off = 1;
  267. } else {
  268. system_setting.screen_auto_off = 0;
  269. }
  270. save_system_setting(&system_setting);
  271. }
  272. void on_set_beep_volume(lv_event_t* e) {
  273. const lv_obj_t *slider = lv_event_get_current_target(e);
  274. uint32_t value = lv_slider_get_value(slider);
  275. ESP_LOGD(TAG, "on_set_beep_volume;%d", (uint8_t) value);
  276. system_setting.sound_volume = (uint8_t) value;
  277. save_system_setting(&system_setting);
  278. }
  279. void on_set_screen_off_minute(lv_event_t * e) {
  280. lv_obj_t *spbox = lv_event_get_current_target(e);
  281. int32_t value = lv_spinbox_get_value(spbox);
  282. ESP_LOGD(TAG, "on_set_screen_off_minute;%d", (uint8_t) value);
  283. system_setting.screen_off_minute = (uint8_t) value;
  284. save_system_setting(&system_setting);
  285. }
  286. void setting_page_init(lv_ui *ui) {
  287. static lv_style_t style_SettingPage_tabview_1_extra_btnm_items_default;
  288. ui_init_style(&style_SettingPage_tabview_1_extra_btnm_items_default);
  289. lv_style_set_text_color(&style_SettingPage_tabview_1_extra_btnm_items_default, lv_color_hex(0xffb204));
  290. lv_style_set_text_font(&style_SettingPage_tabview_1_extra_btnm_items_default, &lv_font_Alibaba_PuHuiTi_Medium_16);
  291. lv_style_set_text_opa(&style_SettingPage_tabview_1_extra_btnm_items_default, 255);
  292. lv_obj_add_style(lv_tabview_get_tab_bar(ui->SettingPage_tabview_1),
  293. &style_SettingPage_tabview_1_extra_btnm_items_default, LV_PART_MAIN | LV_STATE_DEFAULT);
  294. lv_label_set_text(ui->SettingPage_lab_wifi_status, get_wifi_status() == 1 ? "已连接" : "未连接");
  295. // 填充“关于”页面信息
  296. esp_app_desc_t * desc = esp_app_get_description();
  297. lv_label_set_text(ui->SettingPage_lab_version, desc->version);
  298. char device_serial[12];
  299. get_device_serial(device_serial);
  300. lv_qrcode_update(ui->SettingPage_qr_device_code, device_serial, 12);
  301. }
  302. void on_ota_failed_restart(lv_event_t *e) {
  303. esp_restart();
  304. }
  305. void on_reset_wifi(lv_event_t *e) {
  306. yx_data_clear(NVS_SSID_KEY);
  307. yx_data_clear(NVS_PASSWORD_KEY);
  308. esp_restart();
  309. }
  310. void on_restart_device(lv_event_t *e) {
  311. esp_restart();
  312. }
  313. /**
  314. * 空调页面-温度滑动条改变
  315. * @param e
  316. */
  317. void on_ac_page_arc_value_changed(lv_event_t * e) {
  318. lv_obj_t *arc = lv_event_get_current_target(e);
  319. int32_t value = lv_arc_get_value(arc)/10;
  320. lvgl_port_lock(0);
  321. lv_label_set_text_fmt(guider_ui.screen_main_label_temp_set, "%d", (uint16_t) value);
  322. lvgl_port_unlock();
  323. }
  324. /**
  325. * 空调页面-温度滑动条释放
  326. * @param e
  327. */
  328. void on_ac_page_arc_released(lv_event_t * e) {
  329. lv_obj_t *arc = lv_event_get_current_target(e);
  330. int32_t value = lv_arc_get_value(arc)/10;
  331. lvgl_port_lock(0);
  332. lv_label_set_text_fmt(guider_ui.screen_main_label_temp_set, "%d", (uint16_t) value);
  333. lvgl_port_unlock();
  334. ac_status.set_temp = (uint16_t) value;
  335. ac_set_temp(true);
  336. }
  337. /**
  338. * 空调页面-温度加按键
  339. * @param e
  340. */
  341. void on_set_temp_add(lv_event_t * e) {
  342. if (ac_status.set_temp<30) {
  343. ac_status.set_temp++;
  344. }else {
  345. }
  346. lvgl_port_lock(0);
  347. lv_arc_set_value(guider_ui.screen_main_arc_temp,ac_status.set_temp*10);
  348. lv_label_set_text_fmt(guider_ui.screen_main_label_temp_set, "%d", (uint16_t) ac_status.set_temp);
  349. lvgl_port_unlock();
  350. ac_set_temp(true);
  351. }
  352. /**
  353. * 空调页面-温度减按键
  354. * @param e
  355. */
  356. void on_set_temp_minus(lv_event_t * e) {
  357. if (ac_status.set_temp>16) {
  358. ac_status.set_temp--;
  359. }else {
  360. }
  361. lvgl_port_lock(0);
  362. lv_arc_set_value(guider_ui.screen_main_arc_temp,ac_status.set_temp*10);
  363. lv_label_set_text_fmt(guider_ui.screen_main_label_temp_set, "%d", (uint16_t) ac_status.set_temp);
  364. lvgl_port_unlock();
  365. ac_set_temp(true);
  366. }
  367. /**
  368. * 空调页面-风速按键容器
  369. * @param e
  370. */
  371. void on_set_speed(lv_event_t * e) {
  372. lv_obj_t *cont = (lv_obj_t *) lv_event_get_current_target(e);
  373. lv_obj_t *act_cb = lv_event_get_target(e);
  374. char buf[5];
  375. if (act_cb == cont) return;
  376. lvgl_port_lock(-1);
  377. lv_obj_add_state(act_cb, LV_STATE_CHECKED);
  378. uint32_t count = lv_obj_get_child_count(cont);
  379. // 遍历所有子控件
  380. for (int i = 0; i < count; i++) {
  381. lv_obj_t *child = lv_obj_get_child(cont, i);
  382. if (child != act_cb) {
  383. lv_obj_remove_state(child, LV_STATE_CHECKED);
  384. }
  385. }
  386. lvgl_port_unlock();
  387. if (act_cb == guider_ui.screen_main_imgbtn_speed1) {
  388. ac_status.fan_speed = 1;
  389. }
  390. else if (act_cb == guider_ui.screen_main_imgbtn_speed2) {
  391. ac_status.fan_speed =2 ;
  392. } else if (act_cb == guider_ui.screen_main_imgbtn_speed3)
  393. { ac_status.fan_speed = 3;}
  394. xf_set_fan_speed(true);
  395. }