// STD Lib #include // SDK #include "driver/gpio.h" #include "esp_log.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "nvs_flash.h" #include "sdkconfig.h" #include "soc/gpio_num.h" // Project cpp #include "connect.cpp" #include "led_status.cpp" // TODO : Make it configurable internal constexpr bool blockUntilEthernetEstablished = false; internal bool ethernetInitialized = false; internal bool wifiInitialized = false; extern "C" void app_main() { printf("Hello, Worldi!\n"); ESP_ERROR_CHECK(nvs_flash_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); setup_led(); set_led_status(led_status::ConnectingEthernet); ethernetInitialized = true; esp_err_t result = connect_ethernet(blockUntilEthernetEstablished); if (result != ESP_OK) { set_led_status(led_status::Failed); vTaskDelay(pdMS_TO_TICKS(1000)); goto shutdown; } // Check for ethernet connection until its made if (!blockUntilEthernetEstablished) { uint8 retries = 1; do { set_led_status(led_status::ConnectingEthernet); result = check_ethernet_connection(retries); if (result != ESP_OK) { set_led_status(led_status::Failed); vTaskDelay(pdMS_TO_TICKS(1000)); } retries++; } while (result == ESP_ERR_TIMEOUT && retries <= CONFIG_CALENDINK_ETH_RETRIES); } if (result != ESP_OK) { printf("Ethernet failed, trying wifi\n"); disconnect_ethernet(); ethernetInitialized = false; set_led_status(led_status::ConnectingWifi); wifiInitialized = true; result = connect_wifi(CONFIG_CALENDINK_WIFI_SSID, CONFIG_CALENDINK_WIFI_PASSWORD, blockUntilEthernetEstablished); if (result != ESP_OK) { set_led_status(led_status::Failed); vTaskDelay(pdMS_TO_TICKS(1000)); goto shutdown; } if (!blockUntilEthernetEstablished) { uint8 retries = 1; do { set_led_status(led_status::ConnectingWifi); result = check_wifi_connection(retries); if (result != ESP_OK) { set_led_status(led_status::Failed); vTaskDelay(pdMS_TO_TICKS(1000)); } retries++; } while (result == ESP_ERR_TIMEOUT && retries <= CONFIG_CALENDINK_WIFI_RETRIES); } if (result != ESP_OK) { printf("Wifi failed.\n"); goto shutdown; } set_led_status(led_status::ReadyWifi); printf("Will use Wifi!\n"); } else { set_led_status(led_status::ReadyEthernet); printf("Will use Ethernet!\n"); } printf("Connected!\n"); vTaskDelay(pdMS_TO_TICKS(5000)); // TODO Main loop shutdown: printf("Shutting down.\n"); if (ethernetInitialized) { disconnect_ethernet(); ethernetInitialized = false; } if (wifiInitialized) { disconnect_wifi(); wifiInitialized = false; } destroy_led(); ESP_ERROR_CHECK(esp_event_loop_delete_default()); ESP_ERROR_CHECK(nvs_flash_deinit()); }