main.c 3.5 KB

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