Library to control a Graphics TFT connected to 4-wire SPI - revised for the Raio RA8875 Display Controller.

Dependents:   FRDM_RA8875_mPaint RA8875_Demo RA8875_KeyPadDemo SignalGenerator ... more

Fork of SPI_TFT by Peter Drescher

See Components - RA8875 Based Display

Enhanced touch-screen support - where it previous supported both the Resistive Touch and Capacitive Touch based on the FT5206 Touch Controller, now it also has support for the GSL1680 Touch Controller.

Offline Help Manual (Windows chm)

/media/uploads/WiredHome/ra8875.zip.bin (download, rename to .zip and unzip)

Revision:
198:9b6851107426
Parent:
190:3132b7dfad82
Child:
202:a22cbc04f332
--- a/GraphicsDisplay.cpp	Tue Feb 11 21:51:42 2020 +0000
+++ b/GraphicsDisplay.cpp	Sat Mar 28 15:01:38 2020 +0000
@@ -110,6 +110,69 @@
 //{
 //}
 
+#if 0
+rect_t GraphicsDisplay::GetWindow() {
+    return windowrect;
+}
+
+rect_t GraphicsDisplay::SetWindow(rect_t r)
+{
+    return SetWindow(r.p1.x, r.p1.y, r.p2.x, r.p2.y);
+}
+
+rect_t GraphicsDisplay::SetWindow(loc_t x1, loc_t y1, loc_t x2, loc_t y2)
+{
+    rect_t oldWin = windowrect;
+    dim_t tempWidth, tempHeight;
+    INFO("SetWindow(%d,%d)-(%d,%d)", x1, y1, x2, y2);
+    // Correct the initial values (0,-1) - (0,-1)
+    if (x2 == (loc_t)-1) x2 = width() - 1;
+    if (y2 == (loc_t)-1) y2 = height() - 1;
+    // Sanity check the parameters.
+    if (x1 < 0 || y1 < 0 || x2 < 0 || y2 < 0
+        || x1 >= width() || y1 >= height()
+        || x2 >= width() || y2 >= height()) {
+        rect_t nullRect = { 0,0,0,0 };
+        return nullRect;
+    }
+    switch (screen_orientation) {
+    case rotate_0:
+    case rotate_180:
+        windowrect.p1.x = x1;
+        windowrect.p1.y = y1;
+        windowrect.p2.x = x2;
+        windowrect.p2.y = y2;
+        tempWidth = max(virt_screenwidth, virt_screenheight);
+        tempHeight = min(virt_screenwidth, virt_screenheight);
+        virt_screenwidth = tempWidth;
+        virt_screenheight = tempHeight;
+        //GraphicsDisplay::SetWindow(x1, y1, x2, y2);
+        WriteCommandW(RA8875_HSAW0, x1);
+        WriteCommandW(RA8875_VSAW0, y1);
+        WriteCommandW(RA8875_HEAW0, x2);
+        WriteCommandW(RA8875_VEAW0, y2);
+        break;
+    case rotate_90:
+    case rotate_270:
+        windowrect.p1.x = y1;
+        windowrect.p1.y = x1;
+        windowrect.p2.x = y2;
+        windowrect.p2.y = x2;
+        tempWidth = min(virt_screenwidth, virt_screenheight);
+        tempHeight = max(virt_screenwidth, virt_screenheight);
+        virt_screenwidth = tempWidth;
+        virt_screenheight = tempHeight;
+        //GraphicsDisplay::SetWindow(x1, y1, x2, y2);
+        WriteCommandW(RA8875_HSAW0, y1);
+        WriteCommandW(RA8875_VSAW0, x1);
+        WriteCommandW(RA8875_HEAW0, y2);
+        WriteCommandW(RA8875_VEAW0, x2);
+        break;
+    }
+    return oldWin;
+}
+#endif
+
 RetCode_t GraphicsDisplay::SelectUserFont(const unsigned char * _font)
 {
     font = _font;     // trusting them, but it might be good to put some checks in here...
@@ -121,44 +184,17 @@
     return fontblit(x, y, c);
 }
 
