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

Fork of RA8875 by David Smart

Revision:
38:38d503b4fad6
Parent:
37:f19b7e7449dc
Child:
39:805cee56872d
--- a/RA8875.cpp	Sat Jan 25 19:47:33 2014 +0000
+++ b/RA8875.cpp	Sat Jan 25 21:18:03 2014 +0000
@@ -93,6 +93,24 @@
 }
 #endif
 
+RetCode_t RA8875::WriteCommandW(uint8_t command, uint16_t data)
+{
+    #if 1
+    WriteCommand(command, data & 0xFF);
+    WriteCommand(command+1, data >> 8);
+    #else
+    INFO("WriteCommandW(%02X, %04X)", command, data);
+    select(true);
+    spiwrite(0x80);
+    spiwrite(command);
+//    spiwrite(0x00);
+    spiwrite(data & 0xFF);
+    spiwrite(data >> 8);
+    select(false);
+    #endif
+    return noerror;
+}
+
 RetCode_t RA8875::WriteCommand(unsigned char command, unsigned int data)
 {
     select(true);
@@ -106,6 +124,16 @@
     return noerror;
 }
 
+RetCode_t RA8875::WriteDataW(uint16_t data)
+{
+    select(true);
+    spiwrite(0x00);
+    spiwrite(data & 0xFF);
+    spiwrite(data >> 8);
+    select(false);
+    return noerror;
+}
+
 RetCode_t RA8875::WriteData(unsigned char data)
 {
     select(true);
@@ -174,12 +202,12 @@
     return height() / fontheight();
 }
 
-uint16_t RA8875::width(void)
+dim_t RA8875::width(void)
 {
     return (ReadCommand(0x14) + 1) * 8;
 }
 
-uint16_t RA8875::height(void)
+dim_t RA8875::height(void)
 {
     return (ReadCommand(0x19) | (ReadCommand(0x1A) << 8)) + 1;
 }
@@ -187,10 +215,8 @@
 RetCode_t RA8875::SetTextCursor(loc_t x, loc_t 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);
+    WriteCommandW(0x2A, x);
+    WriteCommandW(0x2C, y);
     return noerror;
 }
 
