Buğra Tufan / Mbed 2 deprecated Adafruit_Version_6_K64F_bugra

Dependencies:   mbed BNO055

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RA8875.cpp Source File

RA8875.cpp

00001 /**************************************************************************/
00002 /*!
00003     @file     Adafruit_RA8875.cpp
00004     @author   Limor Friend/Ladyada, K.Townsend/KTOWN for Adafruit Industries
00005     @license  BSD license, all text above and below must be included in
00006               any redistribution
00007 
00008  This is the library for the Adafruit RA8875 Driver board for TFT displays
00009  ---------------> http://www.adafruit.com/products/1590
00010  The RA8875 is a TFT driver for up to 800x480 dotclock'd displays
00011  It is tested to work with displays in the Adafruit shop. Other displays
00012  may need timing adjustments and are not guanteed to work.
00013 
00014  Adafruit invests time and resources providing this open
00015  source code, please support Adafruit and open-source hardware
00016  by purchasing products from Adafruit!
00017 
00018  Written by Limor Fried/Ladyada for Adafruit Industries.
00019  BSD license, check license.txt for more information.
00020  All text above must be included in any redistribution.
00021 
00022     @section  HISTORY
00023 
00024     v1.0 - First release
00025 
00026     Code migrated to Mbed.
00027 */
00028 /**************************************************************************/
00029 #include "RA8875.h"
00030 
00031 #if defined (ARDUINO_ARCH_ARC32)
00032 uint32_t spi_speed = 12000000;
00033 #else
00034 uint32_t spi_speed = 4000000;
00035 #endif
00036 
00037 // NOTE: MBed use transactions inside SPI class itself
00038 //// If the SPI library has transaction support, these functions
00039 //// establish settings and protect from interference from other
00040 //// libraries.  Otherwise, they simply do nothing.
00041 //#ifdef SPI_HAS_TRANSACTION
00042 //    static inline void spi_begin(void) __attribute__((always_inline));
00043 //    static inline void spi_begin(void) {
00044 //        // max speed!
00045 //        SPI.beginTransaction(SPISettings(spi_speed, MSBFIRST, SPI_MODE0));
00046 //    }
00047 //    static inline void spi_end(void) __attribute__((always_inline));
00048 //    static inline void spi_end(void) {
00049 //        SPI.endTransaction();
00050 //    }
00051 //#else
00052 //    #define spi_begin()
00053 //    #define spi_end()
00054 //#endif
00055 
00056 
00057 /**************************************************************************/
00058 /*!
00059       Constructor for a new RA8875 instance
00060 
00061       @args CS[in]  Location of the SPI chip select pin
00062       @args RST[in] Location of the reset pin
00063 */
00064 /**************************************************************************/
00065 Adafruit_RA8875::Adafruit_RA8875(PinName MOSI, PinName MISO, PinName SCLK,
00066                                  PinName CS, PinName RST)
00067     : //Adafruit_GFX(800, 480)
00068       _cs(CS)
00069     , _rst(RST)
00070     , spi(MOSI, MISO, SCLK)
00071 {
00072 }
00073 
00074 /**************************************************************************/
00075 /*!
00076       Initialises the LCD driver and any HW required by the display
00077 
00078       @args s[in] The display size, which can be either:
00079                   'RA8875_480x272' (4.3" displays) r
00080                   'RA8875_800x480' (5" and 7" displays)
00081 */
00082 /**************************************************************************/
00083 bool Adafruit_RA8875::begin(enum RA8875sizes s)
00084 {
00085     _size = s;
00086 
00087     if (_size == RA8875_480x272) {
00088         _width = 480;
00089         _height = 272;
00090     } else if (_size == RA8875_800x480) {
00091         _width = 800;
00092         _height = 480;
00093     } else {
00094         return false;
00095     }
00096 
00097     _cs = 1;
00098 
00099     _rst = 0;
00100     wait_ms(100);
00101     _rst = 1;
00102     wait_ms(100);
00103 
00104     // Setting format
00105     spi.format(8, 0);
00106 
00107     // Configure SPI frequency
00108     spi_speed = 1000000;
00109     spi.frequency(spi_speed);
00110 
00111     uint8_t x = readReg(0);
00112     if (x != 0x75) {
00113         return false;
00114     }
00115 
00116 
00117 
00118     initialize();
00119 
00120 #ifdef SPI_HAS_TRANSACTION
00121 #if defined (ARDUINO_ARCH_ARC32)
00122     spi_speed = 12000000L;
00123 #else
00124     spi_speed = 4000000L;
00125 #endif
00126 #else
00127 #ifdef __AVR__
00128     SPI.setClockDivider(SPI_CLOCK_DIV4);
00129 #endif
00130 #endif
00131 
00132     return true;
00133 }
00134 
00135 /************************* Initialization *********************************/
00136 
00137 /**************************************************************************/
00138 /*!
00139       Performs a SW-based reset of the RA8875
00140 */
00141 /**************************************************************************/
00142 void Adafruit_RA8875::softReset(void)
00143 {
00144     writeCommand(RA8875_PWRR);
00145     writeData(RA8875_PWRR_SOFTRESET);
00146     writeData(RA8875_PWRR_NORMAL);
00147     wait_ms(1);
00148 }
00149 
00150 /**************************************************************************/
00151 /*!
00152       Initialise the PLL
00153 */
00154 /**************************************************************************/
00155 void Adafruit_RA8875::PLLinit(void)
00156 {
00157     if (_size == RA8875_480x272) {
00158         writeReg(RA8875_PLLC1, RA8875_PLLC1_PLLDIV1 + 10);
00159         wait_ms(1);
00160         writeReg(RA8875_PLLC2, RA8875_PLLC2_DIV4);
00161         wait_ms(1);
00162     } else { /* (_size == RA8875_800x480) */
00163         writeReg(RA8875_PLLC1, RA8875_PLLC1_PLLDIV1 + 10);
00164         wait_ms(1);
00165         writeReg(RA8875_PLLC2, RA8875_PLLC2_DIV4);
00166         wait_ms(1);
00167     }
00168 }
00169 
00170 /**************************************************************************/
00171 /*!
00172       Initialises the driver IC (clock setup, etc.)
00173 */
00174 /**************************************************************************/
00175 void Adafruit_RA8875::initialize(void)
00176 {
00177     PLLinit();
00178     writeReg(RA8875_SYSR, RA8875_SYSR_16BPP | RA8875_SYSR_MCU8);
00179 
00180     /* Timing values */
00181     uint8_t pixclk;
00182     uint8_t hsync_start;
00183     uint8_t hsync_pw;
00184     uint8_t hsync_finetune;
00185     uint8_t hsync_nondisp;
00186     uint8_t vsync_pw;
00187     uint16_t vsync_nondisp;
00188     uint16_t vsync_start;
00189 
00190     /* Set the correct values for the display being used */
00191     if (_size == RA8875_480x272) {
00192         pixclk          = RA8875_PCSR_PDATL | RA8875_PCSR_4CLK;
00193         hsync_nondisp   = 10;
00194         hsync_start     = 8;
00195         hsync_pw        = 48;
00196         hsync_finetune  = 0;
00197         vsync_nondisp   = 3;
00198         vsync_start     = 8;
00199         vsync_pw        = 10;
00200     } else { // (_size == RA8875_800x480)
00201         pixclk          = RA8875_PCSR_PDATL | RA8875_PCSR_2CLK;
00202         hsync_nondisp   = 26;
00203         hsync_start     = 32;
00204         hsync_pw        = 96;
00205         hsync_finetune  = 0;
00206         vsync_nondisp   = 32;
00207         vsync_start     = 23;
00208         vsync_pw        = 2;
00209     }
00210 
00211     writeReg(RA8875_PCSR, pixclk);
00212     wait_ms(1);
00213 
00214     /* Horizontal settings registers */
00215     writeReg(RA8875_HDWR, (_width / 8) - 1);                          // H width: (HDWR + 1) * 8 = 480
00216     writeReg(RA8875_HNDFTR, RA8875_HNDFTR_DE_HIGH + hsync_finetune);
00217     writeReg(RA8875_HNDR, (hsync_nondisp - hsync_finetune - 2)/8);    // H non-display: HNDR * 8 + HNDFTR + 2 = 10
00218     writeReg(RA8875_HSTR, hsync_start/8 - 1);                         // Hsync start: (HSTR + 1)*8
00219     writeReg(RA8875_HPWR, RA8875_HPWR_LOW + (hsync_pw/8 - 1));        // HSync pulse width = (HPWR+1) * 8
00220 
00221     /* Vertical settings registers */
00222     writeReg(RA8875_VDHR0, (uint16_t)(_height - 1) & 0xFF);
00223     writeReg(RA8875_VDHR1, (uint16_t)(_height - 1) >> 8);
00224     writeReg(RA8875_VNDR0, vsync_nondisp-1);                          // V non-display period = VNDR + 1
00225     writeReg(RA8875_VNDR1, vsync_nondisp >> 8);
00226     writeReg(RA8875_VSTR0, vsync_start-1);                            // Vsync start position = VSTR + 1
00227     writeReg(RA8875_VSTR1, vsync_start >> 8);
00228     writeReg(RA8875_VPWR, RA8875_VPWR_LOW + vsync_pw - 1);            // Vsync pulse width = VPWR + 1
00229 
00230     /* Set active window X */
00231     writeReg(RA8875_HSAW0, 0);                                        // horizontal start point
00232     writeReg(RA8875_HSAW1, 0);
00233     writeReg(RA8875_HEAW0, (uint16_t)(_width - 1) & 0xFF);            // horizontal end point
00234     writeReg(RA8875_HEAW1, (uint16_t)(_width - 1) >> 8);
00235 
00236     /* Set active window Y */
00237     writeReg(RA8875_VSAW0, 0);                                        // vertical start point
00238     writeReg(RA8875_VSAW1, 0);
00239     writeReg(RA8875_VEAW0, (uint16_t)(_height - 1) & 0xFF);           // horizontal end point
00240     writeReg(RA8875_VEAW1, (uint16_t)(_height - 1) >> 8);
00241 
00242     /* ToDo: Setup touch panel? */
00243 
00244     /* Clear the entire window */
00245     writeReg(RA8875_MCLR, RA8875_MCLR_START | RA8875_MCLR_FULL);
00246     wait_ms(500);
00247 }
00248 
00249 /**************************************************************************/
00250 /*!
00251       Returns the display width in pixels
00252 
00253       @returns  The 1-based display width in pixels
00254 */
00255 /**************************************************************************/
00256 uint16_t Adafruit_RA8875::width(void)
00257 {
00258     return _width;
00259 }
00260 
00261 /**************************************************************************/
00262 /*!
00263       Returns the display height in pixels
00264 
00265       @returns  The 1-based display height in pixels
00266 */
00267 /**************************************************************************/
00268 uint16_t Adafruit_RA8875::height(void)
00269 {
00270     return _height;
00271 }
00272 
00273 /************************* Text Mode ***********************************/
00274 
00275 /**************************************************************************/
00276 /*!
00277       Sets the display in text mode (as opposed to graphics mode)
00278 */
00279 /**************************************************************************/
00280 void Adafruit_RA8875::textMode(void)
00281 {
00282     /* Set text mode */
00283     writeCommand(RA8875_MWCR0);
00284     uint8_t temp = readData();
00285     temp |= RA8875_MWCR0_TXTMODE; // Set bit 7
00286     writeData(temp);
00287 
00288     /* Select the internal (ROM) font */
00289     writeCommand(0x21);
00290     temp = readData();
00291     temp &= ~((1<<7) | (1<<5)); // Clear bits 7 and 5
00292     writeData(temp);
00293 }
00294 
00295 /**************************************************************************/
00296 /*!
00297       Sets the display in text mode (as opposed to graphics mode)
00298 
00299       @args x[in] The x position of the cursor (in pixels, 0..1023)
00300       @args y[in] The y position of the cursor (in pixels, 0..511)
00301 */
00302 /**************************************************************************/
00303 void Adafruit_RA8875::textSetCursor(uint16_t x, uint16_t y)
00304 {
00305     /* Set cursor location */
00306     writeCommand(0x2A);
00307     writeData(x & 0xFF);
00308     writeCommand(0x2B);
00309     writeData(x >> 8);
00310     writeCommand(0x2C);
00311     writeData(y & 0xFF);
00312     writeCommand(0x2D);
00313     writeData(y >> 8);
00314 }
00315 
00316 /**************************************************************************/
00317 /*!
00318       Sets the fore and background color when rendering text
00319 
00320       @args foreColor[in] The RGB565 color to use when rendering the text
00321       @args bgColor[in]   The RGB565 colot to use for the background
00322 */
00323 /**************************************************************************/
00324 void Adafruit_RA8875::textColor(uint16_t foreColor, uint16_t bgColor)
00325 {
00326     if (foreColor == 0) {
00327         // Sets the ForeColor to white //
00328         writeCommand(0x63);
00329         writeData((WHITE & 0xf800) >> 11);
00330         writeCommand(0x64);
00331         writeData((WHITE & 0x07e0) >> 5);
00332         writeCommand(0x65);
00333         writeData((WHITE & 0x001f));
00334     } else {
00335         /* Set Fore Color */
00336         writeCommand(0x63);
00337         writeData((foreColor & 0xf800) >> 11);
00338         writeCommand(0x64);
00339         writeData((foreColor & 0x07e0) >> 5);
00340         writeCommand(0x65);
00341         writeData((foreColor & 0x001f));
00342     }
00343 
00344     if (bgColor == 0) {
00345         /* Set Background Color to black */
00346         writeCommand(0x60);
00347         writeData((BLACK & 0xf800) >> 11);
00348         writeCommand(0x61);
00349         writeData((BLACK & 0x07e0) >> 5);
00350         writeCommand(0x62);
00351         writeData((BLACK & 0x001f));
00352     } else {
00353         /* Set Background Color */
00354         writeCommand(0x60);
00355         writeData((bgColor & 0xf800) >> 11);
00356         writeCommand(0x61);
00357         writeData((bgColor & 0x07e0) >> 5);
00358         writeCommand(0x62);
00359         writeData((bgColor & 0x001f));
00360     }
00361 
00362 
00363     /* Clear transparency flag */
00364     writeCommand(0x22);
00365     uint8_t temp = readData();
00366     temp &= ~(1<<6); // Clear bit 6
00367     writeData(temp);
00368 }
00369 
00370 /**************************************************************************/
00371 /*!
00372       Sets the fore color when rendering text with a transparent bg
00373 
00374       @args foreColor[in] The RGB565 color to use when rendering the text
00375 */
00376 /**************************************************************************/
00377 void Adafruit_RA8875::textTransparent(uint16_t foreColor)
00378 {
00379     /* Set Fore Color */
00380     writeCommand(0x63);
00381     writeData((foreColor & 0xf800) >> 11);
00382     writeCommand(0x64);
00383     writeData((foreColor & 0x07e0) >> 5);
00384     writeCommand(0x65);
00385     writeData((foreColor & 0x001f));
00386 
00387     /* Set transparency flag */
00388     writeCommand(0x22);
00389     uint8_t temp = readData();
00390     temp |= (1<<6); // Set bit 6
00391     writeData(temp);
00392 }
00393 
00394     /**************************************************************************/
00395     /*!
00396           Sets the text enlarge settings, using one of the following values:
00397 
00398           0 = 1x zoom
00399           1 = 2x zoom
00400           2 = 3x zoom
00401           3 = 4x zoom
00402 
00403           @args scale[in]   The zoom factor (0..3 for 1-4x zoom)
00404     */
00405     /**************************************************************************/
00406     void Adafruit_RA8875::textEnlarge(uint8_t scale) {
00407         if (scale > 3) scale = 3;
00408 
00409         /* Set font size flags */
00410         writeCommand(0x22);
00411         uint8_t temp = readData();
00412         temp &= ~(0xF); // Clears bits 0..3
00413         temp |= scale << 2;
00414         temp |= scale;
00415         writeData(temp);
00416 
00417         _textScale = scale;
00418     }
00419 
00420     /**************************************************************************/
00421     /*!
00422           Renders some text on the screen when in text mode
00423 
00424           @args buffer[in]    The buffer containing the characters to render
00425           @args len[in]       The size of the buffer in bytes
00426     */
00427     /**************************************************************************/
00428     void Adafruit_RA8875::textWrite(const char* buffer, uint16_t len) {
00429         if (len == 0) len = strlen(buffer);
00430         writeCommand(RA8875_MRWC);
00431         for (uint16_t i=0; i<len; i++) {
00432             writeData(buffer[i]);
00433 #if defined(__AVR__)
00434             if (_textScale > 1) wait_ms(1);
00435 #elif defined(__arm__)
00436             // This wait_ms is needed with textEnlarge(1) because
00437             // Teensy 3.X is much faster than Arduino Uno
00438             if (_textScale > 0) wait_ms(1);
00439 #endif
00440         }
00441     }
00442 
00443     void Adafruit_RA8875::textWritePro(uint16_t x, uint16_t y, uint16_t foreColor, uint16_t bgColor, const char* buffer, uint16_t len) {
00444         textMode();
00445         textSetCursor(x,y);
00446         textColor(foreColor, bgColor);
00447         textWrite(buffer, len);
00448     }
00449 
00450 
00451     /************************* Graphics ***********************************/
00452 
00453     /**************************************************************************/
00454     /*!
00455           Sets the display in graphics mode (as opposed to text mode)
00456     */
00457     /**************************************************************************/
00458     void Adafruit_RA8875::graphicsMode(void) {
00459         writeCommand(RA8875_MWCR0);
00460         uint8_t temp = readData();
00461         temp &= ~RA8875_MWCR0_TXTMODE; // bit #7
00462         writeData(temp);
00463     }
00464 
00465     /**************************************************************************/
00466     /*!
00467           Waits for screen to finish by polling the status!
00468     */
00469     /**************************************************************************/
00470     bool Adafruit_RA8875::waitPoll(uint8_t regname, uint8_t waitflag) {
00471         /* Wait for the command to finish */
00472         while (1) {
00473             uint8_t temp = readReg(regname);
00474             if (!(temp & waitflag))
00475                 return true;
00476         }
00477         //return false; // MEMEFIX: yeah i know, unreached! - add timeout?
00478     }
00479 
00480 
00481     /**************************************************************************/
00482     /*!
00483           Sets the current X/Y position on the display before drawing
00484 
00485           @args x[in] The 0-based x location
00486           @args y[in] The 0-base y location
00487     */
00488     /**************************************************************************/
00489     void Adafruit_RA8875::setXY(uint16_t x, uint16_t y) {
00490         writeReg(RA8875_CURH0, x);
00491         writeReg(RA8875_CURH1, x >> 8);
00492         writeReg(RA8875_CURV0, y);
00493         writeReg(RA8875_CURV1, y >> 8);
00494     }
00495 
00496     /**************************************************************************/
00497     /*!
00498           HW accelerated function to push a chunk of raw pixel data
00499 
00500           @args num[in] The number of pixels to push
00501           @args p[in]   The pixel color to use
00502     */
00503     /**************************************************************************/
00504     void Adafruit_RA8875::pushPixels(uint32_t num, uint16_t p) {
00505         _cs = 0;
00506 
00507         spi.write(RA8875_DATAWRITE);
00508         while (num--) {
00509             spi.write(p >> 8);
00510             spi.write(p);
00511         }
00512         _cs = 1;
00513     }
00514 
00515     /**************************************************************************/
00516     /*!
00517 
00518     */
00519     /**************************************************************************/
00520     void Adafruit_RA8875::fillRect(void) {
00521         writeCommand(RA8875_DCR);
00522         writeData(RA8875_DCR_LINESQUTRI_STOP | RA8875_DCR_DRAWSQUARE);
00523         writeData(RA8875_DCR_LINESQUTRI_START | RA8875_DCR_FILL | RA8875_DCR_DRAWSQUARE);
00524     }
00525 
00526     /**************************************************************************/
00527     /*!
00528           Draws a single pixel at the specified location
00529 
00530           @args x[in]     The 0-based x location
00531           @args y[in]     The 0-base y location
00532           @args color[in] The RGB565 color to use when drawing the pixel
00533     */
00534     /**************************************************************************/
00535     void Adafruit_RA8875::drawPixel(int16_t x, int16_t y, uint16_t color) {
00536         writeReg(RA8875_CURH0, x);
00537         writeReg(RA8875_CURH1, x >> 8);
00538         writeReg(RA8875_CURV0, y);
00539         writeReg(RA8875_CURV1, y >> 8);
00540         writeCommand(RA8875_MRWC);
00541         _cs = 0;
00542         spi.write(RA8875_DATAWRITE);
00543         spi.write(color >> 8);
00544         spi.write(color);
00545         _cs = 1;
00546     }
00547 
00548     /**************************************************************************/
00549     /*!
00550           Draws a HW accelerated line on the display
00551 
00552           @args x0[in]    The 0-based starting x location
00553           @args y0[in]    The 0-base starting y location
00554           @args x1[in]    The 0-based ending x location
00555           @args y1[in]    The 0-base ending y location
00556           @args color[in] The RGB565 color to use when drawing the pixel
00557     */
00558     /**************************************************************************/
00559     void Adafruit_RA8875::drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color) {
00560         /* Set X */
00561         writeCommand(0x91);
00562         writeData(x0);
00563         writeCommand(0x92);
00564         writeData(x0 >> 8);
00565 
00566         /* Set Y */
00567         writeCommand(0x93);
00568         writeData(y0);
00569         writeCommand(0x94);
00570         writeData(y0 >> 8);
00571 
00572         /* Set X1 */
00573         writeCommand(0x95);
00574         writeData(x1);
00575         writeCommand(0x96);
00576         writeData((x1) >> 8);
00577 
00578         /* Set Y1 */
00579         writeCommand(0x97);
00580         writeData(y1);
00581         writeCommand(0x98);
00582         writeData((y1) >> 8);
00583 
00584         /* Set Color */
00585         writeCommand(0x63);
00586         writeData((color & 0xf800) >> 11);
00587         writeCommand(0x64);
00588         writeData((color & 0x07e0) >> 5);
00589         writeCommand(0x65);
00590         writeData((color & 0x001f));
00591 
00592         /* Draw! */
00593         writeCommand(RA8875_DCR);
00594         writeData(0x80);
00595 
00596         /* Wait for the command to finish */
00597         waitPoll(RA8875_DCR, RA8875_DCR_LINESQUTRI_STATUS);
00598     }
00599 
00600     /**************************************************************************/
00601     /*!
00602 
00603     */
00604     /**************************************************************************/
00605     void Adafruit_RA8875::drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color) {
00606         drawLine(x, y, x, y+h, color);
00607     }
00608 
00609     /**************************************************************************/
00610     /*!
00611 
00612     */
00613     /**************************************************************************/
00614     void Adafruit_RA8875::drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color) {
00615         drawLine(x, y, x+w, y, color);
00616     }
00617 
00618     /**************************************************************************/
00619     /*!
00620           Draws a HW accelerated rectangle on the display
00621 
00622           @args x[in]     The 0-based x location of the top-right corner
00623           @args y[in]     The 0-based y location of the top-right corner
00624           @args w[in]     The rectangle width
00625           @args h[in]     The rectangle height
00626           @args color[in] The RGB565 color to use when drawing the pixel
00627     */
00628     /**************************************************************************/
00629     /*
00630     void Adafruit_RA8875::drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)
00631     {
00632       rectHelper(x, y, x+w, y+h, color, false);
00633     }
00634     */
00635     /**************************************************************************/
00636     /*!
00637           Draws a HW accelerated filled rectangle on the display
00638 
00639           @args x[in]     The 0-based x location of the top-right corner
00640           @args y[in]     The 0-based y location of the top-right corner
00641           @args w[in]     The rectangle width
00642           @args h[in]     The rectangle height
00643           @args color[in] The RGB565 color to use when drawing the pixel
00644     */
00645     /**************************************************************************/
00646     /*
00647     void Adafruit_RA8875::fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)
00648     {
00649       rectHelper(x, y, x+w, y+h, color, true);
00650     }
00651     */
00652     /**************************************************************************/
00653     /*!
00654           Fills the screen with the spefied RGB565 color
00655 
00656           @args color[in] The RGB565 color to use when drawing the pixel
00657     */
00658     /**************************************************************************/
00659 
00660     void Adafruit_RA8875::fillScreen(uint16_t color) {
00661         rectHelper(0, 0, _width-1, _height-1, color, true);
00662     }
00663 
00664     /**************************************************************************/
00665     /*!
00666           Draws a HW accelerated circle on the display
00667 
00668           @args x[in]     The 0-based x location of the center of the circle
00669           @args y[in]     The 0-based y location of the center of the circle
00670           @args w[in]     The circle's radius
00671           @args color[in] The RGB565 color to use when drawing the pixel
00672     */
00673     /**************************************************************************/
00674 
00675     void Adafruit_RA8875::drawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color) {
00676         circleHelper(x0, y0, r, color, false);
00677     }
00678 
00679     /**************************************************************************/
00680     /*!
00681           Draws a HW accelerated filled circle on the display
00682 
00683           @args x[in]     The 0-based x location of the center of the circle
00684           @args y[in]     The 0-based y location of the center of the circle
00685           @args w[in]     The circle's radius
00686           @args color[in] The RGB565 color to use when drawing the pixel
00687     */
00688     /**************************************************************************/
00689 
00690     void Adafruit_RA8875::fillCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color) {
00691         circleHelper(x0, y0, r, color, true);
00692     }
00693 
00694     /**************************************************************************/
00695     /*!
00696           Draws a HW accelerated triangle on the display
00697 
00698           @args x0[in]    The 0-based x location of point 0 on the triangle
00699           @args y0[in]    The 0-based y location of point 0 on the triangle
00700           @args x1[in]    The 0-based x location of point 1 on the triangle
00701           @args y1[in]    The 0-based y location of point 1 on the triangle
00702           @args x2[in]    The 0-based x location of point 2 on the triangle
00703           @args y2[in]    The 0-based y location of point 2 on the triangle
00704           @args color[in] The RGB565 color to use when drawing the pixel
00705     */
00706     /**************************************************************************/
00707 
00708     void Adafruit_RA8875::drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color) {
00709         triangleHelper(x0, y0, x1, y1, x2, y2, color, false);
00710     }
00711 
00712     /**************************************************************************/
00713     /*!
00714           Draws a HW accelerated filled triangle on the display
00715 
00716           @args x0[in]    The 0-based x location of point 0 on the triangle
00717           @args y0[in]    The 0-based y location of point 0 on the triangle
00718           @args x1[in]    The 0-based x location of point 1 on the triangle
00719           @args y1[in]    The 0-based y location of point 1 on the triangle
00720           @args x2[in]    The 0-based x location of point 2 on the triangle
00721           @args y2[in]    The 0-based y location of point 2 on the triangle
00722           @args color[in] The RGB565 color to use when drawing the pixel
00723     */
00724     /**************************************************************************/
00725 
00726     void Adafruit_RA8875::fillTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color) {
00727         triangleHelper(x0, y0, x1, y1, x2, y2, color, true);
00728     }
00729 
00730     /**************************************************************************/
00731     /*!
00732           Draws a HW accelerated ellipse on the display
00733 
00734           @args xCenter[in]   The 0-based x location of the ellipse's center
00735           @args yCenter[in]   The 0-based y location of the ellipse's center
00736           @args longAxis[in]  The size in pixels of the ellipse's long axis
00737           @args shortAxis[in] The size in pixels of the ellipse's short axis
00738           @args color[in]     The RGB565 color to use when drawing the pixel
00739     */
00740     /**************************************************************************/
00741 
00742     void Adafruit_RA8875::drawEllipse(int16_t xCenter, int16_t yCenter, int16_t longAxis, int16_t shortAxis, uint16_t color) {
00743         ellipseHelper(xCenter, yCenter, longAxis, shortAxis, color, false);
00744     }
00745 
00746     /**************************************************************************/
00747     /*!
00748           Draws a HW accelerated filled ellipse on the display
00749 
00750           @args xCenter[in]   The 0-based x location of the ellipse's center
00751           @args yCenter[in]   The 0-based y location of the ellipse's center
00752           @args longAxis[in]  The size in pixels of the ellipse's long axis
00753           @args shortAxis[in] The size in pixels of the ellipse's short axis
00754           @args color[in]     The RGB565 color to use when drawing the pixel
00755     */
00756     /**************************************************************************/
00757 
00758     void Adafruit_RA8875::fillEllipse(int16_t xCenter, int16_t yCenter, int16_t longAxis, int16_t shortAxis, uint16_t color) {
00759         ellipseHelper(xCenter, yCenter, longAxis, shortAxis, color, true);
00760     }
00761 
00762     /**************************************************************************/
00763     /*!
00764           Draws a HW accelerated curve on the display
00765 
00766           @args xCenter[in]   The 0-based x location of the ellipse's center
00767           @args yCenter[in]   The 0-based y location of the ellipse's center
00768           @args longAxis[in]  The size in pixels of the ellipse's long axis
00769           @args shortAxis[in] The size in pixels of the ellipse's short axis
00770           @args curvePart[in] The corner to draw, where in clock-wise motion:
00771                                 0 = 180-270°
00772                                 1 = 270-0°
00773                                 2 = 0-90°
00774                                 3 = 90-180°
00775           @args color[in]     The RGB565 color to use when drawing the pixel
00776     */
00777     /**************************************************************************/
00778 
00779     void Adafruit_RA8875::drawCurve(int16_t xCenter, int16_t yCenter, int16_t longAxis, int16_t shortAxis, uint8_t curvePart, uint16_t color) {
00780         curveHelper(xCenter, yCenter, longAxis, shortAxis, curvePart, color, false);
00781     }
00782 
00783     /**************************************************************************/
00784     /*!
00785           Draws a HW accelerated filled curve on the display
00786 
00787           @args xCenter[in]   The 0-based x location of the ellipse's center
00788           @args yCenter[in]   The 0-based y location of the ellipse's center
00789           @args longAxis[in]  The size in pixels of the ellipse's long axis
00790           @args shortAxis[in] The size in pixels of the ellipse's short axis
00791           @args curvePart[in] The corner to draw, where in clock-wise motion:
00792                                 0 = 180-270°
00793                                 1 = 270-0°
00794                                 2 = 0-90°
00795                                 3 = 90-180°
00796           @args color[in]     The RGB565 color to use when drawing the pixel
00797     */
00798     /**************************************************************************/
00799 
00800     void Adafruit_RA8875::fillCurve(int16_t xCenter, int16_t yCenter, int16_t longAxis, int16_t shortAxis, uint8_t curvePart, uint16_t color) {
00801         curveHelper(xCenter, yCenter, longAxis, shortAxis, curvePart, color, true);
00802     }
00803 
00804     /**************************************************************************/
00805     /*!
00806           Helper function for higher level circle drawing code
00807     */
00808     /**************************************************************************/
00809 
00810     void Adafruit_RA8875::circleHelper(int16_t x0, int16_t y0, int16_t r, uint16_t color, bool filled) {
00811         /* Set X */
00812         writeCommand(0x99);
00813         writeData(x0);
00814         writeCommand(0x9a);
00815         writeData(x0 >> 8);
00816 
00817         /* Set Y */
00818         writeCommand(0x9b);
00819         writeData(y0);
00820         writeCommand(0x9c);
00821         writeData(y0 >> 8);
00822 
00823         /* Set Radius */
00824         writeCommand(0x9d);
00825         writeData(r);
00826 
00827         /* Set Color */
00828         writeCommand(0x63);
00829         writeData((color & 0xf800) >> 11);
00830         writeCommand(0x64);
00831         writeData((color & 0x07e0) >> 5);
00832         writeCommand(0x65);
00833         writeData((color & 0x001f));
00834 
00835         /* Draw! */
00836         writeCommand(RA8875_DCR);
00837         if (filled) {
00838             writeData(RA8875_DCR_CIRCLE_START | RA8875_DCR_FILL);
00839         } else {
00840             writeData(RA8875_DCR_CIRCLE_START | RA8875_DCR_NOFILL);
00841         }
00842 
00843         /* Wait for the command to finish */
00844         waitPoll(RA8875_DCR, RA8875_DCR_CIRCLE_STATUS);
00845     }
00846 
00847     /**************************************************************************/
00848     /*!
00849           Helper function for higher level rectangle drawing code
00850     */
00851     /**************************************************************************/
00852     void Adafruit_RA8875::rectHelper(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color, bool filled) {
00853         /* Set X */
00854         writeCommand(0x91);
00855         writeData(x);
00856         writeCommand(0x92);
00857         writeData(x >> 8);
00858 
00859         /* Set Y */
00860         writeCommand(0x93);
00861         writeData(y);
00862         writeCommand(0x94);
00863         writeData(y >> 8);
00864 
00865         /* Set X1 */
00866         writeCommand(0x95);
00867         writeData(w);
00868         writeCommand(0x96);
00869         writeData((w) >> 8);
00870 
00871         /* Set Y1 */
00872         writeCommand(0x97);
00873         writeData(h);
00874         writeCommand(0x98);
00875         writeData((h) >> 8);
00876 
00877         /* Set Color */
00878         writeCommand(0x63);
00879         writeData((color & 0xf800) >> 11);
00880         writeCommand(0x64);
00881         writeData((color & 0x07e0) >> 5);
00882         writeCommand(0x65);
00883         writeData((color & 0x001f));
00884 
00885         /* Draw! */
00886         writeCommand(RA8875_DCR);
00887         if (filled) {
00888             writeData(0xB0);
00889         } else {
00890             writeData(0x90);
00891         }
00892 
00893         /* Wait for the command to finish */
00894         waitPoll(RA8875_DCR, RA8875_DCR_LINESQUTRI_STATUS);
00895     }
00896     
00897     
00898     
00899 /////// Function to draw a rectangle with a border    
00900     
00901     
00902 void Adafruit_RA8875::boxDraw(int16_t x, int16_t y, int16_t length, int16_t height, int16_t thick, uint16_t color, uint16_t fill) {
00903     
00904     int w = x+length;
00905     int h = y+height;
00906     
00907     graphicsMode();
00908     rectHelper(x,y,w,h,color,1);
00909     rectHelper((x+thick),(y+thick),(w-thick),(h-thick),fill,1);
00910 }    
00911     
00912     
00913     
00914     
00915     
00916     
00917     
00918     
00919     
00920     
00921 
00922     /**************************************************************************/
00923     /*!
00924           Helper function for higher level triangle drawing code
00925     */
00926     /**************************************************************************/
00927     void Adafruit_RA8875::triangleHelper(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color, bool filled) {
00928         /* Set Point 0 */
00929         writeCommand(0x91);
00930         writeData(x0);
00931         writeCommand(0x92);
00932         writeData(x0 >> 8);
00933         writeCommand(0x93);
00934         writeData(y0);
00935         writeCommand(0x94);
00936         writeData(y0 >> 8);
00937 
00938         /* Set Point 1 */
00939         writeCommand(0x95);
00940         writeData(x1);
00941         writeCommand(0x96);
00942         writeData(x1 >> 8);
00943         writeCommand(0x97);
00944         writeData(y1);
00945         writeCommand(0x98);
00946         writeData(y1 >> 8);
00947 
00948         /* Set Point 2 */
00949         writeCommand(0xA9);
00950         writeData(x2);
00951         writeCommand(0xAA);
00952         writeData(x2 >> 8);
00953         writeCommand(0xAB);
00954         writeData(y2);
00955         writeCommand(0xAC);
00956         writeData(y2 >> 8);
00957 
00958         /* Set Color */
00959         writeCommand(0x63);
00960         writeData((color & 0xf800) >> 11);
00961         writeCommand(0x64);
00962         writeData((color & 0x07e0) >> 5);
00963         writeCommand(0x65);
00964         writeData((color & 0x001f));
00965 
00966         /* Draw! */
00967         writeCommand(RA8875_DCR);
00968         if (filled) {
00969             writeData(0xA1);
00970         } else {
00971             writeData(0x81);
00972         }
00973 
00974         /* Wait for the command to finish */
00975         waitPoll(RA8875_DCR, RA8875_DCR_LINESQUTRI_STATUS);
00976     }
00977 
00978     /**************************************************************************/
00979     /*!
00980           Helper function for higher level ellipse drawing code
00981     */
00982     /**************************************************************************/
00983     void Adafruit_RA8875::ellipseHelper(int16_t xCenter, int16_t yCenter, int16_t longAxis, int16_t shortAxis, uint16_t color, bool filled) {
00984         /* Set Center Point */
00985         writeCommand(0xA5);
00986         writeData(xCenter);
00987         writeCommand(0xA6);
00988         writeData(xCenter >> 8);
00989         writeCommand(0xA7);
00990         writeData(yCenter);
00991         writeCommand(0xA8);
00992         writeData(yCenter >> 8);
00993 
00994         /* Set Long and Short Axis */
00995         writeCommand(0xA1);
00996         writeData(longAxis);
00997         writeCommand(0xA2);
00998         writeData(longAxis >> 8);
00999         writeCommand(0xA3);
01000         writeData(shortAxis);
01001         writeCommand(0xA4);
01002         writeData(shortAxis >> 8);
01003 
01004         /* Set Color */
01005         writeCommand(0x63);
01006         writeData((color & 0xf800) >> 11);
01007         writeCommand(0x64);
01008         writeData((color & 0x07e0) >> 5);
01009         writeCommand(0x65);
01010         writeData((color & 0x001f));
01011 
01012         /* Draw! */
01013         writeCommand(0xA0);
01014         if (filled) {
01015             writeData(0xC0);
01016         } else {
01017             writeData(0x80);
01018         }
01019 
01020         /* Wait for the command to finish */
01021         waitPoll(RA8875_ELLIPSE, RA8875_ELLIPSE_STATUS);
01022     }
01023 
01024     /**************************************************************************/
01025     /*!
01026           Helper function for higher level curve drawing code
01027     */
01028     /**************************************************************************/
01029     void Adafruit_RA8875::curveHelper(int16_t xCenter, int16_t yCenter, int16_t longAxis, int16_t shortAxis, uint8_t curvePart, uint16_t color, bool filled) {
01030         /* Set Center Point */
01031         writeCommand(0xA5);
01032         writeData(xCenter);
01033         writeCommand(0xA6);
01034         writeData(xCenter >> 8);
01035         writeCommand(0xA7);
01036         writeData(yCenter);
01037         writeCommand(0xA8);
01038         writeData(yCenter >> 8);
01039 
01040         /* Set Long and Short Axis */
01041         writeCommand(0xA1);
01042         writeData(longAxis);
01043         writeCommand(0xA2);
01044         writeData(longAxis >> 8);
01045         writeCommand(0xA3);
01046         writeData(shortAxis);
01047         writeCommand(0xA4);
01048         writeData(shortAxis >> 8);
01049 
01050         /* Set Color */
01051         writeCommand(0x63);
01052         writeData((color & 0xf800) >> 11);
01053         writeCommand(0x64);
01054         writeData((color & 0x07e0) >> 5);
01055         writeCommand(0x65);
01056         writeData((color & 0x001f));
01057 
01058         /* Draw! */
01059         writeCommand(0xA0);
01060         if (filled) {
01061             writeData(0xD0 | (curvePart & 0x03));
01062         } else {
01063             writeData(0x90 | (curvePart & 0x03));
01064         }
01065 
01066         /* Wait for the command to finish */
01067         waitPoll(RA8875_ELLIPSE, RA8875_ELLIPSE_STATUS);
01068     }
01069 
01070     /************************* Mid Level ***********************************/
01071 
01072     /**************************************************************************/
01073     /*!
01074 
01075     */
01076     /**************************************************************************/
01077     void Adafruit_RA8875::GPIOX(bool on) {
01078         if (on)
01079             writeReg(RA8875_GPIOX, 1);
01080         else
01081             writeReg(RA8875_GPIOX, 0);
01082     }
01083 
01084     /**************************************************************************/
01085     /*!
01086 
01087     */
01088     /**************************************************************************/
01089     void Adafruit_RA8875::PWM1out(uint8_t p) {
01090         writeReg(RA8875_P1DCR, p);
01091     }
01092 
01093     /**************************************************************************/
01094     /*!
01095 
01096     */
01097     /**************************************************************************/
01098     void Adafruit_RA8875::PWM2out(uint8_t p) {
01099         writeReg(RA8875_P2DCR, p);
01100     }
01101 
01102     /**************************************************************************/
01103     /*!
01104 
01105     */
01106     /**************************************************************************/
01107     void Adafruit_RA8875::PWM1config(bool on, uint8_t clock) {
01108         if (on) {
01109             writeReg(RA8875_P1CR, RA8875_P1CR_ENABLE | (clock & 0xF));
01110         } else {
01111             writeReg(RA8875_P1CR, RA8875_P1CR_DISABLE | (clock & 0xF));
01112         }
01113     }
01114 
01115     /**************************************************************************/
01116     /*!
01117 
01118     */
01119     /**************************************************************************/
01120     void Adafruit_RA8875::PWM2config(bool on, uint8_t clock) {
01121         if (on) {
01122             writeReg(RA8875_P2CR, RA8875_P2CR_ENABLE | (clock & 0xF));
01123         } else {
01124             writeReg(RA8875_P2CR, RA8875_P2CR_DISABLE | (clock & 0xF));
01125         }
01126     }
01127 
01128     /**************************************************************************/
01129     /*!
01130           Enables or disables the on-chip touch screen controller
01131     */
01132     /**************************************************************************/
01133     void Adafruit_RA8875::touchEnable(bool on) {
01134         uint8_t   adcClk = (uint8_t) RA8875_TPCR0_ADCCLK_DIV4;
01135 
01136         if ( _size == RA8875_800x480 ) //match up touch size with LCD size
01137             adcClk = (uint8_t) RA8875_TPCR0_ADCCLK_DIV16;
01138 
01139         if (on) {
01140             /* Enable Touch Panel (Reg 0x70) */
01141             writeReg(RA8875_TPCR0, RA8875_TPCR0_ENABLE        |
01142                      RA8875_TPCR0_WAIT_4096CLK  |
01143                      RA8875_TPCR0_WAKEENABLE   |
01144                      adcClk); // 10mhz max!
01145             /* Set Auto Mode      (Reg 0x71) */
01146             writeReg(RA8875_TPCR1, RA8875_TPCR1_AUTO    |
01147                      // RA8875_TPCR1_VREFEXT |
01148                      RA8875_TPCR1_DEBOUNCE);
01149             /* Enable TP INT */
01150             writeReg(RA8875_INTC1, readReg(RA8875_INTC1) | RA8875_INTC1_TP);
01151         } else {
01152             /* Disable TP INT */
01153             writeReg(RA8875_INTC1, readReg(RA8875_INTC1) & ~RA8875_INTC1_TP);
01154             /* Disable Touch Panel (Reg 0x70) */
01155             writeReg(RA8875_TPCR0, RA8875_TPCR0_DISABLE);
01156         }
01157     }
01158 
01159     /**************************************************************************/
01160     /*!
01161           Checks if a touch event has occured
01162 
01163           @returns  True is a touch event has occured (reading it via
01164                     touchRead() will clear the interrupt in memory)
01165     */
01166     /**************************************************************************/
01167     bool Adafruit_RA8875::touched(void) {
01168         if (readReg(RA8875_INTC2) & RA8875_INTC2_TP) return true;
01169         return false;
01170     }
01171 
01172     /**************************************************************************/
01173     /*!
01174           Reads the last touch event
01175 
01176           @args x[out]  Pointer to the uint16_t field to assign the raw X value
01177           @args y[out]  Pointer to the uint16_t field to assign the raw Y value
01178 
01179           @note Calling this function will clear the touch panel interrupt on
01180                 the RA8875, resetting the flag used by the 'touched' function
01181     */
01182     /**************************************************************************/
01183     bool Adafruit_RA8875::touchRead(uint16_t *x, uint16_t *y) {
01184         uint16_t tx, ty;
01185         uint8_t temp;
01186 
01187         tx = readReg(RA8875_TPXH);
01188         ty = readReg(RA8875_TPYH);
01189         temp = readReg(RA8875_TPXYL);
01190         tx <<= 2;
01191         ty <<= 2;
01192         tx |= temp & 0x03;        // get the bottom x bits
01193         ty |= (temp >> 2) & 0x03; // get the bottom y bits
01194 
01195         *x = tx;
01196         *y = ty;
01197 
01198         /* Clear TP INT Status */
01199         writeReg(RA8875_INTC2, RA8875_INTC2_TP);
01200 
01201         return true;
01202     }
01203 
01204     /**************************************************************************/
01205     /*!
01206           Turns the display on or off
01207     */
01208     /**************************************************************************/
01209     void Adafruit_RA8875::displayOn(bool on) {
01210         if (on)
01211             writeReg(RA8875_PWRR, RA8875_PWRR_NORMAL | RA8875_PWRR_DISPON);
01212         else
01213             writeReg(RA8875_PWRR, RA8875_PWRR_NORMAL | RA8875_PWRR_DISPOFF);
01214     }
01215 
01216     /**************************************************************************/
01217     /*!
01218         Puts the display in sleep mode, or disables sleep mode if enabled
01219     */
01220     /**************************************************************************/
01221     void Adafruit_RA8875::sleep(bool sleep) {
01222         if (sleep)
01223             writeReg(RA8875_PWRR, RA8875_PWRR_DISPOFF | RA8875_PWRR_SLEEP);
01224         else
01225             writeReg(RA8875_PWRR, RA8875_PWRR_DISPOFF);
01226     }
01227 
01228     /************************* Low Level ***********************************/
01229 
01230     /**************************************************************************/
01231     /*!
01232 
01233     */
01234     /**************************************************************************/
01235     void  Adafruit_RA8875::writeReg(uint8_t reg, uint8_t val) {
01236         writeCommand(reg);
01237         writeData(val);
01238     }
01239 
01240     /**************************************************************************/
01241     /*!
01242 
01243     */
01244     /**************************************************************************/
01245     uint8_t  Adafruit_RA8875::readReg(uint8_t reg) {
01246         writeCommand(reg);
01247         return readData();
01248     }
01249 
01250     /**************************************************************************/
01251     /*!
01252 
01253     */
01254     /**************************************************************************/
01255     void  Adafruit_RA8875::writeData(uint8_t d) {
01256         _cs = 0;
01257 
01258         spi.write(RA8875_DATAWRITE);
01259         spi.write(d);
01260         _cs = 1;
01261 
01262     }
01263 
01264     /**************************************************************************/
01265     /*!
01266 
01267     */
01268     /**************************************************************************/
01269     uint8_t  Adafruit_RA8875::readData(void) {
01270         _cs = 0;
01271 
01272         spi.write(RA8875_DATAREAD);
01273         uint8_t x = spi.write(0x0);
01274 
01275         _cs = 1;
01276         return x;
01277     }
01278 
01279     /**************************************************************************/
01280     /*!
01281 
01282     */
01283     /**************************************************************************/
01284     void  Adafruit_RA8875::writeCommand(uint8_t d) {
01285         _cs = 0;
01286 
01287         spi.write(RA8875_CMDWRITE);
01288         spi.write(d);
01289 
01290         _cs = 1;
01291     }
01292 
01293     /**************************************************************************/
01294     /*!
01295 
01296     */
01297     /**************************************************************************/
01298     uint8_t  Adafruit_RA8875::readStatus(void) {
01299         _cs = 0;
01300 
01301         spi.write(RA8875_CMDREAD);
01302         uint8_t x = spi.write(0x0);
01303 
01304         _cs = 1;
01305         return x;
01306     }