Byggern
Stream Class Reference

This class is an interface for implementing a duplex FIFO stream. More...

#include <stream.h>

Inheritance diagram for Stream:
ADC ADC_internal I2C OLED Socket SPI::SPI UART OLED_memory OLED_SCP

Public Member Functions

 Stream (uint16_t input_stream_size, uint16_t output_stream_size)
 
virtual void Write (uint8_t *string, uint16_t size)
 
virtual uint16_t Read (uint8_t *string, uint16_t size)
 
virtual bool ReadByte (uint8_t &byte)
 
virtual void WriteByte (uint8_t byte)
 
virtual uint8_t GetAvailableWriteBytes ()
 
virtual uint8_t GetAvailableReadBytes ()
 
virtual bool CheckInputOverflowFlag ()
 
virtual bool CheckOutputOverflowFlag ()
 
virtual uint16_t GetInputBufferLength ()
 
virtual uint16_t GetOutputBufferLength ()
 
virtual void FlushInputBuffer ()
 
virtual void FlushOutputBuffer ()
 

Protected Member Functions

virtual uint16_t ReadFromBuffer (uint8_t *buffer, uint16_t &start_index, uint16_t &stop_index, uint16_t &buffer_size, bool &empty, uint8_t *string, uint16_t &string_size)
 
virtual void WriteToBuffer (uint8_t *buffer, uint16_t &start_index, uint16_t &stop_index, uint16_t &buffer_size, bool &empty, bool &overflow_flag, uint8_t *string, uint16_t &string_size, void(*cb)(Stream *stream))
 
virtual bool ReadByteFromBuffer (uint8_t &byte, uint8_t *buffer, uint16_t &start_index, uint16_t &stop_index, uint16_t &buffer_size, bool &empty)
 
virtual void WriteByteToBuffer (uint8_t *buffer, uint16_t &start_index, uint16_t &stop_index, uint16_t &buffer_size, bool &empty, bool &overflow_flag, uint8_t &byte, void(*cb)(Stream *stream))
 
virtual void WriteByteToInputStream (uint8_t &byte)
 
virtual bool ReadByteFromOutputStream (uint8_t &byte)
 
virtual void WriteToInputStream (uint8_t *string, uint16_t size)
 
virtual uint16_t ReadFromOutputStream (uint8_t *string, uint16_t size)
 
virtual uint16_t CalculateLength (uint16_t &start_index, uint16_t &stop_index, uint16_t &buffer_size, bool &empty)
 
virtual void FlushStream (uint16_t &start_index, uint16_t &stop_index, uint16_t &buffer_size, bool &empty)
 

Protected Attributes

uint8_t * input_buffer
 
uint8_t * output_buffer
 
uint16_t input_buffer_start_index = 0
 
uint16_t output_buffer_start_index = 0
 
uint16_t input_buffer_stop_index
 
uint16_t output_buffer_stop_index
 
uint16_t input_buffer_size
 
uint16_t output_buffer_size
 
void(* event_input_buffer_not_empty )(Stream *stream) = nullptr
 
void(* event_output_buffer_not_empty )(Stream *stream) = nullptr
 
bool input_buffer_empty = true
 
bool output_buffer_empty = true
 
bool input_buffer_overflowed = false
 
bool output_buffer_overflowed = false
 

Detailed Description

This class is an interface for implementing a duplex FIFO stream.

This class implements a duplex FIFO stream using two circular buffers. For instance both an RS232 driver and a CAN driver would need to implement a stream. Inheriting from this interface makes the communication interface for RS232, CAN and other stream based classes/drivers the same, meaning the end user does not need to know whether the data being sent is being sent over a UART or a CAN bus.

As an example you can set up a CAN bus using a CAN driver inheriting from this interface. Then it can be passed to a function expecting to send data to an RS232 class also inheriting from this interface, and it will just work. This interface is in other words written to make it easier to write consistent and driver independent code.

Constructor & Destructor Documentation

§ Stream()

Stream::Stream ( uint16_t  input_stream_size,
uint16_t  output_stream_size 
)

Used to initialize the stream sizes and other data members.

Parameters
input_stream_sizeThe maximum size of the input stream.
output_stream_sizeThe maximum size of the output stream.

Member Function Documentation

§ CalculateLength()

