diff --git a/src/audio/audio_fsm.cpp b/src/audio/audio_fsm.cpp index 3bd4d396..90bfd60b 100644 --- a/src/audio/audio_fsm.cpp +++ b/src/audio/audio_fsm.cpp @@ -50,6 +50,12 @@ std::shared_ptr AudioState::sOutput; std::optional AudioState::sCurrentTrack; +void AudioState::react(const system_fsm::KeyLockChanged& ev) { + if (ev.locking && sServices) { + sServices->nvs().AmpCurrentVolume(sOutput->GetVolume()); + } +} + void AudioState::react(const StepUpVolume& ev) { if (sOutput->AdjustVolumeUp()) { events::Ui().Dispatch(VolumeChanged{}); @@ -63,10 +69,10 @@ void AudioState::react(const StepDownVolume& ev) { } void AudioState::react(const system_fsm::HasPhonesChanged& ev) { - if (ev.falling) { - // ESP_LOGI(kTag, "headphones in!"); + if (ev.has_headphones) { + ESP_LOGI(kTag, "headphones in!"); } else { - // ESP_LOGI(kTag, "headphones out!"); + ESP_LOGI(kTag, "headphones out!"); } } @@ -186,6 +192,12 @@ void Playback::exit() { events::Ui().Dispatch(PlaybackFinished{}); } +void Playback::react(const system_fsm::HasPhonesChanged& ev) { + if (!ev.has_headphones) { + transit(); + } +} + void Playback::react(const QueueUpdate& ev) { if (!ev.current_changed) { return; diff --git a/src/audio/include/audio_fsm.hpp b/src/audio/include/audio_fsm.hpp index c9fac08b..590c6463 100644 --- a/src/audio/include/audio_fsm.hpp +++ b/src/audio/include/audio_fsm.hpp @@ -43,11 +43,12 @@ class AudioState : public tinyfsm::Fsm { void react(const StepUpVolume&); void react(const StepDownVolume&); - void react(const system_fsm::HasPhonesChanged&); + virtual void react(const system_fsm::HasPhonesChanged&); void react(const ChangeMaxVolume&); void react(const OutputModeChanged&); virtual void react(const system_fsm::BootComplete&) {} + void react(const system_fsm::KeyLockChanged&); virtual void react(const PlayFile&) {} virtual void react(const QueueUpdate&) {} @@ -97,6 +98,8 @@ class Playback : public AudioState { void entry() override; void exit() override; + void react(const system_fsm::HasPhonesChanged&) override; + void react(const PlayFile&) override; void react(const QueueUpdate&) override; void react(const PlaybackUpdate&) override; diff --git a/src/system_fsm/include/system_events.hpp b/src/system_fsm/include/system_events.hpp index cbb970ec..4ead9f2f 100644 --- a/src/system_fsm/include/system_events.hpp +++ b/src/system_fsm/include/system_events.hpp @@ -46,7 +46,7 @@ struct KeyLockChanged : tinyfsm::Event { bool locking; }; struct HasPhonesChanged : tinyfsm::Event { - bool falling; + bool has_headphones; }; struct ChargingStatusChanged : tinyfsm::Event {}; diff --git a/src/system_fsm/system_fsm.cpp b/src/system_fsm/system_fsm.cpp index 724d2eea..17f09576 100644 --- a/src/system_fsm/system_fsm.cpp +++ b/src/system_fsm/system_fsm.cpp @@ -57,10 +57,11 @@ void SystemState::react(const internal::GpioInterrupt&) { if (key_lock != prev_key_lock) { KeyLockChanged ev{.locking = key_lock}; events::System().Dispatch(ev); + events::Audio().Dispatch(ev); events::Ui().Dispatch(ev); } if (has_headphones != prev_has_headphones) { - HasPhonesChanged ev{.falling = prev_has_headphones}; + HasPhonesChanged ev{.has_headphones = has_headphones}; events::Audio().Dispatch(ev); } }