From bdf4a73cf2afe29cabdb410c1aa2f7d55152c883 Mon Sep 17 00:00:00 2001 From: Patedam Date: Tue, 3 Mar 2026 10:58:18 -0500 Subject: [PATCH] feat: Implement Ethernet and Wi-Fi connection management with LED status indicators. --- Provider/main/connect.cpp | 45 ++++++++++++++++++++++++++++++++++-- Provider/main/led_status.cpp | 25 +++++++++++++++++++- Provider/main/main.cpp | 2 +- 3 files changed, 68 insertions(+), 4 deletions(-) diff --git a/Provider/main/connect.cpp b/Provider/main/connect.cpp index d443fb8..757f9aa 100644 --- a/Provider/main/connect.cpp +++ b/Provider/main/connect.cpp @@ -1,3 +1,7 @@ +// C STD lib +#include +#include + // SDK #include "esp_err.h" #include "esp_eth.h" @@ -8,12 +12,15 @@ #include "esp_system.h" #include "esp_wifi.h" #include "ethernet_init.h" -#include -#include // Project includes #include "types.hpp" +// Forward declarations +internal esp_err_t get_ip_info(esp_netif_ip_info_t *ip_info); +internal void led_blink_number(int n, uint8_t r, uint8_t g, uint8_t b); +internal void blink_last_ip_octet(); + // === Ethernet === internal constexpr char kLogEthernet[] = "ETH"; @@ -39,6 +46,7 @@ void eth_on_got_ip(void *arg, esp_event_base_t event_base, int32_t event_id, } ESP_LOGI(kLogEthernet, "Got IPv4 event: Interface \"%s\" address: " IPSTR, esp_netif_get_desc(event->esp_netif), IP2STR(&event->ip_info.ip)); + xSemaphoreGive(s_semph_get_ip_addrs); } @@ -109,6 +117,7 @@ internal esp_err_t connect_ethernet(bool blockUntilIPAcquired) { if (blockUntilIPAcquired) { ESP_LOGI(kLogEthernet, "Waiting for IP address."); xSemaphoreTake(s_semph_get_ip_addrs, portMAX_DELAY); + blink_last_ip_octet(); } return ESP_OK; @@ -130,6 +139,7 @@ internal esp_err_t check_ethernet_connection(uint32_t timeoutSeconds) { timeoutSeconds); if (xSemaphoreTake(s_semph_get_ip_addrs, pdMS_TO_TICKS(timeoutSeconds * 1000))) { + blink_last_ip_octet(); return ESP_OK; } else { return ESP_ERR_TIMEOUT; @@ -154,6 +164,7 @@ void wifi_event_handler(void *arg, esp_event_base_t event_base, ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; ESP_LOGI(kLogWifi, "Got IPv4 event: Interface \"%s\" address: " IPSTR, esp_netif_get_desc(event->esp_netif), IP2STR(&event->ip_info.ip)); + xSemaphoreGive(s_semph_get_wifi_ip_addrs); } } @@ -220,6 +231,7 @@ internal esp_err_t connect_wifi(const char *ssid, const char *password, if (blockUntilIPAcquired) { ESP_LOGI(kLogWifi, "Waiting for IP address."); xSemaphoreTake(s_semph_get_wifi_ip_addrs, portMAX_DELAY); + blink_last_ip_octet(); } return ESP_OK; @@ -239,8 +251,37 @@ internal esp_err_t check_wifi_connection(uint32_t timeoutSeconds) { ESP_LOGI(kLogWifi, "Waiting for IP address for %d seconds.", timeoutSeconds); if (xSemaphoreTake(s_semph_get_wifi_ip_addrs, pdMS_TO_TICKS(timeoutSeconds * 1000))) { + blink_last_ip_octet(); return ESP_OK; } else { return ESP_ERR_TIMEOUT; } } + +internal esp_err_t get_ip_info(esp_netif_ip_info_t *ip_info) { + if (s_eth_netif) { + return esp_netif_get_ip_info(s_eth_netif, ip_info); + } else if (s_wifi_netif) { + return esp_netif_get_ip_info(s_wifi_netif, ip_info); + } + return ESP_FAIL; +} + +internal void led_blink_number(int n, uint8_t r, uint8_t g, uint8_t b); + +internal void blink_last_ip_octet() { + esp_netif_ip_info_t ip_info; + if (get_ip_info(&ip_info) == ESP_OK) { + uint8_t last_octet = (ip_info.ip.addr >> 24) & 0xFF; + printf("IP Address: " IPSTR "\n", IP2STR(&ip_info.ip)); + + // Blink digits of last_octet + int h = last_octet / 100; + int t = (last_octet / 10) % 10; + int u = last_octet % 10; + + led_blink_number(h, 255, 255, 255); + led_blink_number(t, 255, 255, 255); + led_blink_number(u, 255, 255, 255); + } +} diff --git a/Provider/main/led_status.cpp b/Provider/main/led_status.cpp index f246756..8f56a35 100644 --- a/Provider/main/led_status.cpp +++ b/Provider/main/led_status.cpp @@ -53,4 +53,27 @@ internal void set_led_status(led_status status) { break; } led_strip_refresh(led_strip); -} \ No newline at end of file +} +internal void led_blink_number(int n, uint8_t r, uint8_t g, uint8_t b) { + if (n <= 0) { + for (int i = 0; i < 2; i++) { + led_strip_set_pixel(led_strip, 0, r, g, b); + led_strip_refresh(led_strip); + vTaskDelay(pdMS_TO_TICKS(50)); + led_strip_clear(led_strip); + led_strip_refresh(led_strip); + vTaskDelay(pdMS_TO_TICKS(50)); + } + vTaskDelay(pdMS_TO_TICKS(1000)); + return; + } + for (int i = 0; i < n; i++) { + led_strip_set_pixel(led_strip, 0, r, g, b); + led_strip_refresh(led_strip); + vTaskDelay(pdMS_TO_TICKS(300)); + led_strip_clear(led_strip); + led_strip_refresh(led_strip); + vTaskDelay(pdMS_TO_TICKS(300)); + } + vTaskDelay(pdMS_TO_TICKS(1000)); +} diff --git a/Provider/main/main.cpp b/Provider/main/main.cpp index 4aed8b9..6f72050 100644 --- a/Provider/main/main.cpp +++ b/Provider/main/main.cpp @@ -15,9 +15,9 @@ // Project cpp (Unity Build entry) // clang-format off +#include "led_status.cpp" #include "connect.cpp" #include "http_server.cpp" -#include "led_status.cpp" // clang-format on internal constexpr bool kBlockUntilEthernetEstablished = false;