Fix Toast timing and alert once per shown Toast

- ToastQueue compared times unsigned: a Toast stamped a few ms after the
  main loop read the clock looked expired, so Toasts were skipped or
  shown late and bursts chained wrongly
- Notifier now beeps and flashes when each Toast appears instead of when
  the Notification arrives, so a burst alerts once per Toast

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EhqxQ49eCju4CzKYNjZzwT
This commit is contained in:
2026-10-02 11:09:46 +02:00
co-authored by Claude Opus 5.5
parent 879cb0d0b6
commit a053b1a5de
5 changed files with 52 additions and 12 deletions
+10 -7
View File
@@ -9,14 +9,12 @@ constexpr int kLedPin = 21; // WS2812, powered with the backlight rail (G38)
}
Notifier::Notifier(EventBus& bus, const Settings& settings) : settings_(settings) {
bus.subscribe(EventType::Notification, [this](const Event& e) { present(e); });
bus.subscribe(EventType::Notification, [this](const Event& e) {
toasts_.push(e.text, static_cast<NotificationLevel>(e.a), millis());
});
}
void Notifier::present(const Event& e) {
auto level = static_cast<NotificationLevel>(e.a);
uint32_t now = millis();
toasts_.push(e.text, level, now);
void Notifier::alert(NotificationLevel level, uint32_t nowMs) {
if (!settings_.getBool(Setting::Sound)) return;
M5Cardputer.Speaker.tone(level == NotificationLevel::Warning ? 880 : 1760, 60);
switch (level) {
@@ -25,10 +23,15 @@ void Notifier::present(const Event& e) {
default: rgbLedWrite(kLedPin, 0, 16, 40); break;
}
ledOn_ = true;
ledOffAtMs_ = now + 150;
ledOffAtMs_ = nowMs + 150;
}
void Notifier::update(uint32_t nowMs) {
const Toast* shown = toasts_.current(nowMs);
if (shown && shown->id != alertedId_) {
alertedId_ = shown->id;
alert(shown->level, nowMs);
}
if (ledOn_ && static_cast<int32_t>(nowMs - ledOffAtMs_) >= 0) {
rgbLedWrite(kLedPin, 0, 0, 0);
ledOn_ = false;
+5 -3
View File
@@ -6,20 +6,22 @@
namespace roro {
// Presents Notifications: a Toast, plus a short beep and LED flash (when Sound is on).
// Presents Notifications as Toasts. Each Toast gets a short beep and LED flash (when Sound is on)
// as it appears, so a burst of Notifications alerts once per Toast, not all at once.
class Notifier {
public:
Notifier(EventBus& bus, const Settings& settings);
void update(uint32_t nowMs); // turns the LED off after the flash
void update(uint32_t nowMs); // alerts for a newly shown Toast, ends the LED flash
ToastQueue& toasts() { return toasts_; }
private:
void present(const Event& e);
void alert(NotificationLevel level, uint32_t nowMs);
const Settings& settings_;
ToastQueue toasts_{3000};
uint32_t ledOffAtMs_ = 0;
bool ledOn_ = false;
uint32_t alertedId_ = 0;
};
} // namespace roro