From fd5d4da3537b5802cd4ff8e6ea50e4f0bdce6325 Mon Sep 17 00:00:00 2001 From: Renze Nicolai Date: Mon, 18 Apr 2022 02:10:44 +0200 Subject: [PATCH] Connect to wifi :-) --- main/CMakeLists.txt | 2 +- main/graphics_wrapper.c | 136 +++++++++++++++++++++++++++- main/include/graphics_wrapper.h | 4 + main/include/wifi_connection.h | 8 ++ main/main.c | 151 +++++--------------------------- main/wifi_connection.c | 88 +++++++++++++++++++ 6 files changed, 257 insertions(+), 132 deletions(-) create mode 100644 main/include/wifi_connection.h create mode 100644 main/wifi_connection.c diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index c83093c..2b2e483 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,4 +1,4 @@ idf_component_register( - SRCS "main.c" "menu.c" "fpga_test.c" "pax_keyboard.c" "button_wrapper.c" "system_wrapper.c" "appfs_wrapper.c" "graphics_wrapper.c" "settings.c" + SRCS "main.c" "menu.c" "fpga_test.c" "pax_keyboard.c" "button_wrapper.c" "system_wrapper.c" "appfs_wrapper.c" "graphics_wrapper.c" "settings.c" "wifi_connection.c" INCLUDE_DIRS "." "include" ) diff --git a/main/graphics_wrapper.c b/main/graphics_wrapper.c index e018cac..f793148 100644 --- a/main/graphics_wrapper.c +++ b/main/graphics_wrapper.c @@ -1,6 +1,10 @@ +#include #include "graphics_wrapper.h" +#include "hardware.h" +#include "pax_keyboard.h" +#include "button_wrapper.h" -void message_render(pax_buf_t *aBuffer, char* message, float aPosX, float aPosY, float aWidth, float aHeight) { +void render_message(pax_buf_t *aBuffer, char* message, float aPosX, float aPosY, float aWidth, float aHeight) { pax_col_t fgColor = 0xFFFF0000; pax_col_t bgColor = 0xFFFFD4D4; pax_clip(aBuffer, aPosX, aPosY, aWidth, aHeight); @@ -18,8 +22,136 @@ esp_err_t graphics_task(pax_buf_t* pax_buffer, ILI9341* ili9341, uint8_t* frameb } if (message != NULL) { - message_render(pax_buffer, message, 20, 110, 320-40, 20); + render_message(pax_buffer, message, 20, 110, 320-40, 20); } return ili9341_write(ili9341, framebuffer); } + +bool keyboard(xQueueHandle buttonQueue, pax_buf_t* aBuffer, ILI9341* ili9341, uint8_t* framebuffer, float aPosX, float aPosY, float aWidth, float aHeight, const char* aTitle, const char* aHint, char* aOutput, size_t aOutputSize) { + bool accepted = false; + pkb_ctx_t kb_ctx; + pkb_init(aBuffer, &kb_ctx, aOutput); + + pax_col_t fgColor = 0xFF000000; + pax_col_t bgColor = 0xFFFFFFFF; + pax_col_t shadowColor = 0xFFC0C3C8; + pax_col_t borderColor = 0xFF0000AA; + pax_col_t titleBgColor = 0xFF080764; + pax_col_t titleColor = 0xFFFFFFFF; + pax_col_t selColor = 0xff007fff; + + kb_ctx.text_col = borderColor; + kb_ctx.sel_text_col = selColor; + kb_ctx.sel_col = selColor; + kb_ctx.bg_col = bgColor; + + kb_ctx.kb_font_size = 18; + + float titleHeight = 20; + float hintHeight = 14; + + pax_noclip(aBuffer); + pax_simple_rect(aBuffer, shadowColor, aPosX+5, aPosY+5, aWidth, aHeight); + pax_simple_rect(aBuffer, bgColor, aPosX, aPosY, aWidth, aHeight); + pax_outline_rect(aBuffer, borderColor, aPosX, aPosY, aWidth, aHeight); + pax_simple_rect(aBuffer, titleBgColor, aPosX, aPosY, aWidth, titleHeight); + pax_simple_line(aBuffer, titleColor, aPosX + 1, aPosY + titleHeight, aPosX + aWidth - 2, aPosY + titleHeight - 1); + pax_clip(aBuffer, aPosX + 1, aPosY + 1, aWidth - 2, titleHeight - 2); + pax_draw_text(aBuffer, titleColor, NULL, titleHeight - 2, aPosX + 1, aPosY + 1, aTitle); + pax_clip(aBuffer, aPosX + 1, aPosY + aHeight - hintHeight, aWidth - 2, hintHeight); + pax_draw_text(aBuffer, borderColor, NULL, hintHeight - 2, aPosX + 1, aPosY + aHeight - hintHeight, aHint); + pax_noclip(aBuffer); + + kb_ctx.x = aPosX + 1; + kb_ctx.y = aPosY + titleHeight + 1 ; + kb_ctx.width = aWidth - 2; + kb_ctx.height = aHeight - 3 - titleHeight - hintHeight; + + bool running = true; + while (running) { + button_message_t buttonMessage = {0}; + if (xQueueReceive(buttonQueue, &buttonMessage, 16 / portTICK_PERIOD_MS) == pdTRUE) { + uint8_t pin = buttonMessage.button; + bool value = buttonMessage.state; + switch(pin) { + case PCA9555_PIN_BTN_JOY_DOWN: + if (value) { + pkb_press(&kb_ctx, PKB_DOWN); + } else { + pkb_release(&kb_ctx, PKB_DOWN); + } + break; + case PCA9555_PIN_BTN_JOY_UP: + if (value) { + pkb_press(&kb_ctx, PKB_UP); + } else { + pkb_release(&kb_ctx, PKB_UP); + } + break; + case PCA9555_PIN_BTN_JOY_LEFT: + if (value) { + pkb_press(&kb_ctx, PKB_LEFT); + } else { + pkb_release(&kb_ctx, PKB_LEFT); + } + break; + case PCA9555_PIN_BTN_JOY_RIGHT: + if (value) { + pkb_press(&kb_ctx, PKB_RIGHT); + } else { + pkb_release(&kb_ctx, PKB_RIGHT); + } + break; + case PCA9555_PIN_BTN_JOY_PRESS: + if (value) { + pkb_press(&kb_ctx, PKB_SHIFT); + } else { + pkb_release(&kb_ctx, PKB_SHIFT); + } + break; + case PCA9555_PIN_BTN_ACCEPT: + if (value) { + pkb_press(&kb_ctx, PKB_CHARSELECT); + } else { + pkb_release(&kb_ctx, PKB_CHARSELECT); + } + break; + case PCA9555_PIN_BTN_BACK: + if (value) { + pkb_press(&kb_ctx, PKB_DELETE_BEFORE); + } else { + pkb_release(&kb_ctx, PKB_DELETE_BEFORE); + } + break; + case PCA9555_PIN_BTN_SELECT: + if (value) { + pkb_press(&kb_ctx, PKB_MODESELECT); + } else { + pkb_release(&kb_ctx, PKB_MODESELECT); + } + break; + case PCA9555_PIN_BTN_HOME: + if (value) { + running = false; + } + break; + default: + break; + } + } + pkb_loop(&kb_ctx); + if (kb_ctx.dirty) { + pkb_redraw(aBuffer, &kb_ctx); + ili9341_write(ili9341, framebuffer); + } + if (kb_ctx.input_accepted) { + memset(aOutput, 0, aOutputSize); + strncpy(aOutput, kb_ctx.content, aOutputSize - 1); + running = false; + accepted = true; + } + } + pkb_destroy(&kb_ctx); + return accepted; +} diff --git a/main/include/graphics_wrapper.h b/main/include/graphics_wrapper.h index 87d585e..b3dd791 100644 --- a/main/include/graphics_wrapper.h +++ b/main/include/graphics_wrapper.h @@ -3,6 +3,9 @@ #include #include #include +#include +#include +#include #include "pax_gfx.h" #include "ili9341.h" @@ -10,3 +13,4 @@ esp_err_t graphics_task(pax_buf_t* pax_buffer, ILI9341* ili9341, uint8_t* framebuffer, menu_t* menu, char* message); +bool keyboard(xQueueHandle buttonQueue, pax_buf_t* aBuffer, ILI9341* ili9341, uint8_t* framebuffer, float aPosX, float aPosY, float aWidth, float aHeight, const char* aTitle, const char* aHint, char* aOutput, size_t aOutputSize); diff --git a/main/include/wifi_connection.h b/main/include/wifi_connection.h new file mode 100644 index 0000000..7c011ab --- /dev/null +++ b/main/include/wifi_connection.h @@ -0,0 +1,8 @@ +#pragma once + +#include +#include + +#include "esp_wifi.h" + +bool wifi_init(const char* aSsid, const char* aPassword, wifi_auth_mode_t aAuthmode, uint8_t aRetryMax); diff --git a/main/main.c b/main/main.c index 38c4429..10c9dbf 100644 --- a/main/main.c +++ b/main/main.c @@ -26,7 +26,7 @@ #include "graphics_wrapper.h" #include "appfs_wrapper.h" #include "settings.h" -#include "pax_keyboard.h" +#include "wifi_connection.h" static const char *TAG = "main"; @@ -200,134 +200,6 @@ void menu_wifi_settings(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341 menu_free(menu); } -bool keyboard(xQueueHandle buttonQueue, pax_buf_t* aBuffer, ILI9341* ili9341, uint8_t* framebuffer, float aPosX, float aPosY, float aWidth, float aHeight, const char* aTitle, const char* aHint, char* aOutput, size_t aOutputSize) { - bool accepted = false; - pkb_ctx_t kb_ctx; - pkb_init(aBuffer, &kb_ctx, aOutput); - - pax_col_t fgColor = 0xFF000000; - pax_col_t bgColor = 0xFFFFFFFF; - pax_col_t shadowColor = 0xFFC0C3C8; - pax_col_t borderColor = 0xFF0000AA; - pax_col_t titleBgColor = 0xFF080764; - pax_col_t titleColor = 0xFFFFFFFF; - pax_col_t selColor = 0xff007fff; - - kb_ctx.text_col = borderColor; - kb_ctx.sel_text_col = selColor; - kb_ctx.sel_col = selColor; - kb_ctx.bg_col = bgColor; - - kb_ctx.kb_font_size = 18; - - float titleHeight = 20; - float hintHeight = 14; - - pax_noclip(aBuffer); - pax_simple_rect(aBuffer, shadowColor, aPosX+5, aPosY+5, aWidth, aHeight); - pax_simple_rect(aBuffer, bgColor, aPosX, aPosY, aWidth, aHeight); - pax_outline_rect(aBuffer, borderColor, aPosX, aPosY, aWidth, aHeight); - pax_simple_rect(aBuffer, titleBgColor, aPosX, aPosY, aWidth, titleHeight); - pax_simple_line(aBuffer, titleColor, aPosX + 1, aPosY + titleHeight, aPosX + aWidth - 2, aPosY + titleHeight - 1); - pax_clip(aBuffer, aPosX + 1, aPosY + 1, aWidth - 2, titleHeight - 2); - pax_draw_text(aBuffer, titleColor, NULL, titleHeight - 2, aPosX + 1, aPosY + 1, aTitle); - pax_clip(aBuffer, aPosX + 1, aPosY + aHeight - hintHeight, aWidth - 2, hintHeight); - pax_draw_text(aBuffer, borderColor, NULL, hintHeight - 2, aPosX + 1, aPosY + aHeight - hintHeight, aHint); - pax_noclip(aBuffer); - - kb_ctx.x = aPosX + 1; - kb_ctx.y = aPosY + titleHeight + 1 ; - kb_ctx.width = aWidth - 2; - kb_ctx.height = aHeight - 3 - titleHeight - hintHeight; - - bool running = true; - while (running) { - button_message_t buttonMessage = {0}; - if (xQueueReceive(buttonQueue, &buttonMessage, 16 / portTICK_PERIOD_MS) == pdTRUE) { - uint8_t pin = buttonMessage.button; - bool value = buttonMessage.state; - switch(pin) { - case PCA9555_PIN_BTN_JOY_DOWN: - if (value) { - pkb_press(&kb_ctx, PKB_DOWN); - } else { - pkb_release(&kb_ctx, PKB_DOWN); - } - break; - case PCA9555_PIN_BTN_JOY_UP: - if (value) { - pkb_press(&kb_ctx, PKB_UP); - } else { - pkb_release(&kb_ctx, PKB_UP); - } - break; - case PCA9555_PIN_BTN_JOY_LEFT: - if (value) { - pkb_press(&kb_ctx, PKB_LEFT); - } else { - pkb_release(&kb_ctx, PKB_LEFT); - } - break; - case PCA9555_PIN_BTN_JOY_RIGHT: - if (value) { - pkb_press(&kb_ctx, PKB_RIGHT); - } else { - pkb_release(&kb_ctx, PKB_RIGHT); - } - break; - case PCA9555_PIN_BTN_JOY_PRESS: - if (value) { - pkb_press(&kb_ctx, PKB_SHIFT); - } else { - pkb_release(&kb_ctx, PKB_SHIFT); - } - break; - case PCA9555_PIN_BTN_ACCEPT: - if (value) { - pkb_press(&kb_ctx, PKB_CHARSELECT); - } else { - pkb_release(&kb_ctx, PKB_CHARSELECT); - } - break; - case PCA9555_PIN_BTN_BACK: - if (value) { - pkb_press(&kb_ctx, PKB_DELETE_BEFORE); - } else { - pkb_release(&kb_ctx, PKB_DELETE_BEFORE); - } - break; - case PCA9555_PIN_BTN_SELECT: - if (value) { - pkb_press(&kb_ctx, PKB_MODESELECT); - } else { - pkb_release(&kb_ctx, PKB_MODESELECT); - } - break; - case PCA9555_PIN_BTN_HOME: - if (value) { - running = false; - } - break; - default: - break; - } - } - pkb_loop(&kb_ctx); - if (kb_ctx.dirty) { - pkb_redraw(aBuffer, &kb_ctx); - ili9341_write(ili9341, framebuffer); - } - if (kb_ctx.input_accepted) { - memset(aOutput, 0, aOutputSize); - strncpy(aOutput, kb_ctx.content, aOutputSize - 1); - running = false; - accepted = true; - } - } - pkb_destroy(&kb_ctx); - return accepted; -} - void app_main(void) { esp_err_t res; @@ -416,6 +288,27 @@ void app_main(void) { } else if (menu_action == ACTION_INSTALLER) { graphics_task(pax_buffer, ili9341, framebuffer, NULL, "INSTALLER"); //appfs_store_app(); + nvs_handle_t handle; + nvs_open("system", NVS_READWRITE, &handle); + char ssid[33]; + char password[33]; + size_t requiredSize; + esp_err_t res = nvs_get_str(handle, "wifi.ssid", NULL, &requiredSize); + if (res != ESP_OK) { + strcpy(ssid, ""); + } else if (requiredSize < sizeof(ssid)) { + res = nvs_get_str(handle, "wifi.ssid", ssid, &requiredSize); + if (res != ESP_OK) strcpy(ssid, ""); + res = nvs_get_str(handle, "wifi.password", NULL, &requiredSize); + if (res != ESP_OK) { + strcpy(password, ""); + } else if (requiredSize < sizeof(password)) { + res = nvs_get_str(handle, "wifi.password", password, &requiredSize); + if (res != ESP_OK) strcpy(password, ""); + } + } + nvs_close(&handle); + wifi_init(ssid, password, WIFI_AUTH_WPA2_PSK, 3); } else if (menu_action == ACTION_OTA) { graphics_task(pax_buffer, ili9341, framebuffer, NULL, "Firmware update..."); } else if (menu_action == ACTION_SETTINGS) { diff --git a/main/wifi_connection.c b/main/wifi_connection.c new file mode 100644 index 0000000..7883289 --- /dev/null +++ b/main/wifi_connection.c @@ -0,0 +1,88 @@ +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/event_groups.h" +#include "esp_system.h" +#include "esp_wifi.h" +#include "esp_event.h" +#include "esp_log.h" +#include "lwip/err.h" +#include "lwip/sys.h" + +#include "wifi_connection.h" + +static const char *TAG = "wifi_connection"; + +#define WIFI_CONNECTED_BIT BIT0 +#define WIFI_FAIL_BIT BIT1 + +static EventGroupHandle_t s_wifi_event_group; + +static uint8_t gRetryCounter = 0; +static uint8_t gRetryMax = 3; + +static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { + 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 (gRetryCounter < 3) { + esp_wifi_connect(); + gRetryCounter++; + ESP_LOGI(TAG, "retry to connect to the AP"); + } else { + ESP_LOGI(TAG,"connect to the AP fail"); + xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT); + } + } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { + ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; + ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip)); + gRetryCounter = 0; + xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); + } +} + +bool wifi_init(const char* aSsid, const char* aPassword, wifi_auth_mode_t aAuthmode, uint8_t aRetryMax) { + gRetryCounter = 0; + gRetryMax = aRetryMax; + s_wifi_event_group = xEventGroupCreate(); + + 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_event_handler_instance_t instance_any_id; + esp_event_handler_instance_t instance_got_ip; + ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, &instance_any_id)); + ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, &instance_got_ip)); + + wifi_config_t wifi_config = {0}; + strcpy((char*) wifi_config.sta.ssid, aSsid); + strcpy((char*) wifi_config.sta.password, aPassword); + wifi_config.sta.threshold.authmode = aAuthmode; + + 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() ); + + ESP_LOGI(TAG, "Connecting to WiFi..."); + + /* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum + * number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */ + EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, pdFALSE, pdFALSE, portMAX_DELAY); + /* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually happened. */ + if (bits & WIFI_CONNECTED_BIT) { + ESP_LOGI(TAG, "Connected to WiFi"); + return true; + } else if (bits & WIFI_FAIL_BIT) { + ESP_LOGE(TAG, "Failed to connect"); + ESP_ERROR_CHECK(esp_wifi_stop()); + } else { + ESP_LOGE(TAG, "Unknown event received while waiting on connection"); + ESP_ERROR_CHECK(esp_wifi_stop()); + } + return false; +}