From 065fcf7a35ad3969bf36173e8724871ab7b99a0e Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Mon, 22 Mar 2021 17:34:47 -0700 Subject: [PATCH 1/6] Qt UI: scale volume with speed --- selfdrive/common/util.h | 22 ++++++++++++++++++---- selfdrive/ui/qt/home.cc | 10 ++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/selfdrive/common/util.h b/selfdrive/common/util.h index 3da88bb7675d0c..b75c2f4343c5a9 100644 --- a/selfdrive/common/util.h +++ b/selfdrive/common/util.h @@ -2,8 +2,11 @@ #include #include +#include #include -#include +#include +#include + #include #include #include @@ -11,9 +14,7 @@ #include #include #include -#include -#include -#include +#include #ifndef sighandler_t typedef void (*sighandler_t)(int sig); @@ -38,6 +39,19 @@ int set_core_affinity(int core); namespace util { +// ***** math helpers ***** + +// map x from [a1, a2] to [b1, b2] +template +T map_val(T x, T a1, T a2, T b1, T b2) { + x = std::clamp(x, a1, a2); + T ra = a2 - a1; + T rb = b2 - b1; + return (x - a1)*rb / ra + b1; +} + +// ***** string helpers ***** + inline bool starts_with(const std::string &s, const std::string &prefix) { return s.compare(0, prefix.size(), prefix) == 0; } diff --git a/selfdrive/ui/qt/home.cc b/selfdrive/ui/qt/home.cc index acb333c5ab686c..0de79f4b0c306f 100644 --- a/selfdrive/ui/qt/home.cc +++ b/selfdrive/ui/qt/home.cc @@ -278,6 +278,16 @@ void GLWindow::timerUpdate() { handle_display_state(&ui_state, false); + // scale volume with speed +#ifdef QCOM + const float vol_max = 1.0; + const float vol_min = 0.5; +#else + const float vol_max = 0.45; + const float vol_min = 0.45; +#endif + util::map_val(ui_state.scene.car_state.getVEgo(), 0.f, 20.f, vol_min, vol_max); + ui_update(&ui_state); repaint(); watchdog_kick(); From 21cae2c939a4ba91b0bbadb5bdad041b99552052 Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Mon, 22 Mar 2021 17:42:44 -0700 Subject: [PATCH 2/6] for all hw --- selfdrive/hardware/hw.h | 9 +++++++++ selfdrive/ui/qt/home.cc | 9 +-------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/selfdrive/hardware/hw.h b/selfdrive/hardware/hw.h index 8115aa946cfd83..64380891599708 100644 --- a/selfdrive/hardware/hw.h +++ b/selfdrive/hardware/hw.h @@ -17,6 +17,9 @@ // no-op base hw class class HardwareNone { public: + static constexpr float MAX_VOLUME = 0; + static constexpr float MIN_VOLUME = 0; + static std::string get_os_version() { return "openpilot for PC"; }; static void reboot() {}; @@ -29,6 +32,9 @@ class HardwareNone { class HardwareEon : public HardwareNone { public: + static constexpr float MAX_VOLUME = 1.0; + static constexpr float MIN_VOLUME = 0.5; + static std::string get_os_version() { return "NEOS " + util::read_file("/VERSION"); }; @@ -54,6 +60,9 @@ class HardwareEon : public HardwareNone { class HardwareTici : public HardwareNone { public: + static constexpr float MAX_VOLUME = 1.0; + static constexpr float MIN_VOLUME = 0.5; + static std::string get_os_version() { return "AGNOS " + util::read_file("/VERSION"); }; diff --git a/selfdrive/ui/qt/home.cc b/selfdrive/ui/qt/home.cc index 0de79f4b0c306f..f4c8abe5fab829 100644 --- a/selfdrive/ui/qt/home.cc +++ b/selfdrive/ui/qt/home.cc @@ -279,14 +279,7 @@ void GLWindow::timerUpdate() { handle_display_state(&ui_state, false); // scale volume with speed -#ifdef QCOM - const float vol_max = 1.0; - const float vol_min = 0.5; -#else - const float vol_max = 0.45; - const float vol_min = 0.45; -#endif - util::map_val(ui_state.scene.car_state.getVEgo(), 0.f, 20.f, vol_min, vol_max); + util::map_val(ui_state.scene.car_state.getVEgo(), 0.f, 20.f, Hardware::MIN_VOLUME, Hardware::MAX_VOLUME); ui_update(&ui_state); repaint(); From 625dafca03c31f0b514a914a97a1f82425631b80 Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Mon, 22 Mar 2021 17:43:59 -0700 Subject: [PATCH 3/6] way too loud for tici --- selfdrive/hardware/hw.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/selfdrive/hardware/hw.h b/selfdrive/hardware/hw.h index 64380891599708..0bf756d490a4ee 100644 --- a/selfdrive/hardware/hw.h +++ b/selfdrive/hardware/hw.h @@ -60,7 +60,7 @@ class HardwareEon : public HardwareNone { class HardwareTici : public HardwareNone { public: - static constexpr float MAX_VOLUME = 1.0; + static constexpr float MAX_VOLUME = 0.4; static constexpr float MIN_VOLUME = 0.5; static std::string get_os_version() { From e180d3878988b21b0252baee3aac0e0db1562f4c Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Mon, 22 Mar 2021 17:46:06 -0700 Subject: [PATCH 4/6] set volume --- selfdrive/ui/qt/home.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/selfdrive/ui/qt/home.cc b/selfdrive/ui/qt/home.cc index f4c8abe5fab829..790abe26cfd04c 100644 --- a/selfdrive/ui/qt/home.cc +++ b/selfdrive/ui/qt/home.cc @@ -279,7 +279,8 @@ void GLWindow::timerUpdate() { handle_display_state(&ui_state, false); // scale volume with speed - util::map_val(ui_state.scene.car_state.getVEgo(), 0.f, 20.f, Hardware::MIN_VOLUME, Hardware::MAX_VOLUME); + sound.volume = util::map_val(ui_state.scene.car_state.getVEgo(), 0.f, 20.f, + Hardware::MIN_VOLUME, Hardware::MAX_VOLUME); ui_update(&ui_state); repaint(); From ae2d5fad5ddd5d29a1a681c9203fe72153827e4c Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Mon, 22 Mar 2021 17:51:01 -0700 Subject: [PATCH 5/6] volume --- selfdrive/ui/qt/qt_sound.cc | 2 +- selfdrive/ui/qt/qt_sound.hpp | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/selfdrive/ui/qt/qt_sound.cc b/selfdrive/ui/qt/qt_sound.cc index 9d1b141950ca11..ee0478574837cd 100644 --- a/selfdrive/ui/qt/qt_sound.cc +++ b/selfdrive/ui/qt/qt_sound.cc @@ -11,7 +11,7 @@ QtSound::QtSound() { bool QtSound::play(AudibleAlert alert) { int loops = sound_map[alert].second> - 1 ? sound_map[alert].second : QSoundEffect::Infinite; sounds[alert].setLoopCount(loops); - sounds[alert].setVolume(0.45); + sounds[alert].setVolume(volume); sounds[alert].play(); return true; } diff --git a/selfdrive/ui/qt/qt_sound.hpp b/selfdrive/ui/qt/qt_sound.hpp index 8c7f8aa348e963..da84c23be10b82 100644 --- a/selfdrive/ui/qt/qt_sound.hpp +++ b/selfdrive/ui/qt/qt_sound.hpp @@ -9,6 +9,7 @@ class QtSound : public Sound { bool play(AudibleAlert alert); void stop(); void setVolume(int volume); + float volume = 0; private: std::map sounds; From 13d3a4a1475e75be989214b59ffd60576e396f93 Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Mon, 22 Mar 2021 19:13:33 -0700 Subject: [PATCH 6/6] swap --- selfdrive/hardware/hw.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/selfdrive/hardware/hw.h b/selfdrive/hardware/hw.h index 0bf756d490a4ee..c4086c9dde9231 100644 --- a/selfdrive/hardware/hw.h +++ b/selfdrive/hardware/hw.h @@ -60,8 +60,8 @@ class HardwareEon : public HardwareNone { class HardwareTici : public HardwareNone { public: - static constexpr float MAX_VOLUME = 0.4; - static constexpr float MIN_VOLUME = 0.5; + static constexpr float MAX_VOLUME = 0.5; + static constexpr float MIN_VOLUME = 0.4; static std::string get_os_version() { return "AGNOS " + util::read_file("/VERSION");