A first port of the excellent Adafruit ST7735 library Also supports the S6D02A1 controller (Use 1k resistors in series on all logic lines).

Fork of Adafruit_ST7735 by Andrew Lindsay

Modified version of the ST7735 library to support the Samsung S6D02A1 controller. These 1.8" 128x160 displays can be purchased dirt cheap (under $5 on eBay or aliexpress with free shipping).

You must also import the Adafruit_GFX library, as this library is dependent upon it.

I've used up to 20 MHz SPI with no problem. That is hardcoded in the library, and is currently set to 4 MHz (unmodified from the original library).

The library is exactly the same for S6D02A1 and ST7735 except for the initialization routines.

Use 1k resistors in series on all 5 logic lines as 3V3 is required (display is not damaged by 5V logic, but it will not work)

Constructor pin names that match the S6D02A1: Adafruit_ST7735(DIN, NC, CLK, CD, D/C, RST) ;

Source of initialization codes for the S6D02A1: https://github.com/zigwart/Adafruit_QDTech/blob/master/Adafruit_QDTech.cpp

Committer:
daneast
Date:
Wed Nov 19 05:10:57 2014 +0000
Revision:
3:6c3a23e87409
Parent:
2:18ecda534e06
Add support for S6D02A1 controller.; Works fine with 20 MHz SPI.; Use 1k resistors in series on all logic lines.

Who changed what in which revision?

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