[136] | 1 | /***************************************************
|
---|
| 2 | This is a library for the Adafruit 1.8" SPI display.
|
---|
| 3 | This library works with the Adafruit 1.8" TFT Breakout w/SD card
|
---|
| 4 | ----> http://www.adafruit.com/products/358
|
---|
| 5 | as well as Adafruit raw 1.8" TFT display
|
---|
| 6 | ----> http://www.adafruit.com/products/618
|
---|
| 7 |
|
---|
| 8 | Check out the links above for our tutorials and wiring diagrams
|
---|
| 9 | These displays use SPI to communicate, 4 or 5 pins are required to
|
---|
| 10 | interface (RST is optional)
|
---|
| 11 | Adafruit invests time and resources providing this open source code,
|
---|
| 12 | please support Adafruit and open-source hardware by purchasing
|
---|
| 13 | products from Adafruit!
|
---|
| 14 |
|
---|
| 15 | Written by Limor Fried/Ladyada for Adafruit Industries.
|
---|
| 16 | MIT license, all text above must be included in any redistribution
|
---|
| 17 | ****************************************************/
|
---|
| 18 |
|
---|
| 19 | #include "Adafruit_ST7735.h"
|
---|
| 20 | #include <avr/pgmspace.h>
|
---|
| 21 | #include <limits.h>
|
---|
| 22 | #include "pins_arduino.h"
|
---|
| 23 | #include "wiring_private.h"
|
---|
| 24 | #include <SPI.h>
|
---|
| 25 |
|
---|
| 26 | inline uint16_t swapcolor(uint16_t x) {
|
---|
| 27 | return (x << 11) | (x & 0x07E0) | (x >> 11);
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 |
|
---|
| 31 | // Constructor when using software SPI. All output pins are configurable.
|
---|
| 32 | Adafruit_ST7735::Adafruit_ST7735(uint8_t cs, uint8_t rs, uint8_t sid,
|
---|
| 33 | uint8_t sclk, uint8_t rst) : Adafruit_GFX(ST7735_TFTWIDTH, ST7735_TFTHEIGHT)
|
---|
| 34 | {
|
---|
| 35 | _cs = cs;
|
---|
| 36 | _rs = rs;
|
---|
| 37 | _sid = sid;
|
---|
| 38 | _sclk = sclk;
|
---|
| 39 | _rst = rst;
|
---|
| 40 | hwSPI = false;
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 |
|
---|
| 44 | // Constructor when using hardware SPI. Faster, but must use SPI pins
|
---|
| 45 | // specific to each board type (e.g. 11,13 for Uno, 51,52 for Mega, etc.)
|
---|
| 46 | Adafruit_ST7735::Adafruit_ST7735(uint8_t cs, uint8_t rs, uint8_t rst) :
|
---|
| 47 | Adafruit_GFX(ST7735_TFTWIDTH, ST7735_TFTHEIGHT)
|
---|
| 48 | {
|
---|
| 49 | _cs = cs;
|
---|
| 50 | _rs = rs;
|
---|
| 51 | _rst = rst;
|
---|
| 52 | hwSPI = true;
|
---|
| 53 | _sid = _sclk = 0;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 |
|
---|
| 57 | inline void Adafruit_ST7735::spiwrite(uint8_t c) {
|
---|
| 58 |
|
---|
| 59 | //Serial.println(c, HEX);
|
---|
| 60 |
|
---|
| 61 | if (hwSPI) {
|
---|
| 62 | SPI.transfer(c);
|
---|
| 63 | } else {
|
---|
| 64 | // Fast SPI bitbang swiped from LPD8806 library
|
---|
| 65 | for(uint8_t bit = 0x80; bit; bit >>= 1) {
|
---|
| 66 | if(c & bit) *dataport |= datapinmask;
|
---|
| 67 | else *dataport &= ~datapinmask;
|
---|
| 68 | *clkport |= clkpinmask;
|
---|
| 69 | *clkport &= ~clkpinmask;
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 |
|
---|
| 75 | void Adafruit_ST7735::writecommand(uint8_t c) {
|
---|
| 76 | *rsport &= ~rspinmask;
|
---|
| 77 | *csport &= ~cspinmask;
|
---|
| 78 |
|
---|
| 79 | //Serial.print("C ");
|
---|
| 80 | spiwrite(c);
|
---|
| 81 |
|
---|
| 82 | *csport |= cspinmask;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 |
|
---|
| 86 | void Adafruit_ST7735::writedata(uint8_t c) {
|
---|
| 87 | *rsport |= rspinmask;
|
---|
| 88 | *csport &= ~cspinmask;
|
---|
| 89 |
|
---|
| 90 | //Serial.print("D ");
|
---|
| 91 | spiwrite(c);
|
---|
| 92 |
|
---|
| 93 | *csport |= cspinmask;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 |
|
---|
| 97 | // Rather than a bazillion writecommand() and writedata() calls, screen
|
---|
| 98 | // initialization commands and arguments are organized in these tables
|
---|
| 99 | // stored in PROGMEM. The table may look bulky, but that's mostly the
|
---|
| 100 | // formatting -- storage-wise this is hundreds of bytes more compact
|
---|
| 101 | // than the equivalent code. Companion function follows.
|
---|
| 102 | #define DELAY 0x80
|
---|
| 103 | PROGMEM const static unsigned char
|
---|
| 104 | Bcmd[] = { // Initialization commands for 7735B screens
|
---|
| 105 | 18, // 18 commands in list:
|
---|
| 106 | ST7735_SWRESET, DELAY, // 1: Software reset, no args, w/delay
|
---|
| 107 | 50, // 50 ms delay
|
---|
| 108 | ST7735_SLPOUT , DELAY, // 2: Out of sleep mode, no args, w/delay
|
---|
| 109 | 255, // 255 = 500 ms delay
|
---|
| 110 | ST7735_COLMOD , 1+DELAY, // 3: Set color mode, 1 arg + delay:
|
---|
| 111 | 0x05, // 16-bit color
|
---|
| 112 | 10, // 10 ms delay
|
---|
| 113 | ST7735_FRMCTR1, 3+DELAY, // 4: Frame rate control, 3 args + delay:
|
---|
| 114 | 0x00, // fastest refresh
|
---|
| 115 | 0x06, // 6 lines front porch
|
---|
| 116 | 0x03, // 3 lines back porch
|
---|
| 117 | 10, // 10 ms delay
|
---|
| 118 | ST7735_MADCTL , 1 , // 5: Memory access ctrl (directions), 1 arg:
|
---|
| 119 | 0x08, // Row addr/col addr, bottom to top refresh
|
---|
| 120 | ST7735_DISSET5, 2 , // 6: Display settings #5, 2 args, no delay:
|
---|
| 121 | 0x15, // 1 clk cycle nonoverlap, 2 cycle gate
|
---|
| 122 | // rise, 3 cycle osc equalize
|
---|
| 123 | 0x02, // Fix on VTL
|
---|
| 124 | ST7735_INVCTR , 1 , // 7: Display inversion control, 1 arg:
|
---|
| 125 | 0x0, // Line inversion
|
---|
| 126 | ST7735_PWCTR1 , 2+DELAY, // 8: Power control, 2 args + delay:
|
---|
| 127 | 0x02, // GVDD = 4.7V
|
---|
| 128 | 0x70, // 1.0uA
|
---|
| 129 | 10, // 10 ms delay
|
---|
| 130 | ST7735_PWCTR2 , 1 , // 9: Power control, 1 arg, no delay:
|
---|
| 131 | 0x05, // VGH = 14.7V, VGL = -7.35V
|
---|
| 132 | ST7735_PWCTR3 , 2 , // 10: Power control, 2 args, no delay:
|
---|
| 133 | 0x01, // Opamp current small
|
---|
| 134 | 0x02, // Boost frequency
|
---|
| 135 | ST7735_VMCTR1 , 2+DELAY, // 11: Power control, 2 args + delay:
|
---|
| 136 | 0x3C, // VCOMH = 4V
|
---|
| 137 | 0x38, // VCOML = -1.1V
|
---|
| 138 | 10, // 10 ms delay
|
---|
| 139 | ST7735_PWCTR6 , 2 , // 12: Power control, 2 args, no delay:
|
---|
| 140 | 0x11, 0x15,
|
---|
| 141 | ST7735_GMCTRP1,16 , // 13: Magical unicorn dust, 16 args, no delay:
|
---|
| 142 | 0x09, 0x16, 0x09, 0x20, // (seriously though, not sure what
|
---|
| 143 | 0x21, 0x1B, 0x13, 0x19, // these config values represent)
|
---|
| 144 | 0x17, 0x15, 0x1E, 0x2B,
|
---|
| 145 | 0x04, 0x05, 0x02, 0x0E,
|
---|
| 146 | ST7735_GMCTRN1,16+DELAY, // 14: Sparkles and rainbows, 16 args + delay:
|
---|
| 147 | 0x0B, 0x14, 0x08, 0x1E, // (ditto)
|
---|
| 148 | 0x22, 0x1D, 0x18, 0x1E,
|
---|
| 149 | 0x1B, 0x1A, 0x24, 0x2B,
|
---|
| 150 | 0x06, 0x06, 0x02, 0x0F,
|
---|
| 151 | 10, // 10 ms delay
|
---|
| 152 | ST7735_CASET , 4 , // 15: Column addr set, 4 args, no delay:
|
---|
| 153 | 0x00, 0x02, // XSTART = 2
|
---|
| 154 | 0x00, 0x81, // XEND = 129
|
---|
| 155 | ST7735_RASET , 4 , // 16: Row addr set, 4 args, no delay:
|
---|
| 156 | 0x00, 0x02, // XSTART = 1
|
---|
| 157 | 0x00, 0x81, // XEND = 160
|
---|
| 158 | ST7735_NORON , DELAY, // 17: Normal display on, no args, w/delay
|
---|
| 159 | 10, // 10 ms delay
|
---|
| 160 | ST7735_DISPON , DELAY, // 18: Main screen turn on, no args, w/delay
|
---|
| 161 | 255 }, // 255 = 500 ms delay
|
---|
| 162 |
|
---|
| 163 | Rcmd1[] = { // Init for 7735R, part 1 (red or green tab)
|
---|
| 164 | 15, // 15 commands in list:
|
---|
| 165 | ST7735_SWRESET, DELAY, // 1: Software reset, 0 args, w/delay
|
---|
| 166 | 150, // 150 ms delay
|
---|
| 167 | ST7735_SLPOUT , DELAY, // 2: Out of sleep mode, 0 args, w/delay
|
---|
| 168 | 255, // 500 ms delay
|
---|
| 169 | ST7735_FRMCTR1, 3 , // 3: Frame rate ctrl - normal mode, 3 args:
|
---|
| 170 | 0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D)
|
---|
| 171 | ST7735_FRMCTR2, 3 , // 4: Frame rate control - idle mode, 3 args:
|
---|
| 172 | 0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D)
|
---|
| 173 | ST7735_FRMCTR3, 6 , // 5: Frame rate ctrl - partial mode, 6 args:
|
---|
| 174 | 0x01, 0x2C, 0x2D, // Dot inversion mode
|
---|
| 175 | 0x01, 0x2C, 0x2D, // Line inversion mode
|
---|
| 176 | ST7735_INVCTR , 1 , // 6: Display inversion ctrl, 1 arg, no delay:
|
---|
| 177 | 0x07, // No inversion
|
---|
| 178 | ST7735_PWCTR1 , 3 , // 7: Power control, 3 args, no delay:
|
---|
| 179 | 0xA2,
|
---|
| 180 | 0x02, // -4.6V
|
---|
| 181 | 0x84, // AUTO mode
|
---|
| 182 | ST7735_PWCTR2 , 1 , // 8: Power control, 1 arg, no delay:
|
---|
| 183 | 0xC5, // VGH25 = 2.4C VGSEL = -10 VGH = 3 * AVDD
|
---|
| 184 | ST7735_PWCTR3 , 2 , // 9: Power control, 2 args, no delay:
|
---|
| 185 | 0x0A, // Opamp current small
|
---|
| 186 | 0x00, // Boost frequency
|
---|
| 187 | ST7735_PWCTR4 , 2 , // 10: Power control, 2 args, no delay:
|
---|
| 188 | 0x8A, // BCLK/2, Opamp current small & Medium low
|
---|
| 189 | 0x2A,
|
---|
| 190 | ST7735_PWCTR5 , 2 , // 11: Power control, 2 args, no delay:
|
---|
| 191 | 0x8A, 0xEE,
|
---|
| 192 | ST7735_VMCTR1 , 1 , // 12: Power control, 1 arg, no delay:
|
---|
| 193 | 0x0E,
|
---|
| 194 | ST7735_INVOFF , 0 , // 13: Don't invert display, no args, no delay
|
---|
| 195 | ST7735_MADCTL , 1 , // 14: Memory access control (directions), 1 arg:
|
---|
| 196 | 0xC8, // row addr/col addr, bottom to top refresh
|
---|
| 197 | ST7735_COLMOD , 1 , // 15: set color mode, 1 arg, no delay:
|
---|
| 198 | 0x05 }, // 16-bit color
|
---|
| 199 |
|
---|
| 200 | Rcmd2green[] = { // Init for 7735R, part 2 (green tab only)
|
---|
| 201 | 2, // 2 commands in list:
|
---|
| 202 | ST7735_CASET , 4 , // 1: Column addr set, 4 args, no delay:
|
---|
| 203 | 0x00, 0x02, // XSTART = 0
|
---|
| 204 | 0x00, 0x7F+0x02, // XEND = 127
|
---|
| 205 | ST7735_RASET , 4 , // 2: Row addr set, 4 args, no delay:
|
---|
| 206 | 0x00, 0x01, // XSTART = 0
|
---|
| 207 | 0x00, 0x9F+0x01 }, // XEND = 159
|
---|
| 208 | Rcmd2red[] = { // Init for 7735R, part 2 (red tab only)
|
---|
| 209 | 2, // 2 commands in list:
|
---|
| 210 | ST7735_CASET , 4 , // 1: Column addr set, 4 args, no delay:
|
---|
| 211 | 0x00, 0x00, // XSTART = 0
|
---|
| 212 | 0x00, 0x7F, // XEND = 127
|
---|
| 213 | ST7735_RASET , 4 , // 2: Row addr set, 4 args, no delay:
|
---|
| 214 | 0x00, 0x00, // XSTART = 0
|
---|
| 215 | 0x00, 0x9F }, // XEND = 159
|
---|
| 216 |
|
---|
| 217 | Rcmd3[] = { // Init for 7735R, part 3 (red or green tab)
|
---|
| 218 | 4, // 4 commands in list:
|
---|
| 219 | ST7735_GMCTRP1, 16 , // 1: Magical unicorn dust, 16 args, no delay:
|
---|
| 220 | 0x02, 0x1c, 0x07, 0x12,
|
---|
| 221 | 0x37, 0x32, 0x29, 0x2d,
|
---|
| 222 | 0x29, 0x25, 0x2B, 0x39,
|
---|
| 223 | 0x00, 0x01, 0x03, 0x10,
|
---|
| 224 | ST7735_GMCTRN1, 16 , // 2: Sparkles and rainbows, 16 args, no delay:
|
---|
| 225 | 0x03, 0x1d, 0x07, 0x06,
|
---|
| 226 | 0x2E, 0x2C, 0x29, 0x2D,
|
---|
| 227 | 0x2E, 0x2E, 0x37, 0x3F,
|
---|
| 228 | 0x00, 0x00, 0x02, 0x10,
|
---|
| 229 | ST7735_NORON , DELAY, // 3: Normal display on, no args, w/delay
|
---|
| 230 | 10, // 10 ms delay
|
---|
| 231 | ST7735_DISPON , DELAY, // 4: Main screen turn on, no args w/delay
|
---|
| 232 | 100 }, // 100 ms delay
|
---|
| 233 | Gcmd[] = { // Initialization commands for 7735B screens
|
---|
| 234 | 19, // 18 commands in list:
|
---|
| 235 | ST7735_SWRESET, DELAY, // 1: Software reset, no args, w/delay
|
---|
| 236 | 50, // 50 ms delay
|
---|
| 237 | ST7735_SLPOUT , DELAY, // 2: Out of sleep mode, no args, w/delay
|
---|
| 238 | 100, // 255 = 500 ms delay
|
---|
| 239 | 0x26 , 1, // 3: Set default gamma
|
---|
| 240 | 0x04, // 16-bit color
|
---|
| 241 | 0xb1, 2, // 4: Frame Rate
|
---|
| 242 | 0x0b,
|
---|
| 243 | 0x14,
|
---|
| 244 | 0xc0, 2, // 5: VRH1[4:0] & VC[2:0]
|
---|
| 245 | 0x08,
|
---|
| 246 | 0x00,
|
---|
| 247 | 0xc1, 1, // 6: BT[2:0]
|
---|
| 248 | 0x05,
|
---|
| 249 | 0xc5, 2, // 7: VMH[6:0] & VML[6:0]
|
---|
| 250 | 0x41,
|
---|
| 251 | 0x30,
|
---|
| 252 | 0xc7, 1, // 8: LCD Driving control
|
---|
| 253 | 0xc1,
|
---|
| 254 | 0xEC, 1, // 9: Set pumping color freq
|
---|
| 255 | 0x1b,
|
---|
| 256 | 0x3a , 1 + DELAY, // 10: Set color format
|
---|
| 257 | 0x55, // 16-bit color
|
---|
| 258 | 100,
|
---|
| 259 | 0x2a, 4, // 11: Set Column Address
|
---|
| 260 | 0x00,
|
---|
| 261 | 0x00,
|
---|
| 262 | 0x00,
|
---|
| 263 | 0x7f,
|
---|
| 264 | 0x2b, 4, // 12: Set Page Address
|
---|
| 265 | 0x00,
|
---|
| 266 | 0x00,
|
---|
| 267 | 0x00,
|
---|
| 268 | 0x9f,
|
---|
| 269 | 0x36, 1, // 12+1: Set Scanning Direction
|
---|
| 270 | 0xc8,
|
---|
| 271 | 0xb7, 1, // 14: Set Source Output Direciton
|
---|
| 272 | 0x00,
|
---|
| 273 | 0xf2, 1, // 15: Enable Gamma bit
|
---|
| 274 | 0x00,
|
---|
| 275 | 0xe0, 15 + DELAY, // 16: magic
|
---|
| 276 | 0x28, 0x24, 0x22, 0x31,
|
---|
| 277 | 0x2b, 0x0e, 0x53, 0xa5,
|
---|
| 278 | 0x42, 0x16, 0x18, 0x12,
|
---|
| 279 | 0x1a, 0x14, 0x03,
|
---|
| 280 | 50,
|
---|
| 281 | 0xe1, 15 + DELAY, // 17: more magic
|
---|
| 282 | 0x17, 0x1b, 0x1d, 0x0e,
|
---|
| 283 | 0x14, 0x11, 0x2c, 0xa5,
|
---|
| 284 | 0x3d, 0x09, 0x27, 0x2d,
|
---|
| 285 | 0x25, 0x2b, 0x3c,
|
---|
| 286 | 50,
|
---|
| 287 | ST7735_NORON , DELAY, // 17: Normal display on, no args, w/delay
|
---|
| 288 | 10, // 10 ms delay
|
---|
| 289 | ST7735_DISPON , DELAY, // 18: Main screen turn on, no args, w/delay
|
---|
| 290 | 255 }; // 255 = 500 ms delay
|
---|
| 291 |
|
---|
| 292 |
|
---|
| 293 |
|
---|
| 294 | // Companion code to the above tables. Reads and issues
|
---|
| 295 | // a series of LCD commands stored in PROGMEM byte array.
|
---|
| 296 | void Adafruit_ST7735::commandList(const uint8_t *addr) {
|
---|
| 297 |
|
---|
| 298 | uint8_t numCommands, numArgs;
|
---|
| 299 | uint16_t ms;
|
---|
| 300 |
|
---|
| 301 | numCommands = pgm_read_byte(addr++); // Number of commands to follow
|
---|
| 302 | while(numCommands--) { // For each command...
|
---|
| 303 | writecommand(pgm_read_byte(addr++)); // Read, issue command
|
---|
| 304 | numArgs = pgm_read_byte(addr++); // Number of args to follow
|
---|
| 305 | ms = numArgs & DELAY; // If hibit set, delay follows args
|
---|
| 306 | numArgs &= ~DELAY; // Mask out delay bit
|
---|
| 307 | while(numArgs--) { // For each argument...
|
---|
| 308 | writedata(pgm_read_byte(addr++)); // Read, issue argument
|
---|
| 309 | }
|
---|
| 310 |
|
---|
| 311 | if(ms) {
|
---|
| 312 | ms = pgm_read_byte(addr++); // Read post-command delay time (ms)
|
---|
| 313 | if(ms == 255) ms = 500; // If 255, delay for 500 ms
|
---|
| 314 | delay(ms);
|
---|
| 315 | }
|
---|
| 316 | }
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 |
|
---|
| 320 | // Initialization code common to both 'B' and 'R' type displays
|
---|
| 321 | void Adafruit_ST7735::commonInit(const uint8_t *cmdList) {
|
---|
| 322 |
|
---|
| 323 | colstart = rowstart = 0; // May be overridden in init func
|
---|
| 324 |
|
---|
| 325 | pinMode(_rs, OUTPUT);
|
---|
| 326 | pinMode(_cs, OUTPUT);
|
---|
| 327 | csport = portOutputRegister(digitalPinToPort(_cs));
|
---|
| 328 | cspinmask = digitalPinToBitMask(_cs);
|
---|
| 329 | rsport = portOutputRegister(digitalPinToPort(_rs));
|
---|
| 330 | rspinmask = digitalPinToBitMask(_rs);
|
---|
| 331 |
|
---|
| 332 | if(hwSPI) { // Using hardware SPI
|
---|
| 333 | SPI.begin();
|
---|
| 334 | #if defined(ARDUINO_ARCH_SAMD)
|
---|
| 335 | SPI.setClockDivider(24); // 4 MHz (half speed)
|
---|
| 336 | #else
|
---|
| 337 | SPI.setClockDivider(SPI_CLOCK_DIV4); // 4 MHz (half speed)
|
---|
| 338 | #endif
|
---|
| 339 | SPI.setBitOrder(MSBFIRST);
|
---|
| 340 | SPI.setDataMode(SPI_MODE0);
|
---|
| 341 | } else {
|
---|
| 342 | pinMode(_sclk, OUTPUT);
|
---|
| 343 | pinMode(_sid , OUTPUT);
|
---|
| 344 | clkport = portOutputRegister(digitalPinToPort(_sclk));
|
---|
| 345 | clkpinmask = digitalPinToBitMask(_sclk);
|
---|
| 346 | dataport = portOutputRegister(digitalPinToPort(_sid));
|
---|
| 347 | datapinmask = digitalPinToBitMask(_sid);
|
---|
| 348 | *clkport &= ~clkpinmask;
|
---|
| 349 | *dataport &= ~datapinmask;
|
---|
| 350 | }
|
---|
| 351 |
|
---|
| 352 | // toggle RST low to reset; CS low so it'll listen to us
|
---|
| 353 | *csport &= ~cspinmask;
|
---|
| 354 | if (_rst) {
|
---|
| 355 | pinMode(_rst, OUTPUT);
|
---|
| 356 | digitalWrite(_rst, HIGH);
|
---|
| 357 | delay(500);
|
---|
| 358 | digitalWrite(_rst, LOW);
|
---|
| 359 | delay(500);
|
---|
| 360 | digitalWrite(_rst, HIGH);
|
---|
| 361 | delay(500);
|
---|
| 362 | }
|
---|
| 363 |
|
---|
| 364 | if(cmdList) commandList(cmdList);
|
---|
| 365 | }
|
---|
| 366 |
|
---|
| 367 |
|
---|
| 368 | // Initialization for ST7735B screens
|
---|
| 369 | void Adafruit_ST7735::initB(void) {
|
---|
| 370 | commonInit(Bcmd);
|
---|
| 371 | }
|
---|
| 372 |
|
---|
| 373 |
|
---|
| 374 | // Initialization for ST7735B screens
|
---|
| 375 | void Adafruit_ST7735::initG(void) {
|
---|
| 376 | commonInit(Gcmd);
|
---|
| 377 | }
|
---|
| 378 |
|
---|
| 379 |
|
---|
| 380 | // Initialization for ST7735R screens (green or red tabs)
|
---|
| 381 | void Adafruit_ST7735::initR(uint8_t options) {
|
---|
| 382 | commonInit(Rcmd1);
|
---|
| 383 | if(options == INITR_GREENTAB) {
|
---|
| 384 | commandList(Rcmd2green);
|
---|
| 385 | colstart = 2;
|
---|
| 386 | rowstart = 1;
|
---|
| 387 | } else {
|
---|
| 388 | // colstart, rowstart left at default '0' values
|
---|
| 389 | commandList(Rcmd2red);
|
---|
| 390 | }
|
---|
| 391 | commandList(Rcmd3);
|
---|
| 392 | tabcolor = options;
|
---|
| 393 | }
|
---|
| 394 |
|
---|
| 395 |
|
---|
| 396 | void Adafruit_ST7735::setAddrWindow(uint8_t x0, uint8_t y0, uint8_t x1,
|
---|
| 397 | uint8_t y1) {
|
---|
| 398 |
|
---|
| 399 | writecommand(ST7735_CASET); // Column addr set
|
---|
| 400 | writedata(0x00);
|
---|
| 401 | writedata(x0+colstart); // XSTART
|
---|
| 402 | writedata(0x00);
|
---|
| 403 | writedata(x1+colstart); // XEND
|
---|
| 404 |
|
---|
| 405 | writecommand(ST7735_RASET); // Row addr set
|
---|
| 406 | writedata(0x00);
|
---|
| 407 | writedata(y0+rowstart); // YSTART
|
---|
| 408 | writedata(0x00);
|
---|
| 409 | writedata(y1+rowstart); // YEND
|
---|
| 410 |
|
---|
| 411 | writecommand(ST7735_RAMWR); // write to RAM
|
---|
| 412 | }
|
---|
| 413 |
|
---|
| 414 |
|
---|
| 415 | void Adafruit_ST7735::pushColor(uint16_t color) {
|
---|
| 416 | *rsport |= rspinmask;
|
---|
| 417 | *csport &= ~cspinmask;
|
---|
| 418 |
|
---|
| 419 | if (tabcolor == INITR_BLACKTAB) color = swapcolor(color);
|
---|
| 420 | spiwrite(color >> 8);
|
---|
| 421 | spiwrite(color);
|
---|
| 422 |
|
---|
| 423 | *csport |= cspinmask;
|
---|
| 424 | }
|
---|
| 425 |
|
---|
| 426 | void Adafruit_ST7735::drawPixel(int16_t x, int16_t y, uint16_t color) {
|
---|
| 427 |
|
---|
| 428 | if((x < 0) ||(x >= _width) || (y < 0) || (y >= _height)) return;
|
---|
| 429 |
|
---|
| 430 | setAddrWindow(x,y,x+1,y+1);
|
---|
| 431 |
|
---|
| 432 | *rsport |= rspinmask;
|
---|
| 433 | *csport &= ~cspinmask;
|
---|
| 434 |
|
---|
| 435 | if (tabcolor == INITR_BLACKTAB) color = swapcolor(color);
|
---|
| 436 |
|
---|
| 437 | spiwrite(color >> 8);
|
---|
| 438 | spiwrite(color);
|
---|
| 439 |
|
---|
| 440 | *csport |= cspinmask;
|
---|
| 441 | }
|
---|
| 442 |
|
---|
| 443 |
|
---|
| 444 | void Adafruit_ST7735::drawFastVLine(int16_t x, int16_t y, int16_t h,
|
---|
| 445 | uint16_t color) {
|
---|
| 446 |
|
---|
| 447 | // Rudimentary clipping
|
---|
| 448 | if((x >= _width) || (y >= _height)) return;
|
---|
| 449 | if((y+h-1) >= _height) h = _height-y;
|
---|
| 450 | setAddrWindow(x, y, x, y+h-1);
|
---|
| 451 |
|
---|
| 452 | if (tabcolor == INITR_BLACKTAB) color = swapcolor(color);
|
---|
| 453 |
|
---|
| 454 | uint8_t hi = color >> 8, lo = color;
|
---|
| 455 | *rsport |= rspinmask;
|
---|
| 456 | *csport &= ~cspinmask;
|
---|
| 457 | while (h--) {
|
---|
| 458 | spiwrite(hi);
|
---|
| 459 | spiwrite(lo);
|
---|
| 460 | }
|
---|
| 461 | *csport |= cspinmask;
|
---|
| 462 | }
|
---|
| 463 |
|
---|
| 464 |
|
---|
| 465 | void Adafruit_ST7735::drawFastHLine(int16_t x, int16_t y, int16_t w,
|
---|
| 466 | uint16_t color) {
|
---|
| 467 |
|
---|
| 468 | // Rudimentary clipping
|
---|
| 469 | if((x >= _width) || (y >= _height)) return;
|
---|
| 470 | if((x+w-1) >= _width) w = _width-x;
|
---|
| 471 | setAddrWindow(x, y, x+w-1, y);
|
---|
| 472 |
|
---|
| 473 | if (tabcolor == INITR_BLACKTAB) color = swapcolor(color);
|
---|
| 474 |
|
---|
| 475 | uint8_t hi = color >> 8, lo = color;
|
---|
| 476 | *rsport |= rspinmask;
|
---|
| 477 | *csport &= ~cspinmask;
|
---|
| 478 | while (w--) {
|
---|
| 479 | spiwrite(hi);
|
---|
| 480 | spiwrite(lo);
|
---|
| 481 | }
|
---|
| 482 | *csport |= cspinmask;
|
---|
| 483 | }
|
---|
| 484 |
|
---|
| 485 |
|
---|
| 486 |
|
---|
| 487 | void Adafruit_ST7735::fillScreen(uint16_t color) {
|
---|
| 488 | fillRect(0, 0, _width, _height, color);
|
---|
| 489 | }
|
---|
| 490 |
|
---|
| 491 |
|
---|
| 492 |
|
---|
| 493 | // fill a rectangle
|
---|
| 494 | void Adafruit_ST7735::fillRect(int16_t x, int16_t y, int16_t w, int16_t h,
|
---|
| 495 | uint16_t color) {
|
---|
| 496 |
|
---|
| 497 | // rudimentary clipping (drawChar w/big text requires this)
|
---|
| 498 | if((x >= _width) || (y >= _height)) return;
|
---|
| 499 | if((x + w - 1) >= _width) w = _width - x;
|
---|
| 500 | if((y + h - 1) >= _height) h = _height - y;
|
---|
| 501 |
|
---|
| 502 | if (tabcolor == INITR_BLACKTAB) color = swapcolor(color);
|
---|
| 503 |
|
---|
| 504 | setAddrWindow(x, y, x+w-1, y+h-1);
|
---|
| 505 |
|
---|
| 506 | uint8_t hi = color >> 8, lo = color;
|
---|
| 507 | *rsport |= rspinmask;
|
---|
| 508 | *csport &= ~cspinmask;
|
---|
| 509 | for(y=h; y>0; y--) {
|
---|
| 510 | for(x=w; x>0; x--) {
|
---|
| 511 | spiwrite(hi);
|
---|
| 512 | spiwrite(lo);
|
---|
| 513 | }
|
---|
| 514 | }
|
---|
| 515 |
|
---|
| 516 | *csport |= cspinmask;
|
---|
| 517 | }
|
---|
| 518 |
|
---|
| 519 |
|
---|
| 520 | #define MADCTL_MY 0x80
|
---|
| 521 | #define MADCTL_MX 0x40
|
---|
| 522 | #define MADCTL_MV 0x20
|
---|
| 523 | #define MADCTL_ML 0x10
|
---|
| 524 | #define MADCTL_RGB 0x08
|
---|
| 525 | #define MADCTL_MH 0x04
|
---|
| 526 |
|
---|
| 527 | void Adafruit_ST7735::setRotation(uint8_t m) {
|
---|
| 528 |
|
---|
| 529 | writecommand(ST7735_MADCTL);
|
---|
| 530 | rotation = m % 4; // can't be higher than 3
|
---|
| 531 | switch (rotation) {
|
---|
| 532 | case 0:
|
---|
| 533 | writedata(MADCTL_MX | MADCTL_MY | MADCTL_RGB);
|
---|
| 534 | _width = ST7735_TFTWIDTH;
|
---|
| 535 | _height = ST7735_TFTHEIGHT;
|
---|
| 536 | break;
|
---|
| 537 | case 1:
|
---|
| 538 | writedata(MADCTL_MY | MADCTL_MV | MADCTL_RGB);
|
---|
| 539 | _width = ST7735_TFTHEIGHT;
|
---|
| 540 | _height = ST7735_TFTWIDTH;
|
---|
| 541 | break;
|
---|
| 542 | case 2:
|
---|
| 543 | writedata(MADCTL_RGB);
|
---|
| 544 | _width = ST7735_TFTWIDTH;
|
---|
| 545 | _height = ST7735_TFTHEIGHT;
|
---|
| 546 | break;
|
---|
| 547 | case 3:
|
---|
| 548 | writedata(MADCTL_MX | MADCTL_MV | MADCTL_RGB);
|
---|
| 549 | _width = ST7735_TFTHEIGHT;
|
---|
| 550 | _height = ST7735_TFTWIDTH;
|
---|
| 551 | break;
|
---|
| 552 | }
|
---|
| 553 | }
|
---|
| 554 |
|
---|
| 555 |
|
---|
| 556 | void Adafruit_ST7735::invertDisplay(boolean i) {
|
---|
| 557 | writecommand(i ? ST7735_INVON : ST7735_INVOFF);
|
---|
| 558 | }
|
---|
| 559 |
|
---|
| 560 |
|
---|
| 561 | ////////// stuff not actively being used, but kept for posterity
|
---|
| 562 | /*
|
---|
| 563 |
|
---|
| 564 | uint8_t Adafruit_ST7735::spiread(void) {
|
---|
| 565 | uint8_t r = 0;
|
---|
| 566 | if (_sid > 0) {
|
---|
| 567 | r = shiftIn(_sid, _sclk, MSBFIRST);
|
---|
| 568 | } else {
|
---|
| 569 | //SID_DDR &= ~_BV(SID);
|
---|
| 570 | //int8_t i;
|
---|
| 571 | //for (i=7; i>=0; i--) {
|
---|
| 572 | // SCLK_PORT &= ~_BV(SCLK);
|
---|
| 573 | // r <<= 1;
|
---|
| 574 | // r |= (SID_PIN >> SID) & 0x1;
|
---|
| 575 | // SCLK_PORT |= _BV(SCLK);
|
---|
| 576 | //}
|
---|
| 577 | //SID_DDR |= _BV(SID);
|
---|
| 578 |
|
---|
| 579 | }
|
---|
| 580 | return r;
|
---|
| 581 | }
|
---|
| 582 |
|
---|
| 583 |
|
---|
| 584 | void Adafruit_ST7735::dummyclock(void) {
|
---|
| 585 |
|
---|
| 586 | if (_sid > 0) {
|
---|
| 587 | digitalWrite(_sclk, LOW);
|
---|
| 588 | digitalWrite(_sclk, HIGH);
|
---|
| 589 | } else {
|
---|
| 590 | // SCLK_PORT &= ~_BV(SCLK);
|
---|
| 591 | //SCLK_PORT |= _BV(SCLK);
|
---|
| 592 | }
|
---|
| 593 | }
|
---|
| 594 | uint8_t Adafruit_ST7735::readdata(void) {
|
---|
| 595 | *portOutputRegister(rsport) |= rspin;
|
---|
| 596 |
|
---|
| 597 | *portOutputRegister(csport) &= ~ cspin;
|
---|
| 598 |
|
---|
| 599 | uint8_t r = spiread();
|
---|
| 600 |
|
---|
| 601 | *portOutputRegister(csport) |= cspin;
|
---|
| 602 |
|
---|
| 603 | return r;
|
---|
| 604 |
|
---|
| 605 | }
|
---|
| 606 |
|
---|
| 607 | uint8_t Adafruit_ST7735::readcommand8(uint8_t c) {
|
---|
| 608 | digitalWrite(_rs, LOW);
|
---|
| 609 |
|
---|
| 610 | *portOutputRegister(csport) &= ~ cspin;
|
---|
| 611 |
|
---|
| 612 | spiwrite(c);
|
---|
| 613 |
|
---|
| 614 | digitalWrite(_rs, HIGH);
|
---|
| 615 | pinMode(_sid, INPUT); // input!
|
---|
| 616 | digitalWrite(_sid, LOW); // low
|
---|
| 617 | spiread();
|
---|
| 618 | uint8_t r = spiread();
|
---|
| 619 |
|
---|
| 620 |
|
---|
| 621 | *portOutputRegister(csport) |= cspin;
|
---|
| 622 |
|
---|
| 623 |
|
---|
| 624 | pinMode(_sid, OUTPUT); // back to output
|
---|
| 625 | return r;
|
---|
| 626 | }
|
---|
| 627 |
|
---|
| 628 |
|
---|
| 629 | uint16_t Adafruit_ST7735::readcommand16(uint8_t c) {
|
---|
| 630 | digitalWrite(_rs, LOW);
|
---|
| 631 | if (_cs)
|
---|
| 632 | digitalWrite(_cs, LOW);
|
---|
| 633 |
|
---|
| 634 | spiwrite(c);
|
---|
| 635 | pinMode(_sid, INPUT); // input!
|
---|
| 636 | uint16_t r = spiread();
|
---|
| 637 | r <<= 8;
|
---|
| 638 | r |= spiread();
|
---|
| 639 | if (_cs)
|
---|
| 640 | digitalWrite(_cs, HIGH);
|
---|
| 641 |
|
---|
| 642 | pinMode(_sid, OUTPUT); // back to output
|
---|
| 643 | return r;
|
---|
| 644 | }
|
---|
| 645 |
|
---|
| 646 | uint32_t Adafruit_ST7735::readcommand32(uint8_t c) {
|
---|
| 647 | digitalWrite(_rs, LOW);
|
---|
| 648 | if (_cs)
|
---|
| 649 | digitalWrite(_cs, LOW);
|
---|
| 650 | spiwrite(c);
|
---|
| 651 | pinMode(_sid, INPUT); // input!
|
---|
| 652 |
|
---|
| 653 | dummyclock();
|
---|
| 654 | dummyclock();
|
---|
| 655 |
|
---|
| 656 | uint32_t r = spiread();
|
---|
| 657 | r <<= 8;
|
---|
| 658 | r |= spiread();
|
---|
| 659 | r <<= 8;
|
---|
| 660 | r |= spiread();
|
---|
| 661 | r <<= 8;
|
---|
| 662 | r |= spiread();
|
---|
| 663 | if (_cs)
|
---|
| 664 | digitalWrite(_cs, HIGH);
|
---|
| 665 |
|
---|
| 666 | pinMode(_sid, OUTPUT); // back to output
|
---|
| 667 | return r;
|
---|
| 668 | }
|
---|
| 669 |
|
---|
| 670 | */
|
---|