UniGraphic-Fork for ST7920-LCD-controller and SH1106. Tested with 128x64 LCD with SPI and 128x64-OLED with IIC

Dependents:   UniGraphic-St7920-Test AfficheurUTILECO

Fork of UniGraphic by GraphicsDisplay

Fork of the UniGraphic-Library for monochrome LCDs with ST7920 controller and 128x64-IIC-OLED-Display with SH1106-Controller

/media/uploads/charly/20170522_210344.jpg

/media/uploads/charly/20180425_230623.jpg

Had to adapt LCD for following reasons:

  • Give access to screenbuffer buffer[] to parent class
  • pixel() and pixel_read() as they are hardware-dependent
  • added reset-pin to IIC-Interface

GraphicDisplay:: sends buffer to LCD when auto_update is set to true.

Testprogram for ST7920 can be found here:

https://developer.mbed.org/users/charly/code/UniGraphic-St7920-Test/

Files at this revision

API Documentation at this revision

Comitter:
Geremia
Date:
Fri Feb 20 21:32:25 2015 +0000
Parent:
10:668cf78ff93a
Child:
12:9c8f3076347c
Commit message:
added auto_gram_read_format() to TFt inits. Even if write is set to 16bit RGB color, for some controllers the read cmd outputs 18bit BGR. Now that function will autodetect and set internal flags accordingly, so pixelread() is always correct.

Changed in this revision

Display/TFT.cpp Show annotated file Show diff for this revision Revisions of this file
Display/TFT.h Show annotated file Show diff for this revision Revisions of this file
Inits/ILI9341.cpp Show annotated file Show diff for this revision Revisions of this file
Inits/ILI9486.cpp Show annotated file Show diff for this revision Revisions of this file
Inits/TFT_MIPI.cpp Show annotated file Show diff for this revision Revisions of this file
Protocols/PAR16.cpp Show annotated file Show diff for this revision Revisions of this file
Protocols/PAR16.h Show annotated file Show diff for this revision Revisions of this file
Protocols/PAR8.cpp Show annotated file Show diff for this revision Revisions of this file
Protocols/PAR8.h Show annotated file Show diff for this revision Revisions of this file
Protocols/Protocols.h Show annotated file Show diff for this revision Revisions of this file
Protocols/SPI16.cpp Show annotated file Show diff for this revision Revisions of this file
Protocols/SPI16.h Show annotated file Show diff for this revision Revisions of this file
Protocols/SPI8.cpp Show annotated file Show diff for this revision Revisions of this file
Protocols/SPI8.h Show annotated file Show diff for this revision Revisions of this file
--- a/Display/TFT.cpp	Thu Feb 19 00:33:27 2015 +0000
+++ b/Display/TFT.cpp	Fri Feb 20 21:32:25 2015 +0000
@@ -18,7 +18,7 @@
 
 #include "TFT.h"
 
-//#include "mbed_debug.h"
+#include "mbed_debug.h"
 
 #define SWAP(a, b)  { a ^= b; b ^= a; a ^= b; }
 
@@ -38,6 +38,8 @@
     scrollareasize=0;
     usefastwindow=false;
     fastwindowready=false;
+    is18bit=false;
+    isBGR=false;
   //  cls();
   //  locate(0,0);
 }
@@ -64,6 +66,8 @@
     scrollareasize=0;
     usefastwindow=false;
     fastwindowready=false;
+    is18bit=false;
+    isBGR=false;
   //  locate(0,0);
 }
 void TFT::wr_cmd8(unsigned char cmd)
@@ -93,7 +97,7 @@
     }
 unsigned short TFT::rd_gram()
     {
-        return proto->rd_gram();
+        return proto->rd_gram(is18bit); // protocol will handle 18to16 bit conversion
     }
 unsigned int TFT::rd_reg_data32(unsigned char reg)
     {
@@ -237,7 +241,7 @@
     unsigned short color;
   //  proto->wr_gram(color);   // 2C expects 16bit parameters
     color = rd_gram();
-    if(mipistd) color = BGR2RGB(color); // in case, convert BGR to RGB (should depend on cmd36 bit3) but maybe is device specific
+    if(isBGR) color = BGR2RGB(color); // in case, convert BGR to RGB (should depend on cmd36 bit3) but maybe is device specific
     return color;
 }
 void TFT::setscrollarea (int startY, int areasize) // ie 0,480 for whole screen
