Skip to content

Commit

Permalink
merged: setting speaker status to running only after task is running.
Browse files Browse the repository at this point in the history
  • Loading branch information
gnumpi committed Jun 12, 2024
1 parent 75afece commit a8f66cb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
14 changes: 12 additions & 2 deletions esphome/components/i2s_audio/speaker/i2s_audio_speaker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,23 @@ void I2SAudioSpeaker::start() {
ESP_LOGE(TAG, "Cannot start audio, speaker failed to setup");
return;
}
if (this->task_created_) {
ESP_LOGW(TAG, "Called start while task has been already created.");
return;
}
this->state_ = speaker::STATE_STARTING;
}

void I2SAudioSpeaker::start_() {
if (this->task_created_) {
return;
}
if (!this->claim_i2s_access()) {
return; // Waiting for another i2s component to return lock
}
this->state_ = speaker::STATE_RUNNING;

xTaskCreate(I2SAudioSpeaker::player_task, "speaker_task", 8192, (void *) this, 1, &this->player_task_handle_);
this->task_created_ = true;
}

void I2SAudioSpeaker::player_task(void *params) {
Expand Down Expand Up @@ -146,7 +153,7 @@ void I2SAudioSpeaker::player_task(void *params) {
}

void I2SAudioSpeaker::stop() {
if (this->is_failed())
if (this->is_failed())
return;
if (this->state_ == speaker::STATE_STOPPED)
return;
Expand All @@ -169,6 +176,7 @@ void I2SAudioSpeaker::watch_() {
break;
case TaskEventType::STARTED:
ESP_LOGD(TAG, "Started I2S Audio Speaker");
this->state_ = speaker::STATE_RUNNING;
break;
case TaskEventType::STOPPING:
ESP_LOGD(TAG, "Stopping I2S Audio Speaker");
Expand All @@ -179,6 +187,7 @@ void I2SAudioSpeaker::watch_() {
case TaskEventType::STOPPED:
this->state_ = speaker::STATE_STOPPED;
vTaskDelete(this->player_task_handle_);
this->task_created_ = false;
this->player_task_handle_ = nullptr;
this->release_i2s_access();
xQueueReset(this->buffer_queue_);
Expand All @@ -196,6 +205,7 @@ void I2SAudioSpeaker::loop() {
switch (this->state_) {
case speaker::STATE_STARTING:
this->start_();
this->watch_();
break;
case speaker::STATE_RUNNING:
case speaker::STATE_STOPPING:
Expand Down
1 change: 1 addition & 0 deletions esphome/components/i2s_audio/speaker/i2s_audio_speaker.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class I2SAudioSpeaker : public Component, public speaker::Speaker, public I2SWri
QueueHandle_t buffer_queue_;
QueueHandle_t event_queue_;

bool task_created_{false};
#if SOC_I2S_SUPPORTS_DAC
i2s_dac_mode_t internal_dac_mode_{I2S_DAC_CHANNEL_DISABLE};
#endif
Expand Down

0 comments on commit a8f66cb

Please sign in to comment.