12 static const char *
const TAG =
"ring_buffer";
16 vRingbufferDelete(this->
handle_);
23 std::unique_ptr<RingBuffer> rb = make_unique<RingBuffer>();
28 rb->storage_ = allocator.
allocate(rb->size_);
29 if (rb->storage_ ==
nullptr) {
33 rb->handle_ = xRingbufferCreateStatic(rb->size_, RINGBUF_TYPE_BYTEBUF, rb->storage_, &rb->structure_);
34 ESP_LOGD(TAG,
"Created ring buffer with size %u", len);
40 size_t bytes_read = 0;
42 void *buffer_data = xRingbufferReceiveUpTo(this->
handle_, &bytes_read, ticks_to_wait, len);
44 if (buffer_data ==
nullptr) {
48 std::memcpy(data, buffer_data, bytes_read);
50 vRingbufferReturnItem(this->
handle_, buffer_data);
52 if (bytes_read < len) {
54 size_t follow_up_bytes_read = 0;
55 size_t bytes_remaining = len - bytes_read;
57 buffer_data = xRingbufferReceiveUpTo(this->
handle_, &follow_up_bytes_read, 0, bytes_remaining);
59 if (buffer_data ==
nullptr) {
63 std::memcpy((
void *) ((uint8_t *) (data) + bytes_read), buffer_data, follow_up_bytes_read);
65 vRingbufferReturnItem(this->
handle_, buffer_data);
66 bytes_read += follow_up_bytes_read;
82 if (!xRingbufferSend(this->
handle_, data, len, ticks_to_wait)) {
84 size_t free = std::min(this->
free(), len);
85 if (xRingbufferSend(this->
handle_, data, free, 0)) {
94 UBaseType_t ux_items_waiting = 0;
95 vRingbufferGetInfo(this->
handle_,
nullptr,
nullptr,
nullptr,
nullptr, &ux_items_waiting);
96 return ux_items_waiting;
107 size_t bytes_read = 0;
109 void *buffer_data = xRingbufferReceiveUpTo(this->
handle_, &bytes_read, 0, discard_bytes);
110 if (buffer_data !=
nullptr)
111 vRingbufferReturnItem(this->
handle_, buffer_data);
113 if (bytes_read < discard_bytes) {
114 size_t wrapped_bytes_read = 0;
115 buffer_data = xRingbufferReceiveUpTo(this->
handle_, &wrapped_bytes_read, 0, discard_bytes - bytes_read);
116 if (buffer_data !=
nullptr) {
117 vRingbufferReturnItem(this->
handle_, buffer_data);
118 bytes_read += wrapped_bytes_read;
122 return (bytes_read == discard_bytes);
bool discard_bytes_(size_t discard_bytes)
Discards data from the ring buffer.
size_t free() const
Returns the number of free bytes in the ring buffer.
size_t write_without_replacement(const void *data, size_t len, TickType_t ticks_to_wait=0)
Writes to the ring buffer without overwriting oldest data.
BaseType_t reset()
Resets the ring buffer, discarding all stored data.
size_t read(void *data, size_t len, TickType_t ticks_to_wait=0)
Reads from the ring buffer, waiting up to a specified number of ticks if necessary.
size_t available() const
Returns the number of available bytes in the ring buffer.
void deallocate(T *p, size_t n)
Implementation of SPI Controller mode.
size_t write(const void *data, size_t len)
Writes to the ring buffer, overwriting oldest data if necessary.
static std::unique_ptr< RingBuffer > create(size_t len)