This is the David Smart RA8875 Library with mods for working with FRDM-K64F

Revision:
194:53c18f0e7922
Parent:
191:0fad2e45e196
Child:
195:17e176dbd6eb
--- a/RA8875.cpp	Mon Nov 25 19:55:17 2019 +0000
+++ b/RA8875.cpp	Tue Feb 11 15:35:02 2020 +0000
@@ -1115,19 +1115,19 @@
 
 RetCode_t RA8875::SetTextCursorControl(cursor_t cursor, bool blink)
 {
-    unsigned char mwcr0 = ReadCommand(0x40) & 0x0F; // retain direction, auto-increase
-    unsigned char mwcr1 = ReadCommand(0x41) & 0x01; // retain currently selected layer
+    unsigned char mwcr0 = ReadCommand(RA8875_MWCR0) & 0x0F; // retain direction, auto-increase
+    unsigned char mwcr1 = ReadCommand(RA8875_MWCR1) & 0x01; // retain currently selected layer
     unsigned char horz = 0;
     unsigned char vert = 0;
 
-    mwcr0 |= 0x80;                  // text mode
+    mwcr0 |= 0x80;                          // text mode
     if (cursor != NOCURSOR)
-        mwcr0 |= 0x40;              // visible
+        mwcr0 |= 0x40;                      // visible
     if (blink)
-        mwcr0 |= 0x20;              // blink
+        mwcr0 |= 0x20;                      // blink
     WriteCommand(RA8875_MWCR0, mwcr0);      // configure the cursor
     WriteCommand(RA8875_MWCR1, mwcr1);      // close the graphics cursor
-    WriteCommand(RA8875_BTCR, 0x1f);       // The cursor flashing cycle
+    WriteCommand(RA8875_BTCR, 0x1f);        // The cursor flashing cycle
     switch (cursor) {
         case IBEAM:
             horz = 0x01;
@@ -1163,7 +1163,7 @@
 
 RA8875::orientation_t RA8875::GetOrientation()
 {
-    uint8_t dpcrVal = ReadCommand(0x20);
+    uint8_t dpcrVal = ReadCommand(RA8875_DPCR);
 
     dpcrVal &= 0x0C;       // keep the scan direction bits
     switch (dpcrVal) {
@@ -1181,13 +1181,13 @@
 
 RetCode_t RA8875::SetOrientation(RA8875::orientation_t angle)
 {
-    uint8_t fncr1Val = ReadCommand(0x22);
-    uint8_t dpcrVal = ReadCommand(0x20);
-    RetCode_t r;
-
-    screen_orientation = angle;
-    fncr1Val &= ~0x10;      // remove the old font rotation bit
+    uint8_t fncr1Val = ReadCommand(RA8875_FNCR1);
+    uint8_t dpcrVal = ReadCommand(RA8875_DPCR);
+    uint8_t mwcr0 = ReadCommand(RA8875_MWCR0);
+    
+    fncr1Val &= ~0x10;      // remove the old direction bit
     dpcrVal &= ~0x0C;       // remove the old scan direction bits
+    mwcr0 &= 0xE3;          // remove the old direction bits
     switch (angle) {
         case RA8875::normal:
             //fncr1Val |= 0x10;
@@ -1197,25 +1197,28 @@
         case RA8875::rotate_90:
             fncr1Val |= 0x10;
             dpcrVal |= 0x08;
+            mwcr0 |= 0x00;
             portraitmode = true;
             break;
         case RA8875::rotate_180:
             //fncr1Val |= 0x00;
             dpcrVal |= 0x0C;
+            //mwcr0 |= 0x00;
             portraitmode = false;
             break;
         case RA8875::rotate_270:
             fncr1Val |= 0x10;
             dpcrVal |= 0x04;
+            mwcr0 |= 0x00;
             portraitmode = true;
             break;
         default:
             return bad_parameter;
     }
     INFO("Orientation: %d, %d", angle, portraitmode);
-    r = WriteCommand(RA8875_FNCR1, fncr1Val);
-    r = WriteCommand(RA8875_DPCR, dpcrVal);
-    return r;
+    WriteCommand(RA8875_MWCR0, mwcr0);
+    WriteCommand(RA8875_FNCR1, fncr1Val);
+    return WriteCommand(RA8875_DPCR, dpcrVal);
 }
 
 
@@ -1316,8 +1319,8 @@
     }
 }
 
-// Questions to ponder -
-// - if we choose to wrap to the next line, because the character won't fit on the current line,
+
+
 // Questions to ponder -
 // - if we choose to wrap to the next line, because the character won't fit on the current line,
 //      should it erase the space to the width of the screen (in case there is leftover junk there)?
@@ -1363,10 +1366,9 @@
     if (c) {
         unsigned char mwcr0;
 
-        mwcr0 = ReadCommand(0x40);
-        if ((mwcr0 & 0x80) == 0x00) {
+        mwcr0 = ReadCommand(RA8875_MWCR0);
+        if ((mwcr0 & 0x80) == 0x00)
             WriteCommand(RA8875_MWCR0, 0x80 | mwcr0);    // Put in Text mode if not already
-        }
         if (c == '\r') {
             loc_t x;
             x = ReadCommand(0x30) | (ReadCommand(0x31) << 8);   // Left edge of active window
@@ -1392,7 +1394,10 @@
 
 RetCode_t RA8875::_StartGraphicsStream(void)
 {
-    WriteCommand(RA8875_MWCR0,0x00);    // Graphics write mode
+    unsigned char mwcr0;
+
+    mwcr0 = ReadCommand(RA8875_MWCR0);
+    WriteCommand(RA8875_MWCR0, mwcr0 & ~0x80);    // Graphics write mode
     WriteCommand(RA8875_MRWC);         // Prepare for streaming data
     return noerror;
 }
@@ -1421,10 +1426,14 @@
 void RA8875::puts(const char * string)
 {
     if (font == NULL) {
-        WriteCommand(RA8875_MWCR0,0x80);    // Put in Text mode if internal font
+        unsigned char mwcr0;
+
+        mwcr0 = ReadCommand(RA8875_MWCR0);
+        if ((mwcr0 & 0x80) == 0x00)
+            WriteCommand(RA8875_MWCR0, 0x80 | mwcr0);    // Put in Text mode if not already
     }
     point_t cursor = GetTextCursor();
-    while (*string) {           // @TODO calling individual _putc is slower... optimizations?
+    while (string && *string) {           // @TODO calling individual _putc is slower... optimizations?
         if (wordwrap) {
             const char * p = string;
             const char * pSpace = NULL;
@@ -1688,9 +1697,12 @@
 color_t RA8875::getPixel(loc_t x, loc_t y)
 {
     color_t pixel;
+    unsigned char mwcr0;
 
     PERFORMANCE_RESET;
-    WriteCommand(RA8875_MWCR0,0x00);    // Graphics write mode
+    mwcr0 = ReadCommand(RA8875_MWCR0);
+    if (mwcr0 & 0x80)
+        WriteCommand(RA8875_MWCR0, mwcr0 & ~0x80);    // Graphics write mode
     SetGraphicsCursorRead(x, y);
     WriteCommand(RA8875_MRWC);
     _select(true);
@@ -1711,10 +1723,13 @@
 RetCode_t RA8875::getPixelStream(color_t * p, uint32_t count, loc_t x, loc_t y)
 {
     color_t pixel;
+    unsigned char mwcr0;
     RetCode_t ret = noerror;
 
     PERFORMANCE_RESET;
-    ret = WriteCommand(RA8875_MWCR0,0x00);    // Graphics write mode
+    mwcr0 = ReadCommand(RA8875_MWCR0);
+    if (mwcr0 & 0x80)
+        WriteCommand(RA8875_MWCR0, mwcr0 & ~0x80);    // Graphics write mode
     ret = SetGraphicsCursorRead(x, y);
     ret = WriteCommand(RA8875_MRWC);
     _select(true);