Library to print to LCD "in the backround" using a buffer

Hi everybody,

I did some qoogling trying to find a library that does printing to LC-Displays "in the background" based on a buffer.

It should work similar to how old fashioned home-computers showed characters on the screen: the program writes to a buffer and some other logic reads the buffer-content and creates the characters on the screen.

I think this is what happends in the LCD itself but I also want it on the microcontroller.

The purpose for this is to create scrolling text that works this way:

write a text with more characters than the display can show at once into the buffer
and if character-sequence written to the buffer is longer than number of characters of one display-line scroll the longer char-sequence but do it in the background while the main-code can do different things.

I guess doing this "in the background" will require an interrupt.
I guess doing it this way for different LCDs is quite challenging as a I²C-interface needs an interrupt for transferring the data two.

Calling "print" inside of an interrupt-routine might block sending the data.
Or is there a lower / higher priority between interrupts that would - eventually - enable to do it?

I know one solution would be to keep the code non-blocking and fast-looping and call the scrolling-function and updating the display regularly for the price of loop beeing no longer as fast as possible due to the permant sending data to the display.

Maybe using the tiniest microcontroller available connected over a serial interface used as the LCD's intelligence would be a solution too.

But does anybody see a chance to realise a buffered LCD in pure software?
(by the way: for an 8-core propeller-chip-microcontroller this would be a "piece of cake" even for three LC-displays)

Be the change you want to see in the world
best regards Stefan

Read up on millis () timing, and redesign your code to do what you want - when you’re ready… not when the delays and time consuming blocks of code are ‘free’

Using millis set your code up as multitasking and it should work just fine. Remember no blocking code or delay() is allowed.

How fast would you want to the scrolling to happen? If you slow it to human-readable, one might have lots of time.

Updating a single random-access character every 2ms in this sketch leaves 325 loops/sec and is still too fast to track.

Not necessarily. Something like this should work if your loop() runs fairly quickly.

String LCDString;
unsigned long LastLCDTime;
const unsigned long LCDCharacterInterval = 200;

