1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-15 17:52:19 +01:00

RingBuffer: Make partial writing optional (#10302)

This commit is contained in:
Mischa Siekmann
2025-09-09 15:39:47 +02:00
committed by GitHub
parent 7adad0ee49
commit 8993f4e6b4
2 changed files with 8 additions and 3 deletions

View File

@@ -78,9 +78,13 @@ size_t RingBuffer::write(const void *data, size_t len) {
return this->write_without_replacement(data, len, 0);
}
size_t RingBuffer::write_without_replacement(const void *data, size_t len, TickType_t ticks_to_wait) {
size_t RingBuffer::write_without_replacement(const void *data, size_t len, TickType_t ticks_to_wait,
bool write_partial) {
if (!xRingbufferSend(this->handle_, data, len, ticks_to_wait)) {
// Couldn't fit all the data, so only write what will fit
if (!write_partial) {
return 0; // Not enough space available and not allowed to write partial data
}
// Couldn't fit all the data, write what will fit
size_t free = std::min(this->free(), len);
if (xRingbufferSend(this->handle_, data, free, 0)) {
return free;

View File

@@ -50,7 +50,8 @@ class RingBuffer {
* @param ticks_to_wait Maximum number of FreeRTOS ticks to wait (default: 0)
* @return Number of bytes written
*/
size_t write_without_replacement(const void *data, size_t len, TickType_t ticks_to_wait = 0);
size_t write_without_replacement(const void *data, size_t len, TickType_t ticks_to_wait = 0,
bool write_partial = true);
/**
* @brief Returns the number of available bytes in the ring buffer.