Forked para SNOCC

Dependencies:   GPS

Dependents:   SNOCC_V1 SNOCC_V2

Fork of RA8875 by SNOCC

Revision:
29:422616aa04bd
Parent:
28:ed102fc442c4
Child:
31:c72e12cd5c67
--- a/RA8875.cpp	Fri Jan 17 17:24:05 2014 +0000
+++ b/RA8875.cpp	Sun Jan 19 04:24:16 2014 +0000
@@ -12,7 +12,7 @@
 DigitalOut zz3(LED3);
 DigitalOut zz4(LED4);
 
-//#define DEBUG "RAIO"
+#define DEBUG "RAIO"
 // ...
 // INFO("Stuff to show %d", var); // new-line is automatically appended
 //
@@ -148,7 +148,7 @@
     if (font == NULL)
         return (((ReadCommand(0x22) >> 2) & 0x3) + 1) * 16;
     else
-        return width() / font[1];
+        return font[1];
 }
 
 unsigned int RA8875::fontheight(void)
@@ -156,7 +156,7 @@
     if (font == NULL)
         return (((ReadCommand(0x22) >> 0) & 0x3) + 1) * 16;
     else
-        return height() / font[2];
+        return font[2];
 }
 
 RetCode_t RA8875::locate(unsigned int x, unsigned int y)
@@ -176,24 +176,42 @@
 
 int RA8875::width(void)
 {
-    return RA8875_DISPLAY_WIDTH;
+    //return RA8875_DISPLAY_WIDTH;
+    return (ReadCommand(0x14) + 1) * 8;
 }
 
 int RA8875::height(void)
 {
-    return RA8875_DISPLAY_HEIGHT;
+    //return RA8875_DISPLAY_HEIGHT;
+    return (ReadCommand(0x19) | (ReadCommand(0x1A) << 8)) + 1;
 }
 
 RetCode_t RA8875::SetTextCursor(unsigned int x, unsigned int y)
 {
+    cursor_x = x; cursor_y = y;     // for non-internal fonts
     WriteCommand(0x2A, x & 0xFF);
     WriteCommand(0x2B, x >> 8);
     WriteCommand(0x2C, y & 0xFF);
     WriteCommand(0x2D, y >> 8);
-    INFO("SetTextCursor(%d,%d)", x,y);
     return noerror;
 }
 
