Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI: add timeout to close settings window while onroad #22980

Merged
merged 11 commits into from
Dec 14, 2021
12 changes: 5 additions & 7 deletions selfdrive/ui/qt/window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
42 changes: 27 additions & 15 deletions selfdrive/ui/ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}
12 changes: 6 additions & 6 deletions selfdrive/ui/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,22 +152,22 @@ class Device : public QObject {
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);
};

Expand Down