Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions selfdrive/common/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

#include <cstdio>
#include <csignal>
#include <cassert>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <fcntl.h>

#include <string>
#include <memory>
#include <atomic>
#include <sstream>
#include <fstream>
#include <thread>
#include <chrono>
#include <cassert>
#include <unistd.h>
#include <fcntl.h>
#include <algorithm>

#ifndef sighandler_t
typedef void (*sighandler_t)(int sig);
Expand All @@ -38,6 +39,19 @@ int set_core_affinity(int core);

namespace util {

// ***** math helpers *****

// map x from [a1, a2] to [b1, b2]
template<typename T>
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;
}
Expand Down
9 changes: 9 additions & 0 deletions selfdrive/hardware/hw.h
Original file line number Diff line number Diff line change
Expand Up @@ -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() {};
Expand All @@ -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");
};
Expand All @@ -54,6 +60,9 @@ class HardwareEon : public HardwareNone {

class HardwareTici : public HardwareNone {
public:
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");
};
Expand Down
4 changes: 4 additions & 0 deletions selfdrive/ui/qt/home.cc
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@ void GLWindow::timerUpdate() {

handle_display_state(&ui_state, false);

// scale volume with speed
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();
watchdog_kick();
Expand Down
2 changes: 1 addition & 1 deletion selfdrive/ui/qt/qt_sound.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions selfdrive/ui/qt/qt_sound.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class QtSound : public Sound {
bool play(AudibleAlert alert);
void stop();
void setVolume(int volume);
float volume = 0;

private:
std::map<AudibleAlert, QSoundEffect> sounds;
Expand Down