feat: Implement Ethernet and Wi-Fi connection management with LED status indicators.
This commit is contained in:
@@ -1,3 +1,7 @@
|
|||||||
|
// C STD lib
|
||||||
|
#include <assert.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
// SDK
|
// SDK
|
||||||
#include "esp_err.h"
|
#include "esp_err.h"
|
||||||
#include "esp_eth.h"
|
#include "esp_eth.h"
|
||||||
@@ -8,12 +12,15 @@
|
|||||||
#include "esp_system.h"
|
#include "esp_system.h"
|
||||||
#include "esp_wifi.h"
|
#include "esp_wifi.h"
|
||||||
#include "ethernet_init.h"
|
#include "ethernet_init.h"
|
||||||
#include <assert.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
// Project includes
|
// Project includes
|
||||||
#include "types.hpp"
|
#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 ===
|
// === Ethernet ===
|
||||||
|
|
||||||
internal constexpr char kLogEthernet[] = "ETH";
|
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_LOGI(kLogEthernet, "Got IPv4 event: Interface \"%s\" address: " IPSTR,
|
||||||
esp_netif_get_desc(event->esp_netif), IP2STR(&event->ip_info.ip));
|
esp_netif_get_desc(event->esp_netif), IP2STR(&event->ip_info.ip));
|
||||||
|
|
||||||
xSemaphoreGive(s_semph_get_ip_addrs);
|
xSemaphoreGive(s_semph_get_ip_addrs);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,6 +117,7 @@ internal esp_err_t connect_ethernet(bool blockUntilIPAcquired) {
|
|||||||
if (blockUntilIPAcquired) {
|
if (blockUntilIPAcquired) {
|
||||||
ESP_LOGI(kLogEthernet, "Waiting for IP address.");
|
ESP_LOGI(kLogEthernet, "Waiting for IP address.");
|
||||||
xSemaphoreTake(s_semph_get_ip_addrs, portMAX_DELAY);
|
xSemaphoreTake(s_semph_get_ip_addrs, portMAX_DELAY);
|
||||||
|
blink_last_ip_octet();
|
||||||
}
|
}
|
||||||
|
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
@@ -130,6 +139,7 @@ internal esp_err_t check_ethernet_connection(uint32_t timeoutSeconds) {
|
|||||||
timeoutSeconds);
|
timeoutSeconds);
|
||||||
if (xSemaphoreTake(s_semph_get_ip_addrs,
|
if (xSemaphoreTake(s_semph_get_ip_addrs,
|
||||||
pdMS_TO_TICKS(timeoutSeconds * 1000))) {
|
pdMS_TO_TICKS(timeoutSeconds * 1000))) {
|
||||||
|
blink_last_ip_octet();
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
} else {
|
} else {
|
||||||
return ESP_ERR_TIMEOUT;
|
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;
|
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
|
||||||
ESP_LOGI(kLogWifi, "Got IPv4 event: Interface \"%s\" address: " IPSTR,
|
ESP_LOGI(kLogWifi, "Got IPv4 event: Interface \"%s\" address: " IPSTR,
|
||||||
esp_netif_get_desc(event->esp_netif), IP2STR(&event->ip_info.ip));
|
esp_netif_get_desc(event->esp_netif), IP2STR(&event->ip_info.ip));
|
||||||
|
|
||||||
xSemaphoreGive(s_semph_get_wifi_ip_addrs);
|
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) {
|
if (blockUntilIPAcquired) {
|
||||||
ESP_LOGI(kLogWifi, "Waiting for IP address.");
|
ESP_LOGI(kLogWifi, "Waiting for IP address.");
|
||||||
xSemaphoreTake(s_semph_get_wifi_ip_addrs, portMAX_DELAY);
|
xSemaphoreTake(s_semph_get_wifi_ip_addrs, portMAX_DELAY);
|
||||||
|
blink_last_ip_octet();
|
||||||
}
|
}
|
||||||
|
|
||||||
return ESP_OK;
|
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);
|
ESP_LOGI(kLogWifi, "Waiting for IP address for %d seconds.", timeoutSeconds);
|
||||||
if (xSemaphoreTake(s_semph_get_wifi_ip_addrs,
|
if (xSemaphoreTake(s_semph_get_wifi_ip_addrs,
|
||||||
pdMS_TO_TICKS(timeoutSeconds * 1000))) {
|
pdMS_TO_TICKS(timeoutSeconds * 1000))) {
|
||||||
|
blink_last_ip_octet();
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
} else {
|
} else {
|
||||||
return ESP_ERR_TIMEOUT;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -53,4 +53,27 @@ internal void set_led_status(led_status status) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
led_strip_refresh(led_strip);
|
led_strip_refresh(led_strip);
|
||||||
}
|
}
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|||||||
@@ -15,9 +15,9 @@
|
|||||||
|
|
||||||
// Project cpp (Unity Build entry)
|
// Project cpp (Unity Build entry)
|
||||||
// clang-format off
|
// clang-format off
|
||||||
|
#include "led_status.cpp"
|
||||||
#include "connect.cpp"
|
#include "connect.cpp"
|
||||||
#include "http_server.cpp"
|
#include "http_server.cpp"
|
||||||
#include "led_status.cpp"
|
|
||||||
// clang-format on
|
// clang-format on
|
||||||
|
|
||||||
internal constexpr bool kBlockUntilEthernetEstablished = false;
|
internal constexpr bool kBlockUntilEthernetEstablished = false;
|
||||||
|
|||||||
Reference in New Issue
Block a user