ESPHome  2024.12.2
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 
11 namespace esphome {
12 
13 class RingBuffer {
14  public:
15  ~RingBuffer();
16 
28  size_t read(void *data, size_t len, TickType_t ticks_to_wait = 0);
29 
40  size_t write(const void *data, size_t len);
41 
53  size_t write_without_replacement(const void *data, size_t len, TickType_t ticks_to_wait = 0);
54 
63  size_t available() const;
64 
73  size_t free() const;
74 
80  BaseType_t reset();
81 
82  static std::unique_ptr<RingBuffer> create(size_t len);
83 
84  protected:
88  bool discard_bytes_(size_t discard_bytes);
89 
90  RingbufHandle_t handle_{nullptr};
91  StaticRingbuffer_t structure_;
92  uint8_t *storage_{nullptr};
93  size_t size_{0};
94 };
95 
96 } // namespace esphome
97 
98 #endif
RingbufHandle_t handle_
Definition: ring_buffer.h:90
uint8_t * storage_
Definition: ring_buffer.h:92
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.
Definition: ring_buffer.cpp:99
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.
Definition: ring_buffer.cpp:81
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.
Definition: ring_buffer.cpp:39
size_t available() const
Returns the number of available bytes in the ring buffer.
Definition: ring_buffer.cpp:93
StaticRingbuffer_t structure_
Definition: ring_buffer.h:91
std::string size_t len
Definition: helpers.h:293
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
size_t write(const void *data, size_t len)
Writes to the ring buffer, overwriting oldest data if necessary.
Definition: ring_buffer.cpp:72
static std::unique_ptr< RingBuffer > create(size_t len)
Definition: ring_buffer.cpp:22