Change output mode when bluetooth connects/disconnects

custom
ailurux 2 months ago
parent 90b302a9f4
commit 4f08a0838a
  1. 19
      src/tangara/audio/audio_fsm.cpp

@ -288,6 +288,8 @@ void AudioState::react(const system_fsm::BluetoothEvent& ev) {
if (bt.connectionState() != if (bt.connectionState() !=
drivers::Bluetooth::ConnectionState::kConnected) { drivers::Bluetooth::ConnectionState::kConnected) {
// If BT Disconnected, move to standby state // If BT Disconnected, move to standby state
events::Audio().Dispatch(audio::OutputModeChanged{
.set_to = drivers::NvsStorage::Output::kHeadphones});
transit<states::Standby>(); transit<states::Standby>();
return; return;
} }
@ -295,6 +297,8 @@ void AudioState::react(const system_fsm::BluetoothEvent& ev) {
if (!dev) { if (!dev) {
return; return;
} }
events::Audio().Dispatch(audio::OutputModeChanged{
.set_to = drivers::NvsStorage::Output::kBluetooth});
sBtOutput->SetVolume(sServices->nvs().BluetoothVolume(dev->mac)); sBtOutput->SetVolume(sServices->nvs().BluetoothVolume(dev->mac));
events::Ui().Dispatch(VolumeChanged{ events::Ui().Dispatch(VolumeChanged{
.percent = sOutput->GetVolumePct(), .percent = sOutput->GetVolumePct(),
@ -386,13 +390,14 @@ void AudioState::react(const OutputModeChanged& ev) {
if (ev.set_to) { if (ev.set_to) {
new_mode = *ev.set_to; new_mode = *ev.set_to;
} }
sOutput->mode(IAudioOutput::Modes::kOff);
switch (new_mode) { switch (new_mode) {
case drivers::NvsStorage::Output::kBluetooth: case drivers::NvsStorage::Output::kBluetooth:
sOutput = sBtOutput; sOutput = sBtOutput;
sI2SOutput->mode(IAudioOutput::Modes::kOff);
break; break;
case drivers::NvsStorage::Output::kHeadphones: case drivers::NvsStorage::Output::kHeadphones:
sOutput = sI2SOutput; sOutput = sI2SOutput;
sBtOutput->mode(IAudioOutput::Modes::kOff);
break; break;
} }
sSampleProcessor->SetOutput(sOutput); sSampleProcessor->SetOutput(sOutput);
@ -429,8 +434,8 @@ auto AudioState::updateTrackData(std::string uri,
}); });
} }
auto AudioState::updateSavedPosition(std::string uri, uint32_t position) auto AudioState::updateSavedPosition(std::string uri,
-> void { uint32_t position) -> void {
updateTrackData( updateTrackData(
uri, [=](database::TrackData& data) { data.last_position = position; }); uri, [=](database::TrackData& data) { data.last_position = position; });
} }
@ -486,13 +491,13 @@ void Uninitialised::react(const system_fsm::BootComplete& ev) {
sI2SOutput->SetVolume(nvs.AmpCurrentVolume()); sI2SOutput->SetVolume(nvs.AmpCurrentVolume());
sI2SOutput->SetVolumeImbalance(nvs.AmpLeftBias()); sI2SOutput->SetVolumeImbalance(nvs.AmpLeftBias());
if (sServices->nvs().OutputMode() == // Always set to headphones output initially
drivers::NvsStorage::Output::kHeadphones) { // Connecting bluetooth will change this
sOutput = sI2SOutput; sOutput = sI2SOutput;
} else { if (sServices->nvs().OutputMode() ==
drivers::NvsStorage::Output::kBluetooth) {
// Ensure Bluetooth gets enabled if it's the default sink. // Ensure Bluetooth gets enabled if it's the default sink.
sServices->bluetooth().enable(true); sServices->bluetooth().enable(true);
sOutput = sBtOutput;
} }
sOutput->mode(IAudioOutput::Modes::kOnPaused); sOutput->mode(IAudioOutput::Modes::kOnPaused);

Loading…
Cancel
Save