58 lines
1.5 KiB
C++
58 lines
1.5 KiB
C++
#pragma once
|
|
|
|
#include "ports/alert_ringer.h"
|
|
#include "ports/audio_engine.h"
|
|
#include "ports/event_publisher.h"
|
|
#include "schedule/alarm_service.h"
|
|
|
|
#include <array>
|
|
#include <atomic>
|
|
#include <cstdint>
|
|
|
|
#include <freertos/FreeRTOS.h>
|
|
#include <freertos/semphr.h>
|
|
#include <freertos/task.h>
|
|
|
|
namespace borla {
|
|
|
|
class AlertRinger final : public IAlertRinger {
|
|
public:
|
|
AlertRinger(
|
|
IAudioEngine& audio,
|
|
AlarmService& alarms)
|
|
: audio_(audio), alarms_(alarms)
|
|
{
|
|
}
|
|
|
|
void SetEventPublisher(IEventPublisher& publisher)
|
|
{
|
|
publisher_ = &publisher;
|
|
}
|
|
void Start(AlertKind kind) override;
|
|
void Stop() override;
|
|
|
|
private:
|
|
static constexpr uint32_t kTaskStackDepth = 3072;
|
|
static void TaskEntry(void* context);
|
|
// The worker is created once and then parks on request_. A static task
|
|
// control block and stack must not be recycled while the previous task is
|
|
// still being torn down, so this worker is never deleted.
|
|
bool EnsureWorker();
|
|
[[noreturn]] void Run();
|
|
void RunOneAlert(uint32_t generation);
|
|
|
|
IAudioEngine& audio_;
|
|
AlarmService& alarms_;
|
|
IEventPublisher* publisher_ = nullptr;
|
|
std::atomic<bool> running_{false};
|
|
std::atomic<uint32_t> generation_{0};
|
|
std::atomic<AlertKind> kind_{AlertKind::kReminder};
|
|
SemaphoreHandle_t request_ = nullptr;
|
|
StaticSemaphore_t request_storage_{};
|
|
TaskHandle_t task_ = nullptr;
|
|
StaticTask_t task_control_{};
|
|
std::array<StackType_t, kTaskStackDepth> task_stack_{};
|
|
};
|
|
|
|
} // namespace borla
|