setting.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /**
  2. * @Author: 李建
  3. * @Date: 2025/5/2 11:48
  4. * Description: 系统设置功能
  5. * Copyright: Copyright (©) 2025 永续绿建. All rights reserved.
  6. */
  7. #include <esp_log.h>
  8. #include <esp_timer.h>
  9. #include "setting.h"
  10. #include "lvgl.h"
  11. #include "system/miscellaneous_interface.h"
  12. #include "lcd_st7701.h"
  13. #include "gui_guider.h"
  14. #define DEFAULT_REPORT_DURATION 30
  15. #define DEFAULT_MIN_HUM 30
  16. #define FILTER_LIFE_TIME_VALVE 51840//六个月,每五分钟计数加一
  17. static const char *TAG = "system_setting";
  18. lv_timer_t * screen_timer;
  19. static uint8_t screen_on_off_status;// 当前屏幕亮或灭的状态
  20. void read_system_setting(system_setting_t *setting) {
  21. uint8_t air_exhaust_fan_vol[5]={75,77,79,81,85};
  22. uint8_t air_supply_fan_vol[5]={30,35,40,45,50};
  23. uint8_t *min_hum_valve = nvs_get_uint8(NVS_MIN_HUM);
  24. uint32_t *filter_life_time_valve = nvs_get_uint32(FILTER_LIFE_TIME);
  25. uint8_t *air_exhaust_first_vol = nvs_get_uint8(AIR_EXHAUST_FIRST_VOL);
  26. uint8_t *air_exhaust_second_vol = nvs_get_uint8(AIR_EXHAUST_SECOND_VOL);
  27. uint8_t *air_exhaust_third_vol = nvs_get_uint8(AIR_EXHAUST_THIRD_VOL);
  28. uint8_t *air_exhaust_fourth_vol = nvs_get_uint8(AIR_EXHAUST_FOURTH_VOL);
  29. uint8_t *air_exhaust_fifth_vol = nvs_get_uint8(AIR_EXHAUST_FIFTH_VOL);
  30. uint8_t *air_supply_first_vol = nvs_get_uint8(AIR_SUPPLY_FIRST_VOL);
  31. uint8_t *air_supply_second_vol = nvs_get_uint8(AIR_SUPPLY_SECOND_VOL);
  32. uint8_t *air_supply_third_vol = nvs_get_uint8(AIR_SUPPLY_THIRD_VOL);
  33. uint8_t *air_supply_fourth_vol = nvs_get_uint8(AIR_SUPPLY_FOURTH_VOL);
  34. uint8_t *air_supply_fifth_vol = nvs_get_uint8(AIR_SUPPLY_FIFTH_VOL);
  35. uint8_t *report_duration = nvs_get_uint8(NVS_REPORT_DATA_DURATION);
  36. uint8_t *screen_auto_off = nvs_get_uint8(NVS_SCREEN_AUTO_OFF_KEY);
  37. if (screen_auto_off == NULL) {
  38. nvs_set_uint8(1, NVS_SCREEN_AUTO_OFF_KEY);
  39. setting->screen_auto_off = 1;
  40. } else {
  41. setting->screen_auto_off = *screen_auto_off;
  42. }
  43. uint8_t *screen_off_minute = nvs_get_uint8(NVS_SCREEN_OFF_MINUTE_KEY);
  44. if (screen_off_minute == NULL) {
  45. nvs_set_uint8(10, NVS_SCREEN_OFF_MINUTE_KEY);
  46. setting->screen_off_minute = 10;
  47. } else {
  48. setting->screen_off_minute = *screen_off_minute;
  49. }
  50. uint8_t *sound_on_off = nvs_get_uint8(NVS_SOUND_ON_OFF_KEY);
  51. if (sound_on_off == NULL) {
  52. nvs_set_uint8(1, NVS_SOUND_ON_OFF_KEY);
  53. setting->sound_on_off = 1;
  54. } else {
  55. setting->sound_on_off = *sound_on_off;
  56. }
  57. uint8_t *sound_volume = nvs_get_uint8(NVS_SOUND_VOLUME_KEY);
  58. if (sound_volume == NULL) {
  59. nvs_set_uint8(50, NVS_SOUND_VOLUME_KEY);
  60. setting->sound_volume = 50;
  61. } else {
  62. setting->sound_volume = *sound_volume;
  63. }
  64. if (min_hum_valve == NULL) {
  65. nvs_set_uint8(DEFAULT_MIN_HUM, NVS_MIN_HUM);
  66. setting->exhaust_first_vol = DEFAULT_MIN_HUM;
  67. } else {
  68. setting->exhaust_first_vol = *min_hum_valve;
  69. }
  70. if (air_exhaust_first_vol == NULL) {
  71. nvs_set_uint32(air_exhaust_fan_vol[0], AIR_EXHAUST_FIRST_VOL);
  72. setting->exhaust_first_vol = air_exhaust_fan_vol[0];
  73. } else {
  74. setting->exhaust_first_vol = *air_exhaust_first_vol;
  75. }
  76. if (air_exhaust_second_vol == NULL) {
  77. nvs_set_uint32(air_exhaust_fan_vol[1], AIR_EXHAUST_SECOND_VOL);
  78. setting->exhaust_second_vol = air_exhaust_fan_vol[1];
  79. } else {
  80. setting->exhaust_second_vol = *air_exhaust_second_vol;
  81. }
  82. if (air_exhaust_third_vol == NULL) {
  83. nvs_set_uint32(air_exhaust_fan_vol[2], AIR_EXHAUST_THIRD_VOL);
  84. setting->exhaust_third_vol = air_exhaust_fan_vol[2];
  85. } else {
  86. setting->exhaust_third_vol = *air_exhaust_third_vol;
  87. }
  88. if (air_exhaust_fourth_vol == NULL) {
  89. nvs_set_uint32(air_exhaust_fan_vol[3], AIR_EXHAUST_FOURTH_VOL);
  90. setting->exhaust_fourth_vol = air_exhaust_fan_vol[3];
  91. } else {
  92. setting->exhaust_fourth_vol = *air_exhaust_fourth_vol;
  93. }
  94. if (air_exhaust_fifth_vol == NULL) {
  95. nvs_set_uint32(air_exhaust_fan_vol[4], AIR_EXHAUST_FIFTH_VOL);
  96. setting->exhaust_fifth_vol = air_exhaust_fan_vol[4];
  97. } else {
  98. setting->exhaust_fifth_vol =*air_exhaust_fifth_vol;
  99. }
  100. if (air_supply_first_vol == NULL) {
  101. nvs_set_uint32(air_supply_fan_vol[0], AIR_SUPPLY_FIRST_VOL);
  102. setting->supply_first_vol = air_supply_fan_vol[0];
  103. } else {
  104. setting->supply_first_vol = *air_supply_first_vol;
  105. }
  106. if (air_supply_second_vol == NULL) {
  107. nvs_set_uint32(air_supply_fan_vol[1], AIR_SUPPLY_SECOND_VOL);
  108. setting->supply_second_vol = air_supply_fan_vol[1];
  109. } else {
  110. setting->supply_second_vol = *air_supply_second_vol;
  111. }
  112. if (air_supply_third_vol == NULL) {
  113. nvs_set_uint32(air_supply_fan_vol[2], AIR_SUPPLY_THIRD_VOL);
  114. setting->supply_third_vol = air_supply_fan_vol[2];
  115. } else {
  116. setting->supply_third_vol = *air_supply_third_vol;
  117. }
  118. if (air_supply_fourth_vol == NULL) {
  119. nvs_set_uint32(air_supply_fan_vol[3], AIR_SUPPLY_FOURTH_VOL);
  120. setting->supply_fourth_vol = air_supply_fan_vol[3];
  121. } else {
  122. setting->supply_fourth_vol = *air_supply_fourth_vol;
  123. }
  124. if (air_supply_fifth_vol == NULL) {
  125. nvs_set_uint32(air_supply_fan_vol[4], AIR_SUPPLY_FIFTH_VOL);
  126. setting->supply_fifth_vol = air_supply_fan_vol[4];
  127. } else {
  128. setting->supply_fifth_vol = *air_supply_fifth_vol;
  129. }
  130. if (filter_life_time_valve == NULL) {
  131. nvs_set_uint32(FILTER_LIFE_TIME_VALVE, FILTER_LIFE_TIME);
  132. setting->filter_life_time = FILTER_LIFE_TIME_VALVE;
  133. } else {
  134. setting->filter_life_time = *filter_life_time_valve;
  135. }
  136. setting->filter_life_time = FILTER_LIFE_TIME_VALVE;
  137. if (report_duration == NULL) {
  138. nvs_set_uint8(DEFAULT_REPORT_DURATION, NVS_REPORT_DATA_DURATION);
  139. setting->report_data_duration = DEFAULT_REPORT_DURATION;
  140. } else {
  141. setting->report_data_duration = *report_duration;
  142. }
  143. }
  144. void save_system_setting(system_setting_t *setting) {
  145. nvs_set_uint16(setting->set_min_hum, NVS_MIN_HUM);
  146. nvs_set_uint32(setting->filter_life_time, FILTER_LIFE_TIME);
  147. nvs_set_uint8(setting->report_data_duration, NVS_REPORT_DATA_DURATION);
  148. nvs_set_uint32(setting->exhaust_first_vol, AIR_EXHAUST_FIRST_VOL);
  149. nvs_set_uint32(setting->exhaust_second_vol, AIR_EXHAUST_SECOND_VOL);
  150. nvs_set_uint32(setting->exhaust_third_vol, AIR_EXHAUST_THIRD_VOL);
  151. nvs_set_uint32(setting->exhaust_fourth_vol, AIR_EXHAUST_FOURTH_VOL);
  152. nvs_set_uint32(setting->exhaust_fifth_vol, AIR_EXHAUST_FIFTH_VOL);
  153. nvs_set_uint32(setting->supply_first_vol, AIR_SUPPLY_FIRST_VOL);
  154. nvs_set_uint32(setting->supply_second_vol, AIR_SUPPLY_SECOND_VOL);
  155. nvs_set_uint32(setting->supply_third_vol, AIR_SUPPLY_THIRD_VOL);
  156. nvs_set_uint32(setting->supply_fourth_vol, AIR_SUPPLY_FOURTH_VOL);
  157. nvs_set_uint32(setting->supply_fifth_vol, AIR_SUPPLY_FIFTH_VOL);
  158. nvs_set_uint8(setting->sound_on_off, NVS_SOUND_ON_OFF_KEY);
  159. nvs_set_uint8(setting->sound_volume, NVS_SOUND_VOLUME_KEY);
  160. nvs_set_uint8(setting->screen_auto_off, NVS_SCREEN_AUTO_OFF_KEY);
  161. nvs_set_uint8(setting->screen_off_minute, NVS_SCREEN_OFF_MINUTE_KEY);
  162. }
  163. static uint8_t set_bits(uint8_t num, uint8_t bit, uint8_t value) {
  164. if (bit > 7)
  165. return num;
  166. if (value)
  167. return num | (1 << bit);
  168. else
  169. return num & ~(1 << bit);
  170. }
  171. static void screen_off_timer_cb(lv_timer_t * timer) {
  172. lcd_st7701_backlight_off();
  173. lv_screen_load_anim(guider_ui.screen_main, LV_SCR_LOAD_ANIM_MOVE_RIGHT, 100, 0, false);
  174. screen_on_off_status = 0;
  175. nvs_set_uint8(1, NVS_SCREEN_ON_OFF);
  176. lv_timer_pause(screen_timer);
  177. }
  178. bool reset_screen_off(){
  179. if(screen_timer==NULL)return false;
  180. lv_timer_reset(screen_timer);
  181. if(screen_on_off_status==0)
  182. {
  183. lcd_st7701_backlight_on();
  184. nvs_set_uint8(0, NVS_SCREEN_ON_OFF);
  185. screen_on_off_status = 1;
  186. lv_timer_resume(screen_timer);
  187. return true;
  188. }
  189. return false;
  190. }
  191. void screen_off_timer_init(system_setting_t* setting) {
  192. ESP_LOGD(TAG, "screen_off_timer_init");
  193. screen_timer = lv_timer_create(screen_off_timer_cb, 1000 * 60 * setting->screen_off_minute, NULL);
  194. }