SC16IS7xxRK
|
Class used internally for buffering data. More...
#include <SC16IS7xxRK.h>
Public Member Functions | |
SC16IS7xxBuffer () | |
Construct a buffer object. You will normally never have to instantiate one. | |
virtual | ~SC16IS7xxBuffer () |
Destructor. You should never need to delete one. | |
bool | init (size_t bufSize) |
Allocate the buffer. | |
size_t | availableToRead () const |
Return the number of bytes that can be read from the buffer. | |
int | read () |
Read a single byte. | |
int | read (uint8_t *buffer, size_t size) |
Read multiple bytes of data to a buffer. | |
size_t | availableToWrite () const |
The number of bytes available to write into the buffer. | |
size_t | write (const uint8_t *buffer, size_t size) |
Write data to the buffer. | |
void | writeCallback (std::function< void(uint8_t *buffer, size_t &size)> callback) |
Write data into buffer using a non-copy callback. | |
void | lock () const |
Lock the buffer mutex. | |
bool | trylock () const |
Attempt to lock the mutex. If already locked from another thread, returns false. | |
bool | try_lock () const |
Attempt to lock the mutex. If already locked from another thread, returns false. | |
void | unlock () const |
Unlock the buffer mutex. | |
Protected Member Functions | |
SC16IS7xxBuffer (const SC16IS7xxBuffer &)=delete | |
This class is not copyable. | |
SC16IS7xxBuffer & | operator= (const SC16IS7xxBuffer &)=delete |
This class is not copyable. | |
void | free () |
Free the allocate buffer buf - used internally. | |
Class used internally for buffering data.
Since the hardware FIFO is only 64 bytes, this class is used to store data in a larger buffer allocated on the heap.
You do not create one of these objects; it's created automatically when using withBufferedRead().
size_t SC16IS7xxBuffer::availableToRead | ( | ) | const |
Return the number of bytes that can be read from the buffer.
size_t SC16IS7xxBuffer::availableToWrite | ( | ) | const |
The number of bytes available to write into the buffer.
bool SC16IS7xxBuffer::init | ( | size_t | bufSize | ) |
Allocate the buffer.
bufSize | Size of the buffer in bytes |
This is called from withBufferedRead(),
|
inline |
Lock the buffer mutex.
A recursive mutex is used to protect access to buf, readOffset, and writeOffset since they can be accessed from two different threads. The buffer is written from the worker thread and read from whatever thread the user is reading from, typically the application loop thread.
int SC16IS7xxBuffer::read | ( | ) |
Read a single byte.
int SC16IS7xxBuffer::read | ( | uint8_t * | buffer, |
size_t | size | ||
) |
Read multiple bytes of data to a buffer.
buffer | The buffer to store data into |
size | The number of bytes requested |
The read() call does not block. It will only return the number of bytes currently available to read, so the number of bytes read will often be smaller than the requested number of bytes in size. The returned data is not null terminated.
|
inline |
Attempt to lock the mutex. If already locked from another thread, returns false.
|
inline |
Attempt to lock the mutex. If already locked from another thread, returns false.
size_t SC16IS7xxBuffer::write | ( | const uint8_t * | buffer, |
size_t | size | ||
) |
Write data to the buffer.
buffer | |
size |
This call does not block until there is space in the buffer! If there is insufficient space, only the bytes that will fit are stored and the return value indicates the amount the was stored.
void SC16IS7xxBuffer::writeCallback | ( | std::function< void(uint8_t *buffer, size_t &size)> | callback | ) |
Write data into buffer using a non-copy callback.
callback | Function or C++ lambda, see below |
This is used internally from the worker thread after reading a block of data from the UART by I2C or SPI. There is no single-byte write because that is never done because it would be extraordinarily inefficient.
Callback prototype: void(uint8_t *buffer, size_t &size)
If there is room in the buffer to write data, your callback is called during the execution of writeCallback. It's passed a buffer you should write data to and a size which is the maximum number of bytes you can write.