-RetCode_t GraphicsDisplay::window(rect_t r)
-{
-    return window(r.p1.x, r.p1.y, r.p2.x + 1 - r.p1.x, r.p2.y + 1 - r.p1.y);
-}
-
-RetCode_t GraphicsDisplay::window(loc_t x, loc_t y, dim_t _width, dim_t _height)
-{
-    if (_width == (dim_t)-1)
-        _width = width() - x;
-    if (_height == (dim_t)-1)
-        _height = height() - y;
-
-    // Save the window metrics
-    windowrect.p1.x = x;
-    windowrect.p1.y = y;
-    windowrect.p2.x = x + _width - 1;
-    windowrect.p2.y = y + _height - 1;
-    // current pixel location
-    _x = x;
-    _y = y;
-    return noerror;
-}
-
-RetCode_t GraphicsDisplay::WindowMax(void)
-{
-    return window(0,0, width(),height());
-}
-
 RetCode_t GraphicsDisplay::_putp(color_t color)
 {
+    rect_t win = GetWindow();
     pixel(_x, _y, color);
     // update pixel location based on window settings
     _x++;
-    if(_x > windowrect.p2.x) {
-        _x = windowrect.p1.x;
+    if(_x > win.p2.x) {
+        _x = win.p1.x;
         _y++;
-        if(_y > windowrect.p2.y) {
-            _y = windowrect.p1.y;
+        if(_y > win.p2.y) {
+            _y = win.p1.y;
         }
     }
     return noerror;
@@ -171,29 +207,30 @@
 
 RetCode_t GraphicsDisplay::cls(uint16_t layers)
 {
-    int restore = GetDrawingLayer();
+    uint16_t oldLayer = 0;
     if (layers & 1) {
-        SelectDrawingLayer(0);
+        oldLayer = SelectDrawingLayer(0);
         fill(0, 0, width(), height(), _background);
     }
     if (layers & 2) {
-        SelectDrawingLayer(1);
+        oldLayer = SelectDrawingLayer(1);
         fill(0, 0, width(), height(), _background);
     }
-    SelectDrawingLayer(restore);
+    SelectDrawingLayer(oldLayer);
     return noerror;
 }
 
+
 RetCode_t GraphicsDisplay::blit(loc_t x, loc_t y, dim_t w, dim_t h, const int * color)
 {
-    rect_t restore = windowrect;
-    window(x, y, w, h);
+    rect_t restore = SetWindow(x, y, x + w, y + h);
     _StartGraphicsStream();
     for (int i=0; i<w*h; i++) {
         _putp(color[i]);
     }
     _EndGraphicsStream();
-    return window(restore);
+    SetWindow(restore);
+    return noerror;
 }
 
 // 8 byte "info" section
@@ -221,7 +258,7 @@
     dim_t charHeight = font[6];
     const unsigned char * charRecord;   // width, data, data, data, ...
 
-    INFO("first:%d, last:%d, c:%d", firstChar, lastChar, c);
+    INFO("first:%d, last:%d, c:%02X '%c'", firstChar, lastChar, c, (c >= ' ' && c <= '~') ? c : '-');
     if (c < firstChar || c > lastChar)
         return NULL;       // advance zero pixels since it was unprintable...
 
@@ -244,7 +281,7 @@
     dim_t charWidth, charHeight;        // metrics for the raw char in the font table
     charRecord = getCharMetrics(c, &charWidth, &charHeight);
     if (charRecord) {
-        INFO("hgt:%d, wdt:%d", charHeight, charWidth);
+        INFO("wdt:%d, hgt:%d", charWidth, charHeight);
         booleanStream(x,y,charWidth, charHeight, charRecord);
         return charWidth * fontScaleX;
     } else {
@@ -268,7 +305,8 @@
     return c;
 }
 
-// RGB16 little endian 
+// RGB16 little endian
+
 //      GGGB BBBB RRRR RGGG
 // swap
 //      RRRR RGGG GGGB BBBB
@@ -305,9 +343,9 @@
 
     // Now, Read the bitmap info header
     fread(&BMP_Info, 1, sizeof(BMP_Info), Image);
-    HexDump("BMP_Info", (uint8_t *)&BMP_Info, sizeof(BMP_Info));
+    //HexDump("BMP_Info", (uint8_t *)&BMP_Info, sizeof(BMP_Info));
     BPP_t = BMP_Info.biBitCount;
-    INFO("biBitCount %04X", BPP_t);
+    //INFO("biBitCount %04X", BPP_t);
     if (BPP_t != 1 && BPP_t != 4 && BPP_t != 8 && BPP_t != 16 && BPP_t != 24) { // Support 4, 8, 16, 24-bits per pixel
         fclose(Image);
         return(not_supported_format);
@@ -318,12 +356,12 @@
     }
     PixelHeight = BMP_Info.biHeight;
     PixelWidth = BMP_Info.biWidth;
-    INFO("(%d,%d) (%d,%d) (%d,%d)", x,y, PixelWidth,PixelHeight, width(), height());
+    //INFO("(%d x,%d h) (%d w,%d h) (%d w,%d h)", x,y, PixelWidth,PixelHeight, width(), height());
     if (infoOnly) {
         infoOnly->p1.x = x;
         infoOnly->p1.y = y;
-        infoOnly->p2.x = x+PixelWidth;
-        infoOnly->p2.y = y+PixelHeight;
+        infoOnly->p2.x = x+PixelWidth-1;
+        infoOnly->p2.y = y+PixelHeight-1;
         return (noerror);
     }
 
@@ -344,11 +382,11 @@
             return(not_enough_ram);
         }
         fread(colorPalette, 1, paletteSize, Image);
-        HexDump("Color Palette", (uint8_t *)colorPalette, paletteSize);
+        //HexDump("Color Palette", (uint8_t *)colorPalette, paletteSize);
     }
 
     int lineBufSize = ((BPP_t * PixelWidth + 7)/8);
-    INFO("BPP_t %d, PixelWidth %d, lineBufSize %d", BPP_t, PixelWidth, lineBufSize);
+    //INFO("BPP_t %d, PixelWidth %d, lineBufSize %d", BPP_t, PixelWidth, lineBufSize);
     lineBuffer = (uint8_t *)swMalloc(lineBufSize);
     if (lineBuffer == NULL) {
         swFree(colorPalette);
@@ -369,14 +407,13 @@
         padd = 4 - padd;
 
     // Define window for top to bottom and left to right so writing auto-wraps
-    rect_t restore = windowrect;
-    window(x,y, PixelWidth,PixelHeight);
+    rect_t restore = SetWindow(x,y, x+PixelWidth-1,y+PixelHeight-1);
 //    SetGraphicsCursor(x, y);
 //    _StartGraphicsStream();
 
     //start_data = BMP_Info.bfOffBits;
     //HexDump("Raw Data", (uint8_t *)&start_data, 32);
-    INFO("(%d,%d) (%d,%d), [%d,%d]", x,y, PixelWidth,PixelHeight, lineBufSize, padd);
+    //INFO("(%d,%d) (%d,%d), [%d,%d]", x,y, PixelWidth,PixelHeight, lineBufSize, padd);
     for (j = PixelHeight - 1; j >= 0; j--) {                //Lines bottom up
         offset = fileOffset + j * (lineBufSize + padd);     // start of line
         fseek(Image, offset, SEEK_SET);
@@ -408,7 +445,7 @@
         pixelStream(pixelBuffer, PixelWidth, x, y++);
     }
 //    _EndGraphicsStream();
-    window(restore);
+    SetWindow(restore);
     swFree(pixelBuffer);      // don't leak memory
     swFree(lineBuffer);
     if (colorPalette)
@@ -416,31 +453,43 @@
     return (noerror);
 }
 
-rect_t GraphicsDisplay::GetImageRect(loc_t x, loc_t y, const char *Filename)
+rect_t GraphicsDisplay::GetImageRect(loc_t x, loc_t y, const char* Filename)
 {
-    rect_t r = { 0, 0, 0, 0};
+    rect_t r = { 0, 0, 0, 0 };
+
+    RenderImageFile(x, y, Filename, &r);
+    return r;
+}
 
-    RenderImageFile(x,y,Filename, &r);
+rect_t GraphicsDisplay::GetImageRect(point_t pt, const char* Filename)
+{
+    rect_t r = { 0, 0, 0, 0 };
+
+    RenderImageFile(pt, Filename, &r);
     return r;
 }
 
+RetCode_t GraphicsDisplay::RenderImageFile(point_t pt, const char* FileName, rect_t* getDim) {
+    return RenderImageFile(pt.x, pt.y, FileName, getDim);
+}
+
 RetCode_t GraphicsDisplay::RenderImageFile(loc_t x, loc_t y, const char * FileName, rect_t * getDim) {
     if (mystrnicmp(FileName + strlen(FileName) - 4, ".bmp", 4) == 0) {
         return RenderBitmapFile(x, y, FileName, getDim);
     } else if (mystrnicmp(FileName + strlen(FileName) - 4, ".jpg", 4) == 0) {
         if (getDim)
-            return not_supported_format;
+            return bad_parameter;
         return RenderJpegFile(x, y, FileName);
     } else if (mystrnicmp(FileName + strlen(FileName) - 4, ".ico", 4) == 0) {
         if (getDim)
-            return not_supported_format;
+            return bad_parameter;
         return RenderIconFile(x, y, FileName);
     } else if (mystrnicmp(FileName + strlen(FileName) - 4, ".gif", 4) == 0) {
         if (getDim)
-            return not_supported_format;
+            return bad_parameter;
         return RenderGIFFile(x, y, FileName);
     } else {
-        return not_supported_format;
+        return bad_parameter;
     }
 }
 
@@ -456,12 +505,12 @@
 
     fread(&BMP_Header, 1, sizeof(BMP_Header), Image);      // get the BMP Header
     INFO("bfType %04X, Header size: %d", BMP_Header.bfType, sizeof(BITMAPFILEHEADER));
-    HexDump("BMP_Header", (uint8_t *)&BMP_Header, sizeof(BMP_Header));
+    //HexDump("BMP_Header", (uint8_t *)&BMP_Header, sizeof(BMP_Header));
     if (BMP_Header.bfType != BF_TYPE) {
         fclose(Image);
         return(not_bmp_format);
     }
-    INFO("bfOffits %04X", BMP_Header.bfOffBits);
+    //INFO("bfOffits %04lX", BMP_Header.bfOffBits);
     RetCode_t rt = _RenderBitmap(x, y, BMP_Header.bfOffBits, Image, getDim);
     if (rt != noerror) {
         return rt;