+unsigned int RA8875::GetTextCursor_Y(void)
+{
+    if (font == NULL)
+        return ReadCommand(0x2C) | (ReadCommand(0x2D) << 8);
+    else
+        return cursor_y;
+}
+
+unsigned int RA8875::GetTextCursor_X(void)
+{
+    if (font == NULL)
+        return ReadCommand(0x2A) | (ReadCommand(0x2B) << 8);
+    else
+        return cursor_x;
+}
+
 RetCode_t RA8875::SetTextCursorControl(cursor_t cursor, bool blink)
 {
     unsigned char mwcr0 = ReadCommand(0x40) & 0x0F; // retain direction, auto-increase
@@ -282,8 +300,47 @@
 
 int RA8875::_putc(int c)
 {
+    if (font == NULL) {
+        return _internal_putc(c);
+    } else {
+        return _external_putc(c);
+    }
+}
+
+int RA8875::_external_putc(int c)
+{
     if (c) {
         if (c == '\r') {
+            cursor_x = 0;
+        } else if (c == '\n') {
+            cursor_y += font[2];
+        } else {
+            int advance = character(cursor_x, cursor_y, c);     // advance tells us how many pixels we advanced
+            if (advance) {
+                cursor_x += advance;
+                if (cursor_x >= width()) {
+                    cursor_x = 0;
+                    cursor_y += font[2];
+                    if (cursor_y >= height()) {
+                        cursor_y = 0;               // @todo Should it scroll?
+                    }
+                }
+            }
+        }
+    }
+    return c;
+}
+
+int RA8875::_internal_putc(int c)
+{
+    if (c) {
+        unsigned char mwcr0;
+        
+        mwcr0 = ReadCommand(0x40);
+        if ((mwcr0 & 0x80) == 0x00) {
+            WriteCommand(0x40, 0x80 | mwcr0);    // Put in Text mode if not already
+        }
+        if (c == '\r') {
             unsigned int x;
             x = ReadCommand(0x30) | (ReadCommand(0x31) << 8);   // Left edge of active window
             WriteCommand(0x2A, x & 0xFF);
@@ -292,33 +349,23 @@
             unsigned int y;
             y = ReadCommand(0x2C) | (ReadCommand(0x2D) << 8);   // current y location
             y += fontheight();
-            if (y > height())               // @TODO > active window, then scroll?
+            if (y > height())               // @TODO after bottom of active window, then scroll window?
                 y = 0;
             WriteCommand(0x2C, y & 0xFF);
             WriteCommand(0x2D, y >> 8);
         } else {
-            if (font == NULL) {
-                unsigned char mwcr0 = ReadCommand(0x40);
-                
-                if (mwcr0 & 0x80 == 0x00)
-                    WriteCommand(0x40,0x80);
-                WriteCommand(0x02);
-                select(true);
-                WriteData(c);
-                while (ReadStatus() & 0x80)
-                    wait_us(POLLWAITuSec);            // Chk_Busy();
-                select(false);
-            } else {
-                unsigned int x = (ReadCommand(0x2A) | (ReadCommand(0x2B) << 8)) / fontwidth();
-                unsigned int y = (ReadCommand(0x2C) | (ReadCommand(0x2D) << 8)) / fontheight();
-                character(x,y,c);
-            }
-            // @TODO right of active window, then wrap?
+            WriteCommand(0x02);                 // RA8875 Internal Fonts
+            select(true);
+            WriteData(c);
+            while (ReadStatus() & 0x80)
+                wait_us(POLLWAITuSec);          // Chk_Busy();
+            select(false);
         }
     }
     return c;
 }
 
+
 void RA8875::puts(unsigned int x, unsigned int y, const char * string)
 {
     SetTextCursor(x,y);
@@ -327,14 +374,17 @@
 
 void RA8875::puts(const char * string)
 {
+    unsigned char mwcr0 = ReadCommand(0x40);
+                
+    if ((mwcr0 & 0x80) == 0x00)
+        WriteCommand(0x40,0x80);    // Put in Text mode if not already
+
     if (*string != '\0') {
-        INFO("puts(%s)", string);
         #if 1
-        while (*string) {           // @TODO calling individual _putc is slower... anything to do?
+        while (*string) {           // @TODO calling individual _putc is slower... optimizations?
             _putc(*string++);
         }
         #else
-        WriteCommand(0x40,0x80);    // Put display into text mode
         WriteCommand(0x02);
         select(true);
         while (*string != '\0') {
@@ -349,7 +399,6 @@
 }
 
 RetCode_t RA8875::SetMemoryCursor(unsigned int x, unsigned int y)
-
 {
     WriteCommand(0x46, x & 0xFF);
     WriteCommand(0x47, x >> 8);
@@ -375,6 +424,7 @@
 {
     PERFORMANCE_RESET;
     clsw(FULLWINDOW);
+    cursor_x = cursor_y = 0;
     REGISTERPERFORMANCE(PRF_CLS);
     return noerror;
 }
@@ -400,7 +450,6 @@
     RetCode_t ret;
     
     PERFORMANCE_RESET;
-    #if 1
     color_t color = GetForeColor();
     WriteCommand(0x40,0x00);    // Graphics write mode
     SetMemoryCursor(x, y);
@@ -408,11 +457,6 @@
     WriteData(color & 0xFF);
     WriteData(color >> 8);
     ret = noerror;
-    #else
-    // There isn't actually a set pixel function that I found
-    // so we'll emulate it as we can.
-    ret = line(x,y, x,y);
-    #endif
     REGISTERPERFORMANCE(PRF_DRAWPOINT);
     return ret;
 }
@@ -744,9 +788,9 @@
 {
     unsigned char b;
     
-    if (brightness > 1.0)
+    if (brightness >= 1.0)
         b = 255;
-    else if (brightness < 0)
+    else if (brightness <= 0.0)
         b = 0;
     else
         b = (unsigned char)(brightness * 255);
@@ -757,7 +801,8 @@
 RetCode_t RA8875::set_font(const unsigned char * _font)
 {
     font = _font;
-    return noerror;     // trusting them, but we could put some checks in here...
+    GraphicsDisplay::set_font(_font);
+    return noerror;     // trusting them, but it might be good to put some checks in here...
 }
 
 RetCode_t RA8875::background(color_t color)
@@ -961,15 +1006,21 @@
 
 void BacklightTest(RA8875 & display, Serial & pc, float ramptime)
 {
+    char buf[60];
+    
     pc.printf("Backlight Test - ramp over %f sec.\r\n", ramptime);
     display.Backlight_u8(0);
-    display.background(Black);
+    display.background(White);
     display.foreground(Blue);
     display.cls();
+    wait_ms(200);
     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(0,40,buf);
         display.Backlight_u8(i);
-        wait_ms((ramptime * 1000)/ 256);
+        wait_ms(w);
     }
 }
 
@@ -1092,21 +1143,6 @@
     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;
@@ -1262,7 +1298,7 @@
                 automode = 0;
                 break;
             case 'B':
-                BacklightTest(lcd, pc, 2);
+                BacklightTest(lcd, pc, 4);
                 break;
             case 'b':
                 BacklightTest2(lcd, pc);