@@ -342,16 +368,14 @@
         if (c == '\r') {
             loc_t x;
             x = ReadCommand(0x30) | (ReadCommand(0x31) << 8);   // Left edge of active window
-            WriteCommand(0x2A, x & 0xFF);
-            WriteCommand(0x2B, x >> 8);
+            WriteCommandW(0x2A, x);
         } else if (c == '\n') {
             loc_t y;
             y = ReadCommand(0x2C) | (ReadCommand(0x2D) << 8);   // current y location
             y += fontheight();
             if (y > height())               // @TODO after bottom of active window, then scroll window?
                 y = 0;
-            WriteCommand(0x2C, y & 0xFF);
-            WriteCommand(0x2D, y >> 8);
+            WriteCommandW(0x2C, y);
         } else {
             WriteCommand(0x02);                 // RA8875 Internal Fonts
             select(true);
@@ -378,8 +402,7 @@
 
 RetCode_t RA8875::putp(color_t pixel)
 {
-    WriteData(pixel >> 8);
-    WriteData(pixel & 0xFF);
+    WriteDataW((pixel>>8) | (pixel<<8));
     return noerror;   
 }
 
@@ -422,24 +445,18 @@
 
 RetCode_t RA8875::SetGraphicsCursor(loc_t x, loc_t y)
 {
-    WriteCommand(0x46, x & 0xFF);
-    WriteCommand(0x47, x >> 8);
-    WriteCommand(0x48, y & 0xFF);
-    WriteCommand(0x49, y >> 8);
+    WriteCommandW(0x46, x);
+    WriteCommandW(0x48, 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);
-    WriteCommand(0x30, x & 0xFF);   // HSAW0
-    WriteCommand(0x31, x >> 8);     // HSAW1
-    WriteCommand(0x32, y & 0xFF);    // VSAW0
-    WriteCommand(0x33, y >> 8);      // VSAW1
-    WriteCommand(0x34, (x+width-1) & 0xFF);  // HEAW0
-    WriteCommand(0x35, (x+width-1) >> 8);    // HEAW1
-    WriteCommand(0x36, (y+height-1) & 0xFF); // VEAW0
-    WriteCommand(0x37, (y+height-1) >> 8);   // VEAW1
+    WriteCommandW(0x30, x);
+    WriteCommandW(0x32, y);
+    WriteCommandW(0x34, (x+width-1));
+    WriteCommandW(0x36, (y+height-1));
     SetGraphicsCursor(x,y);
     return noerror;
 }
@@ -478,9 +495,8 @@
     color_t color = GetForeColor();
     WriteCommand(0x40,0x00);    // Graphics write mode
     SetGraphicsCursor(x, y);
-    WriteCommand(0x02);         // start data write
-    WriteData(color & 0xFF);
-    WriteData(color >> 8);
+    WriteCommand(0x02);
+    WriteDataW(color);
     ret = noerror;
     REGISTERPERFORMANCE(PRF_DRAWPOINT);
     return ret;
@@ -495,15 +511,10 @@
 RetCode_t RA8875::line(loc_t x1, loc_t y1, loc_t x2, loc_t y2)
 {
     PERFORMANCE_RESET;
-    WriteCommand(0x91, x1 & 0xFF);
-    WriteCommand(0x92, x1 >> 8);
-    WriteCommand(0x93, y1 & 0xFF);
-    WriteCommand(0x94, y1 >> 8);
-    WriteCommand(0x95, x2 & 0xFF);
-    WriteCommand(0x96, x2 >> 8);
-    WriteCommand(0x97, y2 & 0xFF);
-    WriteCommand(0x98, y2 >> 8);
- 
+    WriteCommandW(0x91, x1);
+    WriteCommandW(0x93, y1);
+    WriteCommandW(0x95, x2);
+    WriteCommandW(0x97, y2);
     unsigned char drawCmd = 0x00;       // Line
     WriteCommand(0x90, drawCmd);
     WriteCommand(0x90, 0x80 + drawCmd); // Start drawing.
@@ -537,15 +548,10 @@
     } else if (y1 == y2) {
         line(x1, y1, x2, y2);
     } else {
-        WriteCommand(0x91, x1 & 0xFF);
-        WriteCommand(0x92, x1 >> 8);
-        WriteCommand(0x93, y1 & 0xFF);
-        WriteCommand(0x94, y1 >> 8);
-        WriteCommand(0x95, x2 & 0xFF);
-        WriteCommand(0x96, x2 >> 8);
-        WriteCommand(0x97, y2 & 0xFF);
-        WriteCommand(0x98, y2 >> 8);
-        
+        WriteCommandW(0x91, x1);
+        WriteCommandW(0x93, y1);
+        WriteCommandW(0x95, x2);
+        WriteCommandW(0x97, y2);
         unsigned char drawCmd = 0x10;   // Rectangle
         if (fillit == FILL)
             drawCmd |= 0x20;
@@ -587,26 +593,15 @@
     } else if (y1 == y2) {
         line(x1, y1, x2, y2);
     } else {
-        WriteCommand(0x91, x1 & 0xFF);
-        WriteCommand(0x92, x1 >> 8);
-        WriteCommand(0x93, y1 & 0xFF);
-        WriteCommand(0x94, y1 >> 8);
-        WriteCommand(0x95, x2 & 0xFF);
-        WriteCommand(0x96, x2 >> 8);
-        WriteCommand(0x97, y2 & 0xFF);
-        WriteCommand(0x98, y2 >> 8);
-
-        WriteCommand(0xA1, radius1 & 0xFF);
-        WriteCommand(0xA2, radius1 >> 8);
-        WriteCommand(0xA3, radius2 & 0xFF);
-        WriteCommand(0xA4, radius2 >> 8);
-
+        WriteCommandW(0x91, x1);
+        WriteCommandW(0x93, y1);
+        WriteCommandW(0x95, x2);
+        WriteCommandW(0x97, y2);
+        WriteCommandW(0xA1, radius1);
+        WriteCommandW(0xA3, radius2);
         // Should not need this...
-        WriteCommand(0xA5, 0 & 0xFF);
-        WriteCommand(0xA6, 0 >> 8);
-        WriteCommand(0xA7, 0 & 0xFF);
-        WriteCommand(0xA8, 0 >> 8);
-        
+        WriteCommandW(0xA5, 0);
+        WriteCommandW(0xA7, 0);
         unsigned char drawCmd = 0x20;       // Rounded Rectangle
         if (fillit == FILL)
             drawCmd |= 0x40;
@@ -649,21 +644,12 @@
     if (x1 == x2 && y1 == y2 && x1 == x3 && y1 == y3) {
         pixel(x1, y1);
     } else {
-        WriteCommand(0x91, x1 & 0xFF);
-        WriteCommand(0x92, x1 >> 8);
-        WriteCommand(0x93, y1 & 0xFF);
-        WriteCommand(0x94, y1 >> 8);
-        
-        WriteCommand(0x95, x2 & 0xFF);
-        WriteCommand(0x96, x2 >> 8);
-        WriteCommand(0x97, y2 & 0xFF);
-        WriteCommand(0x98, y2 >> 8);
-        
-        WriteCommand(0xA9, x3 & 0xFF);
-        WriteCommand(0xAA, x3 >> 8);
-        WriteCommand(0xAB, y3 & 0xFF);
-        WriteCommand(0xAC, y3 >> 8);
-        
+        WriteCommandW(0x91, x1);
+        WriteCommandW(0x93, y1);
+        WriteCommandW(0x95, x2);
+        WriteCommandW(0x97, y2);
+        WriteCommandW(0xA9, x3);
+        WriteCommandW(0xAB, y3);
         unsigned char drawCmd = 0x01;       // Triangle
         if (fillit == FILL)
             drawCmd |= 0x20;
@@ -700,12 +686,9 @@
     } else if (radius == 1) {
         pixel(x,y);
     } else {
-        WriteCommand(0x99, x & 0xFF);
-        WriteCommand(0x9a, x >> 8);
-        WriteCommand(0x9b, y & 0xFF);
-        WriteCommand(0x9c, y >> 8);
+        WriteCommandW(0x99, x);
+        WriteCommandW(0x9B, y);
         WriteCommand(0x9d, radius & 0xFF);
-        
         unsigned char drawCmd = 0x00;       // Circle
         if (fillit == FILL)
             drawCmd |= 0x20;
@@ -740,15 +723,10 @@
     } else if (radius1 == 1 && radius2 == 1) {
         pixel(x, y);
     } else {
-        WriteCommand(0xA5, x & 0xFF);
-        WriteCommand(0xA6, x >> 8);
-        WriteCommand(0xA7, y & 0xFF);
-        WriteCommand(0xA8, y >> 8);
-        WriteCommand(0xA1, radius1 & 0xFF);
-        WriteCommand(0xA2, radius1 >> 8);
-        WriteCommand(0xA3, radius2 & 0xFF);
-        WriteCommand(0xA4, radius2 >> 8);
-        
+        WriteCommandW(0xA5, x);
+        WriteCommandW(0xA7, y);
+        WriteCommandW(0xA1, radius1);
+        WriteCommandW(0xA3, radius2);
         unsigned char drawCmd = 0x00;   // Ellipse
         if (fillit == FILL)
             drawCmd |= 0x40;
@@ -1048,7 +1026,7 @@
     display.cls();
     wait_ms(200);
     display.puts(0,0, "RA8875 Backlight Test - Ramp up.");
-    for (int i=0; i < 255; i++) {
+    for (int i=0; i <= 255; i++) {
         unsigned int w = (ramptime * 1000)/ 256;
         sprintf(buf, "%3d, %4d", i, w);
         display.puts(100,100,buf);