Simply some I2SDac management to avoid null pointer accesses

Fixes #72; we were destroying the I2SDac instance, but weren't actually
recording that the output was now in the 'off' state.
custom
jacqueline 11 months ago
parent fb6b0ed49c
commit 0da7ead0a8
  1. 11
      src/tangara/audio/i2s_audio_output.cpp

@ -62,14 +62,21 @@ auto I2SAudioOutput::changeMode(Modes mode) -> void {
if (mode == current_mode_) {
return;
}
bool was_off = current_mode_ == Modes::kOff;
current_mode_ = mode;
if (mode == Modes::kOff) {
// Turning off this output. Ensure we clean up the I2SDac instance to
// reclaim its valuable DMA buffers.
if (dac_) {
dac_->Stop();
dac_.reset();
}
return;
}
if (current_mode_ == Modes::kOff) {
if (was_off) {
// Ensure an I2SDac instance actually exists.
if (!dac_) {
auto instance = drivers::I2SDac::create(expander_);
if (!instance) {
@ -77,10 +84,12 @@ auto I2SAudioOutput::changeMode(Modes mode) -> void {
}
dac_.reset(*instance);
}
// Set up the new instance properly.
SetVolume(GetVolume());
dac_->SetSource(stream());
dac_->Start();
}
current_mode_ = mode;
dac_->SetPaused(mode == Modes::kOnPaused);
}

Loading…
Cancel
Save