Fork of David Smart's RA8875 library for the purpose of adding touch screen support

Fork of RA8875 by David Smart

Revision:
23:a50ded45dbaf
Parent:
21:3c1efb192927
Child:
24:8ca861acf12d
--- a/RA8875.cpp	Sun Jan 12 21:18:47 2014 +0000
+++ b/RA8875.cpp	Wed Jan 15 12:24:54 2014 +0000
@@ -30,12 +30,6 @@
 #define RA8875_DISPLAY_WIDTH  480
 #define RA8875_DISPLAY_HEIGHT 272
 
-/// @TODO what is the size?
-/// If we're using the built-in font, read it from the chip.
-/// If we're using a custom font, get it from the font metrics.
-const int size_x = 16;
-const int size_y = 16;
-
 
 #ifdef PERF_METRICS
 #define PERFORMANCE_RESET performance.reset()
@@ -150,7 +144,7 @@
 unsigned int RA8875::fontwidth(void)
 {
     if (font == NULL)
-        return size_x;
+        return (((ReadCommand(0x22) >> 2) & 0x3) + 1) * 16;
     else
         return width() / font[1];
 }
@@ -158,7 +152,7 @@
 unsigned int RA8875::fontheight(void)
 {
     if (font == NULL)
-        return size_y;
+        return (((ReadCommand(0x22) >> 0) & 0x3) + 1) * 16;
     else
         return height() / font[2];
 }
@@ -198,6 +192,23 @@
     return noerror;
 }
 
