Added new display module GREENTAB2 (3.3v compact blue board, 8 pin no SD card)
Adafruit_ST7735.cpp@7:856a3443f68d, 2020-04-20 (annotated)
- Committer:
- peekpt
- Date:
- Mon Apr 20 18:17:52 2020 +0000
- Revision:
- 7:856a3443f68d
- Parent:
- 6:c964b41674fc
+ GREENTAB2 new display module (no SD)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
SomeRandomBloke | 0:99ee879ef5a9 | 1 | /*************************************************** |
SomeRandomBloke | 0:99ee879ef5a9 | 2 | This is a library for the Adafruit 1.8" SPI display. |
SomeRandomBloke | 0:99ee879ef5a9 | 3 | This library works with the Adafruit 1.8" TFT Breakout w/SD card |
SomeRandomBloke | 0:99ee879ef5a9 | 4 | ----> http://www.adafruit.com/products/358 |
SomeRandomBloke | 0:99ee879ef5a9 | 5 | as well as Adafruit raw 1.8" TFT display |
SomeRandomBloke | 0:99ee879ef5a9 | 6 | ----> http://www.adafruit.com/products/618 |
SomeRandomBloke | 0:99ee879ef5a9 | 7 | |
SomeRandomBloke | 0:99ee879ef5a9 | 8 | Check out the links above for our tutorials and wiring diagrams |
SomeRandomBloke | 0:99ee879ef5a9 | 9 | These displays use SPI to communicate, 4 or 5 pins are required to |
SomeRandomBloke | 0:99ee879ef5a9 | 10 | interface (RST is optional) |
SomeRandomBloke | 0:99ee879ef5a9 | 11 | Adafruit invests time and resources providing this open source code, |
SomeRandomBloke | 0:99ee879ef5a9 | 12 | please support Adafruit and open-source hardware by purchasing |
SomeRandomBloke | 0:99ee879ef5a9 | 13 | products from Adafruit! |
SomeRandomBloke | 0:99ee879ef5a9 | 14 | |
SomeRandomBloke | 0:99ee879ef5a9 | 15 | Written by Limor Fried/Ladyada for Adafruit Industries. |
SomeRandomBloke | 0:99ee879ef5a9 | 16 | MIT license, all text above must be included in any redistribution |
SomeRandomBloke | 0:99ee879ef5a9 | 17 | ****************************************************/ |
SomeRandomBloke | 0:99ee879ef5a9 | 18 | |
peekpt | 7:856a3443f68d | 19 | // + GREENTAB2 display |
peekpt | 7:856a3443f68d | 20 | |
SomeRandomBloke | 0:99ee879ef5a9 | 21 | #include "Adafruit_ST7735.h" |
SomeRandomBloke | 0:99ee879ef5a9 | 22 | |
peekpt | 7:856a3443f68d | 23 | #include "mbed.h" |
peekpt | 7:856a3443f68d | 24 | |
peekpt | 7:856a3443f68d | 25 | inline uint16_t swapcolor(uint16_t x) { |
SomeRandomBloke | 4:2123ea52a4d9 | 26 | return (x << 11) | (x & 0x07E0) | (x >> 11); |
SomeRandomBloke | 4:2123ea52a4d9 | 27 | } |
SomeRandomBloke | 4:2123ea52a4d9 | 28 | |
peekpt | 7:856a3443f68d | 29 | // Constructor |
peekpt | 7:856a3443f68d | 30 | Adafruit_ST7735::Adafruit_ST7735(PinName mosi, PinName miso, PinName sck, |
peekpt | 7:856a3443f68d | 31 | PinName cs, PinName rs, PinName rst) |
peekpt | 7:856a3443f68d | 32 | : lcdPort(mosi, miso, sck), _cs(cs), _rs(rs), _rst(rst), |
peekpt | 7:856a3443f68d | 33 | Adafruit_GFX(ST7735_TFTWIDTH, ST7735_TFTHEIGHT) {} |
SomeRandomBloke | 0:99ee879ef5a9 | 34 | |
peekpt | 7:856a3443f68d | 35 | void Adafruit_ST7735::writecommand(uint8_t c) { |
peekpt | 7:856a3443f68d | 36 | _rs = 0; |
peekpt | 7:856a3443f68d | 37 | _cs = 0; |
peekpt | 7:856a3443f68d | 38 | |
peekpt | 7:856a3443f68d | 39 | lcdPort.write(c); |
peekpt | 7:856a3443f68d | 40 | _cs = 1; |
SomeRandomBloke | 0:99ee879ef5a9 | 41 | } |
SomeRandomBloke | 0:99ee879ef5a9 | 42 | |
peekpt | 7:856a3443f68d | 43 | void Adafruit_ST7735::writedata(uint8_t c) { |
peekpt | 7:856a3443f68d | 44 | _rs = 1; |
peekpt | 7:856a3443f68d | 45 | _cs = 0; |
peekpt | 7:856a3443f68d | 46 | lcdPort.write(c); |
SomeRandomBloke | 0:99ee879ef5a9 | 47 | |
peekpt | 7:856a3443f68d | 48 | _cs = 1; |
SomeRandomBloke | 0:99ee879ef5a9 | 49 | } |
SomeRandomBloke | 0:99ee879ef5a9 | 50 | |
SomeRandomBloke | 0:99ee879ef5a9 | 51 | // Rather than a bazillion writecommand() and writedata() calls, screen |
SomeRandomBloke | 0:99ee879ef5a9 | 52 | // initialization commands and arguments are organized in these tables |
SomeRandomBloke | 0:99ee879ef5a9 | 53 | // stored in PROGMEM. The table may look bulky, but that's mostly the |
SomeRandomBloke | 0:99ee879ef5a9 | 54 | // formatting -- storage-wise this is hundreds of bytes more compact |
SomeRandomBloke | 0:99ee879ef5a9 | 55 | // than the equivalent code. Companion function follows. |
SomeRandomBloke | 0:99ee879ef5a9 | 56 | #define DELAY 0x80 |
peekpt | 7:856a3443f68d | 57 | |
peekpt | 7:856a3443f68d | 58 | static unsigned char Bcmd[] = |
peekpt | 7:856a3443f68d | 59 | { // Initialization commands for 7735B screens |
peekpt | 7:856a3443f68d | 60 | 18, // 18 commands in list: |
peekpt | 7:856a3443f68d | 61 | ST7735_SWRESET, DELAY, // 1: Software reset, no args, w/delay |
peekpt | 7:856a3443f68d | 62 | 50, // 50 ms delay |
peekpt | 7:856a3443f68d | 63 | ST7735_SLPOUT, DELAY, // 2: Out of sleep mode, no args, w/delay |
peekpt | 7:856a3443f68d | 64 | 255, // 255 = 500 ms delay |
peekpt | 7:856a3443f68d | 65 | ST7735_COLMOD, 1 + DELAY, // 3: Set color mode, 1 arg + delay: |
peekpt | 7:856a3443f68d | 66 | 0x05, // 16-bit color |
peekpt | 7:856a3443f68d | 67 | 10, // 10 ms delay |
peekpt | 7:856a3443f68d | 68 | ST7735_FRMCTR1, 3 + DELAY, // 4: Frame rate control, 3 args + delay: |
peekpt | 7:856a3443f68d | 69 | 0x00, // fastest refresh |
peekpt | 7:856a3443f68d | 70 | 0x06, // 6 lines front porch |
peekpt | 7:856a3443f68d | 71 | 0x03, // 3 lines back porch |
peekpt | 7:856a3443f68d | 72 | 10, // 10 ms delay |
peekpt | 7:856a3443f68d | 73 | ST7735_MADCTL, 1, // 5: Memory access ctrl (directions), 1 arg: |
peekpt | 7:856a3443f68d | 74 | 0x08, // Row addr/col addr, bottom to top refresh |
peekpt | 7:856a3443f68d | 75 | ST7735_DISSET5, 2, // 6: Display settings #5, 2 args, no delay: |
peekpt | 7:856a3443f68d | 76 | 0x15, // 1 clk cycle nonoverlap, 2 cycle gate |
peekpt | 7:856a3443f68d | 77 | // rise, 3 cycle osc equalize |
peekpt | 7:856a3443f68d | 78 | 0x02, // Fix on VTL |
peekpt | 7:856a3443f68d | 79 | ST7735_INVCTR, 1, // 7: Display inversion control, 1 arg: |
peekpt | 7:856a3443f68d | 80 | 0x0, // Line inversion |
peekpt | 7:856a3443f68d | 81 | ST7735_PWCTR1, 2 + DELAY, // 8: Power control, 2 args + delay: |
peekpt | 7:856a3443f68d | 82 | 0x02, // GVDD = 4.7V |
peekpt | 7:856a3443f68d | 83 | 0x70, // 1.0uA |
peekpt | 7:856a3443f68d | 84 | 10, // 10 ms delay |
peekpt | 7:856a3443f68d | 85 | ST7735_PWCTR2, 1, // 9: Power control, 1 arg, no delay: |
peekpt | 7:856a3443f68d | 86 | 0x05, // VGH = 14.7V, VGL = -7.35V |
peekpt | 7:856a3443f68d | 87 | ST7735_PWCTR3, 2, // 10: Power control, 2 args, no delay: |
peekpt | 7:856a3443f68d | 88 | 0x01, // Opamp current small |
peekpt | 7:856a3443f68d | 89 | 0x02, // Boost frequency |
peekpt | 7:856a3443f68d | 90 | ST7735_VMCTR1, 2 + DELAY, // 11: Power control, 2 args + delay: |
peekpt | 7:856a3443f68d | 91 | 0x3C, // VCOMH = 4V |
peekpt | 7:856a3443f68d | 92 | 0x38, // VCOML = -1.1V |
peekpt | 7:856a3443f68d | 93 | 10, // 10 ms delay |
peekpt | 7:856a3443f68d | 94 | ST7735_PWCTR6, 2, // 12: Power control, 2 args, no delay: |
peekpt | 7:856a3443f68d | 95 | 0x11, 0x15, ST7735_GMCTRP1, |
peekpt | 7:856a3443f68d | 96 | 16, // 13: Magical unicorn dust, 16 args, no delay: |
peekpt | 7:856a3443f68d | 97 | 0x09, 0x16, 0x09, 0x20, // (seriously though, not sure what |
peekpt | 7:856a3443f68d | 98 | 0x21, 0x1B, 0x13, 0x19, // these config values represent) |
peekpt | 7:856a3443f68d | 99 | 0x17, 0x15, 0x1E, 0x2B, 0x04, 0x05, 0x02, 0x0E, ST7735_GMCTRN1, |
peekpt | 7:856a3443f68d | 100 | 16 + DELAY, // 14: Sparkles and rainbows, 16 args + delay: |
peekpt | 7:856a3443f68d | 101 | 0x0B, 0x14, 0x08, 0x1E, // (ditto) |
peekpt | 7:856a3443f68d | 102 | 0x22, 0x1D, 0x18, 0x1E, 0x1B, 0x1A, 0x24, 0x2B, 0x06, 0x06, 0x02, 0x0F, |
peekpt | 7:856a3443f68d | 103 | 10, // 10 ms delay |
peekpt | 7:856a3443f68d | 104 | ST7735_CASET, 4, // 15: Column addr set, 4 args, no delay: |
peekpt | 7:856a3443f68d | 105 | 0x00, 0x02, // XSTART = 2 |
peekpt | 7:856a3443f68d | 106 | 0x00, 0x81, // XEND = 129 |
peekpt | 7:856a3443f68d | 107 | ST7735_RASET, 4, // 16: Row addr set, 4 args, no delay: |
peekpt | 7:856a3443f68d | 108 | 0x00, 0x02, // XSTART = 1 |
peekpt | 7:856a3443f68d | 109 | 0x00, 0x81, // XEND = 160 |
peekpt | 7:856a3443f68d | 110 | ST7735_NORON, DELAY, // 17: Normal display on, no args, w/delay |
peekpt | 7:856a3443f68d | 111 | 10, // 10 ms delay |
peekpt | 7:856a3443f68d | 112 | ST7735_DISPON, DELAY, // 18: Main screen turn on, no args, w/delay |
peekpt | 7:856a3443f68d | 113 | 255}, // 255 = 500 ms delay |
SomeRandomBloke | 3:5cd5edbf5b72 | 114 | |
peekpt | 7:856a3443f68d | 115 | Rcmd1[] = |
peekpt | 7:856a3443f68d | 116 | { // Init for 7735R, part 1 (red or green tab) |
peekpt | 7:856a3443f68d | 117 | 15, // 15 commands in list: |
peekpt | 7:856a3443f68d | 118 | ST7735_SWRESET, |
peekpt | 7:856a3443f68d | 119 | DELAY, // 1: Software reset, 0 args, w/delay |
peekpt | 7:856a3443f68d | 120 | 150, // 150 ms delay |
peekpt | 7:856a3443f68d | 121 | ST7735_SLPOUT, |
peekpt | 7:856a3443f68d | 122 | DELAY, // 2: Out of sleep mode, 0 args, w/delay |
peekpt | 7:856a3443f68d | 123 | 255, // 500 ms delay |
peekpt | 7:856a3443f68d | 124 | ST7735_FRMCTR1, |
peekpt | 7:856a3443f68d | 125 | 3, // 3: Frame rate ctrl - normal mode, 3 args: |
peekpt | 7:856a3443f68d | 126 | 0x01, |
peekpt | 7:856a3443f68d | 127 | 0x2C, |
peekpt | 7:856a3443f68d | 128 | 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D) |
peekpt | 7:856a3443f68d | 129 | ST7735_FRMCTR2, |
peekpt | 7:856a3443f68d | 130 | 3, // 4: Frame rate control - idle mode, 3 args: |
peekpt | 7:856a3443f68d | 131 | 0x01, |
peekpt | 7:856a3443f68d | 132 | 0x2C, |
peekpt | 7:856a3443f68d | 133 | 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D) |
peekpt | 7:856a3443f68d | 134 | ST7735_FRMCTR3, |
peekpt | 7:856a3443f68d | 135 | 6, // 5: Frame rate ctrl - partial mode, 6 args: |
peekpt | 7:856a3443f68d | 136 | 0x01, |
peekpt | 7:856a3443f68d | 137 | 0x2C, |
peekpt | 7:856a3443f68d | 138 | 0x2D, // Dot inversion mode |
peekpt | 7:856a3443f68d | 139 | 0x01, |
peekpt | 7:856a3443f68d | 140 | 0x2C, |
peekpt | 7:856a3443f68d | 141 | 0x2D, // Line inversion mode |
peekpt | 7:856a3443f68d | 142 | ST7735_INVCTR, |
peekpt | 7:856a3443f68d | 143 | 1, // 6: Display inversion ctrl, 1 arg, no delay: |
peekpt | 7:856a3443f68d | 144 | 0x07, // No inversion |
peekpt | 7:856a3443f68d | 145 | ST7735_PWCTR1, |
peekpt | 7:856a3443f68d | 146 | 3, // 7: Power control, 3 args, no delay: |
peekpt | 7:856a3443f68d | 147 | 0xA2, |
peekpt | 7:856a3443f68d | 148 | 0x02, // -4.6V |
peekpt | 7:856a3443f68d | 149 | 0x84, // AUTO mode |
peekpt | 7:856a3443f68d | 150 | ST7735_PWCTR2, |
peekpt | 7:856a3443f68d | 151 | 1, // 8: Power control, 1 arg, no delay: |
peekpt | 7:856a3443f68d | 152 | 0xC5, // VGH25 = 2.4C VGSEL = -10 VGH = 3 * AVDD |
peekpt | 7:856a3443f68d | 153 | ST7735_PWCTR3, |
peekpt | 7:856a3443f68d | 154 | 2, // 9: Power control, 2 args, no delay: |
peekpt | 7:856a3443f68d | 155 | 0x0A, // Opamp current small |
peekpt | 7:856a3443f68d | 156 | 0x00, // Boost frequency |
peekpt | 7:856a3443f68d | 157 | ST7735_PWCTR4, |
peekpt | 7:856a3443f68d | 158 | 2, // 10: Power control, 2 args, no delay: |
peekpt | 7:856a3443f68d | 159 | 0x8A, // BCLK/2, Opamp current small & Medium low |
peekpt | 7:856a3443f68d | 160 | 0x2A, |
peekpt | 7:856a3443f68d | 161 | ST7735_PWCTR5, |
peekpt | 7:856a3443f68d | 162 | 2, // 11: Power control, 2 args, no delay: |
peekpt | 7:856a3443f68d | 163 | 0x8A, |
peekpt | 7:856a3443f68d | 164 | 0xEE, |
peekpt | 7:856a3443f68d | 165 | ST7735_VMCTR1, |
peekpt | 7:856a3443f68d | 166 | 1, // 12: Power control, 1 arg, no delay: |
peekpt | 7:856a3443f68d | 167 | 0x0E, |
peekpt | 7:856a3443f68d | 168 | ST7735_INVOFF, |
peekpt | 7:856a3443f68d | 169 | 0, // 13: Don't invert display, no args, no delay |
peekpt | 7:856a3443f68d | 170 | ST7735_MADCTL, |
peekpt | 7:856a3443f68d | 171 | 1, // 14: Memory access control (directions), 1 arg: |
peekpt | 7:856a3443f68d | 172 | 0xC8, // row addr/col addr, bottom to top refresh |
peekpt | 7:856a3443f68d | 173 | ST7735_COLMOD, |
peekpt | 7:856a3443f68d | 174 | 1, // 15: set color mode, 1 arg, no delay: |
peekpt | 7:856a3443f68d | 175 | 0x05}, // 16-bit color |
SomeRandomBloke | 3:5cd5edbf5b72 | 176 | |
peekpt | 7:856a3443f68d | 177 | Rcmd2green[] = |
peekpt | 7:856a3443f68d | 178 | { // Init for 7735R, part 2 (green tab only) |
peekpt | 7:856a3443f68d | 179 | 2, // 2 commands in list: |
peekpt | 7:856a3443f68d | 180 | ST7735_CASET, |
peekpt | 7:856a3443f68d | 181 | 4, // 1: Column addr set, 4 args, no delay: |
peekpt | 7:856a3443f68d | 182 | 0x00, |
peekpt | 7:856a3443f68d | 183 | 0x02, // XSTART = 0 |
peekpt | 7:856a3443f68d | 184 | 0x00, |
peekpt | 7:856a3443f68d | 185 | 0x7F + 0x02, // XEND = 127 |
peekpt | 7:856a3443f68d | 186 | ST7735_RASET, |
peekpt | 7:856a3443f68d | 187 | 4, // 2: Row addr set, 4 args, no delay: |
peekpt | 7:856a3443f68d | 188 | 0x00, |
peekpt | 7:856a3443f68d | 189 | 0x01, // XSTART = 0 |
peekpt | 7:856a3443f68d | 190 | 0x00, |
peekpt | 7:856a3443f68d | 191 | 0x9F + 0x01}, // XEND = 159 |
peekpt | 7:856a3443f68d | 192 | Rcmd2red[] = |
peekpt | 7:856a3443f68d | 193 | { // Init for 7735R, part 2 (red tab only) |
peekpt | 7:856a3443f68d | 194 | 2, // 2 commands in list: |
peekpt | 7:856a3443f68d | 195 | ST7735_CASET, |
peekpt | 7:856a3443f68d | 196 | 4, // 1: Column addr set, 4 args, no delay: |
peekpt | 7:856a3443f68d | 197 | 0x00, |
peekpt | 7:856a3443f68d | 198 | 0x00, // XSTART = 0 |
peekpt | 7:856a3443f68d | 199 | 0x00, |
peekpt | 7:856a3443f68d | 200 | 0x7F, // XEND = 127 |
peekpt | 7:856a3443f68d | 201 | ST7735_RASET, |
peekpt | 7:856a3443f68d | 202 | 4, // 2: Row addr set, 4 args, no delay: |
peekpt | 7:856a3443f68d | 203 | 0x00, |
peekpt | 7:856a3443f68d | 204 | 0x00, // XSTART = 0 |
peekpt | 7:856a3443f68d | 205 | 0x00, |
peekpt | 7:856a3443f68d | 206 | 0x9F}, // XEND = 159 |
SomeRandomBloke | 3:5cd5edbf5b72 | 207 | |
peekpt | 7:856a3443f68d | 208 | Rcmd2green144[] = |
peekpt | 7:856a3443f68d | 209 | { // Init for 7735R, part 2 (green 1.44 tab) |
peekpt | 7:856a3443f68d | 210 | 2, // 2 commands in list: |
peekpt | 7:856a3443f68d | 211 | ST7735_CASET, |
peekpt | 7:856a3443f68d | 212 | 4, // 1: Column addr set, 4 args, no delay: |
peekpt | 7:856a3443f68d | 213 | 0x00, |
peekpt | 7:856a3443f68d | 214 | 0x00, // XSTART = 0 |
peekpt | 7:856a3443f68d | 215 | 0x00, |
peekpt | 7:856a3443f68d | 216 | 0x7F, // XEND = 127 |
peekpt | 7:856a3443f68d | 217 | ST7735_RASET, |
peekpt | 7:856a3443f68d | 218 | 4, // 2: Row addr set, 4 args, no delay: |
peekpt | 7:856a3443f68d | 219 | 0x00, |
peekpt | 7:856a3443f68d | 220 | 0x00, // XSTART = 0 |
peekpt | 7:856a3443f68d | 221 | 0x00, |
peekpt | 7:856a3443f68d | 222 | 0x7F}, // XEND = 127 |
SomeRandomBloke | 3:5cd5edbf5b72 | 223 | |
peekpt | 7:856a3443f68d | 224 | Rcmd3[] = { // Init for 7735R, part 3 (red or green tab) |
peekpt | 7:856a3443f68d | 225 | 4, // 4 commands in list: |
peekpt | 7:856a3443f68d | 226 | ST7735_GMCTRP1, |
peekpt | 7:856a3443f68d | 227 | 16, // 1: Magical unicorn dust, 16 args, no delay: |
peekpt | 7:856a3443f68d | 228 | 0x02, |
peekpt | 7:856a3443f68d | 229 | 0x1c, |
peekpt | 7:856a3443f68d | 230 | 0x07, |
peekpt | 7:856a3443f68d | 231 | 0x12, |
peekpt | 7:856a3443f68d | 232 | 0x37, |
peekpt | 7:856a3443f68d | 233 | 0x32, |
peekpt | 7:856a3443f68d | 234 | 0x29, |
peekpt | 7:856a3443f68d | 235 | 0x2d, |
peekpt | 7:856a3443f68d | 236 | 0x29, |
peekpt | 7:856a3443f68d | 237 | 0x25, |
peekpt | 7:856a3443f68d | 238 | 0x2B, |
peekpt | 7:856a3443f68d | 239 | 0x39, |
peekpt | 7:856a3443f68d | 240 | 0x00, |
peekpt | 7:856a3443f68d | 241 | 0x01, |
peekpt | 7:856a3443f68d | 242 | 0x03, |
peekpt | 7:856a3443f68d | 243 | 0x10, |
peekpt | 7:856a3443f68d | 244 | ST7735_GMCTRN1, |
peekpt | 7:856a3443f68d | 245 | 16, // 2: Sparkles and rainbows, 16 args, no delay: |
peekpt | 7:856a3443f68d | 246 | 0x03, |
peekpt | 7:856a3443f68d | 247 | 0x1d, |
peekpt | 7:856a3443f68d | 248 | 0x07, |
peekpt | 7:856a3443f68d | 249 | 0x06, |
peekpt | 7:856a3443f68d | 250 | 0x2E, |
peekpt | 7:856a3443f68d | 251 | 0x2C, |
peekpt | 7:856a3443f68d | 252 | 0x29, |
peekpt | 7:856a3443f68d | 253 | 0x2D, |
peekpt | 7:856a3443f68d | 254 | 0x2E, |
peekpt | 7:856a3443f68d | 255 | 0x2E, |
peekpt | 7:856a3443f68d | 256 | 0x37, |
peekpt | 7:856a3443f68d | 257 | 0x3F, |
peekpt | 7:856a3443f68d | 258 | 0x00, |
peekpt | 7:856a3443f68d | 259 | 0x00, |
peekpt | 7:856a3443f68d | 260 | 0x02, |
peekpt | 7:856a3443f68d | 261 | 0x10, |
peekpt | 7:856a3443f68d | 262 | ST7735_NORON, |
peekpt | 7:856a3443f68d | 263 | DELAY, // 3: Normal display on, no args, w/delay |
peekpt | 7:856a3443f68d | 264 | 10, // 10 ms delay |
peekpt | 7:856a3443f68d | 265 | ST7735_DISPON, |
peekpt | 7:856a3443f68d | 266 | DELAY, // 4: Main screen turn on, no args w/delay |
peekpt | 7:856a3443f68d | 267 | 100}; // 100 ms delay |
SomeRandomBloke | 3:5cd5edbf5b72 | 268 | |
SomeRandomBloke | 0:99ee879ef5a9 | 269 | // Companion code to the above tables. Reads and issues |
SomeRandomBloke | 0:99ee879ef5a9 | 270 | // a series of LCD commands stored in byte array. |
peekpt | 7:856a3443f68d | 271 | void Adafruit_ST7735::commandList(uint8_t *addr) { |
peekpt | 7:856a3443f68d | 272 | uint8_t numCommands, numArgs; |
peekpt | 7:856a3443f68d | 273 | uint16_t ms; |
SomeRandomBloke | 0:99ee879ef5a9 | 274 | |
peekpt | 7:856a3443f68d | 275 | numCommands = *addr++; // Number of commands to follow |
peekpt | 7:856a3443f68d | 276 | while (numCommands--) { // For each command... |
peekpt | 7:856a3443f68d | 277 | writecommand(*addr++); // Read, issue command |
peekpt | 7:856a3443f68d | 278 | numArgs = *addr++; // Number of args to follow |
peekpt | 7:856a3443f68d | 279 | ms = numArgs & DELAY; // If hibit set, delay follows args |
peekpt | 7:856a3443f68d | 280 | numArgs &= ~DELAY; // Mask out delay bit |
peekpt | 7:856a3443f68d | 281 | while (numArgs--) { // For each argument... |
peekpt | 7:856a3443f68d | 282 | writedata(*addr++); // Read, issue argument |
peekpt | 7:856a3443f68d | 283 | } |
SomeRandomBloke | 0:99ee879ef5a9 | 284 | |
peekpt | 7:856a3443f68d | 285 | if (ms) { |
peekpt | 7:856a3443f68d | 286 | ms = *addr++; // Read post-command delay time (ms) |
peekpt | 7:856a3443f68d | 287 | if (ms == 255) |
peekpt | 7:856a3443f68d | 288 | ms = 500; // If 255, delay for 500 ms |
peekpt | 7:856a3443f68d | 289 | ThisThread::sleep_for(ms); |
SomeRandomBloke | 0:99ee879ef5a9 | 290 | } |
peekpt | 7:856a3443f68d | 291 | } |
SomeRandomBloke | 0:99ee879ef5a9 | 292 | } |
SomeRandomBloke | 0:99ee879ef5a9 | 293 | |
SomeRandomBloke | 0:99ee879ef5a9 | 294 | // Initialization code common to both 'B' and 'R' type displays |
peekpt | 7:856a3443f68d | 295 | void Adafruit_ST7735::commonInit(uint8_t *cmdList) { |
peekpt | 7:856a3443f68d | 296 | |
peekpt | 7:856a3443f68d | 297 | colstart = rowstart = 0; // May be overridden in init func |
SomeRandomBloke | 0:99ee879ef5a9 | 298 | |
peekpt | 7:856a3443f68d | 299 | _rs = 1; |
peekpt | 7:856a3443f68d | 300 | _cs = 1; |
SomeRandomBloke | 0:99ee879ef5a9 | 301 | |
peekpt | 7:856a3443f68d | 302 | // use default SPI format |
peekpt | 7:856a3443f68d | 303 | lcdPort.format(8, 0); |
peekpt | 7:856a3443f68d | 304 | lcdPort.frequency(20000000); |
SomeRandomBloke | 0:99ee879ef5a9 | 305 | |
peekpt | 7:856a3443f68d | 306 | // toggle RST low to reset; CS low so it'll listen to us |
peekpt | 7:856a3443f68d | 307 | _cs = 0; |
peekpt | 7:856a3443f68d | 308 | _rst = 1; |
peekpt | 7:856a3443f68d | 309 | ThisThread::sleep_for(100); |
peekpt | 7:856a3443f68d | 310 | _rst = 0; |
peekpt | 7:856a3443f68d | 311 | ThisThread::sleep_for(100); |
peekpt | 7:856a3443f68d | 312 | _rst = 1; |
peekpt | 7:856a3443f68d | 313 | ThisThread::sleep_for(100); |
SomeRandomBloke | 0:99ee879ef5a9 | 314 | |
peekpt | 7:856a3443f68d | 315 | if (cmdList) |
peekpt | 7:856a3443f68d | 316 | commandList(cmdList); |
SomeRandomBloke | 0:99ee879ef5a9 | 317 | } |
SomeRandomBloke | 0:99ee879ef5a9 | 318 | |
SomeRandomBloke | 0:99ee879ef5a9 | 319 | // Initialization for ST7735B screens |
peekpt | 7:856a3443f68d | 320 | void Adafruit_ST7735::initB(void) { commonInit(Bcmd); } |
SomeRandomBloke | 0:99ee879ef5a9 | 321 | |
SomeRandomBloke | 0:99ee879ef5a9 | 322 | // Initialization for ST7735R screens (green or red tabs) |
SomeRandomBloke | 4:2123ea52a4d9 | 323 | void Adafruit_ST7735::initR(uint8_t options) { |
SomeRandomBloke | 4:2123ea52a4d9 | 324 | commonInit(Rcmd1); |
peekpt | 7:856a3443f68d | 325 | if (options == INITR_GREENTAB) { |
SomeRandomBloke | 4:2123ea52a4d9 | 326 | commandList(Rcmd2green); |
SomeRandomBloke | 4:2123ea52a4d9 | 327 | colstart = 2; |
SomeRandomBloke | 4:2123ea52a4d9 | 328 | rowstart = 1; |
peekpt | 7:856a3443f68d | 329 | } else if (options == INITR_GREENTAB2) { |
peekpt | 7:856a3443f68d | 330 | commandList(Rcmd2green); |
peekpt | 7:856a3443f68d | 331 | colstart = 1; |
peekpt | 7:856a3443f68d | 332 | rowstart = 2; |
peekpt | 7:856a3443f68d | 333 | } else if (options == INITR_144GREENTAB) { |
SomeRandomBloke | 4:2123ea52a4d9 | 334 | _height = ST7735_TFTHEIGHT_144; |
SomeRandomBloke | 4:2123ea52a4d9 | 335 | commandList(Rcmd2green144); |
SomeRandomBloke | 4:2123ea52a4d9 | 336 | colstart = 2; |
SomeRandomBloke | 4:2123ea52a4d9 | 337 | rowstart = 3; |
SomeRandomBloke | 4:2123ea52a4d9 | 338 | } else { |
SomeRandomBloke | 4:2123ea52a4d9 | 339 | // colstart, rowstart left at default '0' values |
SomeRandomBloke | 4:2123ea52a4d9 | 340 | commandList(Rcmd2red); |
SomeRandomBloke | 4:2123ea52a4d9 | 341 | } |
SomeRandomBloke | 4:2123ea52a4d9 | 342 | commandList(Rcmd3); |
SomeRandomBloke | 4:2123ea52a4d9 | 343 | |
SomeRandomBloke | 4:2123ea52a4d9 | 344 | // if black, change MADCTL color filter |
peekpt | 7:856a3443f68d | 345 | if (options == INITR_BLACKTAB || INITR_GREENTAB2) { |
SomeRandomBloke | 4:2123ea52a4d9 | 346 | writecommand(ST7735_MADCTL); |
SomeRandomBloke | 4:2123ea52a4d9 | 347 | writedata(0xC0); |
SomeRandomBloke | 4:2123ea52a4d9 | 348 | } |
SomeRandomBloke | 4:2123ea52a4d9 | 349 | |
SomeRandomBloke | 4:2123ea52a4d9 | 350 | tabcolor = options; |
SomeRandomBloke | 0:99ee879ef5a9 | 351 | } |
SomeRandomBloke | 0:99ee879ef5a9 | 352 | |
SomeRandomBloke | 0:99ee879ef5a9 | 353 | void Adafruit_ST7735::setAddrWindow(uint8_t x0, uint8_t y0, uint8_t x1, |
peekpt | 7:856a3443f68d | 354 | uint8_t y1) { |
peekpt | 7:856a3443f68d | 355 | writecommand(ST7735_CASET); // Column addr set |
peekpt | 7:856a3443f68d | 356 | writedata(0x00); |
peekpt | 7:856a3443f68d | 357 | writedata(x0 + colstart); // XSTART |
peekpt | 7:856a3443f68d | 358 | writedata(0x00); |
peekpt | 7:856a3443f68d | 359 | writedata(x1 + colstart); // XEND |
SomeRandomBloke | 0:99ee879ef5a9 | 360 | |
peekpt | 7:856a3443f68d | 361 | writecommand(ST7735_RASET); // Row addr set |
peekpt | 7:856a3443f68d | 362 | writedata(0x00); |
peekpt | 7:856a3443f68d | 363 | writedata(y0 + rowstart); // YSTART |
peekpt | 7:856a3443f68d | 364 | writedata(0x00); |
peekpt | 7:856a3443f68d | 365 | writedata(y1 + rowstart); // YEND |
SomeRandomBloke | 0:99ee879ef5a9 | 366 | |
peekpt | 7:856a3443f68d | 367 | writecommand(ST7735_RAMWR); // write to RAM |
SomeRandomBloke | 0:99ee879ef5a9 | 368 | } |
SomeRandomBloke | 0:99ee879ef5a9 | 369 | |
peekpt | 7:856a3443f68d | 370 | void Adafruit_ST7735::pushColor(uint16_t color) { |
peekpt | 7:856a3443f68d | 371 | _rs = 1; |
peekpt | 7:856a3443f68d | 372 | _cs = 0; |
SomeRandomBloke | 0:99ee879ef5a9 | 373 | |
peekpt | 7:856a3443f68d | 374 | lcdPort.write(color >> 8); |
peekpt | 7:856a3443f68d | 375 | lcdPort.write(color); |
peekpt | 7:856a3443f68d | 376 | _cs = 1; |
SomeRandomBloke | 0:99ee879ef5a9 | 377 | } |
SomeRandomBloke | 0:99ee879ef5a9 | 378 | |
peekpt | 7:856a3443f68d | 379 | void Adafruit_ST7735::drawPixel(int16_t x, int16_t y, uint16_t color) { |
peekpt | 7:856a3443f68d | 380 | if ((x < 0) || (x >= _width) || (y < 0) || (y >= _height)) |
peekpt | 7:856a3443f68d | 381 | return; |
SomeRandomBloke | 0:99ee879ef5a9 | 382 | |
peekpt | 7:856a3443f68d | 383 | setAddrWindow(x, y, x + 1, y + 1); |
SomeRandomBloke | 0:99ee879ef5a9 | 384 | |
peekpt | 7:856a3443f68d | 385 | _rs = 1; |
peekpt | 7:856a3443f68d | 386 | _cs = 0; |
SomeRandomBloke | 0:99ee879ef5a9 | 387 | |
peekpt | 7:856a3443f68d | 388 | lcdPort.write(color >> 8); |
peekpt | 7:856a3443f68d | 389 | lcdPort.write(color); |
SomeRandomBloke | 0:99ee879ef5a9 | 390 | |
peekpt | 7:856a3443f68d | 391 | _cs = 1; |
SomeRandomBloke | 0:99ee879ef5a9 | 392 | } |
SomeRandomBloke | 0:99ee879ef5a9 | 393 | |
SomeRandomBloke | 0:99ee879ef5a9 | 394 | void Adafruit_ST7735::drawFastVLine(int16_t x, int16_t y, int16_t h, |
peekpt | 7:856a3443f68d | 395 | uint16_t color) { |
peekpt | 7:856a3443f68d | 396 | // Rudimentary clipping |
peekpt | 7:856a3443f68d | 397 | if ((x >= _width) || (y >= _height)) |
peekpt | 7:856a3443f68d | 398 | return; |
peekpt | 7:856a3443f68d | 399 | if ((y + h - 1) >= _height) |
peekpt | 7:856a3443f68d | 400 | h = _height - y; |
peekpt | 7:856a3443f68d | 401 | setAddrWindow(x, y, x, y + h - 1); |
SomeRandomBloke | 0:99ee879ef5a9 | 402 | |
peekpt | 7:856a3443f68d | 403 | uint8_t hi = color >> 8, lo = color; |
peekpt | 7:856a3443f68d | 404 | _rs = 1; |
peekpt | 7:856a3443f68d | 405 | _cs = 0; |
peekpt | 7:856a3443f68d | 406 | while (h--) { |
peekpt | 7:856a3443f68d | 407 | lcdPort.write(hi); |
peekpt | 7:856a3443f68d | 408 | lcdPort.write(lo); |
peekpt | 7:856a3443f68d | 409 | } |
peekpt | 7:856a3443f68d | 410 | _cs = 1; |
SomeRandomBloke | 0:99ee879ef5a9 | 411 | } |
SomeRandomBloke | 0:99ee879ef5a9 | 412 | |
SomeRandomBloke | 0:99ee879ef5a9 | 413 | void Adafruit_ST7735::drawFastHLine(int16_t x, int16_t y, int16_t w, |
peekpt | 7:856a3443f68d | 414 | uint16_t color) { |
peekpt | 7:856a3443f68d | 415 | // Rudimentary clipping |
peekpt | 7:856a3443f68d | 416 | if ((x >= _width) || (y >= _height)) |
peekpt | 7:856a3443f68d | 417 | return; |
peekpt | 7:856a3443f68d | 418 | if ((x + w - 1) >= _width) |
peekpt | 7:856a3443f68d | 419 | w = _width - x; |
peekpt | 7:856a3443f68d | 420 | setAddrWindow(x, y, x + w - 1, y); |
SomeRandomBloke | 0:99ee879ef5a9 | 421 | |
peekpt | 7:856a3443f68d | 422 | uint8_t hi = color >> 8, lo = color; |
peekpt | 7:856a3443f68d | 423 | _rs = 1; |
peekpt | 7:856a3443f68d | 424 | _cs = 0; |
peekpt | 7:856a3443f68d | 425 | while (w--) { |
peekpt | 7:856a3443f68d | 426 | lcdPort.write(hi); |
peekpt | 7:856a3443f68d | 427 | lcdPort.write(lo); |
peekpt | 7:856a3443f68d | 428 | } |
peekpt | 7:856a3443f68d | 429 | _cs = 1; |
SomeRandomBloke | 0:99ee879ef5a9 | 430 | } |
SomeRandomBloke | 0:99ee879ef5a9 | 431 | |
peekpt | 7:856a3443f68d | 432 | void Adafruit_ST7735::fillScreen(uint16_t color) { |
peekpt | 7:856a3443f68d | 433 | fillRect(0, 0, _width, _height, color); |
SomeRandomBloke | 4:2123ea52a4d9 | 434 | } |
SomeRandomBloke | 4:2123ea52a4d9 | 435 | |
SomeRandomBloke | 0:99ee879ef5a9 | 436 | // fill a rectangle |
SomeRandomBloke | 0:99ee879ef5a9 | 437 | void Adafruit_ST7735::fillRect(int16_t x, int16_t y, int16_t w, int16_t h, |
peekpt | 7:856a3443f68d | 438 | uint16_t color) { |
peekpt | 7:856a3443f68d | 439 | // rudimentary clipping (drawChar w/big text requires this) |
peekpt | 7:856a3443f68d | 440 | if ((x >= _width) || (y >= _height)) |
peekpt | 7:856a3443f68d | 441 | return; |
peekpt | 7:856a3443f68d | 442 | if ((x + w - 1) >= _width) |
peekpt | 7:856a3443f68d | 443 | w = _width - x; |
peekpt | 7:856a3443f68d | 444 | if ((y + h - 1) >= _height) |
peekpt | 7:856a3443f68d | 445 | h = _height - y; |
SomeRandomBloke | 0:99ee879ef5a9 | 446 | |
peekpt | 7:856a3443f68d | 447 | setAddrWindow(x, y, x + w - 1, y + h - 1); |
SomeRandomBloke | 0:99ee879ef5a9 | 448 | |
peekpt | 7:856a3443f68d | 449 | uint8_t hi = color >> 8, lo = color; |
peekpt | 7:856a3443f68d | 450 | _rs = 1; |
peekpt | 7:856a3443f68d | 451 | _cs = 0; |
peekpt | 7:856a3443f68d | 452 | for (y = h; y > 0; y--) { |
peekpt | 7:856a3443f68d | 453 | for (x = w; x > 0; x--) { |
peekpt | 7:856a3443f68d | 454 | lcdPort.write(hi); |
peekpt | 7:856a3443f68d | 455 | lcdPort.write(lo); |
SomeRandomBloke | 0:99ee879ef5a9 | 456 | } |
peekpt | 7:856a3443f68d | 457 | } |
SomeRandomBloke | 0:99ee879ef5a9 | 458 | |
peekpt | 7:856a3443f68d | 459 | _cs = 1; |
SomeRandomBloke | 0:99ee879ef5a9 | 460 | } |
SomeRandomBloke | 0:99ee879ef5a9 | 461 | |
SomeRandomBloke | 0:99ee879ef5a9 | 462 | // Pass 8-bit (each) R,G,B, get back 16-bit packed color |
peekpt | 7:856a3443f68d | 463 | uint16_t Adafruit_ST7735::Color565(uint8_t r, uint8_t g, uint8_t b) { |
peekpt | 7:856a3443f68d | 464 | return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3); |
SomeRandomBloke | 0:99ee879ef5a9 | 465 | } |
SomeRandomBloke | 0:99ee879ef5a9 | 466 | |
peekpt | 7:856a3443f68d | 467 | #define MADCTL_MY 0x80 |
peekpt | 7:856a3443f68d | 468 | #define MADCTL_MX 0x40 |
peekpt | 7:856a3443f68d | 469 | #define MADCTL_MV 0x20 |
peekpt | 7:856a3443f68d | 470 | #define MADCTL_ML 0x10 |
SomeRandomBloke | 6:c964b41674fc | 471 | #define MADCTL_RGB 0x00 |
SomeRandomBloke | 6:c964b41674fc | 472 | #define MADCTL_BGR 0x08 |
peekpt | 7:856a3443f68d | 473 | #define MADCTL_MH 0x04 |
SomeRandomBloke | 0:99ee879ef5a9 | 474 | |
SomeRandomBloke | 6:c964b41674fc | 475 | void Adafruit_ST7735::setRotation(uint8_t m) { |
SomeRandomBloke | 6:c964b41674fc | 476 | writecommand(ST7735_MADCTL); |
SomeRandomBloke | 6:c964b41674fc | 477 | rotation = m % 4; // can't be higher than 3 |
SomeRandomBloke | 6:c964b41674fc | 478 | switch (rotation) { |
peekpt | 7:856a3443f68d | 479 | case 0: |
peekpt | 7:856a3443f68d | 480 | if (tabcolor == INITR_BLACKTAB || INITR_GREENTAB2) { |
peekpt | 7:856a3443f68d | 481 | writedata(MADCTL_MX | MADCTL_MY | MADCTL_RGB); |
peekpt | 7:856a3443f68d | 482 | } else { |
peekpt | 7:856a3443f68d | 483 | writedata(MADCTL_MX | MADCTL_MY | MADCTL_BGR); |
peekpt | 7:856a3443f68d | 484 | } |
peekpt | 7:856a3443f68d | 485 | _width = ST7735_TFTWIDTH; |
SomeRandomBloke | 6:c964b41674fc | 486 | |
peekpt | 7:856a3443f68d | 487 | if (tabcolor == INITR_144GREENTAB) |
peekpt | 7:856a3443f68d | 488 | _height = ST7735_TFTHEIGHT_144; |
peekpt | 7:856a3443f68d | 489 | else |
peekpt | 7:856a3443f68d | 490 | _height = ST7735_TFTHEIGHT_18; |
SomeRandomBloke | 6:c964b41674fc | 491 | |
SomeRandomBloke | 6:c964b41674fc | 492 | break; |
peekpt | 7:856a3443f68d | 493 | case 1: |
peekpt | 7:856a3443f68d | 494 | if (tabcolor == INITR_BLACKTAB || INITR_GREENTAB2) { |
peekpt | 7:856a3443f68d | 495 | writedata(MADCTL_MY | MADCTL_MV | MADCTL_RGB); |
peekpt | 7:856a3443f68d | 496 | } else { |
peekpt | 7:856a3443f68d | 497 | writedata(MADCTL_MY | MADCTL_MV | MADCTL_BGR); |
peekpt | 7:856a3443f68d | 498 | } |
peekpt | 7:856a3443f68d | 499 | |
peekpt | 7:856a3443f68d | 500 | if (tabcolor == INITR_144GREENTAB) |
peekpt | 7:856a3443f68d | 501 | _width = ST7735_TFTHEIGHT_144; |
peekpt | 7:856a3443f68d | 502 | else |
peekpt | 7:856a3443f68d | 503 | _width = ST7735_TFTHEIGHT_18; |
SomeRandomBloke | 6:c964b41674fc | 504 | |
peekpt | 7:856a3443f68d | 505 | _height = ST7735_TFTWIDTH; |
peekpt | 7:856a3443f68d | 506 | break; |
peekpt | 7:856a3443f68d | 507 | case 2: |
peekpt | 7:856a3443f68d | 508 | if (tabcolor == INITR_BLACKTAB || INITR_GREENTAB2) { |
peekpt | 7:856a3443f68d | 509 | writedata(MADCTL_RGB); |
peekpt | 7:856a3443f68d | 510 | } else { |
peekpt | 7:856a3443f68d | 511 | writedata(MADCTL_BGR); |
peekpt | 7:856a3443f68d | 512 | } |
peekpt | 7:856a3443f68d | 513 | _width = ST7735_TFTWIDTH; |
peekpt | 7:856a3443f68d | 514 | if (tabcolor == INITR_144GREENTAB) |
peekpt | 7:856a3443f68d | 515 | _height = ST7735_TFTHEIGHT_144; |
peekpt | 7:856a3443f68d | 516 | else |
peekpt | 7:856a3443f68d | 517 | _height = ST7735_TFTHEIGHT_18; |
peekpt | 7:856a3443f68d | 518 | |
peekpt | 7:856a3443f68d | 519 | break; |
peekpt | 7:856a3443f68d | 520 | case 3: |
peekpt | 7:856a3443f68d | 521 | if (tabcolor == INITR_BLACKTAB || INITR_GREENTAB2) { |
peekpt | 7:856a3443f68d | 522 | writedata(MADCTL_MX | MADCTL_MV | MADCTL_RGB); |
peekpt | 7:856a3443f68d | 523 | } else { |
peekpt | 7:856a3443f68d | 524 | writedata(MADCTL_MX | MADCTL_MV | MADCTL_BGR); |
peekpt | 7:856a3443f68d | 525 | } |
peekpt | 7:856a3443f68d | 526 | if (tabcolor == INITR_144GREENTAB) |
peekpt | 7:856a3443f68d | 527 | _width = ST7735_TFTHEIGHT_144; |
peekpt | 7:856a3443f68d | 528 | else |
peekpt | 7:856a3443f68d | 529 | _width = ST7735_TFTHEIGHT_18; |
peekpt | 7:856a3443f68d | 530 | |
peekpt | 7:856a3443f68d | 531 | _height = ST7735_TFTWIDTH; |
peekpt | 7:856a3443f68d | 532 | break; |
SomeRandomBloke | 6:c964b41674fc | 533 | } |
SomeRandomBloke | 0:99ee879ef5a9 | 534 | } |
SomeRandomBloke | 0:99ee879ef5a9 | 535 | |
peekpt | 7:856a3443f68d | 536 | void Adafruit_ST7735::invertDisplay(boolean i) { |
peekpt | 7:856a3443f68d | 537 | writecommand(i ? ST7735_INVON : ST7735_INVOFF); |
SomeRandomBloke | 0:99ee879ef5a9 | 538 | } |