summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2024-05-24 15:12:41 +1000
committerjacqueline <me@jacqueline.id.au>2024-05-24 15:12:41 +1000
commit0da7ead0a84f6ded885a8b47654543dee70e0957 (patch)
tree9945430d9de171acc0406a25552d58109cd76f70
parentfb6b0ed49c3f4fe84d7708135e02cacfc6e7ceff (diff)
downloadtangara-fw-0da7ead0a84f6ded885a8b47654543dee70e0957.tar.gz
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.
-rw-r--r--src/tangara/audio/i2s_audio_output.cpp11
1 files changed, 10 insertions, 1 deletions
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);
}