ESPHome  2024.11.1
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/stream_buffer.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:
85  StreamBufferHandle_t handle_;
86  StaticStreamBuffer_t structure_;
87  uint8_t *storage_;
88  size_t size_{0};
89 };
90 
91 } // namespace esphome
92 
93 #endif
uint8_t * storage_
Definition: ring_buffer.h:87
size_t free() const
Returns the number of free bytes in the ring buffer.
Definition: ring_buffer.cpp:65
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:59
StaticStreamBuffer_t structure_
Definition: ring_buffer.h:86
BaseType_t reset()
Resets the ring buffer, discarding all stored data.
Definition: ring_buffer.cpp:67
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:38
size_t available() const
Returns the number of available bytes in the ring buffer.
Definition: ring_buffer.cpp:63
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:49
static std::unique_ptr< RingBuffer > create(size_t len)
Definition: ring_buffer.cpp:22
StreamBufferHandle_t handle_
Definition: ring_buffer.h:85