SPI based library for the ST7735 LCD controller.
Dependents: RayCastingEngine RETRO_LCD_PerformanceTest RETRO_loop_test RETRO_RickGame ... more
LCD_ST7735.h@3:451148656b76, 2014-09-20 (annotated)
- Committer:
- taylorza
- Date:
- Sat Sep 20 19:09:16 2014 +0000
- Revision:
- 3:451148656b76
- Parent:
- 1:33ff5fad4320
- Child:
- 4:88d22437119c
Added support for setting the screen orientation and determining the screen width and height based on the orientation.
Who changed what in which revision?
User | Revision | Line number | New 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 | 3:451148656b76 | 13 | /** Orientation of the display */ |
taylorza | 3:451148656b76 | 14 | enum Orientation |
taylorza | 3:451148656b76 | 15 | { |
taylorza | 3:451148656b76 | 16 | /** No rotation of the display image*/ |
taylorza | 3:451148656b76 | 17 | Rotate0, |
taylorza | 3:451148656b76 | 18 | /** Rotate the display image 90 degrees */ |
taylorza | 3:451148656b76 | 19 | Rotate90, |
taylorza | 3:451148656b76 | 20 | /** Rotate the display image 180 degrees */ |
taylorza | 3:451148656b76 | 21 | Rotate180, |
taylorza | 3:451148656b76 | 22 | /** Rotate the display image 270 degrees */ |
taylorza | 3:451148656b76 | 23 | Rotate270 |
taylorza | 3:451148656b76 | 24 | }; |
taylorza | 3:451148656b76 | 25 | |
taylorza | 3:451148656b76 | 26 | public: |
taylorza | 0:7b3fb3085867 | 27 | /**Creates an instance of the LCD_ST7735 driver |
taylorza | 0:7b3fb3085867 | 28 | * @param backlightPin pin used to control the backlight |
taylorza | 0:7b3fb3085867 | 29 | * @param resetPin pin used to reset the display controller |
taylorza | 0:7b3fb3085867 | 30 | * @param dsPin pin used to put the display controller into data mode |
taylorza | 0:7b3fb3085867 | 31 | * @param mosiPin SPI channel MOSI pin |
taylorza | 0:7b3fb3085867 | 32 | * @param misoPin SPI channel MISO pin |
taylorza | 0:7b3fb3085867 | 33 | * @param clkPin SPI channel clock pin |
taylorza | 0:7b3fb3085867 | 34 | * @param csPin SPI chip select pin |
taylorza | 0:7b3fb3085867 | 35 | */ |
taylorza | 0:7b3fb3085867 | 36 | LCD_ST7735( |
taylorza | 0:7b3fb3085867 | 37 | PinName backlightPin, |
taylorza | 0:7b3fb3085867 | 38 | PinName resetPin, |
taylorza | 0:7b3fb3085867 | 39 | PinName dsPin, |
taylorza | 0:7b3fb3085867 | 40 | PinName mosiPin, |
taylorza | 0:7b3fb3085867 | 41 | PinName misoPin, |
taylorza | 0:7b3fb3085867 | 42 | PinName clkPin, |
taylorza | 0:7b3fb3085867 | 43 | PinName csPin |
taylorza | 0:7b3fb3085867 | 44 | ); |
taylorza | 3:451148656b76 | 45 | |
taylorza | 3:451148656b76 | 46 | /** Set the orientation of the display |
taylorza | 3:451148656b76 | 47 | * @param orientation Orientation of the display. |
taylorza | 3:451148656b76 | 48 | * @param flip Flips the display direction |
taylorza | 3:451148656b76 | 49 | */ |
taylorza | 3:451148656b76 | 50 | void setOrientation(Orientation orientation, bool flip); |
taylorza | 3:451148656b76 | 51 | |
taylorza | 3:451148656b76 | 52 | /** Get the width of the display given the current orientation */ |
taylorza | 3:451148656b76 | 53 | int getWidth(); |
taylorza | 3:451148656b76 | 54 | |
taylorza | 3:451148656b76 | 55 | /** Get the height of the display given the current orientation */ |
taylorza | 3:451148656b76 | 56 | int getHeight(); |
taylorza | 0:7b3fb3085867 | 57 | |
taylorza | 0:7b3fb3085867 | 58 | /** Control the display's backlight |
taylorza | 0:7b3fb3085867 | 59 | * @param state true to turn the backlight on, false to turn it off |
taylorza | 0:7b3fb3085867 | 60 | */ |
taylorza | 0:7b3fb3085867 | 61 | void setBacklight(bool state); |
taylorza | 0:7b3fb3085867 | 62 | |
taylorza | 0:7b3fb3085867 | 63 | /** Clear the screen |
taylorza | 0:7b3fb3085867 | 64 | * @param color The color used to clear the screen. Defaults to black if not passed. |
taylorza | 0:7b3fb3085867 | 65 | */ |
taylorza | 0:7b3fb3085867 | 66 | void clearScreen(uint16_t color = 0x0000); |
taylorza | 0:7b3fb3085867 | 67 | |
taylorza | 0:7b3fb3085867 | 68 | /** Set a pixel on the display to the specified color |
taylorza | 0:7b3fb3085867 | 69 | * @param x The X coordinate of the pixel (0..127) |
taylorza | 0:7b3fb3085867 | 70 | * @param y The Y coordinate of the pixel (0..159) |
taylorza | 0:7b3fb3085867 | 71 | * @param color Color to set the pixel to. |
taylorza | 0:7b3fb3085867 | 72 | */ |
taylorza | 0:7b3fb3085867 | 73 | void setPixel(int x, int y, uint16_t color); |
taylorza | 0:7b3fb3085867 | 74 | |
taylorza | 0:7b3fb3085867 | 75 | /** Draw a line on the display |
taylorza | 0:7b3fb3085867 | 76 | * @param x1 The X coordinate of the starting point on the line |
taylorza | 0:7b3fb3085867 | 77 | * @param y1 The Y coordinate of the starting point on the line |
taylorza | 0:7b3fb3085867 | 78 | * @param x2 The X coordinate of the end point on the line |
taylorza | 0:7b3fb3085867 | 79 | * @param y2 The Y coordinate of the end point on the line |
taylorza | 0:7b3fb3085867 | 80 | * @param color The color used to draw the pixel |
taylorza | 0:7b3fb3085867 | 81 | */ |
taylorza | 0:7b3fb3085867 | 82 | void drawLine(int x1, int y1, int x2, int y2, uint16_t color); |
taylorza | 0:7b3fb3085867 | 83 | |
taylorza | 0:7b3fb3085867 | 84 | /** Draw a 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 color The color used to draw the rectangle |
taylorza | 0:7b3fb3085867 | 90 | */ |
taylorza | 0:7b3fb3085867 | 91 | void drawRect(int x1, int y1, int x2, int y2, uint16_t color); |
taylorza | 0:7b3fb3085867 | 92 | |
taylorza | 0:7b3fb3085867 | 93 | /** Draw a circle on the display |
taylorza | 0:7b3fb3085867 | 94 | * @param x The X coordinate of the center of the circle |
taylorza | 0:7b3fb3085867 | 95 | * @param y The Y coordinate of the center of the circle |
taylorza | 0:7b3fb3085867 | 96 | * @param r The radius of the circle |
taylorza | 0:7b3fb3085867 | 97 | * @param color The color used to draw the circle |
taylorza | 0:7b3fb3085867 | 98 | */ |
taylorza | 0:7b3fb3085867 | 99 | void drawCircle(int x, int y, int r, uint16_t color); |
taylorza | 0:7b3fb3085867 | 100 | |
taylorza | 1:33ff5fad4320 | 101 | /** Draw an ellipse on the display |
taylorza | 1:33ff5fad4320 | 102 | * @param x The X coordinate of the center of the ellipse |
taylorza | 1:33ff5fad4320 | 103 | * @param y The Y coordinate of the center of the ellipse |
taylorza | 1:33ff5fad4320 | 104 | * @param rx The X radius of the ellipse |
taylorza | 1:33ff5fad4320 | 105 | * @param ry The X radius of the ellipse |
taylorza | 1:33ff5fad4320 | 106 | * @param color The color used to draw the ellipse |
taylorza | 1:33ff5fad4320 | 107 | */ |
taylorza | 1:33ff5fad4320 | 108 | void drawEllipse(int x, int y, int rx, int ry, uint16_t color); |
taylorza | 1:33ff5fad4320 | 109 | |
taylorza | 0:7b3fb3085867 | 110 | /** Draw a filled rectangle on the display |
taylorza | 0:7b3fb3085867 | 111 | * @param x1 The X coordinate of the upper left corner |
taylorza | 0:7b3fb3085867 | 112 | * @param y1 The Y coordinate of the upper left corner |
taylorza | 0:7b3fb3085867 | 113 | * @param x2 The X coordinate of the lower right corner |
taylorza | 0:7b3fb3085867 | 114 | * @param y2 The Y coordinate of the lower right corner |
taylorza | 0:7b3fb3085867 | 115 | * @param borderColor The color used to draw the rectangle frame |
taylorza | 0:7b3fb3085867 | 116 | * @param fillColor The color used to fill the rectangle |
taylorza | 0:7b3fb3085867 | 117 | */ |
taylorza | 0:7b3fb3085867 | 118 | void fillRect(int x1, int y1, int x2, int y2, uint16_t borderColor, uint16_t fillColor); |
taylorza | 0:7b3fb3085867 | 119 | |
taylorza | 0:7b3fb3085867 | 120 | /** Draw a filled circle on the display |
taylorza | 0:7b3fb3085867 | 121 | * @param x The X coordinate of the center of the circle |
taylorza | 0:7b3fb3085867 | 122 | * @param y The Y coordinate of the center of the circle |
taylorza | 0:7b3fb3085867 | 123 | * @param borderColor The color used to draw the circumference of the circle |
taylorza | 0:7b3fb3085867 | 124 | * @param fillColor The color used to fill the circle |
taylorza | 0:7b3fb3085867 | 125 | */ |
taylorza | 0:7b3fb3085867 | 126 | void fillCircle(int x, int y, int r, uint16_t borderColor, uint16_t fillColor); |
taylorza | 0:7b3fb3085867 | 127 | |
taylorza | 1:33ff5fad4320 | 128 | /** Draw a filled ellipse on the display |
taylorza | 1:33ff5fad4320 | 129 | * @param x The X coordinate of the center of the ellipse |
taylorza | 1:33ff5fad4320 | 130 | * @param y The Y coordinate of the center of the ellipse |
taylorza | 1:33ff5fad4320 | 131 | * @param rx The X radius of the ellipse |
taylorza | 1:33ff5fad4320 | 132 | * @param ry The X radius of the ellipse |
taylorza | 1:33ff5fad4320 | 133 | * @param borderColor The color used to draw the circumference of the circle |
taylorza | 1:33ff5fad4320 | 134 | * @param fillColor The color used to fill the circle |
taylorza | 1:33ff5fad4320 | 135 | */ |
taylorza | 1:33ff5fad4320 | 136 | void fillEllipse(int x, int y, int rx, int ry, uint16_t borderColor, uint16_t fillColor); |
taylorza | 1:33ff5fad4320 | 137 | |
taylorza | 0:7b3fb3085867 | 138 | /** Draw a bitmap on the screen |
taylorza | 0:7b3fb3085867 | 139 | * @param x The X coordinate location to draw the bitmap. |
taylorza | 0:7b3fb3085867 | 140 | * @param y The Y coordinate location to draw the bitmap. |
taylorza | 0:7b3fb3085867 | 141 | * @param pbmp Pointer to the bitmap. |
taylorza | 0:7b3fb3085867 | 142 | * @note The bitmap is an single dimensional uint8_t (unsigned short) array. |
taylorza | 0:7b3fb3085867 | 143 | * The first to elements of the array indicate the width and height of the bitmap repectively. |
taylorza | 0:7b3fb3085867 | 144 | * The rest of the entries int the array make up the pixel data for the array. |
taylorza | 0:7b3fb3085867 | 145 | */ |
taylorza | 0:7b3fb3085867 | 146 | void drawBitmap(int x, int y, const uint16_t *pbmp); |
taylorza | 0:7b3fb3085867 | 147 | |
taylorza | 1:33ff5fad4320 | 148 | /** Extracts a portion of a bitmap and draws it on the screen |
taylorza | 1:33ff5fad4320 | 149 | * @param x The X coordinate location to draw the bitmap. |
taylorza | 1:33ff5fad4320 | 150 | * @param y The Y coordinate location to draw the bitmap. |
taylorza | 1:33ff5fad4320 | 151 | * @param pbmp Pointer to the bitmap. |
taylorza | 1:33ff5fad4320 | 152 | * @param srcX X offset into the source bitmap of the portion to extract |
taylorza | 1:33ff5fad4320 | 153 | * @param srcY Y offset into the source bitmap of the portion to extract |
taylorza | 1:33ff5fad4320 | 154 | * @param srcWidth Width of the bitmap portion to draw |
taylorza | 1:33ff5fad4320 | 155 | * @param srcHeight Height of the bitmap portion to draw |
taylorza | 1:33ff5fad4320 | 156 | * @note The bitmap is an single dimensional uint8_t (unsigned short) array. |
taylorza | 1:33ff5fad4320 | 157 | * The first to elements of the array indicate the width and height of the bitmap repectively. |
taylorza | 1:33ff5fad4320 | 158 | * The rest of the entries int the array make up the pixel data for the array. |
taylorza | 1:33ff5fad4320 | 159 | */ |
taylorza | 1:33ff5fad4320 | 160 | void drawBitmap(int x, int y, const uint16_t *pbmp, int srcX, int srcY, int srcWidth, int srcHeight); |
taylorza | 1:33ff5fad4320 | 161 | |
taylorza | 0:7b3fb3085867 | 162 | /** Set the foreground color used to render text |
taylorza | 0:7b3fb3085867 | 163 | * @param color Color used when drawing text to the display |
taylorza | 0:7b3fb3085867 | 164 | * @note The color can be changed multiple times to render text in various colors on the display |
taylorza | 0:7b3fb3085867 | 165 | */ |
taylorza | 0:7b3fb3085867 | 166 | void setForegroundColor(uint16_t color); |
taylorza | 0:7b3fb3085867 | 167 | |
taylorza | 0:7b3fb3085867 | 168 | /** Set the background color used to render text |
taylorza | 0:7b3fb3085867 | 169 | * @param color Color used when drawing background portions of the text |
taylorza | 0:7b3fb3085867 | 170 | * @note The color can be changed multiple times to render text with various background colors on the display |
taylorza | 0:7b3fb3085867 | 171 | */ |
taylorza | 0:7b3fb3085867 | 172 | void setBackgroundColor(uint16_t color); |
taylorza | 0:7b3fb3085867 | 173 | |
taylorza | 0:7b3fb3085867 | 174 | /** Draw a string to the screen using the currently active foreground and background colors |
taylorza | 0:7b3fb3085867 | 175 | * @param pFont Pointer to the font used to render the string to the display |
taylorza | 0:7b3fb3085867 | 176 | * @param x The X coordinate location to draw the string. |
taylorza | 0:7b3fb3085867 | 177 | * @param y The Y coordinate location to draw the string. |
taylorza | 0:7b3fb3085867 | 178 | * @param pString ASCIIZ string to draw to the display. |
taylorza | 0:7b3fb3085867 | 179 | * @note The font is currently limited to an 8x8 font. See the font_IBM.h file for an example font. |
taylorza | 0:7b3fb3085867 | 180 | */ |
taylorza | 3:451148656b76 | 181 | void drawString(const uint8_t *pFont, int x, int y, const char *pString); |
taylorza | 0:7b3fb3085867 | 182 | |
taylorza | 0:7b3fb3085867 | 183 | private: |
taylorza | 0:7b3fb3085867 | 184 | void drawVertLine(int x1, int y1, int y2, uint16_t color); |
taylorza | 0:7b3fb3085867 | 185 | void drawHorizLine(int x1, int y1, int x2, uint16_t color); |
taylorza | 0:7b3fb3085867 | 186 | void drawChar(const uint8_t *pFont, int x, int y, char c); |
taylorza | 0:7b3fb3085867 | 187 | |
taylorza | 0:7b3fb3085867 | 188 | private: |
taylorza | 0:7b3fb3085867 | 189 | void swap(int &a, int &b); |
taylorza | 0:7b3fb3085867 | 190 | |
taylorza | 0:7b3fb3085867 | 191 | private: |
taylorza | 0:7b3fb3085867 | 192 | void initDisplay(); |
taylorza | 0:7b3fb3085867 | 193 | void reset(); |
taylorza | 0:7b3fb3085867 | 194 | void write(uint8_t cmd); |
taylorza | 0:7b3fb3085867 | 195 | void write(uint8_t cmd, uint8_t data[], int dataLen); |
taylorza | 0:7b3fb3085867 | 196 | void write(uint8_t cmd, uint16_t data); |
taylorza | 0:7b3fb3085867 | 197 | void writeData(uint8_t data); |
taylorza | 0:7b3fb3085867 | 198 | void beginBatchCommand(uint8_t cmd); |
taylorza | 0:7b3fb3085867 | 199 | void writeBatchData(uint8_t data); |
taylorza | 0:7b3fb3085867 | 200 | void writeBatchData(uint16_t data); |
taylorza | 0:7b3fb3085867 | 201 | void endBatchCommand(); |
taylorza | 0:7b3fb3085867 | 202 | |
taylorza | 0:7b3fb3085867 | 203 | void clip(int x, int y, int w, int h); |
taylorza | 0:7b3fb3085867 | 204 | void clipRect(int x1, int y1, int x2, int y2); |
taylorza | 0:7b3fb3085867 | 205 | |
taylorza | 0:7b3fb3085867 | 206 | private: |
taylorza | 3:451148656b76 | 207 | int _width; |
taylorza | 3:451148656b76 | 208 | int _height; |
taylorza | 3:451148656b76 | 209 | Orientation _orientation; |
taylorza | 3:451148656b76 | 210 | bool _flip; |
taylorza | 0:7b3fb3085867 | 211 | uint16_t _foregroundColor; |
taylorza | 0:7b3fb3085867 | 212 | uint16_t _backgroundColor; |
taylorza | 0:7b3fb3085867 | 213 | |
taylorza | 0:7b3fb3085867 | 214 | private: |
taylorza | 0:7b3fb3085867 | 215 | DigitalOut _backlight; |
taylorza | 0:7b3fb3085867 | 216 | DigitalOut _reset; |
taylorza | 0:7b3fb3085867 | 217 | DigitalOut _ds; |
taylorza | 0:7b3fb3085867 | 218 | DigitalOut _cs; |
taylorza | 0:7b3fb3085867 | 219 | SPI _spi; |
taylorza | 0:7b3fb3085867 | 220 | |
taylorza | 0:7b3fb3085867 | 221 | private: |
taylorza | 0:7b3fb3085867 | 222 | static const uint8_t CMD_SLPOUT = 0x11; |
taylorza | 0:7b3fb3085867 | 223 | static const uint8_t CMD_DISPON = 0x29; |
taylorza | 0:7b3fb3085867 | 224 | static const uint8_t CMD_CASET = 0x2a; |
taylorza | 0:7b3fb3085867 | 225 | static const uint8_t CMD_RASET = 0x2b; |
taylorza | 0:7b3fb3085867 | 226 | static const uint8_t CMD_RAMWR = 0x2c; |
taylorza | 0:7b3fb3085867 | 227 | |
taylorza | 0:7b3fb3085867 | 228 | static const uint8_t CMD_MADCTL = 0x36; |
taylorza | 0:7b3fb3085867 | 229 | static const uint8_t CMD_COLMOD = 0x3a; |
taylorza | 0:7b3fb3085867 | 230 | |
taylorza | 0:7b3fb3085867 | 231 | static const uint8_t CMD_FRMCTR1 = 0xb1; |
taylorza | 0:7b3fb3085867 | 232 | static const uint8_t CMD_FRMCTR2 = 0xb2; |
taylorza | 0:7b3fb3085867 | 233 | static const uint8_t CMD_FRMCTR3 = 0xb3; |
taylorza | 0:7b3fb3085867 | 234 | static const uint8_t CMD_INVCTR = 0xb4; |
taylorza | 0:7b3fb3085867 | 235 | |
taylorza | 0:7b3fb3085867 | 236 | static const uint8_t CMD_PWCTR1 = 0xc0; |
taylorza | 0:7b3fb3085867 | 237 | static const uint8_t CMD_PWCTR2 = 0xc1; |
taylorza | 0:7b3fb3085867 | 238 | static const uint8_t CMD_PWCTR3 = 0xc2; |
taylorza | 0:7b3fb3085867 | 239 | static const uint8_t CMD_PWCTR4 = 0xc3; |
taylorza | 0:7b3fb3085867 | 240 | static const uint8_t CMD_PWCTR5 = 0xc4; |
taylorza | 0:7b3fb3085867 | 241 | static const uint8_t CMD_VMCTR1 = 0xc5; |
taylorza | 0:7b3fb3085867 | 242 | |
taylorza | 0:7b3fb3085867 | 243 | static const uint8_t CMD_GAMCTRP1 = 0xe0; |
taylorza | 0:7b3fb3085867 | 244 | static const uint8_t CMD_GAMCTRN1 = 0xe1; |
taylorza | 0:7b3fb3085867 | 245 | |
taylorza | 0:7b3fb3085867 | 246 | static const uint8_t CMD_EXTCTRL = 0xf0; |
taylorza | 0:7b3fb3085867 | 247 | }; |
taylorza | 0:7b3fb3085867 | 248 | |
taylorza | 0:7b3fb3085867 | 249 | #endif // __LCD_ST7735__ |