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) {
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 = 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;

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) {
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();
}

View File

@ -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<AudibleAlert, QPair<QSoundEffect *, int>> sounds;
SubMaster sm;

View File

@ -2,6 +2,7 @@
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <QObject>
@ -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<Alert> 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;
}
};