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