From dd7f56e502926589cc283d7a93e2aa777a733231 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Tue, 16 Jan 2024 14:14:30 +0900 Subject: [PATCH] Use RingBuffer class --- .../esp_adf/microphone/esp_adf_microphone.cpp | 35 ++++++++++++------- .../esp_adf/microphone/esp_adf_microphone.h | 6 ++-- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/esphome/components/esp_adf/microphone/esp_adf_microphone.cpp b/esphome/components/esp_adf/microphone/esp_adf_microphone.cpp index ec3b7cbee2..59f4cb843c 100644 --- a/esphome/components/esp_adf/microphone/esp_adf_microphone.cpp +++ b/esphome/components/esp_adf/microphone/esp_adf_microphone.cpp @@ -24,15 +24,27 @@ namespace esp_adf { static const char *const TAG = "esp_adf.microphone"; void ESPADFMicrophone::setup() { - this->ring_buffer_ = rb_create(8000, sizeof(int16_t)); + ESP_LOGCONFIG(TAG, "Setting up ESP ADF Microphone..."); + this->ring_buffer_ = RingBuffer::create(8000 * sizeof(int16_t)); if (this->ring_buffer_ == nullptr) { - ESP_LOGW(TAG, "Could not allocate ring buffer."); + ESP_LOGE(TAG, "Could not allocate ring buffer"); this->mark_failed(); return; } this->read_event_queue_ = xQueueCreate(20, sizeof(TaskEvent)); + if (this->read_event_queue_ == nullptr) { + ESP_LOGW(TAG, "Could not allocate event queue"); + this->mark_failed(); + return; + } this->read_command_queue_ = xQueueCreate(20, sizeof(CommandEvent)); + if (this->read_command_queue_ == nullptr) { + ESP_LOGW(TAG, "Could not allocate command queue"); + this->mark_failed(); + return; + } + ESP_LOGCONFIG(TAG, "Successfully set up ESP ADF Microphone"); } void ESPADFMicrophone::start() { @@ -177,11 +189,7 @@ void ESPADFMicrophone::read_task(void *params) { continue; } - int available = rb_bytes_available(this_mic->ring_buffer_); - if (available < bytes_read) { - rb_read(this_mic->ring_buffer_, nullptr, bytes_read - available, 0); - } - int written = rb_write(this_mic->ring_buffer_, (char *) buffer, bytes_read, 0); + size_t written = this_mic->ring_buffer_->write((void *) buffer, bytes_read); event.type = TaskEventType::RUNNING; event.err = written; @@ -227,16 +235,17 @@ void ESPADFMicrophone::stop() { } size_t ESPADFMicrophone::read(int16_t *buf, size_t len) { - if (rb_bytes_filled(this->ring_buffer_) == 0) { + if (this->is_failed()) { + ESP_LOGE(TAG, "Microphone is failed, cannot read"); + return 0; + } + if (this->ring_buffer_->available() == 0) { return 0; // No data } - int bytes_read = rb_read(this->ring_buffer_, (char *) buf, len, 0); + size_t bytes_read = this->ring_buffer_->read((void *) buf, len); - if (bytes_read == -4 || bytes_read == -2 || bytes_read == 0) { + if (bytes_read == 0) { // No data in buffers to read. - return 0; - } else if (bytes_read < 0) { - ESP_LOGW(TAG, "Error reading from I2S microphone %s (%d)", esp_err_to_name(bytes_read), bytes_read); this->status_set_warning(); return 0; } diff --git a/esphome/components/esp_adf/microphone/esp_adf_microphone.h b/esphome/components/esp_adf/microphone/esp_adf_microphone.h index 7459242ff9..1fe5cade1d 100644 --- a/esphome/components/esp_adf/microphone/esp_adf_microphone.h +++ b/esphome/components/esp_adf/microphone/esp_adf_microphone.h @@ -4,10 +4,10 @@ #include "../esp_adf.h" -#include "esphome/components/microphone/microphone.h" #include "esphome/core/component.h" +#include "esphome/core/ring_buffer.h" -#include +#include "esphome/components/microphone/microphone.h" namespace esphome { namespace esp_adf { @@ -29,7 +29,7 @@ class ESPADFMicrophone : public ESPADFPipeline, public microphone::Microphone, p static void read_task(void *params); - ringbuf_handle_t ring_buffer_; + RingBuffer *ring_buffer_; TaskHandle_t read_task_handle_{nullptr}; QueueHandle_t read_event_queue_;