From 6dfdd4102f69a65deb4bd427835cbb802b305b8d Mon Sep 17 00:00:00 2001 From: deanlee Date: Sat, 6 Nov 2021 15:53:31 +0800 Subject: [PATCH] static Alert::get --- selfdrive/ui/qt/onroad.cc | 3 +- selfdrive/ui/soundd.cc | 6 ++-- selfdrive/ui/ui.h | 59 +++++++++++++++++++-------------------- 3 files changed, 31 insertions(+), 37 deletions(-) diff --git a/selfdrive/ui/qt/onroad.cc b/selfdrive/ui/qt/onroad.cc index 2f846b725b..a2a596de74 100644 --- a/selfdrive/ui/qt/onroad.cc +++ b/selfdrive/ui/qt/onroad.cc @@ -41,8 +41,7 @@ OnroadWindow::OnroadWindow(QWidget *parent) : QWidget(parent) { void OnroadWindow::updateState(const UIState &s) { QColor bgColor = bg_colors[s.status]; - auto alert = get_alert(*(s.sm), s.scene.started_frame); - if (alert) { + if (auto alert = Alert::get(*(s.sm), s.scene.started_frame)) { if (alert->type == "controlsUnresponsive") { bgColor = bg_colors[STATUS_ALERT]; } diff --git a/selfdrive/ui/soundd.cc b/selfdrive/ui/soundd.cc index 2292b6cde3..9abf42442b 100644 --- a/selfdrive/ui/soundd.cc +++ b/selfdrive/ui/soundd.cc @@ -62,12 +62,10 @@ public: } } - auto alert = get_alert(sm, 1); - if (alert) { + if (auto alert = Alert::get(sm, 1)) { setAlert(alert->type, alert->sound); } else { - // stop alert. - setAlert("", AudibleAlert::NONE); + setAlert({}, AudibleAlert::NONE); } } diff --git a/selfdrive/ui/ui.h b/selfdrive/ui/ui.h index 6693423c0e..c1ceb3f200 100644 --- a/selfdrive/ui/ui.h +++ b/selfdrive/ui/ui.h @@ -27,6 +27,10 @@ #define COLOR_YELLOW nvgRGBA(218, 202, 37, 255) #define COLOR_RED nvgRGBA(201, 34, 49, 255) +const int bdr_s = 30; +const int header_h = 420; +const int footer_h = 280; + const int UI_FREQ = 20; // Hz typedef cereal::CarControl::HUDControl::AudibleAlert AudibleAlert; @@ -47,7 +51,7 @@ typedef struct Rect { } } Rect; -typedef struct Alert { +struct Alert { QString text1; QString text2; QString type; @@ -56,38 +60,31 @@ typedef struct Alert { bool equal(const Alert &a2) { return text1 == a2.text1 && text2 == a2.text2 && type == a2.type; } -} Alert; - -const Alert CONTROLS_WAITING_ALERT = {"openpilot Unavailable", "Waiting for controls to start", - "controlsWaiting", cereal::ControlsState::AlertSize::MID, - AudibleAlert::NONE}; - -const Alert CONTROLS_UNRESPONSIVE_ALERT = {"TAKE CONTROL IMMEDIATELY", "Controls Unresponsive", - "controlsUnresponsive", cereal::ControlsState::AlertSize::FULL, - AudibleAlert::CHIME_WARNING_REPEAT}; -inline std::optional get_alert(const SubMaster &sm, uint64_t started_frame) { - const int CONTROLS_TIMEOUT = 5; - - if (sm.updated("controlsState")) { - const cereal::ControlsState::Reader &cs = sm["controlsState"].getControlsState(); - return Alert{cs.getAlertText1().cStr(), cs.getAlertText2().cStr(), cs.getAlertType().cStr(), - cs.getAlertSize(), cs.getAlertSound()}; - } else if ((sm.frame - started_frame) > 5 * UI_FREQ) { - // Handle controls timeout - if (sm.rcv_frame("controlsState") < started_frame) { - // car is started, but controlsState hasn't been seen at all - return CONTROLS_WAITING_ALERT; - } else if ((nanos_since_boot() - sm.rcv_time("controlsState")) / 1e9 > CONTROLS_TIMEOUT) { - // car is started, but controls is lagging or died - return CONTROLS_UNRESPONSIVE_ALERT; + + static std::optional get(const SubMaster &sm, uint64_t started_frame) { + if (sm.updated("controlsState")) { + const cereal::ControlsState::Reader &cs = sm["controlsState"].getControlsState(); + 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 + 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 + return Alert{"TAKE CONTROL IMMEDIATELY", "Controls Unresponsive", + "controlsUnresponsive", cereal::ControlsState::AlertSize::FULL, + AudibleAlert::CHIME_WARNING_REPEAT}; + } } + return std::nullopt; } - return std::nullopt; -} - -const int bdr_s = 30; -const int header_h = 420; -const int footer_h = 280; +}; typedef enum UIStatus { STATUS_DISENGAGED,