Chris Taylor / Mbed 2 deprecated RETRO-SpaceRaiders Featured

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LCD_ST7735.h Source File

LCD_ST7735.h

00001 ///////////////////////////////////////////////////////////////////////////////
00002 // LCD_ST7735 - Driver for ST7735 LCD display controller
00003 // Author: Chris Taylor (taylorza)
00004 
00005 #include "Bitmap1bpp.h"
00006 #include "Bitmap4bpp.h"
00007 
00008 #ifndef __LCD_ST7735__
00009 #define __LCD_ST7735__
00010 
00011 /** LCD_ST7735 is a simple driver for the ST7735 LCD controller. It provides basic drawing primitives sa well as text and font capabilities.
00012  * The driver is currently hardcoded to support 65K colors using a 565 RGB pixel format.
00013 */
00014 class LCD_ST7735
00015 {    
00016     public:
00017         /** Orientation of the display */
00018         enum Orientation 
00019         {
00020             /** No rotation of the display image*/
00021             Rotate0, 
00022             /** Rotate the display image 90 degrees */
00023             Rotate90, 
00024             /** Rotate the display image 180 degrees */
00025             Rotate180, 
00026             /** Rotate the display image 270 degrees */
00027             Rotate270
00028         };
00029         
00030         /** Type of color filter of the panel */
00031         enum PanelColorFilter
00032         {
00033             /** RGB color filter panel */
00034             RGB = 0,
00035             
00036             /** BGR color filter panel */
00037             BGR = 8,
00038         };
00039         
00040     public:
00041         /**Creates an instance of the LCD_ST7735 driver
00042          * @param backlightPin pin used to control the backlight
00043          * @param resetPin pin used to reset the display controller
00044          * @param dsPin pin used to put the display controller into data mode
00045          * @param mosiPin SPI channel MOSI pin
00046          * @param misoPin SPI channel MISO pin
00047          * @param clkPin SPI channel clock pin
00048          * @param csPin SPI chip select pin 
00049         */
00050         LCD_ST7735(
00051             PinName backlightPin,
00052             PinName resetPin,
00053             PinName dsPin,
00054             PinName mosiPin,
00055             PinName misoPin,
00056             PinName clkPin,
00057             PinName csPin,
00058             PanelColorFilter colorFilter = BGR
00059             );
00060             
00061         /** Set the orientation of the display
00062          * @param orientation Orientation of the display.
00063          * @param flip Flips the display direction
00064          */
00065         void setOrientation(Orientation orientation, bool flip);
00066         
00067         /** Get the width of the display given the current orientation */
00068         int getWidth();
00069         
00070         /** Get the height of the display given the current orientation */
00071         int getHeight();
00072     
00073         /** Control the display's backlight 
00074          * @param state true to turn the backlight on, false to turn it off   
00075         */
00076         void setBacklight(bool state);
00077         
00078         /** Clear the screen 
00079          * @param color The color used to clear the screen. Defaults to black if not passed.
00080         */
00081         void clearScreen(uint16_t color = 0x0000);
00082         
00083         /**  Set a pixel on the display to the specified color 
00084          * @param x The X coordinate of the pixel (0..127)   
00085          * @param y The Y coordinate of the pixel (0..159)   
00086          * @param color Color to set the pixel to.
00087         */
00088         void setPixel(int x, int y, uint16_t color);
00089         
00090         /** Draw a line on the display
00091          * @param x1 The X coordinate of the starting point on the line
00092          * @param y1 The Y coordinate of the starting point on the line
00093          * @param x2 The X coordinate of the end point on the line
00094          * @param y2 The Y coordinate of the end point on the line
00095          * @param color The color used to draw the pixel
00096         */
00097         void drawLine(int x1, int y1, int x2, int y2, uint16_t color);
00098         
00099         /** Draw a rectangle on the display 
00100          * @param x1 The X coordinate of the upper left corner
00101          * @param y1 The Y coordinate of the upper left corner
00102          * @param x2 The X coordinate of the lower right corner
00103          * @param y2 The Y coordinate of the lower right corner
00104          * @param color The color used to draw the rectangle
00105         */
00106         void drawRect(int x1, int y1, int x2, int y2, uint16_t color);
00107         
00108         /** Draw a circle on the display
00109          * @param x The X coordinate of the center of the circle
00110          * @param y The Y coordinate of the center of the circle
00111          * @param r The radius of the circle
00112          * @param color The color used to draw the circle
00113         */
00114         void drawCircle(int x, int y, int r, uint16_t color);
00115         
00116         /** Draw an ellipse on the display
00117          * @param x The X coordinate of the center of the ellipse
00118          * @param y The Y coordinate of the center of the ellipse
00119          * @param rx The X radius of the ellipse
00120          * @param ry The X radius of the ellipse
00121          * @param color The color used to draw the ellipse
00122         */
00123         void drawEllipse(int x, int y, int rx, int ry, uint16_t color);
00124         
00125         /** Draw a filled rectangle on the display 
00126          * @param x1 The X coordinate of the upper left corner
00127          * @param y1 The Y coordinate of the upper left corner
00128          * @param x2 The X coordinate of the lower right corner
00129          * @param y2 The Y coordinate of the lower right corner
00130          * @param fillColor The color used to fill the rectangle
00131         */
00132         void fillRect(int x1, int y1, int x2, int y2, uint16_t fillColor);
00133         
00134         /** Draw a filled rectangle on the display 
00135          * @param x1 The X coordinate of the upper left corner
00136          * @param y1 The Y coordinate of the upper left corner
00137          * @param x2 The X coordinate of the lower right corner
00138          * @param y2 The Y coordinate of the lower right corner
00139          * @param borderColor The color used to draw the rectangle frame
00140          * @param fillColor The color used to fill the rectangle
00141         */
00142         void fillRect(int x1, int y1, int x2, int y2, uint16_t borderColor, uint16_t fillColor);
00143         
00144         /** Draw a filled circle on the display
00145          * @param x The X coordinate of the center of the circle
00146          * @param y The Y coordinate of the center of the circle
00147          * @param borderColor The color used to draw the circumference of the circle
00148          * @param fillColor The color used to fill the circle
00149         */
00150         void fillCircle(int x, int y, int r, uint16_t borderColor, uint16_t fillColor);
00151         
00152         /** Draw a filled ellipse on the display
00153          * @param x The X coordinate of the center of the ellipse
00154          * @param y The Y coordinate of the center of the ellipse
00155          * @param rx The X radius of the ellipse
00156          * @param ry The X radius of the ellipse
00157          * @param borderColor The color used to draw the circumference of the circle
00158          * @param fillColor The color used to fill the circle
00159         */
00160         void fillEllipse(int x, int y, int rx, int ry, uint16_t borderColor, uint16_t fillColor);
00161         
00162         /** Draw a bitmap on the screen 
00163          * @param x The X coordinate location to draw the bitmap.
00164          * @param y The Y coordinate location to draw the bitmap.
00165          * @param pbmp Pointer to the bitmap.
00166          * @note The bitmap is an single dimensional uint8_t (unsigned short) array. 
00167          * The first to elements of the array indicate the width and height of the bitmap repectively.
00168          * The rest of the entries int the array make up the pixel data for the array.
00169         */
00170         void drawBitmap(int x, int y, const uint16_t *pbmp);
00171         
00172         /** Extracts a portion of a bitmap and draws it on the screen 
00173          * @param x The X coordinate location to draw the bitmap.
00174          * @param y The Y coordinate location to draw the bitmap.
00175          * @param pbmp Pointer to the bitmap.
00176          * @param srcX X offset into the source bitmap of the portion to extract
00177          * @param srcY Y offset into the source bitmap of the portion to extract
00178          * @param srcWidth Width of the bitmap portion to draw
00179          * @param srcHeight Height of the bitmap portion to draw
00180          * @note The bitmap is an single dimensional uint8_t (unsigned short) array. 
00181          * The first to elements of the array indicate the width and height of the bitmap repectively.
00182          * The rest of the entries int the array make up the pixel data for the array.
00183         */
00184         void drawBitmap(int x, int y, const uint16_t *pbmp, int srcX, int srcY, int srcWidth, int srcHeight);
00185         
00186         /** Extracts a portion of a bitmap and draws it on the screen 
00187          * @param x The X coordinate location to draw the bitmap.
00188          * @param y The Y coordinate location to draw the bitmap.
00189          * @param bmp refence to a 4-bpp bitmap.
00190          * @param srcX X offset into the source bitmap of the portion to extract
00191          * @param srcY Y offset into the source bitmap of the portion to extract
00192          * @param srcWidth Width of the bitmap portion to draw
00193          * @param srcHeight Height of the bitmap portion to draw         
00194         */
00195         void drawBitmap(int x, int y, Bitmap4bpp &bmp, int srcX, int srcY, int srcWidth, int srcHeight);
00196         
00197         /** Extracts a portion of a bitmap and draws it on the screen 
00198          * @param x The X coordinate location to draw the bitmap.
00199          * @param y The Y coordinate location to draw the bitmap.
00200          * @param bmp refence to a 1-bpp bitmap.
00201          * @param srcX X offset into the source bitmap of the portion to extract
00202          * @param srcY Y offset into the source bitmap of the portion to extract
00203          * @param srcWidth Width of the bitmap portion to draw
00204          * @param srcHeight Height of the bitmap portion to draw
00205          * @param foregroundColor Color to use for pixels in the bitmap
00206          * @param backgroundColor Color to use for off pixels in the bitmap
00207         */
00208         void drawBitmap(int x, int y, Bitmap1bpp &bmp, int srcX, int srcY, int srcWidth, int srcHeight, uint16_t foregroundColor, uint16_t backgroundColor);
00209         
00210         /** Extracts a portion of a bitmap and draws it on the screen 
00211          * @param x The X coordinate location to draw the bitmap.
00212          * @param y The Y coordinate location to draw the bitmap.
00213          * @param pbmp Pointer to the bitmap.
00214          * @param srcX X offset into the source bitmap of the portion to extract
00215          * @param srcY Y offset into the source bitmap of the portion to extract
00216          * @param srcWidth Width of the bitmap portion to draw
00217          * @param srcHeight Height of the bitmap portion to draw
00218          * @param foregroundColor Color to use for pixels in the bitmap
00219          * @param backgroundColor Color to use for off pixels in the bitmap
00220          * @note The bitmap is an single dimensional uint8_t (unsigned short) array. 
00221          * The first two uint16_t elements of the array indicate the width and height of the bitmap repectively.
00222          * The rest of the entries int the array make up the pixel data for the array.
00223         */
00224         void drawBitmap(int x, int y, const uint8_t *pbmp, int srcX, int srcY, int srcWidth, int srcHeight, uint16_t foregroundColor, uint16_t backgroundColor);
00225         
00226         /** Set the foreground color used to render text
00227          * @param color Color used when drawing text to the display
00228          * @note The color can be changed multiple times to render text in various colors on the display
00229         */
00230         void setForegroundColor(uint16_t color);
00231         
00232         /** Set the background color used to render text
00233          * @param color Color used when drawing background portions of the text
00234          * @note The color can be changed multiple times to render text with various background colors on the display
00235         */
00236         void setBackgroundColor(uint16_t color);        
00237         
00238         /** Draw a string to the screen using the currently active foreground and background colors
00239          * @param pFont Pointer to the font used to render the string to the display
00240          * @param x The X coordinate location to draw the string.
00241          * @param y The Y coordinate location to draw the string.
00242          * @param pString ASCIIZ string to draw to the display.
00243          * @note The font is currently limited to an 8x8 font. See the font_IBM.h file for an example font.
00244         */
00245         void drawString(const uint8_t *pFont, int x, int y, const char *pString); 
00246         
00247         /** Select the device on the SPI bus.
00248         selectDevice needs to be called before accessing the screen if there are multiple devices on the SPI bus.
00249         */
00250         void selectDevice();   
00251         
00252     private:
00253         void drawVertLine(int x1, int y1, int y2, uint16_t color);
00254         void drawHorizLine(int x1, int y1, int x2, uint16_t color);
00255         void drawChar(const uint8_t *pFont, int x, int y, char c);
00256         
00257     private:
00258         void swap(int &a, int &b);
00259         
00260     private:
00261         void initDisplay();
00262         void reset();
00263         
00264         void writeCommand(uint8_t cmd);
00265         void write(uint8_t cmd, uint8_t data[], int dataLen);
00266         void write(uint8_t cmd, uint16_t data);        
00267         
00268         void beginBatchCommand(uint8_t cmd);
00269         void writeBatchData(uint8_t data);
00270         void writeBatchData(uint8_t dataHigh, uint8_t dataLow);
00271         void writeBatchData(uint16_t data);
00272         void endBatchCommand();
00273         
00274         void clip(int x, int y, int w, int h);
00275         void clipRect(int x1, int y1, int x2, int y2);
00276         
00277     private:
00278         int         _width;
00279         int         _height;
00280         Orientation _orientation;
00281         PanelColorFilter _colorFilter;
00282         bool        _flip; 
00283         uint8_t    _foregroundColorHigh;
00284         uint8_t    _foregroundColorLow;
00285         uint8_t    _backgroundColorHigh;
00286         uint8_t    _backgroundColorLow;
00287         
00288     private:
00289         class LCDSPI : public SPI
00290         {
00291             public:
00292                 LCDSPI(PinName mosi, PinName miso, PinName sclk) :
00293                     SPI(mosi, miso, sclk)
00294                 {
00295                 }
00296                 
00297                 void prepareFastSPI()
00298                 {
00299                     #ifdef TARGET_LPC11U24
00300                     aquire();
00301                     #endif
00302                 }
00303                 
00304                 inline void waitWhileBusy()
00305                 {
00306                     #ifdef TARGET_LPC11U24
00307                     while (((_spi.spi->SR) & 0x10) != 0);
00308                     #endif
00309                 }
00310                 
00311                 inline void fastWrite(uint8_t data)
00312                 {       
00313                     #ifdef TARGET_LPC11U24
00314                         while (((_spi.spi->SR) & 0x01) == 0);
00315                         _spi.spi->DR = data;
00316                     #else
00317                         SPI::write(data);
00318                     #endif 
00319                 }       
00320                 
00321                 void clearRx()
00322                 { 
00323                     #ifdef TARGET_LPC11U24
00324                         while (((_spi.spi->SR) & 0x14) != 0)
00325                         {
00326                             while (((_spi.spi->SR) & 0x04) == 0);
00327                             int data = _spi.spi->DR;
00328                         }                                         
00329                     #endif  
00330                 }            
00331         };
00332         
00333     private:                
00334         DigitalOut  _backlight;
00335         DigitalOut  _reset;
00336         DigitalOut  _ds;
00337         DigitalOut  _cs;
00338         LCDSPI      _spi;
00339         
00340         uint16_t    *_palette;
00341         
00342         static const uint16_t DefaultPalette[];
00343     private:
00344         static const uint8_t CMD_SLPOUT     = 0x11;
00345         static const uint8_t CMD_DISPON     = 0x29;
00346         static const uint8_t CMD_CASET      = 0x2a;
00347         static const uint8_t CMD_RASET      = 0x2b;
00348         static const uint8_t CMD_RAMWR      = 0x2c;
00349         
00350         static const uint8_t CMD_MADCTL     = 0x36;
00351         static const uint8_t CMD_COLMOD     = 0x3a;
00352         
00353         static const uint8_t CMD_FRMCTR1    = 0xb1;
00354         static const uint8_t CMD_FRMCTR2    = 0xb2;
00355         static const uint8_t CMD_FRMCTR3    = 0xb3;
00356         static const uint8_t CMD_INVCTR     = 0xb4;
00357         
00358         static const uint8_t CMD_PWCTR1     = 0xc0;
00359         static const uint8_t CMD_PWCTR2     = 0xc1;
00360         static const uint8_t CMD_PWCTR3     = 0xc2;
00361         static const uint8_t CMD_PWCTR4     = 0xc3;
00362         static const uint8_t CMD_PWCTR5     = 0xc4;
00363         static const uint8_t CMD_VMCTR1     = 0xc5;
00364         
00365         static const uint8_t CMD_GAMCTRP1   = 0xe0;
00366         static const uint8_t CMD_GAMCTRN1   = 0xe1;
00367         
00368         static const uint8_t CMD_EXTCTRL    = 0xf0;                
00369 };
00370 
00371 #endif // __LCD_ST7735__