From 38b517ca1b4767f8a6f6b1e20912a7874220c5aa Mon Sep 17 00:00:00 2001 From: imi415 Date: Sun, 2 Jan 2022 00:41:18 +0800 Subject: [PATCH] Enabled wifi temporarily --- main/Kconfig.projbuild | 20 +++ .../assets/fonts/material_webfont/charset.txt | 1 + .../material_webfont/material_webfont_32.c | 31 ++++- main/impl/impl_lvgl.c | 7 +- main/interface/if_standby.c | 6 + main/interface/if_wifi.c | 82 ++++++++++- main/interface/if_wifi.h | 6 + main/lib/lv_conf.h | 6 +- main/main.c | 14 +- main/service/service_wifi.c | 129 +++++++++++++++++- main/service/service_wifi.h | 8 ++ 11 files changed, 288 insertions(+), 22 deletions(-) create mode 100644 main/Kconfig.projbuild create mode 100644 main/assets/fonts/material_webfont/charset.txt create mode 100644 main/service/service_wifi.h diff --git a/main/Kconfig.projbuild b/main/Kconfig.projbuild new file mode 100644 index 0000000..bd81891 --- /dev/null +++ b/main/Kconfig.projbuild @@ -0,0 +1,20 @@ +menu "Application Specific Configuration" + + config ESP_WIFI_SSID + string "WiFi SSID" + default "myssid" + help + SSID (network name) for the example to connect to. + + config ESP_WIFI_PASSWORD + string "WiFi Password" + default "mypassword" + help + WiFi password (WPA or WPA2) for the example to use. + + config ESP_WIFI_MAXIMUM_RETRY + int "Maximum retry" + default 5 + help + Set the Maximum retry to avoid station reconnecting to the AP unlimited when the AP is really inexistent. +endmenu diff --git a/main/assets/fonts/material_webfont/charset.txt b/main/assets/fonts/material_webfont/charset.txt new file mode 100644 index 0000000..f343d25 --- /dev/null +++ b/main/assets/fonts/material_webfont/charset.txt @@ -0,0 +1 @@ +0xF007A, 0xF0504, 0xF058C, 0xF0599, 0xF05AD, 0xF092D, 0xF0928 \ No newline at end of file diff --git a/main/assets/fonts/material_webfont/material_webfont_32.c b/main/assets/fonts/material_webfont/material_webfont_32.c index 4b2f695..0d19b2d 100644 --- a/main/assets/fonts/material_webfont/material_webfont_32.c +++ b/main/assets/fonts/material_webfont/material_webfont_32.c @@ -62,6 +62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7, 0x1f, 0xff, 0x80, @@ -90,7 +113,9 @@ static const lv_font_fmt_txt_glyph_dsc_t glyph_dsc[] = { {.bitmap_index = 52, .adv_w = 512, .box_w = 24, .box_h = 23, .ofs_x = 4, .ofs_y = 1}, {.bitmap_index = 121, .adv_w = 512, .box_w = 16, .box_h = 23, .ofs_x = 8, .ofs_y = 1}, {.bitmap_index = 167, .adv_w = 512, .box_w = 24, .box_h = 26, .ofs_x = 4, .ofs_y = -1}, - {.bitmap_index = 245, .adv_w = 512, .box_w = 32, .box_h = 27, .ofs_x = 0, .ofs_y = -1} + {.bitmap_index = 245, .adv_w = 512, .box_w = 19, .box_h = 18, .ofs_x = 6, .ofs_y = 3}, + {.bitmap_index = 288, .adv_w = 512, .box_w = 32, .box_h = 25, .ofs_x = 0, .ofs_y = -1}, + {.bitmap_index = 388, .adv_w = 512, .box_w = 32, .box_h = 27, .ofs_x = 0, .ofs_y = -1} }; /*--------------------- @@ -98,7 +123,7 @@ static const lv_font_fmt_txt_glyph_dsc_t glyph_dsc[] = { *--------------------*/ static const uint16_t unicode_list_0[] = { - 0x0, 0x48a, 0x512, 0x51f, 0x8b3 + 0x0, 0x48a, 0x512, 0x51f, 0x533, 0x8ae, 0x8b3 }; /*Collect the unicode lists and glyph_id offsets*/ @@ -106,7 +131,7 @@ static const lv_font_fmt_txt_cmap_t cmaps[] = { { .range_start = 983162, .range_length = 2228, .glyph_id_start = 1, - .unicode_list = unicode_list_0, .glyph_id_ofs_list = NULL, .list_length = 5, .type = LV_FONT_FMT_TXT_CMAP_SPARSE_TINY + .unicode_list = unicode_list_0, .glyph_id_ofs_list = NULL, .list_length = 7, .type = LV_FONT_FMT_TXT_CMAP_SPARSE_TINY } }; diff --git a/main/impl/impl_lvgl.c b/main/impl/impl_lvgl.c index fe94010..d7e8088 100644 --- a/main/impl/impl_lvgl.c +++ b/main/impl/impl_lvgl.c @@ -10,7 +10,7 @@ #define EPD_DISPLAY_FRAME_SIZE (EPD_DISPLAY_PIXEL_COUNT * 2 / 8) #define EPD_DISPLAY_GS 0 -#define EPD_DISPLAY_MAX_PARTIAL 12 +#define EPD_DISPLAY_MAX_PARTIAL 32 /* !! Must be even !! */ static impl_epd_handle_t s_epd_impl; @@ -38,7 +38,7 @@ static uint8_t s_epd_partial_counter = 0; static uint32_t s_last_key = 0; static lv_disp_draw_buf_t s_disp_buf; -static lv_color_t s_disp_store[EPD_DISPLAY_FRAME_SIZE]; +static lv_color_t *s_disp_store; static lv_disp_drv_t s_disp_drv; static lv_indev_drv_t s_indev_drv; @@ -194,6 +194,9 @@ esp_err_t impl_lvgl_init(void) { lv_log_register_print_cb(impl_lvgl_log_cb); + s_disp_store = malloc(EPD_DISPLAY_FRAME_SIZE); + if(s_disp_store == NULL) return ESP_FAIL; + lv_disp_draw_buf_init(&s_disp_buf, s_disp_store, NULL, EPD_DISPLAY_PIXEL_COUNT); lv_disp_drv_init(&s_disp_drv); diff --git a/main/interface/if_standby.c b/main/interface/if_standby.c index 9381df7..87118ab 100644 --- a/main/interface/if_standby.c +++ b/main/interface/if_standby.c @@ -3,6 +3,7 @@ #include #include "esp_log.h" +#include "if_wifi.h" #include "impl_lvgl.h" #define LOG_TAG "IF_STANDBY" @@ -20,6 +21,7 @@ void if_standby_event_handler(lv_event_t *ev) { switch (button_event) { case IF_EVENT_WIFI_BUTTON: + if_wifi_init(g_standby_screen); break; case IF_EVENT_BAT_BUTTON: break; @@ -172,6 +174,10 @@ void if_standby_task(void *pvParameters) { lv_label_set_text(weekday_label, queue_item.payload); lv_obj_set_align(weekday_label, LV_ALIGN_CENTER); break; + case IF_STANDBY_ITEM_WIFI: + lv_label_set_text(network_status_label, queue_item.payload); + lv_obj_set_align(network_status_label, LV_ALIGN_CENTER); + break; default: break; } diff --git a/main/interface/if_wifi.c b/main/interface/if_wifi.c index 0b08b7f..845883b 100644 --- a/main/interface/if_wifi.c +++ b/main/interface/if_wifi.c @@ -1,18 +1,88 @@ -#include "lvgl.h" - #include "esp_system.h" +#include "freertos/FreeRTOS.h" +#include "freertos/queue.h" +#include "freertos/task.h" +#include "impl_lvgl.h" +#define IF_EVENT_QUEUE_LEN 2 +#define IF_EVENT_QUEUE_SIZE() (sizeof(int)) +#define IF_EVENT_QUEUE_TIMEOUT 100 + +#define IF_EVENT_EXIT_BUTTON 0 + +static TaskHandle_t s_if_wifi_task_handle; +static QueueHandle_t s_if_wifi_event_queue; + +static void if_wifi_event_handler(lv_event_t *ev) { + int button_event = (int)ev->user_data; + xQueueSend(s_if_wifi_event_queue, &button_event, pdMS_TO_TICKS(100)); +} void if_wifi_task(void *pvParameters) { - + lv_obj_t *parent = pvParameters; + + impl_lvgl_lock(); + + lv_obj_t *wifi_main = lv_obj_create(parent); + lv_obj_set_size(wifi_main, 360, 280); + lv_obj_set_align(wifi_main, LV_ALIGN_CENTER); + + lv_obj_t *exit_obj = lv_btn_create(wifi_main); + + lv_obj_set_size(exit_obj, 36, 36); + + lv_obj_align(exit_obj, LV_ALIGN_TOP_RIGHT, 0, 0); + + lv_obj_add_event_cb(exit_obj, if_wifi_event_handler, LV_EVENT_CLICKED, (void *)IF_EVENT_EXIT_BUTTON); + + lv_obj_t *exit_label = lv_label_create(exit_obj); + + lv_obj_set_style_text_font(exit_label, &material_webfont_32, 0); + + lv_label_set_text(exit_label, "\U000F05AD"); + + lv_obj_set_align(exit_label, LV_ALIGN_CENTER); + + impl_lvgl_unlock(); + + int button_event = 0; + uint8_t terminate_flag = 0; + + for (;;) { + if (xQueueReceive(s_if_wifi_event_queue, &button_event, portMAX_DELAY) == pdPASS) { + switch (button_event) { + case IF_EVENT_EXIT_BUTTON: + terminate_flag = 1; + break; + default: + break; + } + } + + if (terminate_flag) { + break; + } + } + + // When we are done, delete UI elements and task + lv_obj_del_async(wifi_main); + + vQueueDelete(s_if_wifi_event_queue); + s_if_wifi_event_queue = NULL; + vTaskDelete(NULL); } /** * @brief Initialize WiFi status/configuration component - * - * @param parent_obj - * @return esp_err_t + * + * @param parent_obj + * @return esp_err_t */ esp_err_t if_wifi_init(lv_obj_t *parent_obj) { + s_if_wifi_event_queue = xQueueCreate(IF_EVENT_QUEUE_LEN, IF_EVENT_QUEUE_SIZE()); + + if (xTaskCreate(if_wifi_task, "STBY_TASK", 4096, parent_obj, 6, &s_if_wifi_task_handle) != pdPASS) { + return ESP_FAIL; + } return ESP_OK; } diff --git a/main/interface/if_wifi.h b/main/interface/if_wifi.h index e69de29..cc3203b 100644 --- a/main/interface/if_wifi.h +++ b/main/interface/if_wifi.h @@ -0,0 +1,6 @@ +#ifndef IF_WIFI_H +#define IF_WIFI_H + +esp_err_t if_wifi_init(lv_obj_t *parent_obj); + +#endif \ No newline at end of file diff --git a/main/lib/lv_conf.h b/main/lib/lv_conf.h index acfb759..c5c1e37 100644 --- a/main/lib/lv_conf.h +++ b/main/lib/lv_conf.h @@ -78,10 +78,10 @@ *====================*/ /*Default display refresh period. LVG will redraw changed areas with this period time*/ -#define LV_DISP_DEF_REFR_PERIOD 30 /*[ms]*/ +#define LV_DISP_DEF_REFR_PERIOD 100 /*[ms]*/ /*Input device read period in milliseconds*/ -#define LV_INDEV_DEF_READ_PERIOD 30 /*[ms]*/ +#define LV_INDEV_DEF_READ_PERIOD 50 /*[ms]*/ /*Use a custom tick source that tells the elapsed time in milliseconds. *It removes the need to manually update the tick with `lv_tick_inc()`)*/ @@ -343,7 +343,7 @@ e.g. "stm32f769xx.h" or "stm32f429xx.h"*/ LV_FONT_DECLARE(material_webfont_32) /*Always set a default font*/ -#define LV_FONT_DEFAULT &lv_font_unscii_16 +#define LV_FONT_DEFAULT &bebas_neue_32 /*Enable handling large font and/or fonts with a lot of characters. *The limit depends on the font size, font face and bpp. diff --git a/main/main.c b/main/main.c index 2c165d6..5dba1d5 100644 --- a/main/main.c +++ b/main/main.c @@ -2,6 +2,7 @@ #include "driver/gpio.h" #include "driver/i2c.h" +#include "esp_sntp.h" #include "esp_spi_flash.h" #include "esp_system.h" #include "nvs_flash.h" @@ -12,8 +13,9 @@ // #include "if_standby.h" #include "impl_lvgl.h" -#include "service_dht.h" #include "service_clock.h" +#include "service_dht.h" +#include "service_wifi.h" #define APP_I2C_MASTER_NUM 0 #define APP_I2C_SDA_NUM GPIO_NUM_39 @@ -50,10 +52,19 @@ static esp_err_t app_init_i2c(void) { return i2c_driver_install(APP_I2C_MASTER_NUM, cfg.mode, 0, 0, 0); } +static esp_err_t app_init_sntp(void) { + sntp_setoperatingmode(SNTP_OPMODE_POLL); + sntp_setservername(0, "pool.ntp.org"); + sntp_init(); + + return ESP_OK; +} + void app_main(void) { ESP_ERROR_CHECK(app_init_nvs()); ESP_ERROR_CHECK(app_init_gpio()); ESP_ERROR_CHECK(app_init_i2c()); + ESP_ERROR_CHECK(app_init_sntp()); ESP_ERROR_CHECK(impl_lvgl_init()); @@ -61,6 +72,7 @@ void app_main(void) { ESP_ERROR_CHECK(service_dht_init()); ESP_ERROR_CHECK(service_clock_init()); + ESP_ERROR_CHECK(service_wifi_init()); /* Dead loop */ for (;;) { diff --git a/main/service/service_wifi.c b/main/service/service_wifi.c index 6f89f27..ba22b57 100644 --- a/main/service/service_wifi.c +++ b/main/service/service_wifi.c @@ -1,17 +1,132 @@ +#include "esp_event.h" #include "esp_log.h" #include "esp_spi_flash.h" #include "esp_system.h" +#include "esp_wifi.h" #include "freertos/FreeRTOS.h" +#include "freertos/queue.h" #include "freertos/task.h" +#include "if_standby.h" +#include "if_wifi.h" +#include "nvs_flash.h" #include "sdkconfig.h" -esp_err_t helper_wifi_init(void) { - // Check stored credentials from NVS, - // if no credentials stored or button pressed, force initiate easy connect +#define LOG_TAG "SVC_WIFI" + +#define WIFI_SERVICE_SSID CONFIG_ESP_WIFI_SSID +#define WIFI_SERVICE_PASS CONFIG_ESP_WIFI_PASSWORD +#define WIFI_SERVICE_MAXIMUM_RETRY CONFIG_ESP_WIFI_MAXIMUM_RETRY + +static TaskHandle_t s_service_wifi_task_handle; +static QueueHandle_t s_service_wifi_event_queue; + +static uint8_t s_retry_count = 0; + +typedef enum { + SERVICE_WIFI_EVENT_CONNECTING, + SERVICE_WIFI_EVENT_DISCONNECTED, + SERVICE_WIFI_EVENT_CONNECTED, + SERVICE_WIFI_EVENT_START_DPP, +} service_wifi_event_t; + +static void service_wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { + service_wifi_event_t service_ev; + if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { + esp_wifi_connect(); + } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { + if (s_retry_count < WIFI_SERVICE_MAXIMUM_RETRY) { + esp_wifi_connect(); + s_retry_count++; + } + } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { + s_retry_count = 0; + service_ev = SERVICE_WIFI_EVENT_CONNECTED; + xQueueSend(s_service_wifi_event_queue, &service_ev, 0); + } +} + +void service_wifi_task(void *pvParameters) { + char wifi_ssid[32]; + char wifi_pass[32]; + + size_t wifi_length = 32; + + uint8_t wifi_config_valid = 0; + + nvs_handle_t n_handle; + if (nvs_open("storage", NVS_READWRITE, &n_handle) != ESP_OK) { + ESP_LOGE(LOG_TAG, "Failed to create NVS handle."); + vTaskSuspend(NULL); + } + + // TODO: check WiFi storage length. + if (nvs_get_str(n_handle, "u_wifi_ssid", wifi_ssid, &wifi_length) == ESP_OK) { + if (nvs_get_str(n_handle, "u_wifi_pass", wifi_pass, &wifi_length) == ESP_OK) { + wifi_config_valid = 1; + } + } + + if_standby_queue_t queue_item; + + if (!wifi_config_valid) { + queue_item.item = IF_STANDBY_ITEM_WIFI; + snprintf(queue_item.payload, IF_STANDBY_PAYLOAD_LENGTH, "\U000F092D"); + xQueueSend(g_standby_screen_queue, &queue_item, 0); + } + + wifi_config_t wifi_config = { + .sta = + { + .ssid = WIFI_SERVICE_SSID, + .password = WIFI_SERVICE_PASS, + .threshold.authmode = WIFI_AUTH_WPA2_WPA3_PSK, + .pmf_cfg = + { + .capable = true, + .required = false, + }, + }, + }; + + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); + ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config)); + ESP_ERROR_CHECK(esp_wifi_start()); + + service_wifi_event_t queue_event; + for (;;) { + if (xQueueReceive(s_service_wifi_event_queue, &queue_event, portMAX_DELAY) == pdPASS) { + switch (queue_event) { + case SERVICE_WIFI_EVENT_CONNECTED: + queue_item.item = IF_STANDBY_ITEM_WIFI; + snprintf(queue_item.payload, IF_STANDBY_PAYLOAD_LENGTH, "\U000F0928"); + xQueueSend(g_standby_screen_queue, &queue_item, 0); + break; + default: + break; + } + } + } +} + +esp_err_t service_wifi_init(void) { + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + esp_netif_create_default_wifi_sta(); + + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, service_wifi_event_handler, NULL)); + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, service_wifi_event_handler, NULL)); + + s_service_wifi_event_queue = xQueueCreate(2, sizeof(service_wifi_event_t)); + if (s_service_wifi_event_queue == NULL) { + return ESP_FAIL; + } + + if (xTaskCreate(service_wifi_task, "SVC_WIFI", 2048, NULL, 6, &s_service_wifi_task_handle) != pdPASS) { + return ESP_FAIL; + } return ESP_OK; } - -void helper_wifi_task(void *pvParameters) { - // -} \ No newline at end of file diff --git a/main/service/service_wifi.h b/main/service/service_wifi.h new file mode 100644 index 0000000..4eaad6c --- /dev/null +++ b/main/service/service_wifi.h @@ -0,0 +1,8 @@ +#ifndef SERVICE_WIFI_H +#define SERVICE_WIFI_H + +#include "esp_system.h" + +esp_err_t service_wifi_init(void); + +#endif \ No newline at end of file