+RetCode_t RA8875::SetTextCursorControl(bool visible, bool blink)
+{
+    unsigned char mwcr0 = ReadCommand(0x40) & 0x0F; // retain direction, auto-increase
+    
+    mwcr0 |= 0x80;      // text mode
+    if (visible)
+        mwcr0 |= 0x40;
+    if (blink)
+        mwcr0 |= 0x20;
+    WriteCommand(0x40, mwcr0);      // configure the cursor
+    WriteCommand(0x41, 0x00);       // close the graphics cursor
+    WriteCommand(0x44,0x1f);        //The cursor flashing cycle
+    WriteCommand(0x4e,0x1f);        //The cursor size
+    WriteCommand(0x4f,0x1f);        //The cursor size
+    return noerror;
+}
+
 RetCode_t RA8875::SetTextFont(RA8875::font_t font)
 {
     if (/*font >= RA8875::ISO8859_1 && */ font <= RA8875::ISO8859_4) {
@@ -826,23 +837,22 @@
     WriteCommand(0x89, 0x02);
     wait_ms(1);
     
-    //?
-    WriteCommand(0x10, 0x0C);  //SYSR   bit[4:3]=00 256 color  bit[2:1]=  00 8bit MPU interface, 1x 64k color             1x   16bit
-
-    //?
-    WriteCommand(0x04, 0x82);    //PCLK
+    // System Config Register (SYSR)
+    WriteCommand(0x10, 0x0C);           // 16-bpp (65K colors) color depth, 8-bit interface
+    // Pixel Clock Setting Register (PCSR)
+    WriteCommand(0x04, 0x82);           // PDAT on PCLK falling edge, PCLK = 4 x System Clock
     wait_ms(1);
 
     // Horizontal Settings
-    WriteCommand(0x14, RA8875_DISPLAY_WIDTH/8 - 1);                     //HDWR//Horizontal Display Width Setting Bit[6:0]
+    WriteCommand(0x14, RA8875_DISPLAY_WIDTH/8 - 1); //HDWR//Horizontal Display Width Setting Bit[6:0]
     WriteCommand(0x15, 0x02);                     //HNDFCR//Horizontal Non-Display Period fine tune Bit[3:0]
     WriteCommand(0x16, 0x03);                     //HNDR//Horizontal Non-Display Period Bit[4:0]
     WriteCommand(0x17, 0x01);                     //HSTR//HSYNC Start Position[4:0]
     WriteCommand(0x18, 0x03);                     //HPWR//HSYNC Polarity ,The period width of HSYNC.
 
     // Vertical Settings
-    WriteCommand(0x19, (RA8875_DISPLAY_HEIGHT-1)&0xFF);                     //VDHR0 //Vertical Display Height Bit [7:0]
-    WriteCommand(0x1a, (RA8875_DISPLAY_HEIGHT-1)>>8);                     //VDHR1 //Vertical Display Height Bit [8]
+    WriteCommand(0x19, (RA8875_DISPLAY_HEIGHT-1)&0xFF); //VDHR0 //Vertical Display Height Bit [7:0]
+    WriteCommand(0x1a, (RA8875_DISPLAY_HEIGHT-1)>>8);   //VDHR1 //Vertical Display Height Bit [8]
     WriteCommand(0x1b, 0x0F);                     //VNDR0 //Vertical Non-Display Period Bit [7:0]
     WriteCommand(0x1c, 0x00);                     //VNDR1 //Vertical Non-Display Period Bit [8]
     WriteCommand(0x1d, 0x0e);                     //VSTR0 //VSYNC Start Position[7:0]
@@ -857,3 +867,436 @@
     return noerror;
 }
 
+#ifdef TESTENABLE
+
+//      ______________  ______________  ______________  _______________
+//     /_____   _____/ /  ___________/ /  ___________/ /_____   ______/
+//          /  /      /  /            /  /                  /  /
+//         /  /      /  /___         /  /__________        /  /
+//        /  /      /  ____/        /__________   /       /  /
+//       /  /      /  /                       /  /       /  /
+//      /  /      /  /__________  ___________/  /       /  /
+//     /__/      /_____________/ /_____________/       /__/
+//
+//    Everything from here down is test code.
+
+void ValentineMessage(RA8875 & display, Serial & pc) 
+{
+    pc.printf("Write Sample text\r\n");
+    display.background(BrightRed);
+    display.foreground(White);
+    display.cls();
+    display.SetTextFontControl(NOFILL);
+    for (int i=0; i<20 ; i++) {
+        int sz = rand() & 1 + 1;
+        int rx = rand() % 360 + 0;
+        int ry = rand() % 250 + 0;
+        if (sz == 1)
+            rx /= 2;
+        display.SetTextCursor(rx, ry);
+        display.SetTextFontSize(sz, sz);
+        display.puts("David loves Louize");
+        wait_ms(50);
+    }
+    display.SetTextFontSize(2,2);
+    wait_ms(1000);
+    // Make a heart shape
+    display.ellipse(300, 120, 40, 50, FILL);
+    display.ellipse(370, 120, 40, 50, FILL);
+    display.triangle(265,145, 405,145, 335,230, FILL);
+    wait_ms(2500);
+    display.foreground(Blue);
+    display.puts(300,90, "Be My");
+    wait_ms(500);
+    display.puts(262,118, "Valentine");
+    // put a few things back the way they were
+    display.SetTextFontControl(FILL);
+    display.SetTextFontSize(1,1);
+    display.background(Black);
+}
+
+
+void TextCursorTest(RA8875 & display, Serial & pc)
+{
+    const char * visCursor = "The cursor should be visible for this text.";
+    const char * p;
+    
+    pc.printf("Text Cursor Test\r\n");
+    display.Backlight_u8(255);
+    display.background(Black);
+    display.foreground(Blue);
+    display.cls();
+    display.puts(0,0, "Text Cursor Test.");
+    
+    // visible, non-blinking
+    display.SetTextCursorControl(true, false);
+    display.SetTextCursor(0,40);
+    p = visCursor;
+    while (*p) {
+        display.putc(*p++);
+        wait_ms(100);
+    }
+}
+
+void BacklightTest(RA8875 & display, Serial & pc, float ramptime)
+{
+    pc.printf("Backlight Test - ramp over %f sec.\r\n", ramptime);
+    display.Backlight_u8(0);
+    display.background(Black);
+    display.foreground(Blue);
+    display.cls();
+    display.puts(0,0, "RA8875 Backlight Test - Ramp up.");
+    for (int i=0; i < 255; i++) {
+        display.Backlight_u8(i);
+        wait_ms((ramptime * 1000)/ 256);
+    }
+}
+
+void BacklightTest2(RA8875 & display, Serial & pc)
+{
+    pc.printf("Backlight Test 2\r\n");
+    // Dim it out at the end of the tests.
+    display.foreground(Yellow);
+    display.puts(0,0, "Ramp Backlight down.");
+    // Ramp it off
+    for (int i=255; i != 0; i--) {
+        display.Backlight_u8(i);
+        wait_ms(20);
+    }
+    display.Backlight_u8(0);
+}
+
+void ExternalFontTest(RA8875 & display, Serial & pc)
+{
+    pc.printf("External Font Test\r\n");
+    display.background(Black);
+    display.foreground(Blue);
+    display.cls();
+    display.Backlight(1);
+    display.set_font(Arial12x12);
+    display.puts(0,0,"ABCDEFGHIJKLMNOPWRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
+}
+
+void DOSColorTest(RA8875 & display, Serial & pc)
+{
+    pc.printf("DOS Color Test\r\n");
+    display.background(Black);
+    display.foreground(Blue);
+    display.cls();
+    display.puts(0,0, "DOS Colors - Fore");
+    display.puts(280,0, "Back");
+    display.background(Gray);
+    for (int i=0; i<16; i++) {
+        display.foreground(display.DOSColor(i));
+        display.puts(160, i*16, display.DOSColorNames(i));
+        display.background(Black);
+    }
+    display.foreground(White);
+    for (int i=0; i<16; i++) {
+        display.background(display.DOSColor(i));
+        display.puts(360, i*16, display.DOSColorNames(i));
+        display.foreground(White);
+    }
+}
+
+void WebColorTest(RA8875 & display, Serial & pc)
+{
+    pc.printf("Web Color Test\r\n");
+    display.background(Black);
+    display.foreground(Blue);
+    display.SetWindow(0,0, display.width(), display.height());
+    display.cls();
+    display.puts(0,0, "Web Color Test\r\n");
+    display.SetTextFontSize(1,2);
+    for (int i=0; i<sizeof(WebColors)/sizeof(WebColors[0]); i++) {
+        display.background(WebColors[i]);
+        display.puts(" ");
+        if (i % 36 == 35)
+            display.puts("\r\n");
+    }
+    display.SetTextFontSize(1,1);
+}
+
+void LineTest(RA8875 & display, Serial & pc)
+{
+    int i, x, y, x2, y2;
+
+    pc.printf("Line Test\r\n");
+    display.background(Black);
+    display.foreground(Blue);
+    display.cls();
+    display.puts(0,0, "Line Test");
+    for (i=0; i<16; i++) {
+        // Lines
+        x = rand() % 480;
+        y = rand() % 272;
+        x2 = rand() % 480;
+        y2 = rand() % 272;
+        display.line(x,y, x2,y2, display.DOSColor(i));
+    }
+}
+
+void RectangleTest(RA8875 & display, Serial & pc)
+{
+    int i, x1,y1, x2,y2;
+
+    pc.printf("Rectangle Test\r\n");
+    display.background(Black);
+    display.foreground(Blue);
+    display.cls();
+    display.puts(0,0, "Rectangle Test");
+    for (i=0; i<16; i++) {
+        x1 = rand() % 240;
+        y1 = 50 + rand() % 200;
+        x2 = rand() % 240;
+        y2 = 50 + rand() % 200;
+        display.rect(x1,y1, x2,y2, display.DOSColor(i));
+
+        x1 = 240 + rand() % 240;
+        y1 = 50 + rand() % 200;
+        x2 = 240 + rand() % 240;
+        y2 = 50 + rand() % 200;
+        display.rect(x1,y1, x2,y2, FILL);
+    }
+}
+
+
+void RoundRectTest(RA8875 & display, Serial & pc)
+{
+    unsigned int i, x1,y1, x2,y2, r1,r2;
+
+    pc.printf("Round Rectangle Test\r\n");
+    display.background(Black);
+    display.foreground(Blue);
+    display.cls();
+    display.puts(0,0, "Rounded Rectangle Test");
+    
+    #if 0
+    i = 3;
+    x1 = 30; y1 = 30;
+    x2 = 200; y2 = 100;
+    r1 = 10; r2 = 20;
+    pc.printf("  (%3d,%3d), (%3d,%3d): (%2d,%2d) - %04X\r\n", x1,y1, x2,y2, r1,r2, display.DOSColor(i));
+    display.roundrect(x1,y1, x2,y2, r1,r2, display.DOSColor(i));
+    
+    x1 = 230; y1 = 30;
+    x2 = 400; y2 = 100;
+    r1 = 30; r2 = 34;
+    pc.printf("  (%3d,%3d), (%3d,%3d): (%2d,%2d) - %04X\r\n", x1,y1, x2,y2, r1,r2, display.DOSColor(i));
+    display.roundrect(x1,y1, x2,y2, r1,r2, display.DOSColor(i), NOFILL);
+    #endif
+    
+    for (i=0; i<16; i++) {
+        x1 = rand() % 240;
+        y1 = 50 + rand() % 200;
+        x2 = x1 + rand() % 100;
+        y2 = y1 + rand() % 100;
+        r1 = rand() % (x2 - x1)/2;
+        r2 = rand() % (y2 - y1)/2;
+        display.roundrect(x1,y1, x2,y2, 5,8, display.DOSColor(i));
+
+        x1 = 240 + rand() % 240;
+        y1 = 50 + rand() % 200;
+        x2 = x1 + rand() % 100;
+        y2 = y1 + rand() % 100;
+        r1 = rand() % (x2 - x1)/2;
+        r2 = rand() % (y2 - y1)/2;
+        display.roundrect(x1,y1, x2,y2, r1,r2, FILL);
+    }
+}
+
+void TriangleTest(RA8875 & display, Serial & pc)
+{
+    int i, x1, y1, x2, y2, x3, y3;
+
+    pc.printf("Triangle Test\r\n");
+    display.background(Black);
+    display.foreground(Blue);
+    display.cls();
+    display.puts(0,0, "Triangle Test");
+
+    x1 = 150;
+    y1 = 2;
+    x2 = 190;
+    y2 = 7;
+    x3 = 170;
+    y3 = 16;
+    display.triangle(x1,y1, x2,y2, x3,y3);
+
+    x1 = 200;
+    y1 = 2;
+    x2 = 240;
+    y2 = 7;
+    x3 = 220;
+    y3 = 16;
+    display.filltriangle(x1,y1, x2,y2, x3,y3, BrightRed);
+
+    x1 = 300;
+    y1 = 2;
+    x2 = 340;
+    y2 = 7;
+    x3 = 320;
+    y3 = 16;
+    display.triangle(x1,y1, x2,y2, x3,y3, NOFILL);
+
+    x1 = 400;
+    y1 = 2;
+    x2 = 440;
+    y2 = 7;
+    x3 = 420;
+    y3 = 16;
+    display.triangle(x1,y1, x2,y2, x3,y3, Blue);
+
+    for (i=0; i<16; i++) {
+        x1 = rand() % 240;
+        y1 = 50 + rand() % 200;
+        x2 = rand() % 240;
+        y2 = 50 + rand() % 200;
+        x3 = rand() % 240;
+        y3 = 50 + rand() % 200;
+        display.triangle(x1,y1, x2,y2, x3,y3, display.DOSColor(i));
+        x1 = 240 + rand() % 240;
+        y1 = 50 + rand() % 200;
+        x2 = 240 + rand() % 240;
+        y2 = 50 + rand() % 200;
+        x3 = 240 + rand() % 240;
+        y3 = 50 + rand() % 200;
+        display.triangle(x1,y1, x2,y2, x3,y3, FILL);
+    }
+}
+
+void CircleTest(RA8875 & display, Serial & pc)
+{
+    int i, x, y, r1;
+
+    pc.printf("Circle Test\r\n");
+    display.background(Black);
+    display.foreground(Blue);
+    display.cls();
+    display.puts(0,0, "Circle Test");
+    for (i=0; i<16; i++) {
+        x = 100 + rand() % 100;
+        y = 70 + rand() % 200;
+        r1 = rand() % min(y - 20, 100);
+        //pc.printf("  (%d,%d) - %d\r\n", x,y,r1);
+        display.circle(x,y,r1, display.DOSColor(i));
+
+        x = 300 + rand() % 100;
+        y = 70 + rand() % 200;
+        r1 = rand() % min(y - 20, 100);
+        //pc.printf("  (%d,%d) - %d FILL\r\n", x,y,r1);
+        display.circle(x,y,r1, display.DOSColor(i), FILL);
+    }
+}
+
+void EllipseTest(RA8875 & display, Serial & pc)
+{
+    int i,x,y,r1,r2;
+
+    pc.printf("Ellipse Test\r\n");
+    display.background(Black);
+    display.foreground(Blue);
+    display.cls();
+    display.puts(0,0, "Ellipse Test");
+    for (i=0; i<16; i++) {
+        x = 100 + rand() % 100;
+        y = 70 + rand() % 200;
+        r1 = rand() % min(y - 20, 100);
+        r2 = rand() % min(y - 20, 100);
+        display.ellipse(x,y,r1,r2, display.DOSColor(i));
+
+        x = 300 + rand() % 100;
+        y = 70 + rand() % 200;
+        r1 = rand() % min(y - 20, 100);
+        r2 = rand() % min(y - 20, 100);
+        display.ellipse(x,y,r1,r2, FILL);
+    }
+}
+
+void RunTestSet(RA8875 & lcd, Serial & pc)
+{
+    int q = 0;
+    int automode = 0;
+    const unsigned char modelist[] = "BDWLROTCEbt";   // auto-test in this order.
+
+    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                      \r\n"
+                  "L - Lines           F - external Font\r\n"
+                  "R - Rectangles      O - rOund rectangles\r\n"
+                  "T - Triangles                        \r\n"
+                  "C - Circles         E - Ellipses\r\n"
+                  "V - Valentine       r - reset  \r\n"
+                  "A - Auto Test mode                  \r\n"
+                  "> ");
+        if (automode == -1 || pc.readable()) {
+            automode = -1;
+            q = getchar();
+        } else if (automode >= 0) {
+            q = modelist[automode];
+        }
+        switch(q) {
+            case 'A':
+                automode = 0;
+                break;
+            case 'B':
+                BacklightTest(lcd, pc, 2);
+                break;
+            case 'b':
+                BacklightTest2(lcd, pc);
+                break;
+            case 'D':
+                DOSColorTest(lcd, pc);
+                break;
+            case 'W':
+                WebColorTest(lcd, pc);
+                break;
+            case 't':
+                TextCursorTest(lcd, pc);
+                break;
+            case 'F':
+                ExternalFontTest(lcd, pc);
+                break;
+            case 'L':
+                LineTest(lcd, pc);
+                break;
+            case 'R':
+                RectangleTest(lcd, pc);
+                break;
+            case 'O':
+                RoundRectTest(lcd, pc);
+                break;
+            case 'T':
+                TriangleTest(lcd, pc);
+                break;
+            case 'C':
+                CircleTest(lcd, pc);
+                break;
+            case 'E':
+                EllipseTest(lcd, pc);
+                break;
+            case 'V':
+                ValentineMessage(lcd, pc);
+                break;
+            case 'r':
+                pc.printf("Resetting ...\r\n");
+                wait_ms(20);
+                mbed_reset();
+                break;
+            default:
+                printf("huh?\n");
+                break;
+        }
+        if (automode >= 0) {
+            automode++;
+            if (automode >= sizeof(modelist))
+                automode = 0;
+            wait_ms(2000);
+        }
+        wait_ms(200);
+    }
+}
+
+#endif // TESTENABLE
\ No newline at end of file