From f098154b8d0b0adc9147070a90ad49f2f90cff07 Mon Sep 17 00:00:00 2001 From: Erki Date: Sat, 10 Feb 2024 23:49:23 +0200 Subject: [PATCH] Firmware: clock printing and regular printing --- firmware/main/clock_core.cpp | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/firmware/main/clock_core.cpp b/firmware/main/clock_core.cpp index b3d3f88..f543fa0 100644 --- a/firmware/main/clock_core.cpp +++ b/firmware/main/clock_core.cpp @@ -7,6 +7,7 @@ #include "esp_expected.hpp" #include +#include #include #include @@ -21,7 +22,8 @@ const char* TAG = "clock_core"; constexpr EventBits_t WIFI_FAIL_BIT = BIT0; constexpr EventBits_t WIFI_CONNECTED_BIT = BIT1; -constexpr EventBits_t SNTP_SYNCED = BIT2; +constexpr EventBits_t SNTP_SYNCED_BIT = BIT2; +constexpr EventBits_t CLOCK_UPDATE_BIT = BIT3; EventGroupHandle_t clock_core_event_group_; void wifiEventHandler(void* arg, esp_event_base_t event_base, @@ -59,7 +61,12 @@ void wifiEventHandler(void* arg, esp_event_base_t event_base, void sntpEventHandler(struct timeval*) { - xEventGroupSetBits(clock_core_event_group_, SNTP_SYNCED); + xEventGroupSetBits(clock_core_event_group_, SNTP_SYNCED_BIT); +} + +void timerEventHandler(TimerHandle_t) +{ + xEventGroupSetBits(clock_core_event_group_, CLOCK_UPDATE_BIT); } esp_err_t sntpInitialize() @@ -122,6 +129,11 @@ namespace clock_core void run(const char* wifi_ssid, const char* wifi_password) { clock_core_event_group_ = xEventGroupCreate(); + auto clock_timer = xTimerCreate("clock_core", + pdMS_TO_TICKS(60'000), + pdTRUE, + nullptr, + timerEventHandler); if (const auto res = wifiInitialize(wifi_ssid, wifi_password); !res.has_value()) @@ -133,7 +145,7 @@ void run(const char* wifi_ssid, const char* wifi_password) while (true) { const EventBits_t bits = xEventGroupWaitBits(clock_core_event_group_, - WIFI_CONNECTED_BIT | WIFI_FAIL_BIT | SNTP_SYNCED, + WIFI_CONNECTED_BIT | WIFI_FAIL_BIT | SNTP_SYNCED_BIT | CLOCK_UPDATE_BIT, pdTRUE, pdFALSE, portMAX_DELAY); @@ -147,15 +159,28 @@ void run(const char* wifi_ssid, const char* wifi_password) ESP_LOGE(TAG, "SNTP setup failed. Error: %s", esp_err_to_name(res)); abort(); } + + xTimerStart(clock_timer, pdMS_TO_TICKS(10)); } else if (bits & WIFI_FAIL_BIT) { ESP_LOGE(TAG, "WiFi setup in clock_core::run failed."); } - else if (bits & SNTP_SYNCED) + else if (bits & SNTP_SYNCED_BIT) { ESP_LOGI(TAG, "SNTP sync successful."); } + else if (bits & CLOCK_UPDATE_BIT) + { + char time_buf[64]; + std::time_t time_now; + std::tm time_info; + std::time(&time_now); + localtime_r(&time_now, &time_info); + + std::strftime(time_buf, sizeof(time_buf), "%c", &time_info); + ESP_LOGI(TAG, "Current time: %s", time_buf); + } else { ESP_LOGE(TAG, "WiFi setup encountered an unexpected set of bits. %lu", bits);