main.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include <sys/cdefs.h>
  2. #include <stdio.h>
  3. #include <nvs_flash.h>
  4. #include <esp_netif.h>
  5. #include "esp_log.h"
  6. #include "freertos/FreeRTOS.h"
  7. #include "freertos/task.h"
  8. #include "app/include/wifi.h"
  9. #include "lcd_st7701.h"
  10. #include "lvgl_port.h"
  11. #include "framework.h"
  12. #include "framework_compat.h"
  13. #include "time_sync.h"
  14. #include "ota.h"
  15. #include "wifi.h"
  16. #include "main.h"
  17. #include "custom.h"
  18. #include "beep.h"
  19. #include "sht30.h"
  20. #include "xf_controller.h"
  21. #include "modbus_master.h"
  22. #include "system/miscellaneous_interface.h"
  23. static const char *TAG = "main";
  24. lv_ui guider_ui;
  25. system_setting_t system_setting = {};
  26. bool cloud_connected = false;
  27. void iot_event_monitor(int event) {
  28. switch (event) {
  29. case IOT_CONNECT_CLOUD_FAILT:
  30. ESP_LOGE(TAG, "IOT_CONNECT_CLOUD_FAILT");
  31. cloud_connected = false;
  32. lvgl_port_lock(-1);
  33. lv_obj_add_flag(guider_ui.screen_main_lab_cloud, LV_OBJ_FLAG_HIDDEN);
  34. lvgl_port_unlock();
  35. break;
  36. case IOT_CONNECT_CLOUD_SUCCESS:
  37. ESP_LOGE(TAG, "IOT_CONNECT_CLOUD_SUCCESS");
  38. cloud_connected = true;
  39. lvgl_port_lock(-1);
  40. lv_obj_remove_flag(guider_ui.screen_main_lab_cloud, LV_OBJ_FLAG_HIDDEN);
  41. lvgl_port_unlock();
  42. break;
  43. case IOT_LOGIN_FAILT:
  44. ESP_LOGE(TAG, "IOT_LOGIN_FAILT");
  45. break;
  46. case IOT_LOGIN_SUCCESS:
  47. ESP_LOGE(TAG, "IOT_LOGIN_SUCCESS");
  48. break;
  49. case IOT_REGISTER_FAILT:
  50. ESP_LOGE(TAG, "IOT_REGISTER_FAILT");
  51. break;
  52. case IOT_REGISTER_SUCCESS:
  53. ESP_LOGE(TAG, "IOT_REGISTER_SUCCESS");
  54. break;
  55. default:
  56. break;
  57. }
  58. }
  59. /**
  60. * wifi连接成功后执行的回调函数
  61. */
  62. void on_wifi_connected(void) {
  63. // 开启时间同步
  64. //time_sync_start();
  65. // 接入云平台
  66. // yxlj_framework_init();
  67. // 初始化OTA功能
  68. //ota_init();
  69. }
  70. _Noreturn void app_main(void) {
  71. esp_log_level_set("*", ESP_LOG_DEBUG);
  72. // esp_log_level_set("wifi-setting", ESP_LOG_DEBUG);
  73. // esp_log_level_set("system_setting", ESP_LOG_DEBUG);
  74. // esp_log_level_set("actions", ESP_LOG_DEBUG);
  75. // esp_log_level_set("mqtt_client", ESP_LOG_DEBUG);
  76. // 初始化NVS
  77. esp_err_t err = nvs_flash_init(); //nvs初始化
  78. if (err == ESP_ERR_NVS_NO_FREE_PAGES) {
  79. ESP_ERROR_CHECK(nvs_flash_erase()); //擦除nvs
  80. nvs_flash_init(); //重新初始化
  81. }
  82. modbus_master_init(); // 初始化 modbus master
  83. ESP_ERROR_CHECK(esp_netif_init());
  84. read_system_setting(&system_setting); // 读取nvs中存储的系统设置
  85. read_xf_status(&system_setting);
  86. xf_controller_init(&system_setting); // 读取nvs中存储的空调状态
  87. iotx_event_regist_cb(&iot_event_monitor); // 注册网关事件
  88. beep_init(); // 蜂鸣器初始化
  89. ESP_ERROR_CHECK(lcd_st7701_init());
  90. uint8_t *s_on_off = nvs_get_uint8(NVS_SCREEN_ON_OFF);
  91. if (s_on_off != NULL) {
  92. if (*s_on_off == 1) {
  93. ESP_ERROR_CHECK(lcd_st7701_backlight_off());
  94. } else {
  95. ESP_ERROR_CHECK(lcd_st7701_backlight_on());
  96. }
  97. } else {
  98. ESP_ERROR_CHECK(lcd_st7701_backlight_on());
  99. }
  100. setup_ui(&guider_ui);
  101. app_wifi_init(on_wifi_connected);
  102. sht30_init(); // 初始化温湿度传感器
  103. // 初始化OTA功能
  104. ota_init();
  105. // 熄屏定时器初始化
  106. screen_off_timer_init(&system_setting);
  107. for (;;) {
  108. vTaskDelay(10 / portTICK_PERIOD_MS);
  109. }
  110. }