ESPHome 2026.5.0b1
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
75 size_t write_without_replacement(const void *data, size_t len, TickType_t ticks_to_wait = 0,
76 bool write_partial = true);
77
86 size_t available() const;
87
96 size_t free() const;
97
103 BaseType_t reset();
104
105 enum class MemoryPreference {
106 EXTERNAL_FIRST, // External RAM preferred, fall back to internal (default)
107 INTERNAL_FIRST, // Internal RAM preferred, fall back to external
108 };
109
110 static std::unique_ptr<RingBuffer> create(size_t len, MemoryPreference preference = MemoryPreference::EXTERNAL_FIRST);
111
112 protected:
116 bool discard_bytes_(size_t discard_bytes);
117
118 RingbufHandle_t handle_{nullptr};
119 StaticRingbuffer_t structure_;
120 uint8_t *storage_{nullptr};
121 size_t size_{0};
122};
123
124} // namespace esphome::ring_buffer
125
126#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.
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.
std::string size_t len
uint16_t length
Definition tt21100.cpp:0