Firmware: clock printing and regular printing

This commit is contained in:
Erki 2024-02-10 23:49:23 +02:00
parent 57d068ffad
commit f098154b8d

View File

@ -7,6 +7,7 @@
#include "esp_expected.hpp" #include "esp_expected.hpp"
#include <cstring> #include <cstring>
#include <ctime>
#include <esp_event.h> #include <esp_event.h>
#include <esp_log.h> #include <esp_log.h>
@ -21,7 +22,8 @@ const char* TAG = "clock_core";
constexpr EventBits_t WIFI_FAIL_BIT = BIT0; constexpr EventBits_t WIFI_FAIL_BIT = BIT0;
constexpr EventBits_t WIFI_CONNECTED_BIT = BIT1; 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_; EventGroupHandle_t clock_core_event_group_;
void wifiEventHandler(void* arg, esp_event_base_t event_base, 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*) 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() esp_err_t sntpInitialize()
@ -122,6 +129,11 @@ namespace clock_core
void run(const char* wifi_ssid, const char* wifi_password) void run(const char* wifi_ssid, const char* wifi_password)
{ {
clock_core_event_group_ = xEventGroupCreate(); 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); if (const auto res = wifiInitialize(wifi_ssid, wifi_password);
!res.has_value()) !res.has_value())
@ -133,7 +145,7 @@ void run(const char* wifi_ssid, const char* wifi_password)
while (true) while (true)
{ {
const EventBits_t bits = xEventGroupWaitBits(clock_core_event_group_, 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, pdTRUE,
pdFALSE, pdFALSE,
portMAX_DELAY); 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)); ESP_LOGE(TAG, "SNTP setup failed. Error: %s", esp_err_to_name(res));
abort(); abort();
} }
xTimerStart(clock_timer, pdMS_TO_TICKS(10));
} }
else if (bits & WIFI_FAIL_BIT) else if (bits & WIFI_FAIL_BIT)
{ {
ESP_LOGE(TAG, "WiFi setup in clock_core::run failed."); 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."); 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 else
{ {
ESP_LOGE(TAG, "WiFi setup encountered an unexpected set of bits. %lu", bits); ESP_LOGE(TAG, "WiFi setup encountered an unexpected set of bits. %lu", bits);