Ring Buffer¶
Ringbuffer is designed in addition to use as a data buffer, also used to connect Audio Elements. Each Element that requests data from the Ringbuffer will block the task until the data is available. Or block the task when writing data and the Buffer is full. Of course, we can stop this block at any time.
Application Example¶
In most of ESP-ADF examples connecting of Elements with Ringbuffers is done “behind the scenes” by a function audio_pipeline_link()
. To see this operation exposed check player/element_sdcard_mp3 example.
API Reference¶
Header File¶
Functions¶
-
ringbuf_handle_t
rb_create
(int block_size, int n_blocks)¶ Create ringbuffer with total size = block_size * n_blocks.
- Return
- ringbuf_handle_t
- Parameters
block_size
: Size of each blockn_blocks
: Number of blocks
-
esp_err_t
rb_destroy
(ringbuf_handle_t rb)¶ Cleanup and free all memory created by ringbuf_handle_t.
- Return
- ESP_OK
- ESP_FAIL
- Parameters
rb
: The Ringbuffer handle
-
esp_err_t
rb_abort
(ringbuf_handle_t rb)¶ Abort waiting until there is space for reading or writing of the ringbuffer.
- Return
- ESP_OK
- ESP_FAIL
- Parameters
rb
: The Ringbuffer handle
-
esp_err_t
rb_reset
(ringbuf_handle_t rb)¶ Reset ringbuffer, clear all values as initial state.
- Return
- ESP_OK
- ESP_FAIL
- Parameters
rb
: The Ringbuffer handle
-
int
rb_bytes_available
(ringbuf_handle_t rb)¶ Get total bytes available of Ringbuffer.
- Return
- total bytes available
- Parameters
rb
: The Ringbuffer handle
-
int
rb_bytes_filled
(ringbuf_handle_t rb)¶ Get the number of bytes that have filled the ringbuffer.
- Return
- The number of bytes that have filled the ringbuffer
- Parameters
rb
: The Ringbuffer handle
-
int
rb_get_size
(ringbuf_handle_t rb)¶ Get total size of Ringbuffer (in bytes)
- Return
- total size of Ringbuffer
- Parameters
rb
: The Ringbuffer handle
-
int
rb_read
(ringbuf_handle_t rb, char *buf, int len, TickType_t ticks_to_wait)¶ Read from Ringbuffer to
buf
with len and waittick_to_wait
ticks until enough bytes to read if the ringbuffer bytes available is less thanlen
. Ifbuf
argument provided isNULL
, then ringbuffer do pseudo reads by simply advancing pointers.- Return
- Number of bytes read
- Parameters
rb
: The Ringbuffer handlebuf
: The buffer pointer to read out datalen
: The length requestticks_to_wait
: The ticks to wait
-
int
rb_write
(ringbuf_handle_t rb, char *buf, int len, TickType_t ticks_to_wait)¶ Write to Ringbuffer from
buf
withlen
and waittick_to_wait
ticks until enough space to write if the ringbuffer space available is less thanlen
- Return
- Number of bytes written
- Parameters
rb
: The Ringbuffer handlebuf
: The bufferlen
: The lengthticks_to_wait
: The ticks to wait
-
esp_err_t
rb_done_write
(ringbuf_handle_t rb)¶ Set status of writing to ringbuffer is done.
- Return
- ESP_OK
- ESP_FAIL
- Parameters
rb
: The Ringbuffer handle
-
esp_err_t
rb_unblock_reader
(ringbuf_handle_t rb)¶ Unblock from rb_read.
- Return
- ESP_OK
- ESP_FAIL
- Parameters
rb
: The Ringbuffer handle