uint16_t Stream::CalculateLength ( uint16_t &  start_index,
uint16_t &  stop_index,
uint16_t &  buffer_size,
bool &  empty 
)
protectedvirtual

Calculates the length of the valid part of the buffer.

Parameters
start_indexThe start index of the buffer.
stop_indexThe stop index of the buffer.
Returns
Length of valid data.

§ CheckInputOverflowFlag()

bool Stream::CheckInputOverflowFlag ( )
virtual

Checks whether or not the input overflow flag has been set. This also clears the flag.

Returns
Returns flag value.

§ CheckOutputOverflowFlag()

bool Stream::CheckOutputOverflowFlag ( )
virtual

Checks whether or not the output overflow flag has been set. This also clears the flag.

Returns
Returns flag value.

§ FlushInputBuffer()

void Stream::FlushInputBuffer ( )
virtual

Flushes the input buffer

§ FlushOutputBuffer()

void Stream::FlushOutputBuffer ( )
virtual

Flushes the output buffer

§ FlushStream()

void Stream::FlushStream ( uint16_t &  start_index,
uint16_t &  stop_index,
uint16_t &  buffer_size,
bool &  empty 
)
protectedvirtual

Flushes the given buffer

Parameters
bufferReference to the buffer which is to be flushed
start_indexStart index of the buffer which is to be flushed
stop_indexStop index of the buffer which is to be flushed
buffer_sizeThe size of the given buffer
emptyReference to the empty flag of the given buffer

§ GetAvailableReadBytes()

uint8_t Stream::GetAvailableReadBytes ( )
virtual

Returns the number of bytes available for reading.

Returns
Returns the length of valid data in the input stream.

§ GetAvailableWriteBytes()

uint8_t Stream::GetAvailableWriteBytes ( )
virtual

Returns number of bytes not being used in the output buffer (size - length)

Returns
Returns the length of the free space in the output stream.

§ GetInputBufferLength()

uint16_t Stream::GetInputBufferLength ( )
virtual

Calculates the length of the readable part of the buffer.

Returns
Length of valid data.

§ GetOutputBufferLength()

uint16_t Stream::GetOutputBufferLength ( )
virtual

Calculates the length of the readable part of the buffer.

Returns
Length of valid data

§ Read()

uint16_t Stream::Read ( uint8_t *  string,
uint16_t  size 
)
virtual

Reads data from the input stream into the specified string.

Parameters
stringString to store read data in.
sizeSize of the string.

§ ReadByte()

bool Stream::ReadByte ( uint8_t &  byte)
virtual

Reads one byte from the input stream.

Returns
Returns the read byte.

§ ReadByteFromBuffer()

bool Stream::ReadByteFromBuffer ( uint8_t &  byte,
uint8_t *  buffer,
uint16_t &  start_index,
uint16_t &  stop_index,
uint16_t &  buffer_size,
bool &  empty 
)
protectedvirtual

Reads a byte from the given buffer.

Parameters
bufferThe buffer to read from.
start_indexThe first valid bit of the buffer.
stop_indexThe last valid bit of the buffer.
buffer_sizeThe size of the buffer.
emptyFlag indicating whether the buffer is empty or completely full.
Returns
The byte that was read.

§ ReadByteFromOutputStream()

bool Stream::ReadByteFromOutputStream ( uint8_t &  byte)
protectedvirtual

Reads a byte from the output stream.

Returns
Returns true if there are more bytes

§ ReadFromBuffer()

uint16_t Stream::ReadFromBuffer ( uint8_t *  buffer,
uint16_t &  start_index,
uint16_t &  stop_index,
uint16_t &  buffer_size,
bool &  empty,
uint8_t *  string,
uint16_t &  string_size 
)
protectedvirtual

Reads a string from the given buffer.

Parameters
bufferBuffer to read from.
start_indexThe first valid byte of the buffer.
stop_indexThe last valid byte of the buffer.
buffer_sizeThe size of the buffer.
emptyFlag indicating whether the buffer is empty or completely full.
stringThe string to read into.
string_sizeThe size of the string.

§ ReadFromOutputStream()

uint16_t Stream::ReadFromOutputStream ( uint8_t *  string,
uint16_t  size 
)
protectedvirtual

Reads data from the input stream into the specified string.

