From ec1d674da7182fd77894e32dd9a15225be606251 Mon Sep 17 00:00:00 2001 From: Neil McKechnie Date: Thu, 18 Feb 2021 16:00:00 +0000 Subject: [PATCH] Enhance OLED/LCD speed Write one character or position command per loop entry so as not to hold up the loop. Add support for SH1106 OLED as 132x64 size option. --- LCDDisplay.cpp | 100 +++++++++++++++++++++++++++++++++-------------- LCDDisplay.h | 4 +- LCD_NONE.h | 2 +- LCD_OLED.h | 21 +++++++--- config.example.h | 1 + 5 files changed, 91 insertions(+), 37 deletions(-) diff --git a/LCDDisplay.cpp b/LCDDisplay.cpp index e031f10..ae5e245 100644 --- a/LCDDisplay.cpp +++ b/LCDDisplay.cpp @@ -43,37 +43,79 @@ size_t LCDDisplay::write(uint8_t b) { if (!lcdDisplay) return; lcdDisplay->loop2(false); } - - LCDDisplay* LCDDisplay::loop2(bool force) { - if ((!force) && (millis() - lastScrollTime)< LCD_SCROLL_TIME) return NULL; - lastScrollTime=millis(); - clearNative(); - int rowFirst=nextFilledRow(); - if (rowFirst<0)return NULL; // No filled rows - setRowNative(0); - writeNative(rowBuffer[rowFirst]); - for (int slot=1;slot= MAX_LCD_COLS) { + // Screen slot completed, move to next one + slot++; + bptr = 0; + if (!done) { + rowNext = (rowNext + 1) % MAX_LCD_ROWS; + if (rowNext == rowFirst) done = true; + } + } + + if (slot >= lcdRows) { + // Last slot done, reset ready for next update. + slot = 0; + done = false; + rowFirst = -1; + lastScrollTime = currentMillis; + return NULL; + } } - return -1; // No slots filled - } + } while (force); + + return NULL; +} diff --git a/LCDDisplay.h b/LCDDisplay.h index 9abcca3..5735cdc 100644 --- a/LCDDisplay.h +++ b/LCDDisplay.h @@ -49,13 +49,15 @@ class LCDDisplay : public Print { void clearNative(); void displayNative(); void setRowNative(byte line); - void writeNative(char * b); + void writeNative(char b); unsigned long lastScrollTime=0; int hotRow=0; int hotCol=0; int topRow=0; int lcdRows; + int lcdCols; + int slot=0; void renderRow(byte row); char rowBuffer[MAX_LCD_ROWS][MAX_LCD_COLS+1]; }; diff --git a/LCD_NONE.h b/LCD_NONE.h index 65e05e4..ddaac48 100644 --- a/LCD_NONE.h +++ b/LCD_NONE.h @@ -22,6 +22,6 @@ void LCDDisplay::interfake(int p1, int p2, int p3) {(void)p1; (void)p2; (void)p3;} void LCDDisplay::setRowNative(byte row) { (void)row;} void LCDDisplay::clearNative() {} - void LCDDisplay::writeNative(char * b){ (void)b;} // + void LCDDisplay::writeNative(char b){ (void)b;} // void LCDDisplay::displayNative(){} diff --git a/LCD_OLED.h b/LCD_OLED.h index a13a1cb..2a99aea 100644 --- a/LCD_OLED.h +++ b/LCD_OLED.h @@ -32,14 +32,21 @@ SSD1306AsciiWire LCDDriver; Wire.begin(); for (byte address=0x3c; address<=0x3d; address++) { Wire.beginTransmission(address); - byte error = Wire.endTransmission(); + byte error = Wire.endTransmission(true); if (!error) { // Device found DIAG(F("\nOLED display found at 0x%x"), address); interfake(OLED_DRIVER,0); - LCDDriver.begin(lcdRows==32 ? &Adafruit128x32 : &Adafruit128x64, address); + const DevType *devType; + if (lcdCols == 132) + devType = &SH1106_128x64; // Actually 132x64 but treated as 128x64 + else if (lcdCols == 128 && lcdRows == 8) + devType = &Adafruit128x64; + else + devType = &Adafruit128x32; + LCDDriver.begin(devType, address); lcdDisplay=this; - LCDDriver.setFont(System5x7); // Normal 1:1 pixel scale + LCDDriver.setFont(System5x7); // Normal 1:1 pixel scale, 8 bits high clear(); return; } @@ -47,7 +54,7 @@ SSD1306AsciiWire LCDDriver; DIAG(F("\nOLED display not found\n")); } - void LCDDisplay::interfake(int p1, int p2, int p3) {(void)p1; lcdRows=p2/8; (void)p3;} + void LCDDisplay::interfake(int p1, int p2, int p3) {lcdCols=p1; lcdRows=p2/8; (void)p3;} void LCDDisplay::clearNative() {LCDDriver.clear();} @@ -55,10 +62,12 @@ SSD1306AsciiWire LCDDriver; // Positions text write to start of row 1..n and clears previous text int y=row; LCDDriver.setCursor(0, y); - LCDDriver.clearToEOL(); + //LCDDriver.clearToEOL(); } - void LCDDisplay::writeNative(char * b){ LCDDriver.write(b); } + void LCDDisplay::writeNative(char b) { + LCDDriver.write(b); + } void LCDDisplay::displayNative() { } diff --git a/config.example.h b/config.example.h index a20e6bb..a191bfc 100644 --- a/config.example.h +++ b/config.example.h @@ -102,6 +102,7 @@ The configuration file for DCC-EX Command Station //OR define OLED_DRIVER width,height in pixels (address auto detected) // 128x32 or 128x64 I2C SSD1306-based devices are supported. +// Also 132x64 I2C SH1106 devices. // This will not work on a UNO due to memory constraints // #define OLED_DRIVER 128,32