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();
}