Parameters
stringString to store the read data in.
sizeSize of the string.

§ Write()

void Stream::Write ( uint8_t *  string,
uint16_t  size 
)
virtual

Writes the specified data to the output stream.

Parameters
stringInput data.
sizeSize of the input data string.

Reimplemented in Socket, and UART.

§ WriteByte()

void Stream::WriteByte ( uint8_t  byte)
virtual

Writes one byte to the output stream.

Parameters
byteThe byte to be written.

Reimplemented in Socket.

§ WriteByteToBuffer()

void Stream::WriteByteToBuffer ( uint8_t *  buffer,
uint16_t &  start_index,
uint16_t &  stop_index,
uint16_t &  buffer_size,
bool &  empty,
bool &  overflow_flag,
uint8_t &  byte,
void(*)(Stream *stream)  cb 
)
protectedvirtual

Writes a byte to the buffer.

Parameters
bufferThe buffer to write to.
start_indexThe first valid bit of the buffer.
stop_indexThe last valid bit of the buffer.
buffer_sizeThe size of the buffer.
emptyFlag indicating whether the buffer is empty or completely full.
overflow_flagA flag indicating whether or not the buffer has overflowed.
byteThe byte to be written.
cb_flagA flag indicating wether or not the cb function should be called
cbCallback function to be called if cb_flag is true

§ WriteByteToInputStream()

void Stream::WriteByteToInputStream ( uint8_t &  byte)
protectedvirtual

Writes a byte to the input stream.

Parameters
byteThe byte to be written.

§ WriteToBuffer()

void Stream::WriteToBuffer ( uint8_t *  buffer,
uint16_t &  start_index,
uint16_t &  stop_index,
uint16_t &  buffer_size,
bool &  empty,
bool &  overflow_flag,
uint8_t *  string,
uint16_t &  string_size,
void(*)(Stream *stream)  cb 
)
protectedvirtual

Writes a string to the given buffer.

Parameters
bufferBuffer to write to.
start_indexThe first valid byte of the buffer.
stop_indexThe last valid byte of the buffer.
buffer_sizeThe size of the buffer.
emptyFlag indicating whether the buffer is empty or completely full.
overflow_flagA flag indicating whether or not the buffer has overflowed.
stringThe string to read from.
string_sizeThe size of the string.
cb_flagA flag indicating wether or not the cb function should be called
cbCallback function to be called if cb_flag is true

§ WriteToInputStream()

void Stream::WriteToInputStream ( uint8_t *  string,
uint16_t  size 
)
protectedvirtual

Writes the specified data to the output stream.

Parameters
stringInput data.
sizeSize of the input data.

Member Data Documentation

§ input_buffer

uint8_t* Stream::input_buffer
protected

Buffer that stores the input stream data.

§ input_buffer_empty

bool Stream::input_buffer_empty = true
protected

Flag indicating whether the buffer is empty or full.

§ input_buffer_overflowed

bool Stream::input_buffer_overflowed = false
protected

Flag indicating whether the input buffer has overflowed or not. This is reset when read by CheckInputOverflowFlag().

§ input_buffer_size

uint16_t Stream::input_buffer_size
protected

The size of the input buffer.

§ input_buffer_start_index

uint16_t Stream::input_buffer_start_index = 0
protected

Indicates the first valid data byte in the buffer.

§ input_buffer_stop_index

uint16_t Stream::input_buffer_stop_index
protected

Indicates the last valid data byte in the buffer.

§ output_buffer

uint8_t* Stream::output_buffer
protected

Buffer that stores the output stream data.

§ output_buffer_empty

bool Stream::output_buffer_empty = true
protected

Flag indicating whether the buffer is empty or full.

§ output_buffer_overflowed

bool Stream::output_buffer_overflowed = false
protected

Flag indicating whether the output buffer has overflowed or not. This is reset when read by CheckOutputOverflowFlag().

§ output_buffer_size

uint16_t Stream::output_buffer_size
protected

The size of the output buffer.

§ output_buffer_start_index

uint16_t Stream::output_buffer_start_index = 0
protected

Indicates the first valid data byte in the buffer.

§ output_buffer_stop_index

uint16_t Stream::output_buffer_stop_index
protected

Indicates the last valid data byte in the buffer.


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