From 3fd007daf6f23b8fec7fc14f6a1147c5c632f0ee Mon Sep 17 00:00:00 2001 From: deanlee Date: Mon, 8 Nov 2021 03:48:34 +0800 Subject: [PATCH] return Alert --- selfdrive/ui/qt/onroad.cc | 9 ++++----- selfdrive/ui/soundd/sound.cc | 7 ++----- selfdrive/ui/ui.h | 11 ++++++----- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/selfdrive/ui/qt/onroad.cc b/selfdrive/ui/qt/onroad.cc index a2a596de74..aa133b948f 100644 --- a/selfdrive/ui/qt/onroad.cc +++ b/selfdrive/ui/qt/onroad.cc @@ -41,12 +41,11 @@ OnroadWindow::OnroadWindow(QWidget *parent) : QWidget(parent) { void OnroadWindow::updateState(const UIState &s) { QColor bgColor = bg_colors[s.status]; - if (auto alert = Alert::get(*(s.sm), s.scene.started_frame)) { - if (alert->type == "controlsUnresponsive") { - bgColor = bg_colors[STATUS_ALERT]; - } - alerts->updateAlert(*alert, bgColor); + auto alert = Alert::get(*(s.sm), s.scene.started_frame); + if (alert.type == "controlsUnresponsive") { + bgColor = bg_colors[STATUS_ALERT]; } + 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 499e8a1170..cdade7d800 100644 --- a/selfdrive/ui/soundd/sound.cc +++ b/selfdrive/ui/soundd/sound.cc @@ -38,11 +38,8 @@ void Sound::update() { } } - if (auto alert = Alert::get(sm, 1)) { - setAlert(alert->type, alert->sound); - } else { - setAlert({}, AudibleAlert::NONE); - } + auto alert = Alert::get(sm, 1); + setAlert(alert.type, alert.sound); } void Sound::setAlert(const QString &alert_type, AudibleAlert sound) { diff --git a/selfdrive/ui/ui.h b/selfdrive/ui/ui.h index c1ceb3f200..0670c2d3ed 100644 --- a/selfdrive/ui/ui.h +++ b/selfdrive/ui/ui.h @@ -61,10 +61,11 @@ struct Alert { return text1 == a2.text1 && text2 == a2.text2 && type == a2.type; } - static std::optional get(const SubMaster &sm, uint64_t started_frame) { + static Alert get(const SubMaster &sm, uint64_t started_frame) { + Alert alert{}; if (sm.updated("controlsState")) { const cereal::ControlsState::Reader &cs = sm["controlsState"].getControlsState(); - return Alert{cs.getAlertText1().cStr(), cs.getAlertText2().cStr(), + alert = {cs.getAlertText1().cStr(), cs.getAlertText2().cStr(), cs.getAlertType().cStr(), cs.getAlertSize(), cs.getAlertSound()}; } else if ((sm.frame - started_frame) > 5 * UI_FREQ) { @@ -72,17 +73,17 @@ struct Alert { // Handle controls timeout if (sm.rcv_frame("controlsState") < started_frame) { // car is started, but controlsState hasn't been seen at all - return Alert{"openpilot Unavailable", "Waiting for controls to start", + 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 - return Alert{"TAKE CONTROL IMMEDIATELY", "Controls Unresponsive", + alert = {"TAKE CONTROL IMMEDIATELY", "Controls Unresponsive", "controlsUnresponsive", cereal::ControlsState::AlertSize::FULL, AudibleAlert::CHIME_WARNING_REPEAT}; } } - return std::nullopt; + return alert; } };