From a66291c51f3a846652837cc4a43d5d60dd81896b Mon Sep 17 00:00:00 2001 From: deanlee Date: Mon, 8 Nov 2021 06:16:34 +0800 Subject: [PATCH] revert to std::optional --- selfdrive/ui/qt/onroad.cc | 9 +++++---- selfdrive/ui/soundd/sound.cc | 18 ++++++++++++------ selfdrive/ui/soundd/sound.h | 5 +++-- selfdrive/ui/ui.h | 24 ++++++++++++------------ 4 files changed, 32 insertions(+), 24 deletions(-) diff --git a/selfdrive/ui/qt/onroad.cc b/selfdrive/ui/qt/onroad.cc index a2195a621..861bff1d2 100644 --- a/selfdrive/ui/qt/onroad.cc +++ b/selfdrive/ui/qt/onroad.cc @@ -41,11 +41,12 @@ OnroadWindow::OnroadWindow(QWidget *parent) : QWidget(parent) { void OnroadWindow::updateState(const UIState &s) { QColor bgColor = bg_colors[s.status]; - Alert alert = Alert::get(*(s.sm), s.scene.started_frame); - if (alert.type == "controlsUnresponsive") { - bgColor = bg_colors[STATUS_ALERT]; + if (std::optional alert = Alert::get(*(s.sm), s.scene.started_frame)) { + if (alert->type == "controlsUnresponsive") { + bgColor = bg_colors[STATUS_ALERT]; + } + alerts->updateAlert(*alert, bgColor); } - alerts->updateAlert(alert, bgColor); if (bg != bgColor) { // repaint border bg = bgColor; diff --git a/selfdrive/ui/soundd/sound.cc b/selfdrive/ui/soundd/sound.cc index 35cdbdc3b..378643e63 100644 --- a/selfdrive/ui/soundd/sound.cc +++ b/selfdrive/ui/soundd/sound.cc @@ -38,12 +38,18 @@ void Sound::update() { } } - setAlert(Alert::get(sm, 1)); + std::optional alert = Alert::get(sm, 1); + if (alert) { + setAlert(alert->type, alert->sound); + } else { + setAlert("", AudibleAlert::NONE); + } } -void Sound::setAlert(const Alert &alert) { - if (!current_alert.equal(alert)) { - current_alert = alert; +void Sound::setAlert(const QString &alert_type, AudibleAlert sound) { + if (current_alert_type != alert_type || current_sound != sound) { + current_alert_type = alert_type; + current_sound = sound; // stop sounds for (auto &[s, loops] : sounds) { // Only stop repeating sounds @@ -53,8 +59,8 @@ void Sound::setAlert(const Alert &alert) { } // play sound - if (alert.sound != AudibleAlert::NONE) { - auto &[s, loops] = sounds[alert.sound]; + if (sound != AudibleAlert::NONE) { + auto &[s, loops] = sounds[sound]; s->setLoopCount(loops); s->play(); } diff --git a/selfdrive/ui/soundd/sound.h b/selfdrive/ui/soundd/sound.h index 041c600b5..daff952c9 100644 --- a/selfdrive/ui/soundd/sound.h +++ b/selfdrive/ui/soundd/sound.h @@ -22,9 +22,10 @@ public: protected: void update(); - void setAlert(const Alert &alert); + void setAlert(const QString &alert_type, AudibleAlert sound); - Alert current_alert = {}; + AudibleAlert current_sound = AudibleAlert::NONE; + QString current_alert_type; float current_volume = Hardware::MIN_VOLUME; QMap> sounds; SubMaster sm; diff --git a/selfdrive/ui/ui.h b/selfdrive/ui/ui.h index ac946faa6..483268003 100644 --- a/selfdrive/ui/ui.h +++ b/selfdrive/ui/ui.h @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -60,29 +61,28 @@ struct Alert { return text1 == a2.text1 && text2 == a2.text2 && type == a2.type; } - static Alert get(const SubMaster &sm, uint64_t started_frame) { - Alert alert{}; + static std::optional get(const SubMaster &sm, uint64_t started_frame) { if (sm.updated("controlsState")) { const cereal::ControlsState::Reader &cs = sm["controlsState"].getControlsState(); - alert = {cs.getAlertText1().cStr(), cs.getAlertText2().cStr(), - cs.getAlertType().cStr(), cs.getAlertSize(), - cs.getAlertSound()}; + return Alert{cs.getAlertText1().cStr(), cs.getAlertText2().cStr(), + cs.getAlertType().cStr(), cs.getAlertSize(), + cs.getAlertSound()}; } else if ((sm.frame - started_frame) > 5 * UI_FREQ) { const int CONTROLS_TIMEOUT = 5; // Handle controls timeout if (sm.rcv_frame("controlsState") < started_frame) { // car is started, but controlsState hasn't been seen at all - alert = {"openpilot Unavailable", "Waiting for controls to start", - "controlsWaiting", cereal::ControlsState::AlertSize::MID, - AudibleAlert::NONE}; + return Alert{"openpilot Unavailable", "Waiting for controls to start", + "controlsWaiting", cereal::ControlsState::AlertSize::MID, + AudibleAlert::NONE}; } else if ((nanos_since_boot() - sm.rcv_time("controlsState")) / 1e9 > CONTROLS_TIMEOUT) { // car is started, but controls is lagging or died - alert = {"TAKE CONTROL IMMEDIATELY", "Controls Unresponsive", - "controlsUnresponsive", cereal::ControlsState::AlertSize::FULL, - AudibleAlert::CHIME_WARNING_REPEAT}; + return Alert{"TAKE CONTROL IMMEDIATELY", "Controls Unresponsive", + "controlsUnresponsive", cereal::ControlsState::AlertSize::FULL, + AudibleAlert::CHIME_WARNING_REPEAT}; } } - return alert; + return std::nullopt; } };