void loop()
{
  if (LCDString.length() > 0 && millis() - LastLCDTime > LCDCharacterInterval)
  {
    LastLCDTime = millis();
    lcd.setCursor(0, 1);
    lcd.print(LCDString.substring(0,16);
    LCDString = LCDString.substring(1);
 }

To 'print':
LCDString += " More text to display.";

the common LCDs have an internal RAM. If you can accept how the hardware "shift left" is working, you could do a call, either bei calling a function with millis timer and/or triggered by yield if you have other blocking code with lot of delay().

#include <Wire.h>
#include <NoiascaLiquidCrystal.h>      // download library from https://werner.rothschopf.net/202003_arduino_liquid_crystal_umlaute.htm  
#include <NoiascaHW/lcd_PCF8574.h>     // include the proper IO interface

const byte cols = 16;                  // columns/characters per row
const byte rows = 2;                   // how many rows
const byte addr = 0x3F;                // most used LCD address is 0x3F or 0x27

LiquidCrystal_PCF8574 lcd(addr, cols, rows);     // create lcd object - with support of special characters

void setup()
{
  Wire.begin();
  lcd.init();                         // .begin()/.init() initialize the lcd
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print(F("Hello, world!"));
  lcd.setCursor(0, 1);
  lcd.print(F("123456789ä123456789ö123456789ü123456789ß"));   // the LCD memory can store up to 40 characters per line
}

// this function will call the scrollDisplayLeft method in a specific period
void tickScroll()
{
  static uint32_t previousMillis = 0;
  const uint16_t interval = 500;
  if (millis() - previousMillis > interval)
  {
    lcd.scrollDisplayLeft();
    previousMillis = millis();
  }
}

void yield()
{
  tickScroll();
}

void loop()
{
  lcd.setCursor(0,0);
  lcd.print(millis()/1000);
  // tickScroll(); // call the shift left routine manually
  yield();         // used if no other code is blocking 
  delay(1000);     // dirty delay - simulate other blocking code
}

to be demonstrated: "scrolling" works even if you block the code

if you don't like the LCD internal shift left, you could implement your own "scrolling" based on an internal buffer and process the scrolling similar millis() function / called by yield to handle the scrolling "in the background".

// https://forum.arduino.cc/t/library-to-print-to-lcd-in-the-backround-using-a-buffer/981749/5
// LCD print "in background"

#include <Wire.h>
#include <NoiascaLiquidCrystal.h>      // download library from https://werner.rothschopf.net/202003_arduino_liquid_crystal_umlaute.htm  
#include <NoiascaHW/lcd_PCF8574.h>     // include the proper IO interface

const byte cols = 16;                  // columns/characters per row
const byte rows = 2;                   // how many rows
const byte addr = 0x3F;                // most used LCD address is 0x3F or 0x27

LiquidCrystal_PCF8574 lcd(addr, cols, rows);     // create lcd object - with support of special characters

constexpr byte buffersize = 40;
char line[rows][buffersize];

void setup()
{
  Wire.begin();
  Serial.begin(115200);
  lcd.init();                         // .begin()/.init() initialize the lcd
  lcd.backlight();

  strcpy(line[1], "123456789A123456789B");
}

// quick'n'dirty "print" to buffer
void newText(const byte currentRow, const char newtext[buffersize])
{
  lcd.setCursor(0, currentRow);
  for (byte i = 0; i < cols; i++) lcd.write(' '); // clear line
  if (strlen(newtext) <= cols)
  {
    //Serial.println("quick print");
    lcd.setCursor(0, currentRow);
    lcd.print(newtext);
  }
  else
  {
    //Serial.println("copy to buffer");
    strcpy(line[currentRow], newtext);
  }
}

// this function will scroll out buffered text
void tickScroll(const byte currentRow)
{
  static uint32_t previousMillis = 0;
  const uint16_t interval = 500;
  if (strlen(line[currentRow]) >= cols && millis() - previousMillis > interval)
  {
    lcd.setCursor(0, currentRow);
    previousMillis = millis();
    lcd.print(line[currentRow]);
    //Serial.println(line[currentRow]);
    char temp[buffersize];
    strcpy(temp, line[currentRow] + 1);
    strcpy(line[currentRow], temp);
  }
}

void yield()
{
  tickScroll(1);
}

void doSimulateUseractions()
{
  static byte state = 0;
  if (state == 0 && millis() > 10000)
  {
    newText(1, "short text");
    state++;
  }
  if (state == 1 && millis() > 20000)
  {
    newText(1, "longer text than 16 chars");
    state++;
  }
  if (state == 2 && millis() > 30000)
  {
    newText(1, "123456789a123456789b123456789c12");
    state++;
  }  
}

void loop()
{
  lcd.setCursor(0, 0);
  lcd.print(millis() / 1000);
  // tickScroll(1);            // call the shift left routine manually
  yield();                     // used if no other code is blocking
  delay(1000);                 // dirty delay - simulate other blocking code
  doSimulateUseractions();
}

to be demonstrated: "self made scrolling" works on a single line

I know, not a very sophisticated "print" function, but more convenient functions can be done with one of the existing streaming libraries anyway

Hello to everybody,

thank you very much for your ideas and code-examples.
@noiasca If I remember right a PCF8574 is an I²C-IO-expander?

This means it uses I²C. I have yet never look into the wire-library how I²C is working inside the wire-library. I assume it uses an interrupt for the answers that come in from the I²C-device towards the microcontroller.

real backround-operation, that would go on scrolling - even while a delay() is executed - as far as I know it - this would require a timer-interrupt to invoke the transferring of the characters to show next on the display.

Could a timer-interrupt be used to do the data-transmission to the display?

best regards Stefan

The PCF8574 is the i2c expander used on lot of "LCD I2C backpacks". If you don't have one available, the codes will work with a common parallel LCD also.

try the code examples. Both codes WILL CONTINUE SCROLLING even during the 1000 delay - therefore I made that examples with a blocking delay to show off the functionality of the background activity.
In my understanding this is the simulation of "background operation" - at least as far as this is possible on a single core system.

yes you could use a timer interrupt to "trigger"/ set a flag in loop and output depending on that flag. But I would not communicate with the LCD within a timer interrupt/ISR.

The "problem" with all this background activities I see is, that each activity will take time. If you have to print 16 characters it will depend on the used library, how long that will take. The fastest LCD communication might be parallel 8 bit mode with direct port access without using Arduino digitalWrite. On the other end there is the "default" I2C LCD Library which will take nearly 24 milliseconds for 16 characters. So you might consider to define: "what's the maximum time a background activity might last".

Hi @noiasca ,
I'm speechless huh ? What ?
I can define a yield() myself and yield() is called even if a delay(10000) is executed?

There must be some more magic in the backround to make this possible.
I used to comment delay(); // with the comment "block any code-execution until delay has finished."

But this is !true for yield()

In none of the postings I have read so far I have read something about a user-defined yield();

Edit: I started googling and found this

This explains how it works.
Wow ! Cool ! very interesting - and I guess - has to be used with care

yield() is defined as a "weak" function. Where "weak" means the function can be re-defined to the users need. Originally intended to be used in libraries.

Quotation from the website

> However,for AVR based Arduino board this function is not used except for one place; inside the delay() function in wiring.c source file .

void delay(unsigned long ms)
{
    uint32_t start = micros();
    while (ms > 0) {
        yield();
        while ( ms > 0 && (micros() - start) >= 1000) {
            ms--;
            start += 1000;
        }
    }
}

again quoting

> A useful usage of yield() is to be redefined in the main application code with some sensitive operation while the Arduino makes nothing except for waiting the delay to end, or even implementing some simple scheduler.

question

Does this mean:
as long as the operations executed in yield are shorter than a millisecond this works the same way as without re-defining yield??

If the execution of the code inside yield() takes longer than a millisecond it will influence the delay()?

Be the change you want to see in the world
best regards Stefan

Which IMO is a perfectly acceptable solution, but YMMV as they say. I did exactly this in an application where I have a 20x04 LCD on which I wanted to display content from a more or less arbitrarily sized buffer because I had some pages of system settings that I wanted to scroll through easily without having to worry too much about where to print what, how it would display etc. So I basically wrote a wrapper class for the display that contained the following main elements:

  • A display buffer of desired cols & lines; I think I tested with something like 20 lines and 80 cols, but you could take it as far as you have memory available. I used a double-buffered approach with the 'virtual screen' buffering being the big one, and a smaller buffer of the same dimensions of as the LCD (20x04 in my example).
  • Function to set the display window, i.e. a function that determined which part of the buffer would be displayed
  • Functions to scroll left, right, up, down
  • Even a set of functions that defined a window-within-the-window so that for instance the first row and column would be locked while the rest of the display cour scroll (analogous to the MS Excel 'split panes' function)
  • An 'update()' function that compared the contents of the viewable window in the virtual buffer to the LCD-sized buffer, which updated the contents of the latter pretty much immediately and relayed the LCD-sized buffer to the actual display periodically. I think I went for every 50ms or so, which is more than enough to create a smooth scrolling experience.
  • User-callable functions that mimic regular LCD functions; so the object accepts regular functions like 'setCursor' and 'print' (by extending Print) making it act as if you're writing directly to an LCD, but in fact the buffer class is between it.

In reality I actually split the above into two classes; one handling the big, scrollable virtual buffer thing and that extended a class that did the updating of the actual display based on its smaller buffer. But for the principle it doesn't matter if it's one or two classes. (Actually the second class also handled some misc functions such as blinking characters and wrapping text).

I never released the code because I never bother finishing it to perfection or documenting it sufficiently for other people to use it, but the above description pretty much captures it. No magic was done anywhere; it's just very basic array functions and millis()-based timings where necessary. I think I wrote this in a day or two, maybe three. It's not rocket science.

Cool, I should look into that.
When I'm done with my STM8S003F3 project that is...I have to live with plain C and a nitpicky Cosmic compiler instead of C++ for a while.

If you're using an ESP32-based "Arduino" board, you could create a separate FreeRTOS task to handle the scrolling.

I assume micros are not effected and therefore millis() will still work because they are based on micros(). But you might do your own tests.

Back to this buffer and I2C ...
Some time ago I have written a faster way to communicate with an I2C LCD. Basic idea is, not to send each character in a single communication to the LCD, but to collect data in a buffer and send the data with one I2C communication. This will bring down the printout time for 16 characters from 11ms to 7ms.

Basically I'm using a knock off of the print class. Each "write" will be stored in an internal buffer.
All collected data will be "flushed" as stream at the and of the print.
see here: Arduino: Noiasca Buffer Print Library
"NoiascaBufferPrint" is a stand alone class and can be used for a LCD also. This was done in LiquidCrystal_PCF8574_fast.

Now, when we have already a buffer class in use, you could use this class also for the scrolling buffer. The concept might get a little complex now:
You define a buffer based on NoiascaBufferPrint for a particular line on your LCD
When you "print" to this lineBuffer, it will be stored in the internal buffer of the object.
The update() function will hand over the 16 characters (= columns of the LCD) to the LCD and shift out the left byte. If there are only 16 characters left in the internal buffer all remaining characters will be printed to the LCD.

You can either call lineBuffer.update() in loop, or call yield, or use unused time in delay().

All format options of print.h are available. char, String, integers, float, F-Makro ... everything should work according to the print.h.

You just have to care about that you have to print to the buffer instead of the LCD directly.

This sketch works on my test box.
It might break, if you add delays between your printout on row 0 - because this might lead to an update of the scrolling on line 1 and therefore the next print (considered for row0) might end on row 1.

// https://forum.arduino.cc/t/library-to-print-to-lcd-in-the-backround-using-a-buffer/981749/5
// LCD print "in background" using buffer print

const byte cols = 16;                  // columns/characters per row
const byte rows = 2;                   // how many rows
const byte addr = 0x3F;                // common LCD address are 0x3F or 0x27

#include <NoiascaBufferPrint.h>                // download library from http://werner.rothschopf.net/microcontroller/202111_arduino_bufferprint.htm
#include <Wire.h>
#include <NoiascaLiquidCrystal.h>              // download library from http://werner.rothschopf.net/202009_arduino_liquid_crystal_i2c_en.htm 
//#include <NoiascaHW/lcd_PCF8574.h>           // include the proper IO interface
//LiquidCrystal_PCF8574 lcd(addr, cols, rows); // create lcd object - with support of special characters
#include <NoiascaHW/lcd_PCF8574_fast.h>
LiquidCrystal_PCF8574_fast lcd(addr, cols, rows);

// we need an interim buffer
// which receives characters into a buffer if called with a "print"
// and prints a maximum of 16 characters if flushed
class MyBuffer : public NoiascaBufferPrint
{
  protected:
    const byte currentRow = 1;
    uint32_t previousMillis;    // time managment scroll

  public:
    MyBuffer (byte currentRow) : currentRow{currentRow} {}
    
    size_t write (uint8_t value)
    {
      return lcd.write(value);
    }

    // "scroll" if internal buffer is larger than colls and remove one char from internal buffer
    // "print" if internal buffer is equal or less then colls and delete buffer
    void update()
    {
      const uint16_t interval = 500;
      uint32_t currentMillis = millis();
      if (currentMillis - previousMillis > interval)
      {
        previousMillis = currentMillis;
        //Serial.print(getBufferLength()); Serial.write(' ');
        //Serial.print(strlen(internal)); Serial.write(' ');
        //Serial.println(internal);
        if (strlen(internal) > cols)
        {
          lcd.setCursor(0, currentRow);
          char temp[buffersize];
          strncpy(temp, internal, cols);
          temp[cols] = '\0';
          lcd.print(temp);
          //Serial.println(temp);
          setBufferLength(strlen(internal) - 1);
          memmove(internal, internal + 1, strlen(internal)); // cut one char
        }
        else if (strlen(internal) > 0)
        {
          lcd.setCursor(0, currentRow);
          lcd.print(internal);
          //Serial.println(internal);
          memcpy(internal, "", buffersize);  // delete any remaining chars
          setBufferLength(0);
        }
      }
    }
    void flush() {} // empty implementation - does nothing - but needed as it will be called after each print
};

MyBuffer lineBuffer(1);  // a buffer for line 1 on LCD

void deleteLine(const byte currentRow)
{
  lcd.setCursor(0, currentRow);
  for (byte i = 0; i < cols; i++) lcd.print(' '); // clear line
  lcd.setCursor(0, currentRow);
}

void setup()
{
  Wire.begin();
  Serial.begin(115200);
  lcd.init();                         // .begin()/.init() initialize the lcd
  lcd.backlight();

  lineBuffer.print("123456789A123456789B123456789C"); // print something to the internal buffer
}

void yield()
{
  lineBuffer.update();
}

void doSimulateUseractions()
{
  static uint32_t previousMillis;
  static byte state = 0;
  if (state == 0 && millis() > 10000)
  {
    deleteLine(1);
    lineBuffer.print("short text");
    state++;
  }
  else if (state == 1 && millis() > 20000)
  {
    deleteLine(1);
    lineBuffer.print(F("longer text than 16 chars"));
    state++;
  }
  else if (state == 2 && millis() > 30000)
  {
    previousMillis = millis();
    deleteLine(1);
    float demo = 12.3456;
    lineBuffer.print(demo, 3);
    state++;
  }
  else if (state >= 3 && millis() - previousMillis > 10000)
  {
    previousMillis = millis();

    lineBuffer.print(F(" seconds passed: "));
    lineBuffer.print(previousMillis / 1000);    // you can use any print you are used to
  }
}

void loop()
{
  lcd.setCursor(0, 0);
  lcd.print(millis() / 1000);  // print something on the first row
  lineBuffer.update();
  yield();                     // used if no other code is blocking
  delay(1000);                 // dirty delay - simulate other blocking code
  doSimulateUseractions();
}