diff --git a/lib/luavgl/src/lvgl.lua b/lib/luavgl/src/lvgl.lua index 13fd6908..c8aba5e0 100644 --- a/lib/luavgl/src/lvgl.lua +++ b/lib/luavgl/src/lvgl.lua @@ -1379,7 +1379,7 @@ end --- @field pad_gap? integer --- @field bg_color? integer | string text color in hex integer or #RGB or #RRGGBB format --- @field bg_opa? integer ---- @field bg_grad_color? integer +--- @field bg_grad_color? integer | string text color in hex integer or #RGB or #RRGGBB format --- @field bg_grad_dir? integer --- @field bg_main_stop? integer --- @field bg_grad_stop? integer diff --git a/src/audio/audio_fsm.cpp b/src/audio/audio_fsm.cpp index 424b0eff..a8f1260f 100644 --- a/src/audio/audio_fsm.cpp +++ b/src/audio/audio_fsm.cpp @@ -276,7 +276,24 @@ void AudioState::react(const system_fsm::HasPhonesChanged& ev) { } void AudioState::react(const SetVolume& ev) { - // TODO. + if (ev.db.has_value()) { + if (sOutput->SetVolumeDb(ev.db.value())) { + commitVolume(); + events::Ui().Dispatch(VolumeChanged{ + .percent = sOutput->GetVolumePct(), + .db = sOutput->GetVolumeDb(), + }); + } + + } else if (ev.percent.has_value()) { + if (sOutput->SetVolumePct(ev.percent.value())) { + commitVolume(); + events::Ui().Dispatch(VolumeChanged{ + .percent = sOutput->GetVolumePct(), + .db = sOutput->GetVolumeDb(), + }); + } + } } void AudioState::react(const SetVolumeLimit& ev) { diff --git a/src/audio/bt_audio_output.cpp b/src/audio/bt_audio_output.cpp index 04daf71f..7d6bade2 100644 --- a/src/audio/bt_audio_output.cpp +++ b/src/audio/bt_audio_output.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -54,11 +55,30 @@ auto BluetoothAudioOutput::GetVolume() -> uint16_t { } auto BluetoothAudioOutput::GetVolumePct() -> uint_fast8_t { - return static_cast(static_cast(volume_) * 100 / 0x7f); + return static_cast(round(static_cast(volume_) * 100.0 / 0x7f)); +} + +auto BluetoothAudioOutput::SetVolumePct(uint_fast8_t val) -> bool { + if (val > 100) { + return false; + } + uint16_t vol = (val * (0x7f))/100; + SetVolume(vol); + return true; } auto BluetoothAudioOutput::GetVolumeDb() -> int_fast16_t { - return 0; + double pct = GetVolumePct()/100.0; + if (pct <= 0) { + pct = 0.01; + } + int_fast16_t db = log(pct) * 20; + return db; +} + +auto BluetoothAudioOutput::SetVolumeDb(int_fast16_t val) -> bool { + double pct = exp(val / 20.0) * 100; + return SetVolumePct(pct); } auto BluetoothAudioOutput::AdjustVolumeUp() -> bool { diff --git a/src/audio/i2s_audio_output.cpp b/src/audio/i2s_audio_output.cpp index 3fb99159..2a251685 100644 --- a/src/audio/i2s_audio_output.cpp +++ b/src/audio/i2s_audio_output.cpp @@ -111,6 +111,15 @@ auto I2SAudioOutput::GetVolumePct() -> uint_fast8_t { return (current_volume_ - kMinVolume) * 100 / (max_volume_ - kMinVolume); } +auto I2SAudioOutput::SetVolumePct(uint_fast8_t val) -> bool { + if (val > 100) { + return false; + } + uint16_t vol = (val * (max_volume_ - kMinVolume))/100 + kMinVolume; + SetVolume(vol); + return true; +} + auto I2SAudioOutput::GetVolumeDb() -> int_fast16_t { // Add two before dividing in order to round correctly. return (static_cast(current_volume_) - @@ -118,6 +127,11 @@ auto I2SAudioOutput::GetVolumeDb() -> int_fast16_t { 4; } +auto I2SAudioOutput::SetVolumeDb(int_fast16_t val) -> bool { + SetVolume(val * 4 + static_cast(drivers::wm8523::kLineLevelReferenceVolume) - 2); + return true; +} + auto I2SAudioOutput::AdjustVolumeUp() -> bool { if (GetVolume() >= max_volume_) { return false; diff --git a/src/audio/include/audio_sink.hpp b/src/audio/include/audio_sink.hpp index e11f3ce0..f31d0d75 100644 --- a/src/audio/include/audio_sink.hpp +++ b/src/audio/include/audio_sink.hpp @@ -59,6 +59,9 @@ class IAudioOutput { virtual auto GetVolumePct() -> uint_fast8_t = 0; virtual auto GetVolumeDb() -> int_fast16_t = 0; + virtual auto SetVolumePct(uint_fast8_t) -> bool = 0; + virtual auto SetVolumeDb(int_fast16_t) -> bool = 0; + virtual auto AdjustVolumeUp() -> bool = 0; virtual auto AdjustVolumeDown() -> bool = 0; diff --git a/src/audio/include/bt_audio_output.hpp b/src/audio/include/bt_audio_output.hpp index a61e718a..74b0301a 100644 --- a/src/audio/include/bt_audio_output.hpp +++ b/src/audio/include/bt_audio_output.hpp @@ -35,7 +35,9 @@ class BluetoothAudioOutput : public IAudioOutput { auto GetVolume() -> uint16_t override; auto GetVolumePct() -> uint_fast8_t override; + auto SetVolumePct(uint_fast8_t val) -> bool override; auto GetVolumeDb() -> int_fast16_t override; + auto SetVolumeDb(int_fast16_t) -> bool override; auto AdjustVolumeUp() -> bool override; auto AdjustVolumeDown() -> bool override; diff --git a/src/audio/include/i2s_audio_output.hpp b/src/audio/include/i2s_audio_output.hpp index 5f3fc3ff..7954257a 100644 --- a/src/audio/include/i2s_audio_output.hpp +++ b/src/audio/include/i2s_audio_output.hpp @@ -33,7 +33,9 @@ class I2SAudioOutput : public IAudioOutput { auto GetVolume() -> uint16_t override; auto GetVolumePct() -> uint_fast8_t override; + auto SetVolumePct(uint_fast8_t val) -> bool override; auto GetVolumeDb() -> int_fast16_t override; + auto SetVolumeDb(int_fast16_t) -> bool override; auto AdjustVolumeUp() -> bool override; auto AdjustVolumeDown() -> bool override;