ESPHome  2024.11.0
Public Member Functions | Protected Attributes
esphome::weikai::WKRingBuffer< T, SIZE > Class Template Reference

This is an helper class that provides a simple ring buffers that works as a FIFO. More...

#include <weikai.h>

Public Member Functions

bool push (const T item)
 pushes an item at the tail of the fifo More...
 
bool pop (T &item)
 return and remove the item at head of the fifo More...
 
bool peek (T &item)
 return the value of the item at fifo's head without removing it More...
 
bool is_empty ()
 test is the Ring Buffer is empty ? More...
 
bool is_full ()
 test is the ring buffer is full ? More...
 
size_t count ()
 return the number of item in the ring buffer More...
 
size_t free ()
 returns the number of free positions in the buffer More...
 
void clear ()
 clear the buffer content More...
 

Protected Attributes

std::array< T, SIZE > rb_ {0}
 the ring buffer More...
 
int tail_ {0}
 position of the next element to read More...
 
int head_ {0}
 position of the next element to write More...
 
size_t count_ {0}
 count number of element in the buffer More...
 

Detailed Description

template<typename T, size_t SIZE>
class esphome::weikai::WKRingBuffer< T, SIZE >

This is an helper class that provides a simple ring buffers that works as a FIFO.

This ring buffer is used to buffer the bytes received in the FIFO of the Weika device. The best way to read characters from the device FIFO, is to first check how many bytes were received and then read them all at once. Unfortunately in all the code I have reviewed the characters are read one by one in a while loop by checking if bytes are available then reading the byte until no more byte available. This is pretty inefficient for two reasons:

Assuming you need to read 100 bytes that results into 200 calls. This is to compare to 2 calls (one to find the number of bytes available plus one to read all the bytes) in the best case! If the registers you read are located on the micro-controller this is acceptable because the registers can be accessed fast. But when the registers are located on a remote device accessing them requires several cycles on a slow bus. As it it not possible to fix this problem by asking users to rewrite their code, I have implemented this ring buffer that store the bytes received locally.

Definition at line 61 of file weikai.h.

Member Function Documentation

◆ clear()

template<typename T, size_t SIZE>
void esphome::weikai::WKRingBuffer< T, SIZE >::clear ( )
inline

clear the buffer content

Definition at line 114 of file weikai.h.

◆ count()

template<typename T, size_t SIZE>
size_t esphome::weikai::WKRingBuffer< T, SIZE >::count ( )
inline

return the number of item in the ring buffer

Returns
the number of items

Definition at line 107 of file weikai.h.

◆ free()

template<typename T, size_t SIZE>
size_t esphome::weikai::WKRingBuffer< T, SIZE >::free ( )
inline

returns the number of free positions in the buffer

Returns
how many items can be added

Definition at line 111 of file weikai.h.

◆ is_empty()

template<typename T, size_t SIZE>
bool esphome::weikai::WKRingBuffer< T, SIZE >::is_empty ( )
inline

test is the Ring Buffer is empty ?

Returns
true if empty

Definition at line 99 of file weikai.h.

◆ is_full()

template<typename T, size_t SIZE>
bool esphome::weikai::WKRingBuffer< T, SIZE >::is_full ( )
inline

test is the ring buffer is full ?

Returns
true if full

Definition at line 103 of file weikai.h.

◆ peek()

template<typename T, size_t SIZE>
bool esphome::weikai::WKRingBuffer< T, SIZE >::peek ( T &  item)
inline

return the value of the item at fifo's head without removing it

Parameters
itempointer to item to return
Returns
true if item has been retrieved, false il no item available (buffer empty)

Definition at line 90 of file weikai.h.

◆ pop()

template<typename T, size_t SIZE>
bool esphome::weikai::WKRingBuffer< T, SIZE >::pop ( T &  item)
inline

return and remove the item at head of the fifo

Parameters
itemitem read
Returns
true if an item has been retrieved, false il no item available (buffer empty)

Definition at line 78 of file weikai.h.

◆ push()

template<typename T, size_t SIZE>
bool esphome::weikai::WKRingBuffer< T, SIZE >::push ( const T  item)
inline

pushes an item at the tail of the fifo

Parameters
itemitem to push
Returns
true if item has been pushed, false il item could not pushed (buffer full)

Definition at line 66 of file weikai.h.

Field Documentation

◆ count_

template<typename T, size_t SIZE>
size_t esphome::weikai::WKRingBuffer< T, SIZE >::count_ {0}
protected

count number of element in the buffer

Definition at line 120 of file weikai.h.

◆ head_

template<typename T, size_t SIZE>
int esphome::weikai::WKRingBuffer< T, SIZE >::head_ {0}
protected

position of the next element to write

Definition at line 119 of file weikai.h.

◆ rb_

template<typename T, size_t SIZE>
std::array<T, SIZE> esphome::weikai::WKRingBuffer< T, SIZE >::rb_ {0}
protected

the ring buffer

Definition at line 117 of file weikai.h.

◆ tail_

template<typename T, size_t SIZE>
int esphome::weikai::WKRingBuffer< T, SIZE >::tail_ {0}
protected

position of the next element to read

Definition at line 118 of file weikai.h.


The documentation for this class was generated from the following file: