From c5dbee4e58aad2efca6d91baa4c7c4171ee8953f Mon Sep 17 00:00:00 2001 From: Dean Lee Date: Tue, 14 Dec 2021 18:22:16 +0800 Subject: [PATCH] UI: add timeout to close settings window while onroad (#22980) * refactor updateWakefulness * don't hide sidebar * rename reset->reset_timeout * move related variables and functions into class Device * reset timeout in ctor * set timeout to 10s if ignition is on * cleanup * remove unused QTimer *timer * check getTimestamp * keep socket reading in update_state * keep filtering Co-authored-by: Willem Melching --- selfdrive/ui/qt/window.cc | 12 +++++------ selfdrive/ui/ui.cc | 42 +++++++++++++++++++++++++-------------- selfdrive/ui/ui.h | 12 +++++------ 3 files changed, 38 insertions(+), 28 deletions(-) diff --git a/selfdrive/ui/qt/window.cc b/selfdrive/ui/qt/window.cc index 7fe87ce698..da83bb97e4 100644 --- a/selfdrive/ui/qt/window.cc +++ b/selfdrive/ui/qt/window.cc @@ -37,17 +37,16 @@ MainWindow::MainWindow(QWidget *parent) : QWidget(parent) { main_layout->setCurrentWidget(onboardingWindow); } - device.setAwake(true, true); QObject::connect(&qs, &QUIState::uiUpdate, &device, &Device::update); QObject::connect(&qs, &QUIState::offroadTransition, [=](bool offroad) { if (!offroad) { closeSettings(); } }); - QObject::connect(&device, &Device::displayPowerChanged, [=]() { - if(main_layout->currentWidget() != onboardingWindow) { - closeSettings(); - } + QObject::connect(&device, &Device::interactiveTimout, [=]() { + if (main_layout->currentWidget() == settingsWindow) { + closeSettings(); + } }); // load fonts @@ -86,9 +85,8 @@ void MainWindow::closeSettings() { } bool MainWindow::eventFilter(QObject *obj, QEvent *event) { - // wake screen on tap if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::TouchBegin) { - device.setAwake(true, true); + device.resetInteractiveTimout(); } #ifdef QCOM diff --git a/selfdrive/ui/ui.cc b/selfdrive/ui/ui.cc index 38c08eb484..3dd044653b 100644 --- a/selfdrive/ui/ui.cc +++ b/selfdrive/ui/ui.cc @@ -251,6 +251,8 @@ void QUIState::update() { } Device::Device(QObject *parent) : brightness_filter(BACKLIGHT_OFFROAD, BACKLIGHT_TS, BACKLIGHT_DT), QObject(parent) { + setAwake(true); + resetInteractiveTimout(); } void Device::update(const UIState &s) { @@ -261,17 +263,17 @@ void Device::update(const UIState &s) { QUIState::ui_state.awake = awake; } -void Device::setAwake(bool on, bool reset) { +void Device::setAwake(bool on) { if (on != awake) { awake = on; Hardware::set_display_power(awake); LOGD("setting display power %d", awake); emit displayPowerChanged(awake); } +} - if (reset) { - awake_timeout = 30 * UI_FREQ; - } +void Device::resetInteractiveTimout() { + interactive_timeout = (ignition_on ? 10 : 30) * UI_FREQ; } void Device::updateBrightness(const UIState &s) { @@ -302,18 +304,28 @@ void Device::updateBrightness(const UIState &s) { last_brightness = brightness; } +bool Device::motionTriggered(const UIState &s) { + static float accel_prev = 0; + static float gyro_prev = 0; + + bool accel_trigger = abs(s.scene.accel_sensor - accel_prev) > 0.2; + bool gyro_trigger = abs(s.scene.gyro_sensor - gyro_prev) > 0.15; + + gyro_prev = s.scene.gyro_sensor; + accel_prev = (accel_prev * (accel_samples - 1) + s.scene.accel_sensor) / accel_samples; + + return (!awake && accel_trigger && gyro_trigger); +} + void Device::updateWakefulness(const UIState &s) { - awake_timeout = std::max(awake_timeout - 1, 0); - - bool should_wake = s.scene.started || s.scene.ignition; - if (!should_wake) { - // tap detection while display is off - bool accel_trigger = abs(s.scene.accel_sensor - accel_prev) > 0.2; - bool gyro_trigger = abs(s.scene.gyro_sensor - gyro_prev) > 0.15; - should_wake = accel_trigger && gyro_trigger; - gyro_prev = s.scene.gyro_sensor; - accel_prev = (accel_prev * (accel_samples - 1) + s.scene.accel_sensor) / accel_samples; + bool ignition_just_turned_off = !s.scene.ignition && ignition_on; + ignition_on = s.scene.ignition; + + if (ignition_just_turned_off || motionTriggered(s)) { + resetInteractiveTimout(); + } else if (interactive_timeout > 0 && --interactive_timeout == 0) { + emit interactiveTimout(); } - setAwake(awake_timeout, should_wake); + setAwake(s.scene.ignition || interactive_timeout > 0); } diff --git a/selfdrive/ui/ui.h b/selfdrive/ui/ui.h index 4b4ef0bb28..924fb8194c 100644 --- a/selfdrive/ui/ui.h +++ b/selfdrive/ui/ui.h @@ -152,22 +152,22 @@ private: const float accel_samples = 5*UI_FREQ; bool awake = false; - int awake_timeout = 0; - float accel_prev = 0; - float gyro_prev = 0; + int interactive_timeout = 0; + bool ignition_on = false; int last_brightness = 0; FirstOrderFilter brightness_filter; - QTimer *timer; - void updateBrightness(const UIState &s); void updateWakefulness(const UIState &s); + bool motionTriggered(const UIState &s); + void setAwake(bool on); signals: void displayPowerChanged(bool on); + void interactiveTimout(); public slots: - void setAwake(bool on, bool reset); + void resetInteractiveTimout(); void update(const UIState &s); };