KSM edits to RA8875

Dependents:   Liz_Test_Code

Revision:
98:ecebed9b80b2
Parent:
96:40b74dd3695b
Child:
100:0b084475d5a9
--- a/RA8875.cpp	Sat Nov 28 15:39:44 2015 +0000
+++ b/RA8875.cpp	Thu Dec 17 12:16:40 2015 +0000
@@ -15,10 +15,10 @@
 // INFO("Stuff to show %d", var); // new-line is automatically appended
 //
 #if (defined(DEBUG) && !defined(TARGET_LPC11U24))
-#define INFO(x, ...) std::printf("[INF %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
-#define WARN(x, ...) std::printf("[WRN %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
-#define ERR(x, ...)  std::printf("[ERR %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
-static void HexDump(char * title, uint8_t * p, int count)
+#define INFO(x, ...) std::printf("[INF %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
+#define WARN(x, ...) std::printf("[WRN %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
+#define ERR(x, ...)  std::printf("[ERR %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
+static void HexDump(const char * title, const uint8_t * p, int count)
 {
     int i;
     char buf[100] = "0000: ";
@@ -108,10 +108,10 @@
 
 RetCode_t RA8875::init(int width, int height, int color_bpp, bool poweron, bool keypadon, bool touchscreenon)
 {
-    font = NULL;                        // no external font, use internal.
-    pKeyMap = DefaultKeyMap;            // set default key map
-    _select(false);                      // deselect the display
-    frequency(RA8875_DEFAULT_SPI_FREQ); // data rate
+    font = NULL;                                // no external font, use internal.
+    pKeyMap = DefaultKeyMap;                    // set default key map
+    _select(false);                             // deselect the display
+    frequency(RA8875_DEFAULT_SPI_FREQ);         // data rate
     Reset();
     WriteCommand(0x88, 0x0B);                   // PLLC1 - Phase Lock Loop registers
     wait_ms(1);
@@ -144,7 +144,7 @@
     WriteCommand(0x1c, 0x00);                   //VNDR1 //Vertical Non-Display Period Bit [8]
     WriteCommand(0x1d, 0x0e);                   //VSTR0 //VSYNC Start Position[7:0]
     WriteCommand(0x1e, 0x06);                   //VSTR1 //VSYNC Start Position[8]
-    WriteCommand(0x1f, 0x01);                   //VPWR //VSYNC Polarity ,VSYNC Pulse Width[6:0]
+    WriteCommand(0x1f, 0x01);                   //VPWR  //VSYNC Polarity ,VSYNC Pulse Width[6:0]
 
     if (width >= 800 && height >= 480 && color_bpp > 8) {
         WriteCommand(0x20, 0x00);               // DPCR - 1-layer mode when the resolution is too high
@@ -563,7 +563,7 @@
     if (font == NULL)
         return (((ReadCommand(0x22) >> 2) & 0x3) + 1) * 8;
     else
-        return font[1];
+        return extFontWidth;
 }
 
 
@@ -572,7 +572,7 @@
     if (font == NULL)
         return (((ReadCommand(0x22) >> 0) & 0x3) + 1) * 16;
     else
-        return font[2];
+        return extFontHeight;
 }
 
 
@@ -600,7 +600,6 @@
         return screenheight;
     else
         return screenwidth;
-//    return (ReadCommand(0x14) + 1) * 8;
 }
 
 
@@ -610,7 +609,6 @@
         return screenwidth;
     else
         return screenheight;
-//    return (ReadCommand(0x19) | (ReadCommand(0x1A) << 8)) + 1;
 }
 
 
