From c9db9b1fb4a62b1b1a68e42e32b7e17127894847 Mon Sep 17 00:00:00 2001 From: Dean Lee Date: Thu, 24 Dec 2020 05:53:39 +0800 Subject: [PATCH] improved handle_display_state (#19574) old-commit-hash: e032c310695f935b46f37b4bc6f5777c4de05586 --- selfdrive/ui/android/ui.cc | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/selfdrive/ui/android/ui.cc b/selfdrive/ui/android/ui.cc index 7017bd2835..3078466415 100644 --- a/selfdrive/ui/android/ui.cc +++ b/selfdrive/ui/android/ui.cc @@ -30,27 +30,27 @@ static void ui_set_brightness(UIState *s, int brightness) { } static void handle_display_state(UIState *s, bool user_input) { - static int awake_timeout = 0; - awake_timeout = std::max(awake_timeout-1, 0); - - // tap detection while display is off - const float accel_samples = 5*UI_FREQ; - static float accel_prev, gyro_prev = 0; - bool accel_trigger = abs(s->accel_sensor - accel_prev) > 0.2; - bool gyro_trigger = abs(s->gyro_sensor - gyro_prev) > 0.15; - user_input = user_input || (accel_trigger && gyro_trigger); - gyro_prev = s->gyro_sensor; - accel_prev = (accel_prev*(accel_samples - 1) + s->accel_sensor) / accel_samples; + constexpr float accel_samples = 5*UI_FREQ; + static float accel_prev = 0., gyro_prev = 0.; + + bool should_wake = s->started || s->ignition || user_input; + if (!should_wake) { + // tap detection while display is off + bool accel_trigger = abs(s->accel_sensor - accel_prev) > 0.2; + bool gyro_trigger = abs(s->gyro_sensor - gyro_prev) > 0.15; + should_wake = accel_trigger && gyro_trigger; + gyro_prev = s->gyro_sensor; + accel_prev = (accel_prev * (accel_samples - 1) + s->accel_sensor) / accel_samples; + } // determine desired state - bool should_wake = s->awake; - if (user_input || s->ignition || s->started) { - should_wake = true; + if (should_wake) { awake_timeout = 30*UI_FREQ; - } else if (awake_timeout == 0){ - should_wake = false; + } else if (awake_timeout > 0){ + --awake_timeout; + should_wake = true; } // handle state transition