ESPHome  2024.11.1
ring_buffer.cpp
Go to the documentation of this file.
1 #include "ring_buffer.h"
2 
3 #include "esphome/core/helpers.h"
4 #include "esphome/core/log.h"
5 
6 #ifdef USE_ESP32
7 
8 #include "helpers.h"
9 
10 namespace esphome {
11 
12 static const char *const TAG = "ring_buffer";
13 
15  if (this->handle_ != nullptr) {
16  vStreamBufferDelete(this->handle_);
18  allocator.deallocate(this->storage_, this->size_);
19  }
20 }
21 
22 std::unique_ptr<RingBuffer> RingBuffer::create(size_t len) {
23  std::unique_ptr<RingBuffer> rb = make_unique<RingBuffer>();
24 
25  rb->size_ = len + 1;
26 
28  rb->storage_ = allocator.allocate(rb->size_);
29  if (rb->storage_ == nullptr) {
30  return nullptr;
31  }
32 
33  rb->handle_ = xStreamBufferCreateStatic(rb->size_, 1, rb->storage_, &rb->structure_);
34  ESP_LOGD(TAG, "Created ring buffer with size %u", len);
35  return rb;
36 }
37 
38 size_t RingBuffer::read(void *data, size_t len, TickType_t ticks_to_wait) {
39  if (ticks_to_wait > 0)
40  xStreamBufferSetTriggerLevel(this->handle_, len);
41 
42  size_t bytes_read = xStreamBufferReceive(this->handle_, data, len, ticks_to_wait);
43 
44  xStreamBufferSetTriggerLevel(this->handle_, 1);
45 
46  return bytes_read;
47 }
48 
49 size_t RingBuffer::write(const void *data, size_t len) {
50  size_t free = this->free();
51  if (free < len) {
52  size_t needed = len - free;
53  uint8_t discard[needed];
54  xStreamBufferReceive(this->handle_, discard, needed, 0);
55  }
56  return xStreamBufferSend(this->handle_, data, len, 0);
57 }
58 
59 size_t RingBuffer::write_without_replacement(const void *data, size_t len, TickType_t ticks_to_wait) {
60  return xStreamBufferSend(this->handle_, data, len, ticks_to_wait);
61 }
62 
63 size_t RingBuffer::available() const { return xStreamBufferBytesAvailable(this->handle_); }
64 
65 size_t RingBuffer::free() const { return xStreamBufferSpacesAvailable(this->handle_); }
66 
67 BaseType_t RingBuffer::reset() { return xStreamBufferReset(this->handle_); }
68 
69 } // namespace esphome
70 
71 #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
T * allocate(size_t n)
Definition: helpers.h:681
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
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
const char *const TAG
Definition: spi.cpp:8
size_t available() const
Returns the number of available bytes in the ring buffer.
Definition: ring_buffer.cpp:63
void deallocate(T *p, size_t n)
Definition: helpers.h:700
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
An STL allocator that uses SPI or internal RAM.
Definition: helpers.h:666
static std::unique_ptr< RingBuffer > create(size_t len)
Definition: ring_buffer.cpp:22
StreamBufferHandle_t handle_
Definition: ring_buffer.h:85