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:
41:2956a0a221e5
Parent:
40:04aa280dfa39
Child:
43:3becae133285
--- a/RA8875.cpp	Tue Feb 04 02:58:06 2014 +0000
+++ b/RA8875.cpp	Sat Feb 08 17:35:45 2014 +0000
@@ -7,12 +7,7 @@
 ///
 #include "RA8875.h"
 
-DigitalOut zz1(LED1);
-DigitalOut zz2(LED2);
-DigitalOut zz3(LED3);
-DigitalOut zz4(LED4);
-
-#define DEBUG "RAIO"
+//#define DEBUG "RAIO"
 // ...
 // INFO("Stuff to show %d", var); // new-line is automatically appended
 //
@@ -30,13 +25,15 @@
 #define RA8875_DISPLAY_WIDTH  480
 #define RA8875_DISPLAY_HEIGHT 272
 
-
 #ifdef PERF_METRICS
 #define PERFORMANCE_RESET performance.reset()
 #define REGISTERPERFORMANCE(a) RegisterPerformance(a)
 static const char *metricsName[] = 
 {
-    "Point", "Line", "Rectangle", "Rounded Rectangle", "Triangle", "Circle", "Ellipse"
+    "Cls", "Pixel", "Pixel Stream", 
+    "Read Pixel", "Read Pixel Stream",
+    "Line", 
+    "Rectangle", "Rounded Rectangle", "Triangle", "Circle", "Ellipse"
 };
 #else
 #define PERFORMANCE_RESET
@@ -85,10 +82,11 @@
         metrics[method] = elapsed;
 }
 
-void RA8875::ReportPerformance()
+void RA8875::ReportPerformance(Serial & pc)
 {
+    pc.printf("\r\nPerformance Metrics\r\n");
     for (int i=0; i<METRICCOUNT; i++) {
-        printf("%10d uS %s\r\n", metrics[i], metricsName[i]);
+        pc.printf("%10d uS %s\r\n", metrics[i], metricsName[i]);
     }    
 }
 #endif
@@ -99,11 +97,12 @@
     WriteCommand(command, data & 0xFF);
     WriteCommand(command+1, data >> 8);
     #else
+    // This should be a little faster, but doesn't work...
     INFO("WriteCommandW(%02X, %04X)", command, data);
     select(true);
     spiwrite(0x80);
     spiwrite(command);
-//    spiwrite(0x00);
+    //spiwrite(0x00);     // dummy
     spiwrite(data & 0xFF);
     spiwrite(data >> 8);
     select(false);
