SPI based library for the ST7735 LCD controller.

Dependents:   RayCastingEngine RETRO_LCD_PerformanceTest RETRO_loop_test RETRO_RickGame ... more

Committer:
taylorza
Date:
Sat Sep 20 04:28:41 2014 +0000
Revision:
1:33ff5fad4320
Parent:
0:7b3fb3085867
Child:
3:451148656b76
Added support for:
; 1. Drawing ellipses and drawing filled ellipses.
; 2. Drawing a bitmap from a subsection of an existing bitmap

Who changed what in which revision?

UserRevisionLine numberNew contents of line
taylorza 0:7b3fb3085867 1 ///////////////////////////////////////////////////////////////////////////////
taylorza 0:7b3fb3085867 2 // LCD_ST7735 - Driver for ST7735 LCD display controller
taylorza 0:7b3fb3085867 3 // Author: Chris Taylor (taylorza)
taylorza 0:7b3fb3085867 4
taylorza 0:7b3fb3085867 5 #ifndef __LCD_ST7735__
taylorza 0:7b3fb3085867 6 #define __LCD_ST7735__
taylorza 0:7b3fb3085867 7 /** LCD_ST7735 is a simple driver for the ST7735 LCD controller. It provides basic drawing primitives sa well as text and font capabilities.
taylorza 0:7b3fb3085867 8 * The driver is currently hardcoded to support 65K colors using a 565 RGB pixel format.
taylorza 0:7b3fb3085867 9 */
taylorza 0:7b3fb3085867 10 class LCD_ST7735
taylorza 0:7b3fb3085867 11 {
taylorza 0:7b3fb3085867 12 public:
taylorza 0:7b3fb3085867 13 /**Creates an instance of the LCD_ST7735 driver
taylorza 0:7b3fb3085867 14 * @param backlightPin pin used to control the backlight
taylorza 0:7b3fb3085867 15 * @param resetPin pin used to reset the display controller
taylorza 0:7b3fb3085867 16 * @param dsPin pin used to put the display controller into data mode
taylorza 0:7b3fb3085867 17 * @param mosiPin SPI channel MOSI pin
taylorza 0:7b3fb3085867 18 * @param misoPin SPI channel MISO pin
taylorza 0:7b3fb3085867 19 * @param clkPin SPI channel clock pin
taylorza 0:7b3fb3085867 20 * @param csPin SPI chip select pin
taylorza 0:7b3fb3085867 21 */
taylorza 0:7b3fb3085867 22 LCD_ST7735(
taylorza 0:7b3fb3085867 23 PinName backlightPin,
taylorza 0:7b3fb3085867 24 PinName resetPin,
taylorza 0:7b3fb3085867 25 PinName dsPin,
taylorza 0:7b3fb3085867 26 PinName mosiPin,
taylorza 0:7b3fb3085867 27 PinName misoPin,
taylorza 0:7b3fb3085867 28 PinName clkPin,
taylorza 0:7b3fb3085867 29 PinName csPin
taylorza 0:7b3fb3085867 30 );
taylorza 0:7b3fb3085867 31
taylorza 0:7b3fb3085867 32 /** Control the display's backlight
taylorza 0:7b3fb3085867 33 * @param state true to turn the backlight on, false to turn it off
taylorza 0:7b3fb3085867 34 */
taylorza 0:7b3fb3085867 35 void setBacklight(bool state);
taylorza 0:7b3fb3085867 36
taylorza 0:7b3fb3085867 37 /** Clear the screen
taylorza 0:7b3fb3085867 38 * @param color The color used to clear the screen. Defaults to black if not passed.
taylorza 0:7b3fb3085867 39 */
taylorza 0:7b3fb3085867 40 void clearScreen(uint16_t color = 0x0000);
taylorza 0:7b3fb3085867 41
taylorza 0:7b3fb3085867 42 /** Set a pixel on the display to the specified color
taylorza 0:7b3fb3085867 43 * @param x The X coordinate of the pixel (0..127)
taylorza 0:7b3fb3085867 44 * @param y The Y coordinate of the pixel (0..159)
taylorza 0:7b3fb3085867 45 * @param color Color to set the pixel to.
taylorza 0:7b3fb3085867 46 */
taylorza 0:7b3fb3085867 47 void setPixel(int x, int y, uint16_t color);
taylorza 0:7b3fb3085867 48
taylorza 0:7b3fb3085867 49 /** Draw a line on the display
taylorza 0:7b3fb3085867 50 * @param x1 The X coordinate of the starting point on the line
taylorza 0:7b3fb3085867 51 * @param y1 The Y coordinate of the starting point on the line
taylorza 0:7b3fb3085867 52 * @param x2 The X coordinate of the end point on the line
taylorza 0:7b3fb3085867 53 * @param y2 The Y coordinate of the end point on the line
taylorza 0:7b3fb3085867 54 * @param color The color used to draw the pixel
taylorza 0:7b3fb3085867 55 */
taylorza 0:7b3fb3085867 56 void drawLine(int x1, int y1, int x2, int y2, uint16_t color);
taylorza 0:7b3fb3085867 57
taylorza 0:7b3fb3085867 58 /** Draw a rectangle on the display
taylorza 0:7b3fb3085867 59 * @param x1 The X coordinate of the upper left corner
taylorza 0:7b3fb3085867 60 * @param y1 The Y coordinate of the upper left corner
taylorza 0:7b3fb3085867 61 * @param x2 The X coordinate of the lower right corner
taylorza 0:7b3fb3085867 62 * @param y2 The Y coordinate of the lower right corner
taylorza 0:7b3fb3085867 63 * @param color The color used to draw the rectangle
taylorza 0:7b3fb3085867 64 */
taylorza 0:7b3fb3085867 65 void drawRect(int x1, int y1, int x2, int y2, uint16_t color);
taylorza 0:7b3fb3085867 66
taylorza 0:7b3fb3085867 67 /** Draw a circle on the display
taylorza 0:7b3fb3085867 68 * @param x The X coordinate of the center of the circle
taylorza 0:7b3fb3085867 69 * @param y The Y coordinate of the center of the circle
taylorza 0:7b3fb3085867 70 * @param r The radius of the circle
taylorza 0:7b3fb3085867 71 * @param color The color used to draw the circle
taylorza 0:7b3fb3085867 72 */
taylorza 0:7b3fb3085867 73 void drawCircle(int x, int y, int r, uint16_t color);
taylorza 0:7b3fb3085867 74
taylorza 1:33ff5fad4320 75 /** Draw an ellipse on the display
taylorza 1:33ff5fad4320 76 * @param x The X coordinate of the center of the ellipse
taylorza 1:33ff5fad4320 77 * @param y The Y coordinate of the center of the ellipse
taylorza 1:33ff5fad4320 78 * @param rx The X radius of the ellipse
taylorza 1:33ff5fad4320 79 * @param ry The X radius of the ellipse
taylorza 1:33ff5fad4320 80 * @param color The color used to draw the ellipse
taylorza 1:33ff5fad4320 81 */
taylorza 1:33ff5fad4320 82 void drawEllipse(int x, int y, int rx, int ry, uint16_t color);
taylorza 1:33ff5fad4320 83
taylorza 0:7b3fb3085867 84 /** Draw a filled rectangle on the display
taylorza 0:7b3fb3085867 85 * @param x1 The X coordinate of the upper left corner
taylorza 0:7b3fb3085867 86 * @param y1 The Y coordinate of the upper left corner
taylorza 0:7b3fb3085867 87 * @param x2 The X coordinate of the lower right corner
taylorza 0:7b3fb3085867 88 * @param y2 The Y coordinate of the lower right corner
taylorza 0:7b3fb3085867 89 * @param borderColor The color used to draw the rectangle frame
taylorza 0:7b3fb3085867 90 * @param fillColor The color used to fill the rectangle
taylorza 0:7b3fb3085867 91 */
taylorza 0:7b3fb3085867 92 void fillRect(int x1, int y1, int x2, int y2, uint16_t borderColor, uint16_t fillColor);
taylorza 0:7b3fb3085867 93
taylorza 0:7b3fb3085867 94 /** Draw a filled circle on the display
taylorza 0:7b3fb3085867 95 * @param x The X coordinate of the center of the circle
taylorza 0:7b3fb3085867 96 * @param y The Y coordinate of the center of the circle
taylorza 0:7b3fb3085867 97 * @param borderColor The color used to draw the circumference of the circle
taylorza 0:7b3fb3085867 98 * @param fillColor The color used to fill the circle
taylorza 0:7b3fb3085867 99 */
taylorza 0:7b3fb3085867 100 void fillCircle(int x, int y, int r, uint16_t borderColor, uint16_t fillColor);
taylorza 0:7b3fb3085867 101
taylorza 1:33ff5fad4320 102 /** Draw a filled ellipse on the display
taylorza 1:33ff5fad4320 103 * @param x The X coordinate of the center of the ellipse
taylorza 1:33ff5fad4320 104 * @param y The Y coordinate of the center of the ellipse
taylorza 1:33ff5fad4320 105 * @param rx The X radius of the ellipse
taylorza 1:33ff5fad4320 106 * @param ry The X radius of the ellipse
taylorza 1:33ff5fad4320 107 * @param borderColor The color used to draw the circumference of the circle
taylorza 1:33ff5fad4320 108 * @param fillColor The color used to fill the circle
taylorza 1:33ff5fad4320 109 */
taylorza 1:33ff5fad4320 110 void fillEllipse(int x, int y, int rx, int ry, uint16_t borderColor, uint16_t fillColor);
taylorza 1:33ff5fad4320 111
taylorza 0:7b3fb3085867 112 /** Draw a bitmap on the screen
taylorza 0:7b3fb3085867 113 * @param x The X coordinate location to draw the bitmap.
taylorza 0:7b3fb3085867 114 * @param y The Y coordinate location to draw the bitmap.
taylorza 0:7b3fb3085867 115 * @param pbmp Pointer to the bitmap.
taylorza 0:7b3fb3085867 116 * @note The bitmap is an single dimensional uint8_t (unsigned short) array.
taylorza 0:7b3fb3085867 117 * The first to elements of the array indicate the width and height of the bitmap repectively.
taylorza 0:7b3fb3085867 118 * The rest of the entries int the array make up the pixel data for the array.
taylorza 0:7b3fb3085867 119 */
taylorza 0:7b3fb3085867 120 void drawBitmap(int x, int y, const uint16_t *pbmp);
taylorza 0:7b3fb3085867 121
taylorza 1:33ff5fad4320 122 /** Extracts a portion of a bitmap and draws it on the screen
taylorza 1:33ff5fad4320 123 * @param x The X coordinate location to draw the bitmap.
taylorza 1:33ff5fad4320 124 * @param y The Y coordinate location to draw the bitmap.
taylorza 1:33ff5fad4320 125 * @param pbmp Pointer to the bitmap.
taylorza 1:33ff5fad4320 126 * @param srcX X offset into the source bitmap of the portion to extract
taylorza 1:33ff5fad4320 127 * @param srcY Y offset into the source bitmap of the portion to extract
taylorza 1:33ff5fad4320 128 * @param srcWidth Width of the bitmap portion to draw
taylorza 1:33ff5fad4320 129 * @param srcHeight Height of the bitmap portion to draw
taylorza 1:33ff5fad4320 130 * @note The bitmap is an single dimensional uint8_t (unsigned short) array.
taylorza 1:33ff5fad4320 131 * The first to elements of the array indicate the width and height of the bitmap repectively.
taylorza 1:33ff5fad4320 132 * The rest of the entries int the array make up the pixel data for the array.
taylorza 1:33ff5fad4320 133 */
taylorza 1:33ff5fad4320 134 void drawBitmap(int x, int y, const uint16_t *pbmp, int srcX, int srcY, int srcWidth, int srcHeight);
taylorza 1:33ff5fad4320 135
taylorza 0:7b3fb3085867 136 /** Set the foreground color used to render text
taylorza 0:7b3fb3085867 137 * @param color Color used when drawing text to the display
taylorza 0:7b3fb3085867 138 * @note The color can be changed multiple times to render text in various colors on the display
taylorza 0:7b3fb3085867 139 */
taylorza 0:7b3fb3085867 140 void setForegroundColor(uint16_t color);
taylorza 0:7b3fb3085867 141
taylorza 0:7b3fb3085867 142 /** Set the background color used to render text
taylorza 0:7b3fb3085867 143 * @param color Color used when drawing background portions of the text
taylorza 0:7b3fb3085867 144 * @note The color can be changed multiple times to render text with various background colors on the display
taylorza 0:7b3fb3085867 145 */
taylorza 0:7b3fb3085867 146 void setBackgroundColor(uint16_t color);
taylorza 0:7b3fb3085867 147
taylorza 0:7b3fb3085867 148 /** Draw a string to the screen using the currently active foreground and background colors
taylorza 0:7b3fb3085867 149 * @param pFont Pointer to the font used to render the string to the display
taylorza 0:7b3fb3085867 150 * @param x The X coordinate location to draw the string.
taylorza 0:7b3fb3085867 151 * @param y The Y coordinate location to draw the string.
taylorza 0:7b3fb3085867 152 * @param pString ASCIIZ string to draw to the display.
taylorza 0:7b3fb3085867 153 * @note The font is currently limited to an 8x8 font. See the font_IBM.h file for an example font.
taylorza 0:7b3fb3085867 154 */
taylorza 0:7b3fb3085867 155 void drawString(const uint8_t *pFont, int x, int y, const char *pString);
taylorza 0:7b3fb3085867 156
taylorza 0:7b3fb3085867 157 private:
taylorza 0:7b3fb3085867 158 void drawVertLine(int x1, int y1, int y2, uint16_t color);
taylorza 0:7b3fb3085867 159 void drawHorizLine(int x1, int y1, int x2, uint16_t color);
taylorza 0:7b3fb3085867 160 void drawChar(const uint8_t *pFont, int x, int y, char c);
taylorza 0:7b3fb3085867 161
taylorza 0:7b3fb3085867 162 private:
taylorza 0:7b3fb3085867 163 void swap(int &a, int &b);
taylorza 0:7b3fb3085867 164
taylorza 0:7b3fb3085867 165 private:
taylorza 0:7b3fb3085867 166 void initDisplay();
taylorza 0:7b3fb3085867 167 void reset();
taylorza 0:7b3fb3085867 168 void write(uint8_t cmd);
taylorza 0:7b3fb3085867 169 void write(uint8_t cmd, uint8_t data[], int dataLen);
taylorza 0:7b3fb3085867 170 void write(uint8_t cmd, uint16_t data);
taylorza 0:7b3fb3085867 171 void writeData(uint8_t data);
taylorza 0:7b3fb3085867 172 void beginBatchCommand(uint8_t cmd);
taylorza 0:7b3fb3085867 173 void writeBatchData(uint8_t data);
taylorza 0:7b3fb3085867 174 void writeBatchData(uint16_t data);
taylorza 0:7b3fb3085867 175 void endBatchCommand();
taylorza 0:7b3fb3085867 176
taylorza 0:7b3fb3085867 177 void clip(int x, int y, int w, int h);
taylorza 0:7b3fb3085867 178 void clipRect(int x1, int y1, int x2, int y2);
taylorza 0:7b3fb3085867 179
taylorza 0:7b3fb3085867 180 private:
taylorza 0:7b3fb3085867 181 uint16_t _foregroundColor;
taylorza 0:7b3fb3085867 182 uint16_t _backgroundColor;
taylorza 0:7b3fb3085867 183
taylorza 0:7b3fb3085867 184 private:
taylorza 0:7b3fb3085867 185 DigitalOut _backlight;
taylorza 0:7b3fb3085867 186 DigitalOut _reset;
taylorza 0:7b3fb3085867 187 DigitalOut _ds;
taylorza 0:7b3fb3085867 188 DigitalOut _cs;
taylorza 0:7b3fb3085867 189 SPI _spi;
taylorza 0:7b3fb3085867 190
taylorza 0:7b3fb3085867 191 private:
taylorza 0:7b3fb3085867 192 static const uint8_t CMD_SLPOUT = 0x11;
taylorza 0:7b3fb3085867 193 static const uint8_t CMD_DISPON = 0x29;
taylorza 0:7b3fb3085867 194 static const uint8_t CMD_CASET = 0x2a;
taylorza 0:7b3fb3085867 195 static const uint8_t CMD_RASET = 0x2b;
taylorza 0:7b3fb3085867 196 static const uint8_t CMD_RAMWR = 0x2c;
taylorza 0:7b3fb3085867 197
taylorza 0:7b3fb3085867 198 static const uint8_t CMD_MADCTL = 0x36;
taylorza 0:7b3fb3085867 199 static const uint8_t CMD_COLMOD = 0x3a;
taylorza 0:7b3fb3085867 200
taylorza 0:7b3fb3085867 201 static const uint8_t CMD_FRMCTR1 = 0xb1;
taylorza 0:7b3fb3085867 202 static const uint8_t CMD_FRMCTR2 = 0xb2;
taylorza 0:7b3fb3085867 203 static const uint8_t CMD_FRMCTR3 = 0xb3;
taylorza 0:7b3fb3085867 204 static const uint8_t CMD_INVCTR = 0xb4;
taylorza 0:7b3fb3085867 205
taylorza 0:7b3fb3085867 206 static const uint8_t CMD_PWCTR1 = 0xc0;
taylorza 0:7b3fb3085867 207 static const uint8_t CMD_PWCTR2 = 0xc1;
taylorza 0:7b3fb3085867 208 static const uint8_t CMD_PWCTR3 = 0xc2;
taylorza 0:7b3fb3085867 209 static const uint8_t CMD_PWCTR4 = 0xc3;
taylorza 0:7b3fb3085867 210 static const uint8_t CMD_PWCTR5 = 0xc4;
taylorza 0:7b3fb3085867 211 static const uint8_t CMD_VMCTR1 = 0xc5;
taylorza 0:7b3fb3085867 212
taylorza 0:7b3fb3085867 213 static const uint8_t CMD_GAMCTRP1 = 0xe0;
taylorza 0:7b3fb3085867 214 static const uint8_t CMD_GAMCTRN1 = 0xe1;
taylorza 0:7b3fb3085867 215
taylorza 0:7b3fb3085867 216 static const uint8_t CMD_EXTCTRL = 0xf0;
taylorza 0:7b3fb3085867 217 };
taylorza 0:7b3fb3085867 218
taylorza 0:7b3fb3085867 219 #endif // __LCD_ST7735__