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
|
||||
#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 <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user