@@ -268,6 +272,27 @@
   //  proto->wr_gram(0,screensize_X*screensize_Y);
     wr_gram(_background,screensize_X*screensize_Y);
 }
+// try to get read gram pixel format, could be 16bit or 18bit, RGB or BGR
+void TFT::auto_gram_read_format()
+{
+    unsigned short px=0xCDB1;
+    unsigned short rback, rback18;
+    pixel(0,0,px);
+    window4read(0,0,1,1);
+    rback=proto->rd_gram(0); // try 16bit
+    window4read(0,0,1,1);
+    rback18=proto->rd_gram(1); // try 18bit converted to 16
+    if((rback18==px) || (BGR2RGB(rback18)==px))
+    {
+        is18bit=true;
+        if(BGR2RGB(rback18)==px) isBGR=true;
+    }
+    else if((rback==px) || (BGR2RGB(rback)==px))
+    {
+        if(BGR2RGB(rback)==px) isBGR=true;
+    }
+ //   else debug("\r\nfail to identify gram read color format,\r\nsent %.4X read16 %.4X read18 %.4X", px, rback, rback18);    
+}
 // try to identify display controller
 void TFT::identify()
 {
--- a/Display/TFT.h	Thu Feb 19 00:33:27 2015 +0000
+++ b/Display/TFT.h	Fri Feb 20 21:32:25 2015 +0000
@@ -208,6 +208,7 @@
     
     /** Read 16bit pixeldata from display controller (with dummy cycle)
     *
+    * @note autoconverts 18to16bit based on display identify info
     * @returns 16bit color
     */ 
     virtual unsigned short rd_gram();
@@ -231,13 +232,18 @@
     */
     void hw_reset();
     
+    /** Try to get read gram pixel format, could be 16bit or 18bit, RGB or BGR
+    * autoset internal flags so pixelread() will always return correct value.
+    */
+    virtual void auto_gram_read_format();
+    
     /** Try to identify display ID
     * @note support ILI9341,94xx, MIPI standard. May be be overridden in Init class for other specific IC
     */
     virtual void identify();
     
     unsigned int scrollbugfix;
-    bool mipistd;
+    
     
     
 private:
@@ -259,6 +265,9 @@
     bool useNOP;
     bool usefastwindow;
     bool fastwindowready;
+    bool mipistd;
+    bool is18bit;
+    bool isBGR;
     
 };
 
--- a/Inits/ILI9341.cpp	Thu Feb 19 00:33:27 2015 +0000
+++ b/Inits/ILI9341.cpp	Fri Feb 20 21:32:25 2015 +0000
@@ -23,6 +23,7 @@
     BusEnable(true);
     identify(); // will collect tftID and set mipistd flag
     init();
+    auto_gram_read_format();
     set_orientation(0);
     cls();
     FastWindow(true); // most but not all controllers support this, even if datasheet tells they should. 
@@ -35,6 +36,7 @@
     BusEnable(true); //TFT class forwards to Protocol class
     identify(); // will collect tftID and set mipistd flag
     init(); // per display custom init cmd sequence, implemented here
+    auto_gram_read_format();// try to get read gram pixel format, could be 16bit or 18bit, RGB or BGR. Will set flags accordingly
     set_orientation(0); //TFT class does for MIPI standard and some ILIxxx
     FastWindow(true); // most but not all controllers support this, even if datasheet tells they should. 
     cls();
--- a/Inits/ILI9486.cpp	Thu Feb 19 00:33:27 2015 +0000
+++ b/Inits/ILI9486.cpp	Fri Feb 20 21:32:25 2015 +0000
@@ -22,6 +22,7 @@
     BusEnable(true);
     identify(); // will collect tftID and set mipistd flag
     init();
+    auto_gram_read_format();// try to get read gram pixel format, could be 16bit or 18bit, RGB or BGR. Will set flags accordingly
     set_orientation(0);
  //   FastWindow(true); // most but not all controllers support this, even if datasheet tells they should. ILI9486 does not, at least in par mode
     cls();
