Forked para SNOCC

Dependencies:   GPS

Dependents:   SNOCC_V1 SNOCC_V2

Fork of RA8875 by SNOCC

Revision:
43:3becae133285
Parent:
41:2956a0a221e5
Child:
44:207594dece70
--- a/RA8875.cpp	Sun Feb 23 17:58:39 2014 +0000
+++ b/RA8875.cpp	Sun Mar 09 23:45:23 2014 +0000
@@ -24,6 +24,7 @@
 
 #define RA8875_DISPLAY_WIDTH  480
 #define RA8875_DISPLAY_HEIGHT 272
+#define RA8875_COLORDEPTH_BPP 16    /* Not an API */
 
 #ifdef PERF_METRICS
 #define PERFORMANCE_RESET performance.reset()
@@ -63,10 +64,26 @@
 #endif
 }
 
+
 //RA8875::~RA8875()
 //{
 //}
 
+
+RetCode_t RA8875::SelectLayer(uint16_t layer)
+{
+    unsigned char mwcr1 = ReadCommand(0x41) & ~0x01; // retain all but the currently selected layer
+
+    if (width() >= 800 && height() >= 480 && color_bpp() == 8) {
+        return bad_parameter;
+    } else if (layer > 1) {
+        return bad_parameter;
+    } else { // layer == 0 ro 1
+        WriteCommand(0x41, mwcr1 | layer);
+    }
+    return noerror;
+}
+
 #ifdef PERF_METRICS
 void RA8875::ClearPerformance()
 {
@@ -223,6 +240,14 @@
     return (ReadCommand(0x19) | (ReadCommand(0x1A) << 8)) + 1;
 }
 
+dim_t RA8875::color_bpp(void)
+{
+    if ((ReadCommand(0x10) & 0x0C) == 0x04)
+        return 16;
+    else
+        return 8;
+}
+
 RetCode_t RA8875::SetTextCursor(loc_t x, loc_t y)
 {
     cursor_x = x; cursor_y = y;     // for non-internal fonts
@@ -250,6 +275,7 @@
 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 horz = 0;
     unsigned char vert = 0;
     
@@ -259,7 +285,7 @@
     if (blink)
         mwcr0 |= 0x20;              // blink
     WriteCommand(0x40, mwcr0);      // configure the cursor
-    WriteCommand(0x41, 0x00);       // close the graphics cursor
+    WriteCommand(0x41, mwcr1);      // close the graphics cursor
     WriteCommand(0x44, 0x1f);       // The cursor flashing cycle
     switch (cursor) {
         case IBEAM:
@@ -831,7 +857,6 @@
     //       ___ ____
     // Data  ___X____
     spi.format(8, 3);           // 8 bits and clock to data phase 0
-    init();
     return noerror;
 }
 
@@ -847,7 +872,7 @@
     wait_ms(2);                     // no idea if I need to wait, or how long
     WriteCommand(0x01, 0x00);   // Display off, Remove reset
     wait_ms(2);                     // no idea if I need to wait, or how long    
-    init();
+    init(RA8875_DISPLAY_WIDTH, RA8875_DISPLAY_HEIGHT, RA8875_COLORDEPTH_BPP);
     return noerror;
 }
 
@@ -996,7 +1021,7 @@
     return noerror;
 }
 
-RetCode_t RA8875::init(void)
+RetCode_t RA8875::init(int width, int height, int color_bpp)
 {
     Backlight_u8(0);
     WriteCommand(0x88, 0x0a);                   // PLLC1 - Phase Lock Loop registers
@@ -1005,29 +1030,39 @@
     wait_ms(1);
     
     // System Config Register (SYSR)
-    WriteCommand(0x10, 0x0C);                   // 16-bpp (65K colors) color depth, 8-bit interface
+    if (color_bpp == 16) {
+        WriteCommand(0x10, 0x0C);               // 16-bpp (65K colors) color depth, 8-bit interface
+    } else { // color_bpp == 8
+        WriteCommand(0x10, 0x00);               // 8-bpp (256 colors)
+    }
     // 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(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.
+    WriteCommand(0x14, 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(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]
-    WriteCommand(0x1e, 0x06);                     //VSTR1 //VSYNC Start Position[8]
-    WriteCommand(0x1f, 0x01);                     //VPWR //VSYNC Polarity ,VSYNC Pulse Width[6:0]
+    WriteCommand(0x19, (height-1)&0xFF);        //VDHR0 //Vertical Display Height Bit [7:0]
+    WriteCommand(0x1a, (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]
+    WriteCommand(0x1e, 0x06);                   //VSTR1 //VSYNC Start Position[8]
+    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
+    } else {
+        WriteCommand(0x20, 0x80);               // DPCR - 2-layer mode
+    }
+    
     // Clear ram image
-    window(0,0, width(), height());             // Initialize to full screen
+    window(0,0, width, height);             // Initialize to full screen
     SetTextCursorControl();
     foreground(Blue);
     background(Black);