From 5fd6510adf6990c60ebcb327f5c83956e23fcf87 Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Tue, 11 Apr 2017 20:31:09 +0200 Subject: [PATCH] freertos task test sdcard->i2s buffer --- src/main.cpp | 63 ++++++++++++++++++++++++++++++++++++------------- src/ringbuf.cpp | 49 ++++++++++++++++++++++++++++++++++++++ src/ringbuf.h | 23 ++++++++++++++++++ src/sdcard.cpp | 2 +- src/sound.h | 3 ++- 5 files changed, 122 insertions(+), 18 deletions(-) create mode 100644 src/ringbuf.cpp create mode 100644 src/ringbuf.h diff --git a/src/main.cpp b/src/main.cpp index d7ec437..aea8a45 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,15 +10,56 @@ #include "keyboard.h" #include "sdcard.h" #include "sound.h" +#include "ringbuf.h" #define VERSION "0.0" #define LED_PIN 21 +#define BUF_LENGTH 1024 Keyboard keyboard; SDCard sdcard; Sound sound; +Ringbuf ringbuf = Ringbuf((size_t) BUF_LENGTH); -uint16_t buffer[2048]; +void sd_read_task(void *pvParameter) { + sdcard.open("/T1.wav"); + for ( ;; ) { + uint16_t *start_ptr = ringbuf.getWrite(); + size_t size_avail = ringbuf.getWriteAvail(); + int num_samples = 0; + if (size_avail > 0) { + num_samples = sdcard.read(start_ptr, size_avail); + ringbuf.setWrite(num_samples); + } + Serial.print(num_samples,DEC); + Serial.println(" read samples from sdcard"); + + if (!sdcard.available()) break; // EOF + vTaskDelay(20 / portTICK_PERIOD_MS); // 20ms + } + Serial.println("sdcard: EOF"); + sdcard.close(); + vTaskDelete( NULL ); +} + +void i2s_write_task(void *pvParameter) { + for ( ;; ) { + TickType_t delay = 10 / portTICK_PERIOD_MS; // max delay: 10ms instead of portMAX_DELAY + uint16_t *start_ptr = ringbuf.getRead(); + size_t size_avail = ringbuf.getReadAvail(); + int num_samples = 0; + for (int i=0; ibuf = (uint16_t *)malloc(buf_length); + Serial.print("Allocated buffer at: "); + Serial.println((int)this->buf,HEX); + this->buf_length = buf_length; + this->read_pos = 0; + this->write_pos = 0; + this->full = false; +} + +Ringbuf::~Ringbuf() { + free(this->buf); +} + +uint16_t *Ringbuf::getWrite() { + return this->buf + write_pos; +} + +size_t Ringbuf::getWriteAvail() { + if (full) return 0; // full + else if (read_pos<=write_pos) return buf_length - write_pos; // to the end + else return read_pos - write_pos; // between +} + +uint16_t *Ringbuf::getRead() { + return this->buf + read_pos; +} + +size_t Ringbuf::getReadAvail() { + if (read_pos==write_pos && !full) return 0; // empty + else if (read_pos>=write_pos) return buf_length - read_pos; // read til end + else return write_pos - read_pos; +} + +void Ringbuf::setWrite(size_t bytes){ + write_pos = (write_pos+bytes)%buf_length; + if(read_pos==write_pos) full = true; +} + +void Ringbuf::setRead(size_t bytes){ + read_pos = (read_pos+bytes)%buf_length; + if(read_pos==write_pos) full = false; +} diff --git a/src/ringbuf.h b/src/ringbuf.h new file mode 100644 index 0000000..97b627e --- /dev/null +++ b/src/ringbuf.h @@ -0,0 +1,23 @@ +#ifndef _RINGBUF_H +#define _RINGBUF_H + + +class Ringbuf { + public: + Ringbuf(size_t buf_length); + ~Ringbuf(); + uint16_t *buf; + unsigned int buf_length; + unsigned int read_pos; + unsigned int write_pos; + uint16_t *getWrite(); + size_t getWriteAvail(); + uint16_t *getRead(); + size_t getReadAvail(); + void setWrite(size_t bytes); + void setRead(size_t bytes); + bool full; + private: +}; + +#endif /* _RINGBUF_H */ diff --git a/src/sdcard.cpp b/src/sdcard.cpp index e4cda9c..cc84e6e 100644 --- a/src/sdcard.cpp +++ b/src/sdcard.cpp @@ -73,5 +73,5 @@ bool SDCard::available() { } size_t SDCard::read(uint16_t *buffer, size_t len){ - return file.read((uint8_t *)buffer, len); + return file.read((uint8_t *)buffer, len*sizeof(uint16_t))/sizeof(uint16_t); } diff --git a/src/sound.h b/src/sound.h index 928c1d0..763e4bc 100644 --- a/src/sound.h +++ b/src/sound.h @@ -26,9 +26,10 @@ class Sound { // void pause(); void stop(); int render_sample_block(uint16_t *sample_buf_left, uint16_t *sample_buf_right, int num_samples); + const i2s_port_t i2s_num = (i2s_port_t)I2S_NUM; private: i2sbuffer_t buffer; - const i2s_port_t i2s_num = (i2s_port_t)I2S_NUM; + }; #endif /* _SOUND_H */