SC16IS7xxRK
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Protected Attributes
SC16IS7xxBuffer Class Reference

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.
 
SC16IS7xxBufferoperator= (const SC16IS7xxBuffer &)=delete
 This class is not copyable.
 
void free ()
 Free the allocate buffer buf - used internally.
 

Protected Attributes

uint8_t * buf = nullptr
 Buffer, allocated on heap.
 
size_t bufSize = 0
 Size of buffer in bytes.
 
size_t readOffset = 0
 Where to read from next, may be larger than bufSize.
 
size_t writeOffset = 0
 Where to write to next, may be larger than bufSize.
 
RecursiveMutex mutex
 Mutex to use to access buf, readOffset, or writeOffset.
 

Detailed Description

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().

Member Function Documentation

◆ availableToRead()

size_t SC16IS7xxBuffer::availableToRead ( ) const

Return the number of bytes that can be read from the buffer.

Returns
size_t Number of bytes that can be read. 0 = no data can be read now

◆ availableToWrite()

size_t SC16IS7xxBuffer::availableToWrite ( ) const

The number of bytes available to write into the buffer.

Returns
size_t Number of bytes that can be written

◆ init()

bool SC16IS7xxBuffer::init ( size_t  bufSize)

Allocate the buffer.

Parameters
bufSizeSize of the buffer in bytes
Returns
true The buffer was allocated
false The buffer could not be allocated, typically out of heap space, or no contiguous block available

This is called from withBufferedRead(),

◆ lock()

void SC16IS7xxBuffer::lock ( ) const
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.

◆ read() [1/2]

int SC16IS7xxBuffer::read ( )

Read a single byte.

Returns
int The byte value (0 - 255) or -1 if there is no data available to read.

◆ read() [2/2]

int SC16IS7xxBuffer::read ( uint8_t *  buffer,
size_t  size 
)

Read multiple bytes of data to a buffer.

Parameters
bufferThe buffer to store data into
sizeThe number of bytes requested
Returns
int The number of bytes actually read.

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.

◆ try_lock()

bool SC16IS7xxBuffer::try_lock ( ) const
inline

Attempt to lock the mutex. If already locked from another thread, returns false.

Returns
true
false

◆ trylock()

bool SC16IS7xxBuffer::trylock ( ) const
inline

Attempt to lock the mutex. If already locked from another thread, returns false.

Returns
true
false

◆ write()

size_t SC16IS7xxBuffer::write ( const uint8_t *  buffer,
size_t  size 
)

Write data to the buffer.

Parameters
buffer
size
Returns
size_t The number of bytes written

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.

◆ writeCallback()

void SC16IS7xxBuffer::writeCallback ( std::function< void(uint8_t *buffer, size_t &size)>  callback)

Write data into buffer using a non-copy callback.

Parameters
callbackFunction 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.


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