bolar_2 backup
This commit is contained in:
117
main/schedule/alert_ringer.cc
Normal file
117
main/schedule/alert_ringer.cc
Normal file
@@ -0,0 +1,117 @@
|
||||
#include "schedule/alert_ringer.h"
|
||||
|
||||
#include <esp_log.h>
|
||||
#include <esp_timer.h>
|
||||
|
||||
namespace borla {
|
||||
namespace {
|
||||
|
||||
constexpr const char* kTag = "AlertRinger2";
|
||||
constexpr uint32_t kMaximumRingMs = 60000;
|
||||
constexpr uint32_t kRingPeriodMs = 2500;
|
||||
|
||||
uint64_t NowMs()
|
||||
{
|
||||
return static_cast<uint64_t>(esp_timer_get_time() / 1000);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void AlertRinger::Start(AlertKind kind)
|
||||
{
|
||||
kind_.store(kind);
|
||||
if (!EnsureWorker()) {
|
||||
if (publisher_ != nullptr) {
|
||||
publisher_->Publish(AppEvent::Make(
|
||||
AppEventType::kAlertFinished));
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (running_.exchange(true)) {
|
||||
return;
|
||||
}
|
||||
generation_.fetch_add(1);
|
||||
xSemaphoreGive(request_);
|
||||
}
|
||||
|
||||
void AlertRinger::Stop()
|
||||
{
|
||||
running_.store(false);
|
||||
audio_.CancelPlayback(0);
|
||||
}
|
||||
|
||||
bool AlertRinger::EnsureWorker()
|
||||
{
|
||||
if (task_ != nullptr) {
|
||||
return true;
|
||||
}
|
||||
if (request_ == nullptr) {
|
||||
request_ = xSemaphoreCreateBinaryStatic(&request_storage_);
|
||||
if (request_ == nullptr) {
|
||||
ESP_LOGE(kTag, "alert request semaphore creation failed");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
task_ = xTaskCreateStatic(
|
||||
&AlertRinger::TaskEntry,
|
||||
"alert_ringer2",
|
||||
task_stack_.size(),
|
||||
this,
|
||||
4,
|
||||
task_stack_.data(),
|
||||
&task_control_);
|
||||
if (task_ == nullptr) {
|
||||
ESP_LOGE(kTag, "alert ringer worker creation failed");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void AlertRinger::TaskEntry(void* context)
|
||||
{
|
||||
static_cast<AlertRinger*>(context)->Run();
|
||||
}
|
||||
|
||||
[[noreturn]] void AlertRinger::Run()
|
||||
{
|
||||
for (;;) {
|
||||
if (xSemaphoreTake(request_, portMAX_DELAY) != pdTRUE) {
|
||||
continue;
|
||||
}
|
||||
RunOneAlert(generation_.load());
|
||||
}
|
||||
}
|
||||
|
||||
void AlertRinger::RunOneAlert(uint32_t generation)
|
||||
{
|
||||
const uint64_t deadline = NowMs() + kMaximumRingMs;
|
||||
while (running_.load() &&
|
||||
generation_.load() == generation &&
|
||||
NowMs() < deadline) {
|
||||
audio_.PlayLocalPrompt(
|
||||
LocalPrompt::kAlarmWake,
|
||||
PlaybackPriority::kAlarm,
|
||||
alarms_.wake_tone_index());
|
||||
for (uint32_t waited = 0;
|
||||
running_.load() &&
|
||||
generation_.load() == generation &&
|
||||
waited < kRingPeriodMs;
|
||||
waited += 50) {
|
||||
vTaskDelay(pdMS_TO_TICKS(50));
|
||||
}
|
||||
}
|
||||
if (generation_.load() != generation) {
|
||||
// A newer Start() already owns the ringing state. Its pending request
|
||||
// token drives the next cycle, so this one must not finish it.
|
||||
return;
|
||||
}
|
||||
if (running_.exchange(false)) {
|
||||
ESP_LOGI(kTag, "alert ringing timed out");
|
||||
}
|
||||
if (publisher_ != nullptr) {
|
||||
publisher_->Publish(AppEvent::Make(
|
||||
AppEventType::kAlertFinished));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace borla
|
||||
Reference in New Issue
Block a user