From e6c77f17b87a4cbf358bd2ddd3b82724771ee03c Mon Sep 17 00:00:00 2001 From: ailurux Date: Mon, 26 Aug 2024 15:43:00 +1000 Subject: Switch output mode to headphones when plugged in --- src/tangara/audio/audio_fsm.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src/tangara/audio/audio_fsm.cpp') 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(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: -- cgit v1.2.3