SafeString  4.1.27
SafeString is a safe, robust and debuggable replacement for string processing in Arduino
BufferedOutput Class Reference

To create a BufferedOutput use the macro createBufferedOutput see the detailed description. More...

#include <BufferedOutput.h>

Inherits Stream.

Public Member Functions

 BufferedOutput (size_t _bufferSize, uint8_t *_buf, BufferedOutputMode=BLOCK_IF_FULL, bool allOrNothing=true)
 use createBufferedOutput(name, size, mode); instead BufferedOutput(size_t _bufferSize, uint8_t *_buf, BufferedOutputMode = BLOCK_IF_FULL, bool allOrNothing = true); More...
 
void connect (HardwareSerial &_serial)
 void connect(HardwareSerial& _serial); // the output to write to, can also read from serial – the HardwareSerial to buffer output to, usually Serial. More...
 
void connect (Stream &_stream, const uint32_t baudRate=0)
 void connect(Stream& _stream, const uint32_t baudRate); // the stream to write to and how fast to write output, can also read from stream – the stream to buffer output to baudRate – the maximum rate at which the bytes are to be released. More...
 
void nextByteOut ()
 
virtual size_t write (uint8_t)
 
virtual size_t write (const uint8_t *buf, size_t size)
 
virtual int available ()
 
virtual int read ()
 
virtual int peek ()
 
virtual void flush ()
 
virtual int availableForWrite ()
 
size_t getSize ()
 
int clearSpace (size_t len)
 
void protect ()
 
void clear ()
 
size_t terminateLastLine ()
 

Detailed Description

To create a BufferedOutput use the macro createBufferedOutput see the detailed description.

The createBufferedOutput macro takes 2, 3 or 4 arguments.

createBufferedOutput(name, size); creates a BufferedOutput called name which can buffer upto size chars without blocking and then will block once the buffer fills up.
This default blocking when the buffer is full is not recommended.

Add a call to
bufferedOutput.nextByteOut();
at the top of the loop() to release the buffered chars. You can add more of these calls through out the loop() code if needed.
Most BufferedOutput methods also release the buffered chars.

createBufferedOutput(name, size, mode ); creates a BufferedOutput called name which can buffer upto size chars without blocking and mode determines what to do when the buffer is full.
mode can be one of BLOCK_IF_FULL, DROP_UNTIL_EMPTY or DROP_IF_FULL
BLOCK_IF_FULL just blocks until some chars can be sent to the output stream, so freeing up space in the buffer to accept more output. This mode is not recommended, but can be used for testing to force ALL output to be sent.
DROP_UNTIL_EMPTY will drop further output until all the output currently in the full buffer is sent to the output stream.
DROP_IF_FULL will drop further output until enough chars have been sent to the output stream to free up space for the output printed to the buffer.

If any chars are dropped then ~~ is added to the output sent so you can see where there is missing output.

This 3 argument version uses the default AllOrNothing == true setting. If the whole print(..) cannot fit in the buffer none of it is sent.

createBufferedOutput(name, size, mode, allOrNothing ); creates a BufferedOutput called name which can buffer upto size chars without blocking and mode determines what to do when the buffer is full and how to handle prints that do not entirely fit in the buffer.
AllOrNothing true will drop the entire print( ) if it will not completely fit in the buffer.
AllOrNothing false will only drop the part of the print( ) that will not fit in the buffer.

See Arduino Serial I/O for the Real World - BufferedOutput for an example of its use.

Definition at line 78 of file BufferedOutput.h.

Constructor & Destructor Documentation

◆ BufferedOutput()

BufferedOutput::BufferedOutput ( size_t  _bufferSize,
uint8_t *  _buf,
BufferedOutputMode  = BLOCK_IF_FULL,
bool  allOrNothing = true 
)

use createBufferedOutput(name, size, mode); instead BufferedOutput(size_t _bufferSize, uint8_t *_buf, BufferedOutputMode = BLOCK_IF_FULL, bool allOrNothing = true);

buf – the user allocated buffer to store the bytes, must be at least bufferSize long. Defaults to an internal 8 char buffer if buf is omitted or NULL bufferSize – number of bytes to buffer,max bufferSize is limited to 32766. Defaults to an internal 8 char buffer if bufferSize is < 8 or is omitted mode – BLOCK_IF_FULL (default), DROP_UNTIL_EMPTY or DROP_IF_FULL BLOCK_IF_FULL, like normal print, but with a buffer. Use this to see ALL the output, but will block the loop() when the output buffer fills DROP_UNTIL_EMPTY, when the output buffer is full, drop any more chars until it completely empties. ~~<CR><NL> is inserted in the output to show chars were dropped. Useful when there too much output. It allow multiple prints to be output consecutively to give meaning full output avaliableForWrite() will return 0 from when the buffer fills until is empties DROP_IF_FULL, when the output buffer is full, drop any more chars until here is space. ~~<CR><NL> is inserted in the output to show chars were dropped. allOrNothing – defaults to true, If true AND output buffer not empty then if write(buf,size) will not all fit don't output any of it. Else if false OR output buffer is empty then write(buf,size) will output partial data to fill output buffer. allOrNothing setting is ignored if mode is BLOCK_IF_FULL

Member Function Documentation

◆ available()

virtual int BufferedOutput::available ( )
virtual

◆ availableForWrite()

virtual int BufferedOutput::availableForWrite ( )
virtual

◆ clear()

void BufferedOutput::clear ( )

◆ clearSpace()

int BufferedOutput::clearSpace ( size_t  len)

◆ connect() [1/2]

void BufferedOutput::connect ( HardwareSerial &  _serial)

void connect(HardwareSerial& _serial); // the output to write to, can also read from serial – the HardwareSerial to buffer output to, usually Serial.

You must call nextByteOut() each loop() in order to release the buffered chars.

◆ connect() [2/2]

void BufferedOutput::connect ( Stream &  _stream,
const uint32_t  baudRate = 0 
)

void connect(Stream& _stream, const uint32_t baudRate); // the stream to write to and how fast to write output, can also read from stream – the stream to buffer output to baudRate – the maximum rate at which the bytes are to be released.

Bytes will be relased slower depending on how long your loop() method takes to execute You must call nextByteOut() each loop() in order to release the buffered chars.

◆ flush()

virtual void BufferedOutput::flush ( )
virtual

◆ getSize()

size_t BufferedOutput::getSize ( )

◆ nextByteOut()

void BufferedOutput::nextByteOut ( )

◆ peek()

virtual int BufferedOutput::peek ( )
virtual

◆ protect()

void BufferedOutput::protect ( )

◆ read()

virtual int BufferedOutput::read ( )
virtual

◆ terminateLastLine()

size_t BufferedOutput::terminateLastLine ( )

◆ write() [1/2]

virtual size_t BufferedOutput::write ( const uint8_t *  buf,
size_t  size 
)
virtual

◆ write() [2/2]

virtual size_t BufferedOutput::write ( uint8_t  )
virtual