Fork of David Smart's RA8875 library

Fork of RA8875 by David Smart

Revision:
19:3f82c1161fd2
Child:
20:6e2e4a8372eb
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RA8875.cpp	Sun Jan 12 17:40:32 2014 +0000
@@ -0,0 +1,842 @@
+/// RA8875 Display Controller Library.
+/// 
+/// This is being created for a specific display from buydisplay.com,
+/// which is 480 x xxx. It has other attributes (like display controller
+/// managed backlight brightness. So, there are expectations and some
+/// defined constants based on that specific display.
+///
+#include "RA8875.h"
+
+//#define DEBUG "RAIO"
+// ...
+// 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__);
+#else
+#define INFO(x, ...)
+#define WARN(x, ...)
+#define ERR(x, ...)
+#endif
+
+
+#define RA8875_DISPLAY_WIDTH  480
+#define RA8875_DISPLAY_HEIGHT 272
+
+/// @TODO what is the size?
+/// If we're using the built-in font, read it from the chip.
+/// If we're using a custom font, get it from the font metrics.
+const int size_x = 16;
+const int size_y = 16;
+
+
+#ifdef PERF_METRICS
+#define PERFORMANCE_RESET performance.reset()
+#define REGISTERPERFORMANCE(a) RegisterPerformance(a)
+static const char *metricsName[] = 
+{
+    "Point", "Line", "Rectangle", "Rounded Rectangle", "Triangle", "Circle", "Ellipse"
+};
+#else
+#define PERFORMANCE_RESET
+#define REGISTERPERFORMANCE(a)
+#endif
+
+// When it is going to poll a register for completion, how many 
+// uSec should it wait between each polling activity.
+#define POLLWAITuSec 10
+
+
+RA8875::RA8875(PinName mosi, PinName miso, PinName sclk, PinName csel, PinName reset, const char *name)
+    : spi(mosi, miso, sclk)
+    , cs(csel)
+    , res(reset)
+    , GraphicsDisplay(name)
+{
+    font = NULL;                        // no external font, use internal.
+    select(false);                      // deselect the display
+    frequency(RA8875_DEFAULT_SPI_FREQ); // data rate
+    init();
+#ifdef PERF_METRICS
+    performance.start();
+    ClearPerformance();
+#endif
+}
+
+//RA8875::~RA8875()
+//{
+//}
+
+#ifdef PERF_METRICS
+void RA8875::ClearPerformance()
+{
+    for (int i=0; i<METRICCOUNT; i++)
+        metrics[i] = 0;
+}
+
+void RA8875::RegisterPerformance(method_e method)
+{
+    unsigned long elapsed = performance.read_us();
+    
+    if (method < METRICCOUNT && elapsed > metrics[method])
+        metrics[method] = elapsed;
+}
+
+void RA8875::ReportPerformance()
+{
+    for (int i=0; i<METRICCOUNT; i++) {
+        printf("%10d uS %s\r\n", metrics[i], metricsName[i]);
+    }    
+}
+#endif
+
+RetCode_t RA8875::WriteCommand(unsigned char command, unsigned int data)
+{
+    select(true);
+    spiwrite(0x80);
+    spiwrite(command);
+    if (data <= 0xFF) {   // only if in the valid range
+        spiwrite(0x00);
+        spiwrite(data);
+    }
+    select(false);
+    return noerror;
+}
+
+RetCode_t RA8875::WriteData(unsigned char data)
+{
+    select(true);
+    spiwrite(0x00);
+    spiwrite(data);
+    select(false);
+    return noerror;
+}
+
+unsigned char RA8875::ReadCommand(unsigned char command)
+{
+    WriteCommand(command);
+    return ReadData();
+}
+
+unsigned char RA8875::ReadData(void)
+{
+    unsigned char data;
+    
+    select(true);
+    spiwrite(0x40);
+    data = spiread();
+    select(false);
+    return data;
+}
+
+unsigned char RA8875::ReadStatus(void)
+{
+    unsigned char data;
+    
+    select(true);
+    spiwrite(0xC0);
+    data = spiread();
+    select(false);
+    return data;
+}
+
+unsigned int RA8875::fontwidth(void)
+{
+    if (font == NULL)
+        return size_x;
+    else
+        return width() / font[1];
+}
+
+unsigned int RA8875::fontheight(void)
+{
+    if (font == NULL)
+        return size_y;
+    else
+        return height() / font[2];
+}
+
+RetCode_t RA8875::locate(unsigned int x, unsigned int y)
+{
+    return SetTextCursor(x * fontwidth(), y * fontheight());
+}
+
+int RA8875::columns(void)
+{
+    return width() / fontwidth();
+}
+
+int RA8875::rows(void)
+{
+    return height() / fontheight();
+}
+
+int RA8875::width(void)
+{
+    return RA8875_DISPLAY_WIDTH;
+}
+
+int RA8875::height(void)
+{
+    return RA8875_DISPLAY_HEIGHT;
+}
+
+RetCode_t RA8875::SetTextCursor(unsigned int x, unsigned int y)
+{
+    WriteCommand(0x2A, x & 0xFF);
+    WriteCommand(0x2B, x >> 8);
+    WriteCommand(0x2C, y & 0xFF);
+    WriteCommand(0x2D, y >> 8);
+    INFO("SetTextCursor(%d,%d)", x,y);
+    return noerror;
+}
+
+RetCode_t RA8875::SetTextFont(RA8875::font_t font)
+{
+    if (/*font >= RA8875::ISO8859_1 && */ font <= RA8875::ISO8859_4) {
+        WriteCommand(0x21, (unsigned int)(font));
+        return noerror;
+    } else {
+        return bad_parameter;
+    }
+}
+
+RetCode_t RA8875::SetTextFontControl(fill_t fillit,
+    RA8875::font_angle_t angle, 
+    RA8875::HorizontalScale hScale, 
+    RA8875::VerticalScale vScale, 
+    RA8875::alignment_t alignment)
+{
+    if (hScale >= 1 && hScale <= 4 && 
+    vScale >= 1 && vScale <= 4) {
+        unsigned char x = 0;
+        
+        if (alignment == align_full)
+            x |= 0x80;
+        if (fillit == NOFILL)
+            x |= 0x40;
+        if (angle == rotated)
+            x |= 0x10;
+        x |= ((hScale - 1) << 2);
+        x |= ((vScale - 1) << 0);
+        WriteCommand(0x22, x);
+        return noerror;
+    } else {
+        return bad_parameter;
+    }
+}
+
+RetCode_t RA8875::SetTextFontSize(RA8875::HorizontalScale hScale, RA8875::VerticalScale vScale)
+{
+    unsigned char reg = ReadCommand(0x22);
+    
+    if (hScale >= 1 && hScale <= 4 && vScale >= 1 && vScale <= 4) {
+        reg &= 0xF0;    // keep the high nibble as is.
+        reg |= ((hScale - 1) << 2);
+        reg |= ((vScale - 1) << 0);
+        WriteCommand(0x22, reg);
+        return noerror;
+    } else {
+        return bad_parameter;
+    }
+}
+
+int RA8875::_putc(int c)
+{
+    if (c) {
+        if (c == '\r') {
+            unsigned int x;
+            x = ReadCommand(0x30) | (ReadCommand(0x31) << 8);   // Left edge of active window
+            WriteCommand(0x2A, x & 0xFF);
+            WriteCommand(0x2B, x >> 8);
+        } else if (c == '\n') {
+            unsigned int y;
+            y = ReadCommand(0x2C) | (ReadCommand(0x2D) << 8);   // current y location
+            y += fontheight();
+            if (y > height())               // @TODO > active window, then scroll?
+                y = 0;
+            WriteCommand(0x2C, y & 0xFF);
+            WriteCommand(0x2D, y >> 8);
+        } else {
+            if (font == NULL) {
+                WriteCommand(0x40,0x80);
+                WriteCommand(0x02);
+                select(true);
+                WriteData(c);
+                while (ReadStatus() & 0x80)
+                    wait_us(POLLWAITuSec);            // Chk_Busy();
+                select(false);
+            } else {
+                unsigned int x = (ReadCommand(0x2A) | (ReadCommand(0x2B) << 8)) / fontwidth();
+                unsigned int y = (ReadCommand(0x2C) | (ReadCommand(0x2D) << 8)) / fontheight();
+                character(x,y,c);
+            }
+            // @TODO right of active window, then wrap?
+        }
+    }
+    return c;
+}
+
+void RA8875::puts(unsigned int x, unsigned int y, const char * string)
+{
+    SetTextCursor(x,y);
+    puts(string);
+}
+
+void RA8875::puts(const char * string)
+{
+    if (*string != '\0') {
+        INFO("puts(%s)", string);
+        #if 1
+        while (*string) {           // @TODO calling individual _putc is slower... anything to do?
+            _putc(*string++);
+        }
+        #else
+        WriteCommand(0x40,0x80);    // Put display into text mode
+        WriteCommand(0x02);
+        select(true);
+        while (*string != '\0') {
+            WriteData(*string);
+            ++string;
+            while (ReadStatus() & 0x80)
+                wait_us(POLLWAITuSec);            // Chk_Busy();
+        }
+        select(false);
+        #endif
+    }
+}
+
+RetCode_t RA8875::SetMemoryCursor(unsigned int x, unsigned int y)
+
+{
+    WriteCommand(0x46, x & 0xFF);
+    WriteCommand(0x47, x >> 8);
+    WriteCommand(0x48, y & 0xFF);
+    WriteCommand(0x49, y >> 8);
+    return noerror;
+}
+
+RetCode_t RA8875::SetWindow(unsigned int x, unsigned int y, unsigned int width, unsigned int 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
+    return noerror;
+}
+
+RetCode_t RA8875::cls(void)
+{
+    PERFORMANCE_RESET;
+    clsw(FULLWINDOW);
+    REGISTERPERFORMANCE(PRF_CLS);
+    return noerror;
+}
+
+RetCode_t RA8875::clsw(RA8875::Region_t region)
+{
+    PERFORMANCE_RESET;
+    WriteCommand(0x8E, (region == ACTIVEWINDOW) ? 0xC0 : 0x80);
+    while (ReadCommand(0x8E) & 0x80)
+        wait_us(POLLWAITuSec);
+    REGISTERPERFORMANCE(PRF_CLS);
+    return noerror;
+}
+
+RetCode_t RA8875::pixel(unsigned int x, unsigned int y, color_t color)
+{
+    foreground(color);
+    return pixel(x,y);
+}
+
+RetCode_t RA8875::pixel(unsigned int x, unsigned int y)
+{
+    RetCode_t ret;
+    
+    PERFORMANCE_RESET;
+    #if 1
+    color_t color = GetForeColor();
+    WriteCommand(0x40,0x00);    // Graphics write mode
+    SetMemoryCursor(x, y);
+    WriteCommand(0x02);         //start data write
+    WriteData(color & 0xFF);
+    WriteData(color >> 8);
+    ret = noerror;
+    #else
+    // There isn't actually a set pixel function that I found
+    // so we'll emulate it as we can.
+    ret = line(x,y, x,y);
+    #endif
+    REGISTERPERFORMANCE(PRF_DRAWPOINT);
+    return ret;
+}
+
+RetCode_t RA8875::line(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, color_t color)
+{
+    foreground(color);
+    return line(x1,y1,x2,y2);
+}
+
+RetCode_t RA8875::line(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int 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);
+ 
+    unsigned char drawCmd = 0x00;       // Line
+    WriteCommand(0x90, drawCmd);
+    WriteCommand(0x90, 0x80 + drawCmd); // Start drawing.
+    while (ReadCommand(0x90) & 0x80)    // await completion.
+        wait_us(POLLWAITuSec);
+    REGISTERPERFORMANCE(PRF_DRAWLINE);
+    return noerror;
+}
+
+RetCode_t RA8875::fillrect(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, 
+    color_t color, fill_t fillit)
+{
+    return rect(x1,y1,x2,y2,color,fillit);
+}
+
+RetCode_t RA8875::rect(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, 
+    color_t color, fill_t fillit)
+{
+    foreground(color);
+    return rect(x1,y1,x2,y2,fillit);
+}
+
+RetCode_t RA8875::rect(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, 
+    fill_t fillit)
+{
+    PERFORMANCE_RESET;
+    if (x1 == x2 && y1 == y2) {
+        pixel(x1, y1);
+    } else if (x1 == x2) {
+        line(x1, y1, x2, y2);
+    } 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);
+        
+        unsigned char drawCmd = 0x10;   // Rectangle
+        if (fillit == FILL)
+            drawCmd |= 0x20;
+        WriteCommand(0x90, drawCmd);
+        WriteCommand(0x90, 0x80 + drawCmd); // Start drawing.
+        while (ReadCommand(0x90) & 0x80)    // await completion.
+            wait_us(POLLWAITuSec);
+    }
+    REGISTERPERFORMANCE(PRF_DRAWRECTANGLE);
+    return noerror;
+}
+
+RetCode_t RA8875::fillroundrect(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, 
+    unsigned int radius1, unsigned int radius2, color_t color, fill_t fillit)
+{
+    foreground(color);
+    return roundrect(x1,y1,x2,y2,radius1,radius2,fillit);
+}
+
+RetCode_t RA8875::roundrect(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, 
+    unsigned int radius1, unsigned int radius2, color_t color, fill_t fillit)
+{
+    foreground(color);
+    return roundrect(x1,y1,x2,y2,radius1,radius2,fillit);
+}
+
+RetCode_t RA8875::roundrect(unsigned int x1, unsigned int y1 ,unsigned int x2, unsigned int y2, 
+    unsigned int radius1, unsigned int radius2, fill_t fillit)
+{
+    RetCode_t ret = noerror;
+    
+    PERFORMANCE_RESET;
+    if (x1 == x2 && y1 == y2) {
+        pixel(x1, y1);
+    } else if (x1 == x2) {
+        line(x1, y1, x2, y2);
+    } 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);
+
+        //while (r && r > (x2 - x1) || r > (y2 - y1))
+        //    r--;
+        WriteCommand(0xA1, radius1 & 0xFF);
+        WriteCommand(0xA2, radius1 >> 8);
+        WriteCommand(0xA3, radius2 & 0xFF);
+        WriteCommand(0xA4, radius2 >> 8);
+        
+        unsigned char drawCmd = 0x20;       // Rounded Rectangle
+        if (fillit == FILL)
+            drawCmd |= 0x40;
+        WriteCommand(0xA0, drawCmd);
+        WriteCommand(0xA0, 0x80 + drawCmd); // Start drawing.
+        while (ReadCommand(0xA0) & 0x80) {   // await completion.
+            wait_us(POLLWAITuSec);
+        }        
+    }
+    REGISTERPERFORMANCE(PRF_DRAWROUNDEDRECTANGLE);
+    return ret;
+}
+
+RetCode_t RA8875::triangle(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, 
+    unsigned int x3, unsigned int y3, color_t color, fill_t fillit)
+{
+    foreground(color);
+    return triangle(x1,y1,x2,y2,x3,y3,color,fillit);
+}
+
+RetCode_t RA8875::filltriangle(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, 
+    unsigned int x3, unsigned int y3, color_t color, fill_t fillit)
+{
+    foreground(color);
+    return triangle(x1,y1,x2,y2,x3,y3,color,fillit);
+}
+
+RetCode_t RA8875::triangle(unsigned int x1, unsigned int y1 ,unsigned int x2, unsigned int y2, 
+    unsigned int x3, unsigned int y3, fill_t fillit)
+{
+    RetCode_t ret = noerror;
+    
+    PERFORMANCE_RESET;
+    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);
+        
+        unsigned char drawCmd = 0x01;       // Triangle
+        if (fillit == FILL)
+            drawCmd |= 0x20;
+        WriteCommand(0x90, drawCmd);
+        WriteCommand(0x90, 0x80 + drawCmd); // Start drawing.
+        while (ReadCommand(0x90) & 0x80)    // await completion.
+            wait_us(POLLWAITuSec);
+    }
+    REGISTERPERFORMANCE(PRF_DRAWTRIANGLE);
+    return ret;
+}
+
+RetCode_t RA8875::circle(unsigned int x, unsigned int y, unsigned int radius, 
+    color_t color, fill_t fillit)
+{
+    foreground(color);
+    return circle(x,y,radius,fillit);
+}
+
+RetCode_t RA8875::fillcircle(unsigned int x, unsigned int y, unsigned int radius, 
+    color_t color, fill_t fillit)
+{
+    foreground(color);
+    return circle(x,y,radius,fillit);
+}
+
+RetCode_t RA8875::circle(unsigned int x, unsigned int y, unsigned int radius, fill_t fillit)
+{
+    RetCode_t ret = noerror;
+    
+    PERFORMANCE_RESET;
+    if (radius <= 0) {
+        ret = bad_parameter;
+    } else if (radius == 1) {
+        pixel(x,y);
+    } else {
+        WriteCommand(0x99, x & 0xFF);
+        WriteCommand(0x9a, x >> 8);
+        WriteCommand(0x9b, y & 0xFF);
+        WriteCommand(0x9c, y >> 8);
+        WriteCommand(0x9d, radius & 0xFF);
+        
+        unsigned char drawCmd = 0x00;       // Circle
+        if (fillit == FILL)
+            drawCmd |= 0x20;
+        WriteCommand(0x90, drawCmd);
+        WriteCommand(0x90, 0x40 + drawCmd); // Start drawing.
+        while (ReadCommand(0x90) & 0x40)    // await completion.
+            wait_us(POLLWAITuSec);
+    }
+    REGISTERPERFORMANCE(PRF_DRAWCIRCLE);
+    return ret;
+}
+
+RetCode_t RA8875::ellipse(unsigned int x, unsigned int y, unsigned int R1, unsigned int R2, color_t color, fill_t fillit)
+{
+    foreground(color);
+    return ellipse(x,y,R1,R2,fillit);
+}
+
+RetCode_t RA8875::ellipse(unsigned int x, unsigned int y, unsigned int R1, unsigned int R2, fill_t fillit)
+{
+    RetCode_t ret = noerror;
+    
+    PERFORMANCE_RESET;
+    if (R1 <= 0 || R2 <= 0) {
+        ;   // do nothing
+    } else if (R1 == 1 && R2 == 1) {
+        pixel(x, y);
+    } else {
+        WriteCommand(0xA5, x & 0xFF);
+        WriteCommand(0xA6, x >> 8);
+        WriteCommand(0xA7, y & 0xFF);
+        WriteCommand(0xA8, y >> 8);
+        WriteCommand(0xA1, R1 & 0xFF);
+        WriteCommand(0xA2, R1 >> 8);
+        WriteCommand(0xA3, R2 & 0xFF);
+        WriteCommand(0xA4, R2 >> 8);
+        
+        unsigned char drawCmd = 0x00;   // Ellipse
+        if (fillit == FILL)
+            drawCmd |= 0x40;
+        WriteCommand(0xA0, drawCmd);
+        WriteCommand(0xA0, 0x80 + drawCmd); // Start drawing.
+        while (ReadCommand(0xA0) & 0x80)    // await completion.
+            wait_us(POLLWAITuSec);
+    }
+    REGISTERPERFORMANCE(PRF_DRAWELLIPSE);
+    return ret;
+}
+
+
+RetCode_t RA8875::frequency(unsigned long Hz)
+{
+    spi.frequency(Hz);
+    //       __   ___
+    // Clock   ___A     Rising edge latched
+    //       ___ ____
+    // Data  ___X____
+    spi.format(8, 3);           // 8 bits and clock to data phase 0
+    init();
+    return noerror;
+}
+
+RetCode_t RA8875::Power(bool on)
+{
+    WriteCommand(0x01, (on) ? 0x80 : 0x00);
+    return noerror;
+}
+
+RetCode_t RA8875::Reset(void)
+{
+    WriteCommand(0x01, 0x01);   // Apply Display Off, Reset
+    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();
+    return noerror;
+}
+
+
+RetCode_t RA8875::Backlight_u8(unsigned char brightness)
+{
+    static bool is_enabled = false;
+    if (brightness == 0) {
+        WriteCommand(0x8a); // Disable the PWM
+        WriteData(0x00);
+        is_enabled = false;
+    } else if (!is_enabled) {
+        WriteCommand(0x8a); // Enable the PWM
+        WriteData(0x80);
+        WriteCommand(0x8a); // Not sure why this is needed, but following the pattern
+        WriteData(0x81);    // open PWM (SYS_CLK / 2 as best I can tell)
+        is_enabled = true;
+    }
+    WriteCommand(0x8b, brightness);  // Brightness parameter 0xff-0x00
+    return noerror;
+}
+
+RetCode_t RA8875::Backlight(float brightness)
+{
+    unsigned char b;
+    
+    if (brightness > 1.0)
+        b = 255;
+    else if (brightness < 0)
+        b = 0;
+    else
+        b = (unsigned char)(brightness * 255);
+    return Backlight_u8(b);
+}
+
+
+RetCode_t RA8875::set_font(const unsigned char * _font)
+{
+    font = _font;
+    return noerror;     // trusting them, but we could put some checks in here...
+}
+
+RetCode_t RA8875::background(color_t color)
+{
+    WriteCommand(0x60, (color>>11));                  // BGCR0
+    WriteCommand(0x61, (unsigned char)(color>>5));    // BGCR0
+    WriteCommand(0x62, (unsigned char)(color));       // BGCR0
+    return noerror;
+}
+
+RetCode_t RA8875::background(unsigned char r, unsigned char g, unsigned char b)
+{
+    WriteCommand(0x60, r);
+    WriteCommand(0x61, g);
+    WriteCommand(0x62, b);
+    return noerror;
+}
+
+RetCode_t RA8875::foreground(color_t color)
+{
+    WriteCommand(0x63, (unsigned char)(color>>11));
+    WriteCommand(0x64, (unsigned char)(color>>5));
+    WriteCommand(0x65, (unsigned char)(color));
+    return noerror;
+}
+
+RetCode_t RA8875::foreground(unsigned char setR, unsigned char setG, unsigned char setB)
+{
+    WriteCommand(0x63, setR);
+    WriteCommand(0x64, setG);
+    WriteCommand(0x65, setB);
+    return noerror;
+}
+
+unsigned int RA8875::GetForeColor(void)
+{
+    color_t color;
+    
+    color  = (ReadCommand(0x63) & 0x1F) << 11;
+    color |= (ReadCommand(0x64) & 0x3F) << 5;
+    color |= (ReadCommand(0x65) & 0x1F);
+    return color;
+}
+
+color_t RA8875::DOSColor(int i)
+    {
+    const color_t colors[16] = 
+        {
+        Black,    Blue,       Green,       Cyan,
+        Red,      Magenta,    Brown,       Gray,
+        Charcoal, BrightBlue, BrightGreen, BrightCyan,
+        Orange,   Pink,       Yellow,      White
+        };
+    if (i < 16)
+        return colors[i];
+    else
+        return 0;
+    }
+
+const char * RA8875::DOSColorNames(int i) 
+    {
+    const char * names[16] = 
+        {
+        "Black",    "Blue",       "Green",       "Cyan",
+        "Red",      "Magenta",    "Brown",       "Gray",
+        "Charcoal", "BrightBlue", "BrightGreen", "BrightCyan",
+        "Orange",   "Pink",       "Yellow",      "White"
+        };
+    if (i < 16)
+        return names[i];
+    else
+        return NULL;
+    }
+
+
+///////////////////////////////////////////////////////////////
+// Private functions
+
+unsigned char RA8875::spiwrite(unsigned char data)
+{
+    unsigned char retval;
+    
+    retval = spi.write(data);
+    return retval;
+}
+
+unsigned char RA8875::spiread(void)
+{
+    unsigned char retval;
+    unsigned char data = 0;
+    
+    retval = spi.write(data);
+    return retval;
+}
+
+RetCode_t RA8875::select(bool chipsel)
+{
+    cs = (chipsel == true) ? 0 : 1;
+    return noerror;
+}
+
+RetCode_t RA8875::init(void)
+{
+    Backlight_u8(0);
+    WriteCommand(0x88, 0x0a);               // PLLC1 - Phase Lock Loop registers
+    wait_ms(1);
+    WriteCommand(0x89, 0x02);
+    wait_ms(1);
+    
+    //?
+    WriteCommand(0x10, 0x0C);  //SYSR   bit[4:3]=00 256 color  bit[2:1]=  00 8bit MPU interface, 1x 64k color             1x   16bit
+
+    //?
+    WriteCommand(0x04, 0x82);    //PCLK
+    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.
+
+    // 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]
+
+    // Clear ram image
+    SetWindow(0,0, width(), height());          // Initialize to full screen
+    foreground(Black);
+    background(Black);
+    cls();
+    return noerror;
+}
+