Firmware: Refactor events a little
This commit is contained in:
parent
a763095f96
commit
2240213efd
@ -5,6 +5,7 @@
|
||||
#include "clock_core.hpp"
|
||||
|
||||
#include "esp_expected.hpp"
|
||||
#include "clock_core_event.hpp"
|
||||
|
||||
#include <cstring>
|
||||
#include <ctime>
|
||||
@ -20,16 +21,30 @@ namespace
|
||||
|
||||
const char* TAG = "clock_core";
|
||||
|
||||
constexpr EventBits_t WIFI_FAIL_BIT = BIT0;
|
||||
constexpr EventBits_t WIFI_CONNECTED_BIT = BIT1;
|
||||
constexpr EventBits_t SNTP_SYNCED_BIT = BIT2;
|
||||
constexpr EventBits_t CLOCK_UPDATE_BIT = BIT3;
|
||||
EventGroupHandle_t clock_core_event_group_;
|
||||
struct Context
|
||||
{
|
||||
clock_core::EventGroup* events = nullptr;
|
||||
|
||||
Context(const Context&) = delete;
|
||||
Context(Context&&) = delete;
|
||||
Context& operator=(const Context&) = delete;
|
||||
Context& operator=(Context&&) = delete;
|
||||
|
||||
static Context& getContext()
|
||||
{
|
||||
static Context ctx;
|
||||
return ctx;
|
||||
}
|
||||
|
||||
private:
|
||||
Context() = default;
|
||||
};
|
||||
|
||||
void wifiEventHandler(void* arg, esp_event_base_t event_base,
|
||||
int32_t event_id, void* event_data)
|
||||
{
|
||||
static int retry_num = 0;
|
||||
auto* ctx = static_cast<Context*>(event_data);
|
||||
|
||||
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START)
|
||||
{
|
||||
@ -45,7 +60,7 @@ void wifiEventHandler(void* arg, esp_event_base_t event_base,
|
||||
}
|
||||
else
|
||||
{
|
||||
xEventGroupSetBits(clock_core_event_group_, WIFI_FAIL_BIT);
|
||||
ctx->events->setEvent(clock_core::EventGroup::WIFI_FAILED);
|
||||
}
|
||||
ESP_LOGW(TAG, "Connection to AP failed.");
|
||||
}
|
||||
@ -55,18 +70,18 @@ void wifiEventHandler(void* arg, esp_event_base_t event_base,
|
||||
ESP_LOGI(TAG, "Connected to AP. Got IP: " IPSTR, IP2STR(&event->ip_info.ip));
|
||||
retry_num = 0;
|
||||
|
||||
xEventGroupSetBits(clock_core_event_group_, WIFI_CONNECTED_BIT);
|
||||
ctx->events->setEvent(clock_core::EventGroup::WIFI_CONNECTED);
|
||||
}
|
||||
}
|
||||
|
||||
void sntpEventHandler(struct timeval*)
|
||||
{
|
||||
xEventGroupSetBits(clock_core_event_group_, SNTP_SYNCED_BIT);
|
||||
Context::getContext().events->setEvent(clock_core::EventGroup::SNTP_SYNCED);
|
||||
}
|
||||
|
||||
void timerEventHandler(TimerHandle_t)
|
||||
{
|
||||
xEventGroupSetBits(clock_core_event_group_, CLOCK_UPDATE_BIT);
|
||||
Context::getContext().events->setEvent(clock_core::EventGroup::CLOCK_UPDATE);
|
||||
}
|
||||
|
||||
esp_err_t sntpInitialize()
|
||||
@ -98,12 +113,12 @@ std::expected<void, esp_err_t> wifiInitialize(const char* wifi_ssid, const char*
|
||||
TRY_ESP(esp_event_handler_instance_register(WIFI_EVENT,
|
||||
ESP_EVENT_ANY_ID,
|
||||
&wifiEventHandler,
|
||||
nullptr,
|
||||
&(Context::getContext()),
|
||||
&instance_any_id));
|
||||
TRY_ESP(esp_event_handler_instance_register(IP_EVENT,
|
||||
IP_EVENT_STA_GOT_IP,
|
||||
&wifiEventHandler,
|
||||
nullptr,
|
||||
&(Context::getContext()),
|
||||
&instance_got_ip));
|
||||
|
||||
wifi_sta_config_t sta_config;
|
||||
@ -128,7 +143,9 @@ namespace clock_core
|
||||
|
||||
void run(const char* wifi_ssid, const char* wifi_password)
|
||||
{
|
||||
clock_core_event_group_ = xEventGroupCreate();
|
||||
EventGroup events;
|
||||
Context::getContext().events = &events;
|
||||
|
||||
auto clock_timer = xTimerCreate("clock_core",
|
||||
pdMS_TO_TICKS(60'000),
|
||||
pdTRUE,
|
||||
@ -144,13 +161,9 @@ 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_BIT | CLOCK_UPDATE_BIT,
|
||||
pdTRUE,
|
||||
pdFALSE,
|
||||
portMAX_DELAY);
|
||||
const EventBits_t bits = events.waitForEvent();
|
||||
|
||||
if (bits & WIFI_CONNECTED_BIT)
|
||||
if (bits & EventGroup::WIFI_CONNECTED)
|
||||
{
|
||||
ESP_LOGI(TAG, "WiFi setup in clock_core::run successful.");
|
||||
if (const auto res = esp_netif_sntp_start();
|
||||
@ -162,15 +175,15 @@ void run(const char* wifi_ssid, const char* wifi_password)
|
||||
|
||||
xTimerStart(clock_timer, pdMS_TO_TICKS(10));
|
||||
}
|
||||
else if (bits & WIFI_FAIL_BIT)
|
||||
else if (bits & EventGroup::WIFI_FAILED)
|
||||
{
|
||||
ESP_LOGE(TAG, "WiFi setup in clock_core::run failed.");
|
||||
}
|
||||
else if (bits & SNTP_SYNCED_BIT)
|
||||
else if (bits & EventGroup::SNTP_SYNCED)
|
||||
{
|
||||
ESP_LOGI(TAG, "SNTP sync successful.");
|
||||
}
|
||||
else if (bits & CLOCK_UPDATE_BIT)
|
||||
else if (bits & EventGroup::CLOCK_UPDATE)
|
||||
{
|
||||
char time_buf[64];
|
||||
std::time_t time_now;
|
||||
|
||||
45
firmware/main/clock_core_event.hpp
Normal file
45
firmware/main/clock_core_event.hpp
Normal file
@ -0,0 +1,45 @@
|
||||
//
|
||||
// Created by erki on 11/02/24.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <freertos/FreeRTOS.h>
|
||||
|
||||
namespace clock_core
|
||||
{
|
||||
|
||||
struct EventGroup
|
||||
{
|
||||
static constexpr EventBits_t WIFI_FAILED = BIT0;
|
||||
static constexpr EventBits_t WIFI_CONNECTED = BIT1;
|
||||
static constexpr EventBits_t SNTP_SYNCED = BIT2;
|
||||
static constexpr EventBits_t CLOCK_UPDATE = BIT3;
|
||||
static constexpr EventBits_t ALL_EVENTS = WIFI_FAILED | WIFI_CONNECTED | SNTP_SYNCED | CLOCK_UPDATE;
|
||||
|
||||
EventGroupHandle_t rtos_event_group = nullptr;
|
||||
|
||||
EventGroup()
|
||||
: rtos_event_group(xEventGroupCreate())
|
||||
{ }
|
||||
EventGroup(const EventGroup&) = delete;
|
||||
EventGroup(EventGroup&&) = delete;
|
||||
EventGroup& operator=(const EventGroup&) = delete;
|
||||
EventGroup& operator=(EventGroup&&) = delete;
|
||||
|
||||
void setEvent(const EventBits_t& bits)
|
||||
{
|
||||
xEventGroupSetBits(rtos_event_group, bits);
|
||||
}
|
||||
|
||||
EventBits_t waitForEvent()
|
||||
{
|
||||
return xEventGroupWaitBits(rtos_event_group,
|
||||
ALL_EVENTS,
|
||||
pdTRUE,
|
||||
pdFALSE,
|
||||
portMAX_DELAY);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user