setting.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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//六个月,每五分钟计数加一(4320个小时)
  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 *report_duration = nvs_get_uint8(NVS_REPORT_DATA_DURATION);
  26. uint8_t *screen_auto_off = nvs_get_uint8(NVS_SCREEN_AUTO_OFF_KEY);
  27. if (screen_auto_off == NULL) {
  28. nvs_set_uint8(1, NVS_SCREEN_AUTO_OFF_KEY);
  29. setting->screen_auto_off = 1;
  30. } else {
  31. setting->screen_auto_off = *screen_auto_off;
  32. }
  33. uint8_t *_mould_proof_runtime = nvs_get_uint8(NVS_MOULD_PROOF_RUNTIME);
  34. if (_mould_proof_runtime == NULL) {
  35. nvs_set_uint8(1, NVS_MOULD_PROOF_RUNTIME);
  36. // setting->mould_proof_runtime = 1;//测试
  37. setting->mould_proof_runtime = 60;
  38. } else {
  39. setting->mould_proof_runtime = *_mould_proof_runtime;
  40. }
  41. uint8_t *screen_off_minute = nvs_get_uint8(NVS_SCREEN_OFF_MINUTE_KEY);
  42. if (screen_off_minute == NULL) {
  43. nvs_set_uint8(1, NVS_SCREEN_OFF_MINUTE_KEY);
  44. setting->screen_off_minute = 1;
  45. } else {
  46. setting->screen_off_minute = *screen_off_minute;
  47. }
  48. uint8_t *sound_on_off = nvs_get_uint8(NVS_SOUND_ON_OFF_KEY);
  49. if (sound_on_off == NULL) {
  50. nvs_set_uint8(1, NVS_SOUND_ON_OFF_KEY);
  51. setting->sound_on_off = 1;
  52. } else {
  53. setting->sound_on_off = *sound_on_off;
  54. }
  55. uint8_t *sound_volume = nvs_get_uint8(NVS_SOUND_VOLUME_KEY);
  56. if (sound_volume == NULL) {
  57. nvs_set_uint8(50, NVS_SOUND_VOLUME_KEY);
  58. setting->sound_volume = 50;
  59. } else {
  60. setting->sound_volume = *sound_volume;
  61. }
  62. if (filter_life_time_valve == NULL) {
  63. nvs_set_uint32(FILTER_LIFE_TIME_VALVE, FILTER_LIFE_TIME);
  64. setting->filter_life_time = FILTER_LIFE_TIME_VALVE;
  65. } else {
  66. setting->filter_life_time = *filter_life_time_valve;
  67. }
  68. setting->filter_life_time = FILTER_LIFE_TIME_VALVE;
  69. if (report_duration == NULL) {
  70. nvs_set_uint8(DEFAULT_REPORT_DURATION, NVS_REPORT_DATA_DURATION);
  71. setting->report_data_duration = DEFAULT_REPORT_DURATION;
  72. } else {
  73. setting->report_data_duration = *report_duration;
  74. }
  75. uint8_t *devic_type = nvs_get_uint8(NVS_SLAVE_ADDR);
  76. if (devic_type == NULL) {
  77. nvs_set_uint8(0, NVS_SLAVE_ADDR);
  78. setting->devic_type = 0;
  79. } else {
  80. setting->devic_type = *devic_type;
  81. }
  82. uint8_t *slave_addr = nvs_get_uint8(NVS_DEVIC_TYPE);
  83. if (devic_type == NULL) {
  84. nvs_set_uint8(0, NVS_DEVIC_TYPE);
  85. setting->slave_addr = 0;
  86. } else {
  87. setting->slave_addr = *slave_addr;
  88. }
  89. }
  90. void save_system_setting(system_setting_t *setting) {
  91. setting->set_min_hum=30;
  92. nvs_set_uint16(setting->set_min_hum, NVS_MIN_HUM);
  93. nvs_set_uint32(setting->filter_life_time, FILTER_LIFE_TIME);
  94. nvs_set_uint8(setting->report_data_duration, NVS_REPORT_DATA_DURATION);
  95. nvs_set_uint8(setting->sound_on_off, NVS_SOUND_ON_OFF_KEY);
  96. nvs_set_uint8(setting->sound_volume, NVS_SOUND_VOLUME_KEY);
  97. nvs_set_uint8(setting->screen_auto_off, NVS_SCREEN_AUTO_OFF_KEY);
  98. nvs_set_uint8(setting->screen_off_minute, NVS_SCREEN_OFF_MINUTE_KEY);
  99. nvs_set_uint8(setting->mould_proof_runtime, NVS_MOULD_PROOF_RUNTIME);
  100. }
  101. static uint8_t set_bits(uint8_t num, uint8_t bit, uint8_t value) {
  102. if (bit > 7)
  103. return num;
  104. if (value)
  105. return num | (1 << bit);
  106. else
  107. return num & ~(1 << bit);
  108. }
  109. static void screen_off_timer_cb(lv_timer_t * timer) {
  110. lcd_st7701_backlight_off();
  111. lv_screen_load_anim(guider_ui.screen_main, LV_SCR_LOAD_ANIM_MOVE_RIGHT, 100, 0, false);
  112. screen_on_off_status = 0;
  113. nvs_set_uint8(1, NVS_SCREEN_ON_OFF);
  114. lv_timer_pause(screen_timer);
  115. }
  116. bool reset_screen_off(){
  117. if(screen_timer==NULL)return false;
  118. lv_timer_reset(screen_timer);
  119. if(screen_on_off_status==0)
  120. {
  121. lcd_st7701_backlight_on();
  122. nvs_set_uint8(0, NVS_SCREEN_ON_OFF);
  123. screen_on_off_status = 1;
  124. lv_timer_resume(screen_timer);
  125. return true;
  126. }
  127. return false;
  128. }
  129. void screen_off_timer_init(system_setting_t* setting) {
  130. ESP_LOGD(TAG, "screen_off_timer_init");
  131. screen_timer = lv_timer_create(screen_off_timer_cb, 1000 * 60 * setting->screen_off_minute, NULL);
  132. }