Noiasca Buffer Print
Print to an internal buffer
The Noiasca Buffer Print Library

Introduction

The Noiasca Buffer Print is a library very similar to Print.h but will buffer outputs until flush is called. Despite other libraries, this class calls flush at the end of each print call. The objective is to have an replacement for the Print.h which collects data and sends data to an interface not bytewise with single writes but as one stream with several bytes. Like the Print.h library NoiascaBufferPrint.h is not a stand alone library, but can be used with other classes.

How Noiasca Buffer Print work

All print functions call the function .add instead of .write

add() adds the value to the internal buffer. add() doesn't output - in other words add() doesn't call write().

It is not needed that add() is implemented in the derived class

flush() empties the buffer and streams to your class flush() needs to be implemented in the derived class

write() exists similar to the Print.h library. write() is used for the bytewise output to your class. write() doesn't use the internal buffer - but you could implement that in your class. write() needs to be implemented in the derived class (like with Print.h).

print() and println() have now an additional boolean parameter autoflush (default true). By default print() and println() will flush the buffer as last statement. If you want several print()/println() in the buffer, just add false. In this case - don't forget to flush().

Some notes on the library behaviour

There should be no need for an explicit flush() in your local write function because each print ends with a flush(). But you could implement a trailing flush() in your write().

If write() should be buffered also - implement it in the derived class. In this case don't forget to flush after write() also.

Currently this library doesn't implement "printable" interface.

How to use Noiasca Buffer Print for your own classes

When you define your class, don't inherit from Print.h but from NoiascaBufferPrint.h. You will need to implement two member functions in your class:

  • write() will be used for a single byte transmission. It is just a pass through from NoiascaBufferPrint to your class. It will look like the write() you would have used with Print.h
  • flush() will be called to transmit a stream after the end of transmission. You can access the internal buffer with the member variable internal[].
    I suggest to reset the internal buffer with resetBuffer() at last line of code of your flush() function.

The example 01_hello_world shows a simple sketch using a buffered print to the serial interface.

Installation

Download the library

The newest version of this library can be downloaded from https://werner.rothschopf.net/microcontroller/202111_arduino_bufferprint.htm .

Install the library

In the Arduino IDE use the Menu
Sketch / Include Library / Add .ZIP Library
to install the library.