setting.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 60*24*30*3
  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_fan_vol[5]={30,35,40,45,50};
  22. uint8_t *min_hum_valve = nvs_get_uint8(NVS_MIN_HUM);
  23. uint32_t *filter_life_time_valve = nvs_get_uint32(FILTER_LIFE_TIME);
  24. uint8_t *air_exhaust_first_vol = nvs_get_uint8(AIR_EXHAUST_FIRST_VOL);
  25. uint8_t *air_exhaust_second_vol = nvs_get_uint8(AIR_EXHAUST_SECOND_VOL);
  26. uint8_t *air_exhaust_third_vol = nvs_get_uint8(AIR_EXHAUST_THIRD_VOL);
  27. uint8_t *air_exhaust_fourth_vol = nvs_get_uint8(AIR_EXHAUST_FOURTH_VOL);
  28. uint8_t *air_exhaust_fifth_vol = nvs_get_uint8(AIR_EXHAUST_FIFTH_VOL);
  29. uint8_t *air_supply_first_vol = nvs_get_uint8(AIR_SUPPLY_FIRST_VOL);
  30. uint8_t *air_supply_second_vol = nvs_get_uint8(AIR_SUPPLY_SECOND_VOL);
  31. uint8_t *air_supply_third_vol = nvs_get_uint8(AIR_SUPPLY_THIRD_VOL);
  32. uint8_t *air_supply_fourth_vol = nvs_get_uint8(AIR_SUPPLY_FOURTH_VOL);
  33. uint8_t *air_supply_fifth_vol = nvs_get_uint8(AIR_SUPPLY_FIFTH_VOL);
  34. uint8_t *report_duration = nvs_get_uint8(NVS_REPORT_DATA_DURATION);
  35. uint8_t *screen_auto_off = nvs_get_uint8(NVS_SCREEN_AUTO_OFF_KEY);
  36. if (screen_auto_off == NULL) {
  37. nvs_set_uint8(1, NVS_SCREEN_AUTO_OFF_KEY);
  38. setting->screen_auto_off = 1;
  39. } else {
  40. setting->screen_auto_off = *screen_auto_off;
  41. }
  42. uint8_t *screen_off_minute = nvs_get_uint8(NVS_SCREEN_OFF_MINUTE_KEY);
  43. if (screen_off_minute == NULL) {
  44. nvs_set_uint8(10, NVS_SCREEN_OFF_MINUTE_KEY);
  45. setting->screen_off_minute = 10;
  46. } else {
  47. setting->screen_off_minute = *screen_off_minute;
  48. }
  49. uint8_t *sound_on_off = nvs_get_uint8(NVS_SOUND_ON_OFF_KEY);
  50. if (sound_on_off == NULL) {
  51. nvs_set_uint8(1, NVS_SOUND_ON_OFF_KEY);
  52. setting->sound_on_off = 1;
  53. } else {
  54. setting->sound_on_off = *sound_on_off;
  55. }
  56. uint8_t *sound_volume = nvs_get_uint8(NVS_SOUND_VOLUME_KEY);
  57. if (sound_volume == NULL) {
  58. nvs_set_uint8(50, NVS_SOUND_VOLUME_KEY);
  59. setting->sound_volume = 50;
  60. } else {
  61. setting->sound_volume = *sound_volume;
  62. }
  63. if (min_hum_valve == NULL) {
  64. nvs_set_uint8(DEFAULT_MIN_HUM, NVS_MIN_HUM);
  65. setting->exhaust_first_vol = DEFAULT_MIN_HUM;
  66. } else {
  67. setting->exhaust_first_vol = *min_hum_valve;
  68. }
  69. if (air_exhaust_first_vol == NULL) {
  70. nvs_set_uint32(air_fan_vol[0], AIR_EXHAUST_FIRST_VOL);
  71. setting->exhaust_first_vol = air_fan_vol[0];
  72. } else {
  73. setting->exhaust_first_vol = *air_exhaust_first_vol;
  74. }
  75. if (air_exhaust_second_vol == NULL) {
  76. nvs_set_uint32(air_fan_vol[1], AIR_EXHAUST_SECOND_VOL);
  77. setting->exhaust_second_vol = air_fan_vol[1];
  78. } else {
  79. setting->exhaust_second_vol = *air_exhaust_second_vol;
  80. }
  81. if (air_exhaust_third_vol == NULL) {
  82. nvs_set_uint32(air_fan_vol[2], AIR_EXHAUST_THIRD_VOL);
  83. setting->exhaust_third_vol = air_fan_vol[2];
  84. } else {
  85. setting->exhaust_third_vol = *air_exhaust_third_vol;
  86. }
  87. if (air_exhaust_fourth_vol == NULL) {
  88. nvs_set_uint32(air_fan_vol[3], AIR_EXHAUST_FOURTH_VOL);
  89. setting->exhaust_fourth_vol = air_fan_vol[3];
  90. } else {
  91. setting->exhaust_fourth_vol = *air_exhaust_fourth_vol;
  92. }
  93. if (air_exhaust_fifth_vol == NULL) {
  94. nvs_set_uint32(air_fan_vol[4], AIR_EXHAUST_FIFTH_VOL);
  95. setting->exhaust_fifth_vol = air_fan_vol[4];
  96. } else {
  97. setting->exhaust_fifth_vol =*air_exhaust_fifth_vol;
  98. }
  99. if (air_supply_first_vol == NULL) {
  100. nvs_set_uint32(air_fan_vol[0], AIR_SUPPLY_FIRST_VOL);
  101. setting->supply_first_vol = air_fan_vol[0];
  102. } else {
  103. setting->supply_first_vol = *air_supply_first_vol;
  104. }
  105. if (air_supply_second_vol == NULL) {
  106. nvs_set_uint32(air_fan_vol[1], AIR_SUPPLY_SECOND_VOL);
  107. setting->supply_second_vol = air_fan_vol[1];
  108. } else {
  109. setting->supply_second_vol = *air_supply_second_vol;
  110. }
  111. if (air_supply_third_vol == NULL) {
  112. nvs_set_uint32(air_fan_vol[2], AIR_SUPPLY_THIRD_VOL);
  113. setting->supply_third_vol = air_fan_vol[2];
  114. } else {
  115. setting->supply_third_vol = *air_supply_third_vol;
  116. }
  117. if (air_supply_fourth_vol == NULL) {
  118. nvs_set_uint32(air_fan_vol[3], AIR_SUPPLY_FOURTH_VOL);
  119. setting->supply_fourth_vol = air_fan_vol[3];
  120. } else {
  121. setting->supply_fourth_vol = *air_supply_fourth_vol;
  122. }
  123. if (air_supply_fifth_vol == NULL) {
  124. nvs_set_uint32(air_fan_vol[4], AIR_SUPPLY_FIFTH_VOL);
  125. setting->supply_fifth_vol = air_fan_vol[4];
  126. } else {
  127. setting->supply_fifth_vol = *air_supply_fifth_vol;
  128. }
  129. if (filter_life_time_valve == NULL) {
  130. nvs_set_uint32(FILTER_LIFE_TIME_VALVE, FILTER_LIFE_TIME);
  131. setting->filter_life_time = FILTER_LIFE_TIME_VALVE;
  132. } else {
  133. setting->filter_life_time = *filter_life_time_valve;
  134. }
  135. if (report_duration == NULL) {
  136. nvs_set_uint8(DEFAULT_REPORT_DURATION, NVS_REPORT_DATA_DURATION);
  137. setting->report_data_duration = DEFAULT_REPORT_DURATION;
  138. } else {
  139. setting->report_data_duration = *report_duration;
  140. }
  141. }
  142. void save_system_setting(system_setting_t *setting) {
  143. nvs_set_uint16(setting->set_min_hum, NVS_MIN_HUM);
  144. nvs_set_uint32(setting->filter_life_time, FILTER_LIFE_TIME);
  145. nvs_set_uint8(setting->report_data_duration, NVS_REPORT_DATA_DURATION);
  146. nvs_set_uint32(setting->exhaust_first_vol, AIR_EXHAUST_FIRST_VOL);
  147. nvs_set_uint32(setting->exhaust_second_vol, AIR_EXHAUST_SECOND_VOL);
  148. nvs_set_uint32(setting->exhaust_third_vol, AIR_EXHAUST_THIRD_VOL);
  149. nvs_set_uint32(setting->exhaust_fourth_vol, AIR_EXHAUST_FOURTH_VOL);
  150. nvs_set_uint32(setting->exhaust_fifth_vol, AIR_EXHAUST_FIFTH_VOL);
  151. nvs_set_uint32(setting->supply_first_vol, AIR_SUPPLY_FIRST_VOL);
  152. nvs_set_uint32(setting->supply_second_vol, AIR_SUPPLY_SECOND_VOL);
  153. nvs_set_uint32(setting->supply_third_vol, AIR_SUPPLY_THIRD_VOL);
  154. nvs_set_uint32(setting->supply_fourth_vol, AIR_SUPPLY_FOURTH_VOL);
  155. nvs_set_uint32(setting->supply_fifth_vol, AIR_SUPPLY_FIFTH_VOL);
  156. nvs_set_uint8(setting->sound_on_off, NVS_SOUND_ON_OFF_KEY);
  157. nvs_set_uint8(setting->sound_volume, NVS_SOUND_VOLUME_KEY);
  158. nvs_set_uint8(setting->screen_auto_off, NVS_SCREEN_AUTO_OFF_KEY);
  159. nvs_set_uint8(setting->screen_off_minute, NVS_SCREEN_OFF_MINUTE_KEY);
  160. }
  161. static uint8_t set_bits(uint8_t num, uint8_t bit, uint8_t value) {
  162. if (bit > 7)
  163. return num;
  164. if (value)
  165. return num | (1 << bit);
  166. else
  167. return num & ~(1 << bit);
  168. }
  169. static void screen_off_timer_cb(lv_timer_t * timer) {
  170. lcd_st7701_backlight_off();
  171. lv_screen_load_anim(guider_ui.screen_main, LV_SCR_LOAD_ANIM_MOVE_RIGHT, 100, 0, false);
  172. screen_on_off_status = 0;
  173. nvs_set_uint8(1, NVS_SCREEN_ON_OFF);
  174. lv_timer_pause(screen_timer);
  175. }
  176. bool reset_screen_off(){
  177. if(screen_timer==NULL)return false;
  178. lv_timer_reset(screen_timer);
  179. if(screen_on_off_status==0)
  180. {
  181. lcd_st7701_backlight_on();
  182. nvs_set_uint8(0, NVS_SCREEN_ON_OFF);
  183. screen_on_off_status = 1;
  184. lv_timer_resume(screen_timer);
  185. return true;
  186. }
  187. return false;
  188. }
  189. void screen_off_timer_init(system_setting_t* setting) {
  190. ESP_LOGD(TAG, "screen_off_timer_init");
  191. screen_timer = lv_timer_create(screen_off_timer_cb, 1000 * 60 * setting->screen_off_minute, NULL);
  192. }