make sure that only the component locking is in place.

This commit is contained in:
NP v/d Spek 2024-10-25 21:06:35 +02:00
parent 12dd41ab34
commit c245c554e2

View file

@ -26,6 +26,7 @@ static const char *const TAG = "i2s_audio.speaker";
enum SpeakerEventGroupBits : uint32_t { enum SpeakerEventGroupBits : uint32_t {
COMMAND_START = (1 << 0), // Starts the main task purpose COMMAND_START = (1 << 0), // Starts the main task purpose
COMMAND_STOP = (1 << 1), // stops the main task COMMAND_STOP = (1 << 1), // stops the main task
COMMAND_STOP_GRACEFULLY = (1 << 2), // Stops the task once all data has been written
MESSAGE_RING_BUFFER_AVAILABLE_TO_WRITE = (1 << 5), // Locks the ring buffer when not set MESSAGE_RING_BUFFER_AVAILABLE_TO_WRITE = (1 << 5), // Locks the ring buffer when not set
STATE_STARTING = (1 << 10), STATE_STARTING = (1 << 10),
STATE_RUNNING = (1 << 11), STATE_RUNNING = (1 << 11),
@ -236,15 +237,17 @@ bool I2SAudioSpeaker::has_buffered_data() const {
void I2SAudioSpeaker::speaker_task(void *params) { void I2SAudioSpeaker::speaker_task(void *params) {
I2SAudioSpeaker *this_speaker = (I2SAudioSpeaker *) params; I2SAudioSpeaker *this_speaker = (I2SAudioSpeaker *) params;
uint32_t event_group_bits = xEventGroupWaitBits( uint32_t event_group_bits =
this_speaker->event_group_, xEventGroupWaitBits(this_speaker->event_group_,
SpeakerEventGroupBits::COMMAND_START | SpeakerEventGroupBits::COMMAND_STOP, // Bit message to read SpeakerEventGroupBits::COMMAND_START | SpeakerEventGroupBits::COMMAND_STOP |
SpeakerEventGroupBits::COMMAND_STOP_GRACEFULLY, // Bit message to read
pdTRUE, // Clear the bits on exit pdTRUE, // Clear the bits on exit
pdFALSE, // Don't wait for all the bits, pdFALSE, // Don't wait for all the bits,
portMAX_DELAY); // Block indefinitely until a bit is set portMAX_DELAY); // Block indefinitely until a bit is set
if (event_group_bits & (SpeakerEventGroupBits::COMMAND_STOP)) { if (event_group_bits & (SpeakerEventGroupBits::COMMAND_STOP |
// Received a stop signal before the task was requested to start SpeakerEventGroupBits::COMMAND_STOP_GRACEFULLY)) { // Received a stop signal before the task
// was requested to start
this_speaker->delete_task_(0); this_speaker->delete_task_(0);
} }
@ -285,6 +288,9 @@ void I2SAudioSpeaker::speaker_task(void *params) {
if (event_group_bits & SpeakerEventGroupBits::COMMAND_STOP) { if (event_group_bits & SpeakerEventGroupBits::COMMAND_STOP) {
break; break;
} }
if (event_group_bits & SpeakerEventGroupBits::COMMAND_STOP_GRACEFULLY) {
stop_gracefully = true;
}
size_t bytes_to_read = dma_buffers_size; size_t bytes_to_read = dma_buffers_size;
size_t bytes_read = this_speaker->audio_ring_buffer_->read((void *) this_speaker->data_buffer_, bytes_to_read, size_t bytes_read = this_speaker->audio_ring_buffer_->read((void *) this_speaker->data_buffer_, bytes_to_read,
@ -312,6 +318,15 @@ void I2SAudioSpeaker::speaker_task(void *params) {
if (bytes_written != bytes_read) { if (bytes_written != bytes_read) {
xEventGroupSetBits(this_speaker->event_group_, SpeakerEventGroupBits::ERR_ESP_INVALID_SIZE); xEventGroupSetBits(this_speaker->event_group_, SpeakerEventGroupBits::ERR_ESP_INVALID_SIZE);
} }
} else {
// No data received
if (stop_gracefully) {
break;
}
i2s_zero_dma_buffer(this_speaker->parent_->get_port());
} }
} }
} else { } else {
@ -367,7 +382,7 @@ void I2SAudioSpeaker::stop_(bool wait_on_empty) {
return; return;
if (wait_on_empty) { if (wait_on_empty) {
this->state_ = speaker::STATE_STOPPING; xEventGroupSetBits(this->event_group_, SpeakerEventGroupBits::COMMAND_STOP_GRACEFULLY);
} else { } else {
xEventGroupSetBits(this->event_group_, SpeakerEventGroupBits::COMMAND_STOP); xEventGroupSetBits(this->event_group_, SpeakerEventGroupBits::COMMAND_STOP);
} }