Fork of David Smart's RA8875 library

Fork of RA8875 by David Smart

Files at this revision

API Documentation at this revision

Comitter:
WiredHome
Date:
Sun Mar 09 23:45:23 2014 +0000
Parent:
42:7cbdfd2bbfc5
Child:
44:207594dece70
Commit message:
Initial support (not yet complete) for more screen resolutions and for display layers.

Changed in this revision

RA8875.cpp Show annotated file Show diff for this revision Revisions of this file
RA8875.h Show annotated file Show diff for this revision Revisions of this file
--- 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);
--- a/RA8875.h	Sun Feb 23 17:58:39 2014 +0000
+++ b/RA8875.h	Sun Mar 09 23:45:23 2014 +0000
@@ -135,6 +135,24 @@
     /// at startup, and not at runtime.
     //~RA8875();
     
+    /// Select the layer for subsequent commands.
+    ///
+    /// If the screen configuration is 480 x 272, or if it is 800 x 480 
+    /// and 8-bit color, the the display supports two layers, which can 
+    /// be independently drawn on and shown. Additionally, complex
+    /// operations involving both layers are permitted.
+    ///
+    /// @note The user manual refers to Layer 1 and Layer 2, however the
+    ///     actual register values are value 0 and 1. This and other APIs
+    ///     use the values 0 and 1.
+    ///
+    /// @param layer selects the layer for subsequence commands, where
+    ///     the values 0 and 1 represent layers 1 and 2 (as referred
+    ///     to in the user manual).
+    /// @returns success/failure code. @see RetCode_t.
+    ///
+    RetCode_t SelectLayer(uint16_t layer);
+    
     /// Write a command to the display with a word of data.
     ///
     /// This is a high level command, and may invoke several primitives.
@@ -241,6 +259,12 @@
     ///
     virtual dim_t height(void);
 
+    /// get the color depth in bits per pixel.
+    ///
+    /// @returns 8 or 16 only.
+    ///
+    virtual dim_t color_bpp(void);
+
     /// Set cursor position based on the current font size.
     /// 
     /// @param column is the horizontal position in character positions
@@ -901,9 +925,18 @@
     /// Initialize the chip, which is normally done as part of the
     /// constructor, so not called by the user.
     ///
+    /// @note This API permits configuration, however it is not [yet]
+    ///     available to the end user. Be sure the parameters
+    ///     are consistent with each other - see the RA8875 user
+    ///     manual.
+    ///
+    /// @param width in pixels to configure the display for.
+    /// @param height in pixels to configure the display for.
+    /// @param color_bpp can be either 8 or 16, but must be consistent
+    ///     with the width and height parameters.
     /// @returns success/failure code. @see RetCode_t.
     ///
-    RetCode_t init(void);
+    RetCode_t init(int width, int height, int color_bpp);
     
     /// method indicating the start of a graphics stream.
     ///