@@ -34,6 +35,7 @@
     BusEnable(true); //TFT class forwards to Protocol class
     identify(); // will collect tftID and set mipistd flag
     init(); // per display custom init cmd sequence, implemented here
+    auto_gram_read_format();// try to get read gram pixel format, could be 16bit or 18bit, RGB or BGR. Will set flags accordingly
     set_orientation(0); //TFT class does for MIPI standard and some ILIxxx
  //   FastWindow(true); // most but not all controllers support this, even if datasheet tells they should. ILI9486 does not, at least in par mode
     cls();
--- a/Inits/TFT_MIPI.cpp	Thu Feb 19 00:33:27 2015 +0000
+++ b/Inits/TFT_MIPI.cpp	Fri Feb 20 21:32:25 2015 +0000
@@ -22,6 +22,7 @@
     BusEnable(true);
     identify(); // will collect tftID, set mipistd flag
     init();
+    auto_gram_read_format();// try to get read gram pixel format, could be 16bit or 18bit, RGB or BGR. Will set flags accordingly
 //    scrollbugfix=1; // when scrolling 1 line, the last line disappears, set to 1 to fix it, for ili9481 is set automatically in identify()
     set_orientation(0);
  //   FastWindow(true); // most but not all controllers support this, even if datasheet tells they should. Give a try
@@ -35,6 +36,7 @@
     BusEnable(true); //TFT class forwards to Protocol class
     identify(); // will collect tftID and set mipistd flag
     init(); // per display custom init cmd sequence, implemented here
+    auto_gram_read_format();// try to get read gram pixel format, could be 16bit or 18bit, RGB or BGR. Will set flags accordingly
  //   scrollbugfix=1; // when scrolling 1 line, the last line disappears, set to 1 to fix it, for ili9481 is set automatically in identify()
     set_orientation(0); //TFT class does for MIPI standard and some ILIxxx
  //   FastWindow(true); // most but not all controllers support this, even if datasheet tells they should. Give a try
--- a/Protocols/PAR16.cpp	Thu Feb 19 00:33:27 2015 +0000
+++ b/Protocols/PAR16.cpp	Fri Feb 20 21:32:25 2015 +0000
@@ -150,12 +150,12 @@
     _CS = 1;
 #endif
 }