@@ -625,6 +623,7 @@
 
 RetCode_t RA8875::SetTextCursor(loc_t x, loc_t y)
 {
+    INFO("SetTextCursor(%d, %d)", x, y);
     cursor_x = x;     // set these values for non-internal fonts
     cursor_y = y;
     WriteCommandW(0x2A, x);
@@ -635,19 +634,27 @@
 
 loc_t RA8875::GetTextCursor_Y(void)
 {
+    loc_t y;
+    
     if (font == NULL)
-        return ReadCommand(0x2C) | (ReadCommand(0x2D) << 8);
+        y = ReadCommand(0x2C) | (ReadCommand(0x2D) << 8);
     else
-        return cursor_y;
+        y = cursor_y;
+    INFO("GetTextCursor_Y = %d", y);
+    return y;
 }
 
 
 loc_t RA8875::GetTextCursor_X(void)
 {
+    loc_t x;
+    
     if (font == NULL)
-        return ReadCommand(0x2A) | (ReadCommand(0x2B) << 8);
+        x = ReadCommand(0x2A) | (ReadCommand(0x2B) << 8);
     else
-        return cursor_x;
+        x = cursor_x;
+    INFO("GetTextCursor_X = %d", x);
+    return x;
 }
 
 
@@ -745,7 +752,7 @@
             vScale >= 1 && vScale <= 4) {
         uint8_t fncr1Val = ReadCommand(0x22);
         
-        fncr1Val &= ~0x10;      // do not disturbe the rotate flag
+        fncr1Val &= ~0x10;      // do not disturb the rotate flag
         if (alignment == align_full)
             fncr1Val |= 0x80;
         if (fillit == NOFILL)
@@ -793,15 +800,15 @@
         if (c == '\r') {
             cursor_x = 0;
         } else if (c == '\n') {
-            cursor_y += font[2];
+            cursor_y += extFontHeight;
         } else {
             int advance = character(cursor_x, cursor_y, c);     // advance tells us how many pixels we advanced
-            //INFO("x,y,advance %d,%d,%d", cursor_x, cursor_y, advance);
+            INFO("x,y,advance %d,%d,%d '%c", cursor_x, cursor_y, advance, c);
             if (advance) {
                 cursor_x += advance;
                 if (cursor_x >= width()) {
                     cursor_x = 0;
-                    cursor_y += font[2];
+                    cursor_y += extFontHeight;
                     if (cursor_y >= height()) {
                         cursor_y = 0;               // @todo Should it scroll?
                     }
@@ -878,10 +885,11 @@
     unsigned char mwcr0 = ReadCommand(0x40);
 
     if (font == NULL) {
-        if ((mwcr0 & 0x80) == 0x00)
+        if ((mwcr0 & 0x80) == 0x00) {
             WriteCommand(0x40,0x80);    // Put in Text mode if not already
-    } else {
-        _StartGraphicsStream();
+        }
+    //} else {
+    //    _StartGraphicsStream();
     }
     if (*string != '\0') {
 #if 1
@@ -899,8 +907,8 @@
         _select(false);
 #endif
     }
-    if (font)
-        _EndGraphicsStream();
+    //if (font)
+    //    _EndGraphicsStream();
 }
 
 
@@ -1474,10 +1482,33 @@
         SetTextCursor(cursor_x, cursor_y);  // soft-font cursor -> hw cursor
     }
     font = _font;
-    GraphicsDisplay::set_font(_font);
-    return noerror;     // trusting them, but it might be good to put some checks in here...
+    return GraphicsDisplay::set_font(_font);    // trusting them, but it might be good to put some checks in here...
 }
 
+RetCode_t RA8875::SelectUserFont(const uint8_t * _font)
+{
+    INFO("Cursor(%d,%d)  %p", cursor_x, cursor_y, _font);
+    INFO("Text C(%d,%d)", GetTextCursor_X(), GetTextCursor_Y());
+    if (_font) {
+        HexDump("Font Memory", _font, 16);
+        extFontHeight = _font[6];
+        uint32_t totalWidth = 0;
+        uint16_t firstChar = _font[3] * 256 + _font[2];
+        uint16_t lastChar  = _font[5] * 256 + _font[4];
+        uint16_t i;
+        
+        for (i=firstChar; i<=lastChar; i++) {
+            // 8 bytes of preamble to the first level lookup table
+            uint16_t offsetToCharLookup = 8 + 4 * (i - firstChar);    // 4-bytes: width(pixels), 16-bit offset from table start, 0
+            totalWidth += _font[offsetToCharLookup];
+        }
+        extFontWidth = totalWidth / (lastChar - firstChar);
+        INFO("Font Metrics: Avg W: %2d, H: %2d, First:%d, Last:%d", extFontWidth, extFontHeight, firstChar, lastChar);
+    }
+    SetTextCursor(GetTextCursor_X(), GetTextCursor_Y());  // soft-font cursor -> hw cursor
+    font = _font;
+    return GraphicsDisplay::SelectUserFont(_font);
+}
 
 RetCode_t RA8875::background(color_t color)
 {
@@ -1631,7 +1662,7 @@
     color_t * pixelBuffer = NULL;
     color_t * pixelBuffer2 = NULL;
     
-    INFO("(%d,%d) - (%d,%d) %s", x,y,w,h,Name_BMP);
+    INFO("(%d,%d) - (%d,%d)", x,y,w,h);
     if (x >= 0 && x < width()
             && y >= 0 && y < height()
             && w > 0 && x + w <= width()
@@ -1971,8 +2002,8 @@
 
 #ifdef TESTENABLE
 
-#include "Arial12x12.h"
-#include "Small_6.h"
+#include "BPG_Arial08x08.h"
+#include "BPG_Arial20x20.h"
 
 //      ______________  ______________  ______________  _______________
 //     /_____   _____/ /  ___________/ /  ___________/ /_____   ______/
@@ -2202,12 +2233,14 @@
     display.Backlight(1);
     display.puts(0,0, "External Font Test.");
 
-    display.set_font(Small_6);
+    display.SelectUserFont(BPG_Arial08x08);
     display.puts(0,30, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\r\n");
 
-    display.set_font(Arial12x12);
+    display.SelectUserFont(BPG_Arial20x20);
     display.puts("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\r\n");
-    display.set_font();     // restore to internal
+    
+    display.SelectUserFont();
+    //display.set_font();     // restore to internal
 
     display.puts("Normal font again.");
     //display.window(0,0, display.width(), display.height());
@@ -2590,7 +2623,7 @@
 void TouchPanelTest(RA8875 & display, Serial & pc)
 {
     Timer t;
-    loc_t x, y;
+    int x, y;
     tpMatrix_t calmatrix;
     
     display.background(Black);