summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorailurux <ailurux@noreply.codeberg.org>2024-08-27 12:44:29 +0000
committerailurux <ailurux@noreply.codeberg.org>2024-08-27 12:44:29 +0000
commit88e44694f0639e3c05d50a8a605c030fc1ffc4c9 (patch)
tree72bd45e300cdf2f8835565ab1b1d71ead3b1af11 /src
parente0b057b3fa6ee1e8e8fd90d1abd6f201f73937ab (diff)
parente6c77f17b87a4cbf358bd2ddd3b82724771ee03c (diff)
downloadtangara-fw-88e44694f0639e3c05d50a8a605c030fc1ffc4c9.tar.gz
Merge pull request 'Switch output mode to headphones when plugged in' (#90) from daniel/output_mode_switch into main
Reviewed-on: https://codeberg.org/cool-tech-zone/tangara-fw/pulls/90 Reviewed-by: cooljqln <cooljqln@noreply.codeberg.org>
Diffstat (limited to 'src')
-rw-r--r--src/tangara/audio/audio_events.hpp5
-rw-r--r--src/tangara/audio/audio_fsm.cpp13
-rw-r--r--src/tangara/audio/audio_fsm.hpp1
3 files changed, 18 insertions, 1 deletions
diff --git a/src/tangara/audio/audio_events.hpp b/src/tangara/audio/audio_events.hpp
index 503664cc..50fd4b00 100644
--- a/src/tangara/audio/audio_events.hpp
+++ b/src/tangara/audio/audio_events.hpp
@@ -16,6 +16,7 @@
#include "tinyfsm.hpp"
#include "database/track.hpp"
+#include "drivers/nvs.hpp"
#include "types.hpp"
namespace audio {
@@ -137,7 +138,9 @@ struct SetVolumeLimit : tinyfsm::Event {
int limit_db;
};
-struct OutputModeChanged : tinyfsm::Event {};
+struct OutputModeChanged : tinyfsm::Event {
+ std::optional<drivers::NvsStorage::Output> set_to;
+};
namespace internal {
diff --git a/src/tangara/audio/audio_fsm.cpp b/src/tangara/audio/audio_fsm.cpp
index 8da11665..16c16002 100644
--- a/src/tangara/audio/audio_fsm.cpp
+++ b/src/tangara/audio/audio_fsm.cpp
@@ -231,6 +231,16 @@ void AudioState::react(const internal::StreamEnded& ev) {
sStreamCues.addCue({}, ev.cue_at_sample);
}
+void AudioState::react(const system_fsm::HasPhonesChanged& ev) {
+ if (ev.has_headphones) {
+ events::Audio().Dispatch(audio::OutputModeChanged{.set_to = drivers::NvsStorage::Output::kHeadphones});
+ } else {
+ if (sServices->bluetooth().enabled()) {
+ events::Audio().Dispatch(audio::OutputModeChanged{.set_to = drivers::NvsStorage::Output::kBluetooth});
+ }
+ }
+}
+
void AudioState::react(const system_fsm::BluetoothEvent& ev) {
using drivers::bluetooth::SimpleEvent;
if (std::holds_alternative<SimpleEvent>(ev.event)) {
@@ -334,6 +344,9 @@ void AudioState::react(const SetVolumeBalance& ev) {
void AudioState::react(const OutputModeChanged& ev) {
ESP_LOGI(kTag, "output mode changed");
auto new_mode = sServices->nvs().OutputMode();
+ if (ev.set_to) {
+ new_mode = *ev.set_to;
+ }
sOutput->mode(IAudioOutput::Modes::kOff);
switch (new_mode) {
case drivers::NvsStorage::Output::kBluetooth:
diff --git a/src/tangara/audio/audio_fsm.hpp b/src/tangara/audio/audio_fsm.hpp
index f949ce8a..0644375f 100644
--- a/src/tangara/audio/audio_fsm.hpp
+++ b/src/tangara/audio/audio_fsm.hpp
@@ -67,6 +67,7 @@ class AudioState : public tinyfsm::Fsm<AudioState> {
virtual void react(const system_fsm::KeyLockChanged&){};
virtual void react(const system_fsm::SdStateChanged&) {}
virtual void react(const system_fsm::BluetoothEvent&);
+ virtual void react(const system_fsm::HasPhonesChanged&);
protected:
auto emitPlaybackUpdate(bool paused) -> void;