-unsigned short PAR16::rd_gram()
+unsigned short PAR16::rd_gram(bool convert)
 {
 #ifdef USE_CS
     _CS = 0;
 #endif
-    unsigned short r=0;
+    unsigned int r=0;
     _DC = 1; // 1=data
    _port.input();
    
@@ -167,12 +167,22 @@
 //    _RD = 0; // add wait
     r |= _port.read();
     _RD = 1;
-    
+    if(convert)
+    {
+        r <<= 8;
+        _RD = 0;
+  //      _RD = 0; // add wait
+        r |= _port.read()>>8; //MSB of port read is blue, LSB is red of next pixel
+        _RD = 1;
+        // gram is 18bit/pixel, if you set 16bit/pixel (cmd 3A), during writing the 16bits are expanded to 18bit
+        // during reading, you read the raw 18bit gram
+        r = RGB24to16((r&0xFF0000)>>16, (r&0xFF00)>>8, r&0xFF);// 18bit pixel padded to 24bits, rrrrrr00_gggggg00_bbbbbb00, converted to 16bit
+    }
 #ifdef USE_CS
     _CS = 1;
 #endif
     _port.output();
-    return r;
+    return (unsigned short)r;
 }
 unsigned int PAR16::rd_reg_data32(unsigned char reg)
 {
--- a/Protocols/PAR16.h	Thu Feb 19 00:33:27 2015 +0000
+++ b/Protocols/PAR16.h	Fri Feb 20 21:32:25 2015 +0000
@@ -77,9 +77,10 @@
     
     /** Read 16bit pixeldata from display controller (with dummy cycle)
     *
+    * @param convert true/false. Convert 18bit to 16bit, some controllers returns 18bit
     * @returns 16bit color
     */ 
-    virtual unsigned short rd_gram();
+    virtual unsigned short rd_gram(bool convert);
     
     /** Read 4x8bit register data (with dummy cycle)
     * @param reg the register to read
--- a/Protocols/PAR8.cpp	Thu Feb 19 00:33:27 2015 +0000
+++ b/Protocols/PAR8.cpp	Fri Feb 20 21:32:25 2015 +0000
@@ -174,12 +174,12 @@
     _CS = 1;
 #endif
 }
-unsigned short PAR8::rd_gram()
+unsigned short PAR8::rd_gram(bool convert)
 {
 #ifdef USE_CS
     _CS = 0;
 #endif
-    unsigned short r=0;
+    unsigned int r=0;
     _DC = 1; // 1=data
    _port.input();
    
@@ -197,12 +197,22 @@
 //    _RD = 0; // add wait
     r |= (_port.read()&0xFF);
     _RD = 1;
-    
+    if(convert)
+    {
+        r <<= 8;
+        _RD = 0;
+  //      _RD = 0; // add wait
+        r |= _port.read();
+        _RD = 1;
+        // gram is 18bit/pixel, if you set 16bit/pixel (cmd 3A), during writing the 16bits are expanded to 18bit
+        // during reading, you read the raw 18bit gram
+        r = RGB24to16((r&0xFF0000)>>16, (r&0xFF00)>>8, r&0xFF);// 18bit pixel padded to 24bits, rrrrrr00_gggggg00_bbbbbb00, converted to 16bit
+    }
 #ifdef USE_CS
     _CS = 1;
 #endif
     _port.output();
-    return r;
+    return (unsigned short)r;
 }
 unsigned int PAR8::rd_reg_data32(unsigned char reg)
 {
--- a/Protocols/PAR8.h	Thu Feb 19 00:33:27 2015 +0000
+++ b/Protocols/PAR8.h	Fri Feb 20 21:32:25 2015 +0000
@@ -77,9 +77,10 @@
     
     /** Read 16bit pixeldata from display controller (with dummy cycle)
     *
+    * @param convert true/false. Convert 18bit to 16bit, some controllers returns 18bit
     * @returns 16bit color
     */ 
-    virtual unsigned short rd_gram();
+    virtual unsigned short rd_gram(bool convert);
     
     /** Read 4x8bit register data (with dummy cycle)
     * @param reg the register to read
--- a/Protocols/Protocols.h	Thu Feb 19 00:33:27 2015 +0000
+++ b/Protocols/Protocols.h	Fri Feb 20 21:32:25 2015 +0000
@@ -10,7 +10,7 @@
 
 #include "mbed.h"
 
-#define RGB18to16(r,g,b)  (((r&0xF8)<<8)|((g&0xFC)<<3)|((b&0xF8)>>3)) //5 red | 6 green | 5 blue
+#define RGB24to16(r,g,b)  (((r&0xF8)<<8)|((g&0xFC)<<3)|((b&0xF8)>>3)) //5 red | 6 green | 5 blue
 #define BGR2RGB(color) (((color&0x1F)<<11) | (color&0x7E0) | ((color&0xF800)>>11))
 
 //#define USE_CS
@@ -84,9 +84,10 @@
     
     /** Read 16bit pixeldata from display controller (with dummy cycle)
     *
+    * @param convert true/false. Convert 18bit to 16bit, some controllers returns 18bit
     * @returns 16bit color
     */ 
-    virtual unsigned short rd_gram() = 0;
+    virtual unsigned short rd_gram(bool convert) = 0;
     
     /** Read 4x8bit register data (with dummy cycle)
     * @param reg the register to read
--- a/Protocols/SPI16.cpp	Thu Feb 19 00:33:27 2015 +0000
+++ b/Protocols/SPI16.cpp	Fri Feb 20 21:32:25 2015 +0000
@@ -121,7 +121,7 @@
     _CS = 1;
 #endif
 }
-unsigned short SPI16::rd_gram()
+unsigned short SPI16::rd_gram(bool convert)
 {
 #ifdef USE_CS
     _CS = 0;
@@ -131,13 +131,17 @@
     r |= _spi.write(0); // 16bit, whole first byte is dummy, second is red
     r <<= 16;
     r |= _spi.write(0);  
+    if(convert)
+    {
+        // gram is 18bit/pixel, if you set 16bit/pixel (cmd 3A), during writing the 16bits are expanded to 18bit
+        // during reading, you read the raw 18bit gram
+        r = RGB24to16((r&0xFF0000)>>16, (r&0xFF00)>>8, r&0xFF);// 18bit pixel padded to 24bits, rrrrrr00_gggggg00_bbbbbb00, converted to 16bit
+    }
+    else r >>= 8;
 _CS = 1; // force CS HIG to interupt the "read state"
 #ifndef USE_CS //if CS is not used, force fixed LOW again
     _CS = 0;
 #endif
-    // gram is 18bit/pixel, if you set 16bit/pixel (cmd 3A), during writing the 16bits are expanded to 18bit
-    // during reading, you read the raw 18bit gram
-    r = RGB18to16((r&0xFC0000)>>16, (r&0xFC00)>>8, r&0xFC);// 18bit pixel, rrrrrr00_gggggg00_bbbbbb00, converted to 16bit
     return (unsigned short)r;
 }
 unsigned int SPI16::rd_reg_data32(unsigned char reg)
--- a/Protocols/SPI16.h	Thu Feb 19 00:33:27 2015 +0000
+++ b/Protocols/SPI16.h	Fri Feb 20 21:32:25 2015 +0000
@@ -79,9 +79,10 @@
     
     /** Read 16bit pixeldata from display controller (with dummy cycle)
     *
+    * @param convert true/false. Convert 18bit to 16bit, some controllers returns 18bit
     * @returns 16bit color
     */ 
-    virtual unsigned short rd_gram();
+    virtual unsigned short rd_gram(bool convert);
     
     /** Read 4x8bit register data (with dummy cycle)
     * @param reg the register to read
--- a/Protocols/SPI8.cpp	Thu Feb 19 00:33:27 2015 +0000
+++ b/Protocols/SPI8.cpp	Fri Feb 20 21:32:25 2015 +0000
@@ -135,7 +135,7 @@
     _CS = 1;
 #endif
 }
-unsigned short SPI8::rd_gram()
+unsigned short SPI8::rd_gram(bool convert)
 {
 #ifdef USE_CS
     _CS = 0;
@@ -146,15 +146,18 @@
     r |= _spi.write(0);
     r <<= 8;
     r |= _spi.write(0);
-    r <<= 8;
-    r |= _spi.write(0);  
+    if(convert)
+    {
+        r <<= 8;
+        r |= _spi.write(0);
+        // gram is 18bit/pixel, if you set 16bit/pixel (cmd 3A), during writing the 16bits are expanded to 18bit
+        // during reading, you read the raw 18bit gram
+        r = RGB24to16((r&0xFF0000)>>16, (r&0xFF00)>>8, r&0xFF);// 18bit pixel padded to 24bits, rrrrrr00_gggggg00_bbbbbb00, converted to 16bit
+    } 
     _CS = 1; // force CS HIG to interupt the "read state"
 #ifndef USE_CS //if CS is not used, force fixed LOW again
     _CS = 0;
 #endif
-    // gram is 18bit/pixel, if you set 16bit/pixel (cmd 3A), during writing the 16bits are expanded to 18bit
-    // during reading, you read the raw 18bit gram
-    r = RGB18to16((r&0xFC0000)>>16, (r&0xFC00)>>8, r&0xFC);// 18bit pixel, rrrrrr00_gggggg00_bbbbbb00, converted to 16bit
     return (unsigned short)r;
 }
 unsigned int SPI8::rd_reg_data32(unsigned char reg)
--- a/Protocols/SPI8.h	Thu Feb 19 00:33:27 2015 +0000
+++ b/Protocols/SPI8.h	Fri Feb 20 21:32:25 2015 +0000
@@ -76,9 +76,10 @@
     
     /** Read 16bit pixeldata from display controller (with dummy cycle)
     *
+    * @param convert true/false. Convert 18bit to 16bit, some controllers returns 18bit
     * @returns 16bit color
     */ 
-    virtual unsigned short rd_gram();
+    virtual unsigned short rd_gram(bool convert);
     
     /** Read 4x8bit register data (with dummy cycle)
     * @param reg the register to read