revert to std::optional

pull/22796/head
deanlee 2021-11-08 06:16:34 +08:00
parent 5b0088a9ab
commit a66291c51f
4 changed files with 32 additions and 24 deletions

View File

@ -41,11 +41,12 @@ OnroadWindow::OnroadWindow(QWidget *parent) : QWidget(parent) {
void OnroadWindow::updateState(const UIState &s) { void OnroadWindow::updateState(const UIState &s) {
QColor bgColor = bg_colors[s.status]; QColor bgColor = bg_colors[s.status];
Alert alert = Alert::get(*(s.sm), s.scene.started_frame); if (std::optional<Alert> alert = Alert::get(*(s.sm), s.scene.started_frame)) {
if (alert.type == "controlsUnresponsive") { if (alert->type == "controlsUnresponsive") {
bgColor = bg_colors[STATUS_ALERT]; bgColor = bg_colors[STATUS_ALERT];
}
alerts->updateAlert(*alert, bgColor);
} }
alerts->updateAlert(alert, bgColor);
if (bg != bgColor) { if (bg != bgColor) {
// repaint border // repaint border
bg = bgColor; bg = bgColor;

View File

@ -38,12 +38,18 @@ void Sound::update() {
} }
} }
setAlert(Alert::get(sm, 1)); std::optional<Alert> alert = Alert::get(sm, 1);
if (alert) {
setAlert(alert->type, alert->sound);
} else {
setAlert("", AudibleAlert::NONE);
}
} }
void Sound::setAlert(const Alert &alert) { void Sound::setAlert(const QString &alert_type, AudibleAlert sound) {
if (!current_alert.equal(alert)) { if (current_alert_type != alert_type || current_sound != sound) {
current_alert = alert; current_alert_type = alert_type;
current_sound = sound;
// stop sounds // stop sounds
for (auto &[s, loops] : sounds) { for (auto &[s, loops] : sounds) {
// Only stop repeating sounds // Only stop repeating sounds
@ -53,8 +59,8 @@ void Sound::setAlert(const Alert &alert) {
} }
// play sound // play sound
if (alert.sound != AudibleAlert::NONE) { if (sound != AudibleAlert::NONE) {
auto &[s, loops] = sounds[alert.sound]; auto &[s, loops] = sounds[sound];
s->setLoopCount(loops); s->setLoopCount(loops);
s->play(); s->play();
} }

View File

@ -22,9 +22,10 @@ public:
protected: protected:
void update(); 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; float current_volume = Hardware::MIN_VOLUME;
QMap<AudibleAlert, QPair<QSoundEffect *, int>> sounds; QMap<AudibleAlert, QPair<QSoundEffect *, int>> sounds;
SubMaster sm; SubMaster sm;

View File

@ -2,6 +2,7 @@
#include <map> #include <map>
#include <memory> #include <memory>
#include <optional>
#include <string> #include <string>
#include <QObject> #include <QObject>
@ -60,29 +61,28 @@ struct Alert {
return text1 == a2.text1 && text2 == a2.text2 && type == a2.type; return text1 == a2.text1 && text2 == a2.text2 && type == a2.type;
} }
static Alert get(const SubMaster &sm, uint64_t started_frame) { static std::optional<Alert> get(const SubMaster &sm, uint64_t started_frame) {
Alert alert{};
if (sm.updated("controlsState")) { if (sm.updated("controlsState")) {
const cereal::ControlsState::Reader &cs = sm["controlsState"].getControlsState(); const cereal::ControlsState::Reader &cs = sm["controlsState"].getControlsState();
alert = {cs.getAlertText1().cStr(), cs.getAlertText2().cStr(), return Alert{cs.getAlertText1().cStr(), cs.getAlertText2().cStr(),
cs.getAlertType().cStr(), cs.getAlertSize(), cs.getAlertType().cStr(), cs.getAlertSize(),
cs.getAlertSound()}; cs.getAlertSound()};
} else if ((sm.frame - started_frame) > 5 * UI_FREQ) { } else if ((sm.frame - started_frame) > 5 * UI_FREQ) {
const int CONTROLS_TIMEOUT = 5; const int CONTROLS_TIMEOUT = 5;
// Handle controls timeout // Handle controls timeout
if (sm.rcv_frame("controlsState") < started_frame) { if (sm.rcv_frame("controlsState") < started_frame) {
// car is started, but controlsState hasn't been seen at all // car is started, but controlsState hasn't been seen at all
alert = {"openpilot Unavailable", "Waiting for controls to start", return Alert{"openpilot Unavailable", "Waiting for controls to start",
"controlsWaiting", cereal::ControlsState::AlertSize::MID, "controlsWaiting", cereal::ControlsState::AlertSize::MID,
AudibleAlert::NONE}; AudibleAlert::NONE};
} else if ((nanos_since_boot() - sm.rcv_time("controlsState")) / 1e9 > CONTROLS_TIMEOUT) { } else if ((nanos_since_boot() - sm.rcv_time("controlsState")) / 1e9 > CONTROLS_TIMEOUT) {
// car is started, but controls is lagging or died // car is started, but controls is lagging or died
alert = {"TAKE CONTROL IMMEDIATELY", "Controls Unresponsive", return Alert{"TAKE CONTROL IMMEDIATELY", "Controls Unresponsive",
"controlsUnresponsive", cereal::ControlsState::AlertSize::FULL, "controlsUnresponsive", cereal::ControlsState::AlertSize::FULL,
AudibleAlert::CHIME_WARNING_REPEAT}; AudibleAlert::CHIME_WARNING_REPEAT};
} }
} }
return alert; return std::nullopt;
} }
}; };