ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
ring_buffer.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_ESP32
4
5#include <freertos/FreeRTOS.h>
6#include <freertos/ringbuf.h>
7
8#include <cinttypes>
9#include <memory>
10
11namespace esphome::ring_buffer {
12
14 public:
16
28 size_t read(void *data, size_t len, TickType_t ticks_to_wait = 0);
29
41 void *receive_acquire(size_t &length, size_t max_length, TickType_t ticks_to_wait = 0);
42
50 void receive_release(void *item);
51
62 size_t write(const void *data, size_t len);
63
77 size_t write_without_replacement(const void *data, size_t len, TickType_t ticks_to_wait = 0,
78 bool write_partial = true);
79
88 size_t available() const;
89
98 size_t free() const;
99
105 BaseType_t reset();
106
107 enum class MemoryPreference {
108 EXTERNAL_FIRST, // External RAM preferred, fall back to internal (default)
109 INTERNAL_FIRST, // Internal RAM preferred, fall back to external
110 };
111
112 static std::unique_ptr<RingBuffer> create(size_t len, MemoryPreference preference = MemoryPreference::EXTERNAL_FIRST);
113
114 protected:
118 bool discard_bytes_(size_t discard_bytes);
119
120 // xRingbufferCreateStatic() returns the static structure itself as the handle
121 RingbufHandle_t handle_() const { return const_cast<StaticRingbuffer_t *>(&this->structure_); }
122
123 StaticRingbuffer_t structure_;
124 uint8_t *storage_{nullptr};
125 size_t size_{0};
126};
127
128} // namespace esphome::ring_buffer
129
130#endif // USE_ESP32
BaseType_t reset()
Resets the ring buffer, discarding all stored data.
size_t write(const void *data, size_t len)
Writes to the ring buffer, overwriting oldest data if necessary.
RingbufHandle_t handle_() const
size_t write_without_replacement(const void *data, size_t len, TickType_t ticks_to_wait=0, bool write_partial=true)
Writes to the ring buffer without overwriting oldest data.
size_t free() const
Returns the number of free bytes in the ring buffer.
size_t available() const
Returns the number of available bytes in the ring buffer.
bool discard_bytes_(size_t discard_bytes)
Discards data from the ring buffer.
void receive_release(void *item)
Releases a previously acquired ring buffer item.
static std::unique_ptr< RingBuffer > create(size_t len, MemoryPreference preference=MemoryPreference::EXTERNAL_FIRST)
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.
void * receive_acquire(size_t &length, size_t max_length, TickType_t ticks_to_wait=0)
Acquires a pointer into the ring buffer's internal storage without copying.
const void size_t len
Definition hal.h:68
uint16_t length
Definition tt21100.cpp:0