2024-01-16 21:37:57 +13:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#ifdef USE_ESP32
|
|
|
|
|
|
|
|
#include <freertos/FreeRTOS.h>
|
2024-12-18 21:07:07 -05:00
|
|
|
#include <freertos/ringbuf.h>
|
2024-01-16 21:37:57 +13:00
|
|
|
|
|
|
|
#include <cinttypes>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
namespace esphome {
|
|
|
|
|
|
|
|
class RingBuffer {
|
|
|
|
public:
|
2024-09-26 17:25:20 -04:00
|
|
|
~RingBuffer();
|
|
|
|
|
2024-09-02 20:47:54 -04:00
|
|
|
/**
|
|
|
|
* @brief Reads from the ring buffer, waiting up to a specified number of ticks if necessary.
|
|
|
|
*
|
|
|
|
* Available bytes are read into the provided data pointer. If not enough bytes are available,
|
|
|
|
* the function will wait up to `ticks_to_wait` FreeRTOS ticks before reading what is available.
|
|
|
|
*
|
|
|
|
* @param data Pointer to copy read data into
|
|
|
|
* @param len Number of bytes to read
|
|
|
|
* @param ticks_to_wait Maximum number of FreeRTOS ticks to wait (default: 0)
|
|
|
|
* @return Number of bytes read
|
|
|
|
*/
|
2024-01-20 03:38:37 +13:00
|
|
|
size_t read(void *data, size_t len, TickType_t ticks_to_wait = 0);
|
2024-01-16 21:37:57 +13:00
|
|
|
|
2024-09-02 20:47:54 -04:00
|
|
|
/**
|
|
|
|
* @brief Writes to the ring buffer, overwriting oldest data if necessary.
|
|
|
|
*
|
|
|
|
* The provided data is written to the ring buffer. If not enough space is available,
|
|
|
|
* the function will overwrite the oldest data in the ring buffer.
|
|
|
|
*
|
|
|
|
* @param data Pointer to data for writing
|
|
|
|
* @param len Number of bytes to write
|
|
|
|
* @return Number of bytes written
|
|
|
|
*/
|
2024-11-12 13:48:03 -05:00
|
|
|
size_t write(const void *data, size_t len);
|
2024-01-16 21:37:57 +13:00
|
|
|
|
2024-09-02 20:47:54 -04:00
|
|
|
/**
|
|
|
|
* @brief Writes to the ring buffer without overwriting oldest data.
|
|
|
|
*
|
|
|
|
* The provided data is written to the ring buffer. If not enough space is available,
|
|
|
|
* the function will wait up to `ticks_to_wait` FreeRTOS ticks before writing as much as possible.
|
|
|
|
*
|
|
|
|
* @param data Pointer to data for writing
|
|
|
|
* @param len Number of bytes to write
|
|
|
|
* @param ticks_to_wait Maximum number of FreeRTOS ticks to wait (default: 0)
|
|
|
|
* @return Number of bytes written
|
|
|
|
*/
|
2024-11-12 13:48:03 -05:00
|
|
|
size_t write_without_replacement(const void *data, size_t len, TickType_t ticks_to_wait = 0);
|
2024-09-02 20:47:54 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns the number of available bytes in the ring buffer.
|
|
|
|
*
|
|
|
|
* This function provides the number of bytes that can be read from the ring buffer
|
|
|
|
* without blocking the calling FreeRTOS task.
|
|
|
|
*
|
|
|
|
* @return Number of available bytes
|
|
|
|
*/
|
2024-01-16 21:37:57 +13:00
|
|
|
size_t available() const;
|
2024-09-02 20:47:54 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns the number of free bytes in the ring buffer.
|
|
|
|
*
|
|
|
|
* This function provides the number of bytes that can be written to the ring buffer
|
|
|
|
* without overwriting data or blocking the calling FreeRTOS task.
|
|
|
|
*
|
|
|
|
* @return Number of free bytes
|
|
|
|
*/
|
2024-01-16 21:37:57 +13:00
|
|
|
size_t free() const;
|
|
|
|
|
2024-09-02 20:47:54 -04:00
|
|
|
/**
|
|
|
|
* @brief Resets the ring buffer, discarding all stored data.
|
|
|
|
*
|
|
|
|
* @return pdPASS if successful, pdFAIL otherwise
|
|
|
|
*/
|
2024-01-16 21:37:57 +13:00
|
|
|
BaseType_t reset();
|
|
|
|
|
|
|
|
static std::unique_ptr<RingBuffer> create(size_t len);
|
|
|
|
|
|
|
|
protected:
|
2024-12-18 21:07:07 -05:00
|
|
|
/// @brief Discards data from the ring buffer.
|
|
|
|
/// @param discard_bytes amount of bytes to discard
|
|
|
|
/// @return True if all bytes were successfully discarded, false otherwise
|
|
|
|
bool discard_bytes_(size_t discard_bytes);
|
|
|
|
|
|
|
|
RingbufHandle_t handle_{nullptr};
|
|
|
|
StaticRingbuffer_t structure_;
|
|
|
|
uint8_t *storage_{nullptr};
|
2024-09-26 17:25:20 -04:00
|
|
|
size_t size_{0};
|
2024-01-16 21:37:57 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace esphome
|
|
|
|
|
|
|
|
#endif
|