From 0da7ead0a84f6ded885a8b47654543dee70e0957 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Fri, 24 May 2024 15:12:41 +1000 Subject: [PATCH] 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. --- src/tangara/audio/i2s_audio_output.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/tangara/audio/i2s_audio_output.cpp b/src/tangara/audio/i2s_audio_output.cpp index 684bfa92..d7be9a4b 100644 --- a/src/tangara/audio/i2s_audio_output.cpp +++ b/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); }