@@ -114,7 +113,7 @@
 RetCode_t RA8875::WriteCommand(unsigned char command, unsigned int data)
 {
     select(true);
-    spiwrite(0x80);
+    spiwrite(0x80);         // cmd: write command
     spiwrite(command);
     if (data <= 0xFF) {   // only if in the valid range
         spiwrite(0x00);
@@ -127,7 +126,7 @@
 RetCode_t RA8875::WriteDataW(uint16_t data)
 {
     select(true);
-    spiwrite(0x00);
+    spiwrite(0x00);         // cmd: write data
     spiwrite(data & 0xFF);
     spiwrite(data >> 8);
     select(false);
@@ -160,6 +159,18 @@
     return data;
 }
 
+uint16_t RA8875::ReadDataW(void)
+{
+    uint16_t data;
+    
+    select(true);
+    spiwrite(0x40);
+    data  = spiread();
+    data |= (spiread() << 8);
+    select(false);
+    return data;
+}
+
 unsigned char RA8875::ReadStatus(void)
 {
     unsigned char data;
@@ -452,6 +463,15 @@
     return noerror;
 }
 
+RetCode_t RA8875::SetGraphicsCursorRead(loc_t x, loc_t y)
+{
+    //WriteCommand(0x40, 0);  // Graphics mode
+    //WriteCommand(0x45, 0);  // left->right, top->bottom
+    WriteCommandW(0x4A, x);
+    WriteCommandW(0x4C, y);
+    return noerror;
+}
+
 RetCode_t RA8875::window(loc_t x, loc_t y, dim_t width, dim_t height)
 {
     GraphicsDisplay::window(x,y, width,height);
@@ -484,9 +504,12 @@
 
 RetCode_t RA8875::pixel(loc_t x, loc_t y, color_t color)
 {
-    //INFO("pixel(%d,%d, %04X)", x,y, color);
+    #if 1
+    return pixelStream(&color, 1, x,y);
+    #else
     foreground(color);
     return pixel(x,y);
+    #endif
 }
 
 RetCode_t RA8875::pixel(loc_t x, loc_t y)
@@ -500,10 +523,69 @@
     WriteCommand(0x02);
     WriteDataW(color);
     ret = noerror;
-    REGISTERPERFORMANCE(PRF_DRAWPOINT);
+    REGISTERPERFORMANCE(PRF_DRAWPIXEL);
     return ret;
 }
 
+RetCode_t RA8875::pixelStream(color_t * p, uint32_t count, loc_t x, loc_t y)
+{
+    PERFORMANCE_RESET;
+    WriteCommand(0x40,0x00);    // Graphics write mode
+    SetGraphicsCursor(x, y);
+    WriteCommand(0x02);
+    select(true);
+    spiwrite(0x00);         // Cmd: write data
+    while (count--) {
+        spiwrite(*p >> 8);
+        spiwrite(*p & 0xFF);
+        p++;
+    }
+    select(false);
+    REGISTERPERFORMANCE(PRF_PIXELSTREAM);
+    return(noerror);
+}
+
+color_t RA8875::getPixel(loc_t x, loc_t y)
+{
+    color_t pixel;
+    
+    PERFORMANCE_RESET;
+    //WriteCommand(0x45,0x00);    // read left->right, top->bottom
+    WriteCommand(0x40,0x00);    // Graphics write mode
+    SetGraphicsCursorRead(x, y);
+    WriteCommand(0x02);
+    select(true);
+    spiwrite(0x40);         // Cmd: read data
+    spiwrite(0x00);         // dummy read
+    pixel  = spiread();
+    pixel |= (spiread() << 8);
+    select(false);
+    REGISTERPERFORMANCE(PRF_READPIXEL);
+    return pixel;
+}
+
+RetCode_t RA8875::getPixelStream(color_t * p, uint32_t count, loc_t x, loc_t y)
+{
+    color_t pixel;
+    
+    PERFORMANCE_RESET;
+    //WriteCommand(0x45,0x00);    // read left->right, top->bottom
+    WriteCommand(0x40,0x00);    // Graphics write mode
+    SetGraphicsCursorRead(x, y);
+    WriteCommand(0x02);
+    select(true);
+    spiwrite(0x40);         // Cmd: read data
+    spiwrite(0x00);         // dummy read
+    while (count--) {
+        pixel  = spiread();
+        pixel |= (spiread() << 8);
+        *p++ = pixel;
+    }
+    select(false);
+    REGISTERPERFORMANCE(PRF_READPIXELSTREAM);
+    return noerror;
+}
+
 RetCode_t RA8875::line(loc_t x1, loc_t y1, loc_t x2, loc_t y2, color_t color)
 {
     foreground(color);
@@ -968,6 +1050,7 @@
 //     /__/      /_____________/ /_____________/       /__/
 //
 //    Everything from here down is test code.
+bool SuppressSlowStuff = false;
 
 void TextCursorTest(RA8875 & display, Serial & pc)
 {
@@ -976,8 +1059,12 @@
     const char * bCursor = "The Block cursor should be visible for this text.\r\n";
     const char * bbCursor = "The Blinking Block cursor should be visible for this text.\r\n";
     const char * p;
+    int delay = 100;
     
-    pc.printf("Text Cursor Test\r\n");
+    if (!SuppressSlowStuff)
+        pc.printf("Text Cursor Test\r\n");
+    else 
+        delay = 0;
     display.background(Black);
     display.foreground(Blue);
     display.cls();
@@ -990,46 +1077,52 @@
     p = iCursor;
     while (*p) {
         display._putc(*p++);
-        wait_ms(100);
+        wait_ms(delay);
     }
 
     display.SetTextCursorControl(UNDER, false);
     p = uCursor;
     while (*p) {
         display._putc(*p++);
-        wait_ms(100);
+        wait_ms(delay);
     }
     
     display.SetTextCursorControl(BLOCK, false);
     p = bCursor;
     while (*p) {
         display._putc(*p++);
-        wait_ms(100);
+        wait_ms(delay);
     }
 
     display.SetTextCursorControl(BLOCK, true);
     p = bbCursor;
     while (*p) {
         display._putc(*p++);
-        wait_ms(100);
+        wait_ms(delay);
     }
-    wait_ms(2000);
+    wait_ms(delay * 20);
     display.SetTextCursorControl(NOCURSOR, false);
 }
 
 void BacklightTest(RA8875 & display, Serial & pc, float ramptime)
 {
     char buf[60];
-    
-    pc.printf("Backlight Test - ramp over %f sec.\r\n", ramptime);
+    unsigned int w = (ramptime * 1000)/ 256;
+    int delay = 200;
+
+    if (!SuppressSlowStuff)
+        pc.printf("Backlight Test - ramp over %f sec.\r\n", ramptime);
+    else {
+        delay = 0;
+        w = 0;
+    }
     display.Backlight_u8(0);
     display.background(White);
     display.foreground(Blue);
     display.cls();
-    wait_ms(200);
+    wait_ms(delay);
     display.puts(0,0, "RA8875 Backlight Test - Ramp up.");
     for (int i=0; i <= 255; i++) {
-        unsigned int w = (ramptime * 1000)/ 256;
         sprintf(buf, "%3d, %4d", i, w);
         display.puts(100,100,buf);
         display.Backlight_u8(i);
@@ -1039,21 +1132,28 @@
 
 void BacklightTest2(RA8875 & display, Serial & pc)
 {
-    pc.printf("Backlight Test 2\r\n");
+    int delay = 20;
+
+    if (!SuppressSlowStuff)
+        pc.printf("Backlight Test 2\r\n");
+    else
+        delay = 0;
+
     // Dim it out at the end of the tests.
     display.foreground(Blue);
     display.puts(0,0, "Ramp Backlight down.");
     // Ramp it off
     for (int i=255; i != 0; i--) {
         display.Backlight_u8(i);
-        wait_ms(20);
+        wait_ms(delay);
     }
     display.Backlight_u8(0);
 }
 
 void ExternalFontTest(RA8875 & display, Serial & pc)
 {
-    pc.printf("External Font Test\r\n");
+    if (!SuppressSlowStuff)
+        pc.printf("External Font Test\r\n");
     display.background(Black);
     display.foreground(Blue);
     display.cls();
@@ -1073,7 +1173,8 @@
 
 void DOSColorTest(RA8875 & display, Serial & pc)
 {
-    pc.printf("DOS Color Test\r\n");
+    if (!SuppressSlowStuff)
+        pc.printf("DOS Color Test\r\n");
     display.background(Black);
     display.foreground(Blue);
     display.cls();
@@ -1095,7 +1196,8 @@
 
 void WebColorTest(RA8875 & display, Serial & pc)
 {
-    pc.printf("Web Color Test\r\n");
+    if (!SuppressSlowStuff)
+        pc.printf("Web Color Test\r\n");
     display.background(Black);
     display.foreground(Blue);
     display.window(0,0, display.width(), display.height());
@@ -1115,7 +1217,8 @@
 {
     int i, c, x, y;
 
-    pc.printf("Pixel Test\r\n");
+    if (!SuppressSlowStuff)
+        pc.printf("Pixel Test\r\n");
     display.background(Black);
     display.foreground(Blue);
     display.cls();
@@ -1133,7 +1236,8 @@
 {
     int i, x, y, x2, y2;
 
-    pc.printf("Line Test\r\n");
+    if (!SuppressSlowStuff)
+        pc.printf("Line Test\r\n");
     display.background(Black);
     display.foreground(Blue);
     display.cls();
@@ -1152,7 +1256,8 @@
 {
     int i, x1,y1, x2,y2;
 
-    pc.printf("Rectangle Test\r\n");
+    if (!SuppressSlowStuff)
+        pc.printf("Rectangle Test\r\n");
     display.background(Black);
     display.foreground(Blue);
     display.cls();
@@ -1176,7 +1281,8 @@
 {
     loc_t i, x1,y1, x2,y2, r1,r2;
 
-    pc.printf("Round Rectangle Test\r\n");
+    if (!SuppressSlowStuff)
+        pc.printf("Round Rectangle Test\r\n");
     display.background(Black);
     display.foreground(Blue);
     display.cls();
@@ -1205,7 +1311,8 @@
 {
     int i, x1, y1, x2, y2, x3, y3;
 
-    pc.printf("Triangle Test\r\n");
+    if (!SuppressSlowStuff)
+        pc.printf("Triangle Test\r\n");
     display.background(Black);
     display.foreground(Blue);
     display.cls();
@@ -1265,7 +1372,8 @@
 {
     int i, x, y, r1;
 
-    pc.printf("Circle Test\r\n");
+    if (!SuppressSlowStuff)
+        pc.printf("Circle Test\r\n");
     display.background(Black);
     display.foreground(Blue);
     display.cls();
@@ -1289,7 +1397,8 @@
 {
     int i,x,y,r1,r2;
 
-    pc.printf("Ellipse Test\r\n");
+    if (!SuppressSlowStuff)
+        pc.printf("Ellipse Test\r\n");
     display.background(Black);
     display.foreground(Blue);
     display.cls();
@@ -1312,6 +1421,8 @@
 void TestGraphicsBitmap(RA8875 & display, Serial & pc)
 {
     LocalFileSystem local("local");
+    if (!SuppressSlowStuff)
+        pc.printf("Bitmap File Load\r\n");    
     display.background(Black);
     display.foreground(Blue);
     display.cls();
@@ -1321,6 +1432,42 @@
     int r = display.RenderBitmapFile(0,0, "/local/TestPat.bmp");
 }
 
+void SpeedTest(RA8875 & display, Serial & pc)
+{
+    Timer t;
+    SuppressSlowStuff = true;
+    pc.printf("\r\nSpeedTest disables delays, runs tests, reports overall time.\r\n");
+    t.start();
+    // do stuff fast
+    TextCursorTest(display, pc);
+    BacklightTest(display, pc, 0);
+    BacklightTest2(display, pc);
+    ExternalFontTest(display, pc);
+    DOSColorTest(display, pc);
+    WebColorTest(display, pc);
+    PixelTest(display, pc);
+    LineTest(display, pc);
+    RectangleTest(display, pc);
+    RoundRectTest(display, pc);
+    TriangleTest(display, pc);
+    CircleTest(display, pc);
+    EllipseTest(display, pc);
+    //TestGraphicsBitmap(display, pc);
+    pc.printf("SpeedTest completed in %d msec\r\n", t.read_ms());
+    #ifdef DEBUG
+    display.ReportPerformance(pc);
+    #endif
+    SuppressSlowStuff = false;
+}
+
+void PrintScreen(RA8875 & display, Serial & pc)
+{
+    LocalFileSystem local("local");
+    if (!SuppressSlowStuff)
+        pc.printf("PrintScreen\r\n");    
+    display.PrintScreen( 0,0, 480,272, "/local/Capture.bmp");
+}
+
 void RunTestSet(RA8875 & lcd, Serial & pc)
 {
     int q = 0;
@@ -1329,14 +1476,18 @@
 
     while(1) {
         pc.printf("\r\n"
-                  "B - Backlight up    b - backlight dim\r\n"
-                  "D - DOS Colors      W - Web Colors\r\n"
-                  "t - text cursor     G - Graphics Bitmap\r\n"
-                  "L - Lines           F - external Font\r\n"
-                  "R - Rectangles      O - rOund rectangles\r\n"
-                  "T - Triangles       P - Pixels  \r\n"
-                  "C - Circles         E - Ellipses\r\n"
-                  "A - Auto Test mode  r - reset  \r\n"
+                  "B - Backlight up      b - backlight dim\r\n"
+                  "D - DOS Colors        W - Web Colors\r\n"
+                  "t - text cursor       G - Graphics Bitmap\r\n"
+                  "L - Lines             F - external Font\r\n"
+                  "R - Rectangles        O - rOund rectangles\r\n"
+                  "T - Triangles         P - Pixels  \r\n"
+                  "C - Circles           E - Ellipses\r\n"
+                  "A - Auto Test mode    S - Speed Test\r\n"
+                  "p - print screen      r - reset  \r\n"
+                  #ifdef DEBUG
+                  "0 - clear performance 1 - report performance\r\n"
+                  #endif
                   "> ");
         if (automode == -1 || pc.readable()) {
             automode = -1;
@@ -1347,11 +1498,19 @@
             q = modelist[automode];
         }
         switch(q) {
+            #ifdef DEBUG
+            case '0':
+                lcd.ClearPerformance();
+                break;
+            case '1':
+                lcd.ReportPerformance(pc);
+                break;
+            #endif
             case 'A':
                 automode = 0;
                 break;
             case 'B':
-                BacklightTest(lcd, pc, 4);
+                BacklightTest(lcd, pc, 2);
                 break;
             case 'b':
                 BacklightTest2(lcd, pc);
@@ -1377,6 +1536,12 @@
             case 'O':
                 RoundRectTest(lcd, pc);
                 break;
+            case 'p':
+                PrintScreen(lcd, pc);
+                break;
+            case 'S':
+                SpeedTest(lcd, pc);
+                break;
             case 'T':
                 TriangleTest(lcd, pc);
                 break;