Port of the Adafruit_ILI9341 library for MBED. Depends on BurstSPI. fillRect, drawFastVLine, drawFastHLine are optimized to use BurstSPI fastwrites and perform a clearRX afterwards. MIT license Due to 600 character limit please find complete license/Adafruit header in license.txt Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit! http://www.adafruit.com Original written by Limor Fried/Ladyada for Adafruit Industries.
Adafruit_ILI9341.cpp@0:16926c5097c9, 2014-12-05 (annotated)
- Committer:
- infotech1
- Date:
- Fri Dec 05 06:42:42 2014 +0000
- Revision:
- 0:16926c5097c9
Initial commit.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
infotech1 | 0:16926c5097c9 | 1 | /*************************************************** |
infotech1 | 0:16926c5097c9 | 2 | This is our GFX example for the Adafruit ILI9341 Breakout and Shield |
infotech1 | 0:16926c5097c9 | 3 | ----> http://www.adafruit.com/products/1651 |
infotech1 | 0:16926c5097c9 | 4 | Check out the links above for our tutorials and wiring diagrams |
infotech1 | 0:16926c5097c9 | 5 | These displays use SPI to communicate, 4 or 5 pins are required to |
infotech1 | 0:16926c5097c9 | 6 | interface (RST is optional) |
infotech1 | 0:16926c5097c9 | 7 | Adafruit invests time and resources providing this open source code, |
infotech1 | 0:16926c5097c9 | 8 | please support Adafruit and open-source hardware by purchasing |
infotech1 | 0:16926c5097c9 | 9 | products from Adafruit! |
infotech1 | 0:16926c5097c9 | 10 | Written by Limor Fried/Ladyada for Adafruit Industries. |
infotech1 | 0:16926c5097c9 | 11 | MIT license, all text above must be included in any redistribution |
infotech1 | 0:16926c5097c9 | 12 | ****************************************************/ |
infotech1 | 0:16926c5097c9 | 13 | /* |
infotech1 | 0:16926c5097c9 | 14 | Ported to mbed by James Kidd |
infotech1 | 0:16926c5097c9 | 15 | */ |
infotech1 | 0:16926c5097c9 | 16 | #include <stdint.h> |
infotech1 | 0:16926c5097c9 | 17 | #include <Adafruit_GFX.h> |
infotech1 | 0:16926c5097c9 | 18 | #include "Adafruit_ILI9341.h" |
infotech1 | 0:16926c5097c9 | 19 | #include "mbed.h" |
infotech1 | 0:16926c5097c9 | 20 | #include "PinNames.h" |
infotech1 | 0:16926c5097c9 | 21 | #include "BurstSPI.h" |
infotech1 | 0:16926c5097c9 | 22 | |
infotech1 | 0:16926c5097c9 | 23 | Adafruit_ILI9341::Adafruit_ILI9341(PinName DC, PinName CS, PinName RST) : Adafruit_GFX(ILI9341_TFTWIDTH,ILI9341_TFTHEIGHT) { |
infotech1 | 0:16926c5097c9 | 24 | _pins.rst = RST; |
infotech1 | 0:16926c5097c9 | 25 | _pins.cs = CS; |
infotech1 | 0:16926c5097c9 | 26 | _pins.dc = DC; |
infotech1 | 0:16926c5097c9 | 27 | _pins.miso = SPI_MISO; |
infotech1 | 0:16926c5097c9 | 28 | _pins.mosi = SPI_MOSI; |
infotech1 | 0:16926c5097c9 | 29 | _pins.sclk = SPI_SCK; |
infotech1 | 0:16926c5097c9 | 30 | hwSPI = true; |
infotech1 | 0:16926c5097c9 | 31 | } |
infotech1 | 0:16926c5097c9 | 32 | |
infotech1 | 0:16926c5097c9 | 33 | |
infotech1 | 0:16926c5097c9 | 34 | inline void Adafruit_ILI9341::spiwrite(uint16_t c) { |
infotech1 | 0:16926c5097c9 | 35 | |
infotech1 | 0:16926c5097c9 | 36 | //Serial.print("0x"); Serial.print(c, HEX); Serial.print(", "); |
infotech1 | 0:16926c5097c9 | 37 | LcdSPI->write(c); |
infotech1 | 0:16926c5097c9 | 38 | //LcdSPI->fastWrite(c >> 8); |
infotech1 | 0:16926c5097c9 | 39 | //LcdSPI->fastWrite(c); |
infotech1 | 0:16926c5097c9 | 40 | } |
infotech1 | 0:16926c5097c9 | 41 | void Adafruit_ILI9341::writecommand(uint8_t c) { |
infotech1 | 0:16926c5097c9 | 42 | |
infotech1 | 0:16926c5097c9 | 43 | LcdSPI->format(8,3); |
infotech1 | 0:16926c5097c9 | 44 | LcdSPI->setFormat(); |
infotech1 | 0:16926c5097c9 | 45 | Pins[PIN_DC]->write(0); |
infotech1 | 0:16926c5097c9 | 46 | if(Pins[PIN_SCE] > 0) |
infotech1 | 0:16926c5097c9 | 47 | Pins[PIN_SCE]->write(0); |
infotech1 | 0:16926c5097c9 | 48 | LcdSPI->write(c); |
infotech1 | 0:16926c5097c9 | 49 | //spiwrite(c); |
infotech1 | 0:16926c5097c9 | 50 | if(Pins[PIN_SCE] > 0) |
infotech1 | 0:16926c5097c9 | 51 | Pins[PIN_SCE]->write(1); |
infotech1 | 0:16926c5097c9 | 52 | LcdSPI->format(16,3); |
infotech1 | 0:16926c5097c9 | 53 | LcdSPI->setFormat(); |
infotech1 | 0:16926c5097c9 | 54 | } |
infotech1 | 0:16926c5097c9 | 55 | |
infotech1 | 0:16926c5097c9 | 56 | void Adafruit_ILI9341::writedata(uint16_t c) { |
infotech1 | 0:16926c5097c9 | 57 | |
infotech1 | 0:16926c5097c9 | 58 | LcdSPI->format(8,3); |
infotech1 | 0:16926c5097c9 | 59 | LcdSPI->setFormat(); |
infotech1 | 0:16926c5097c9 | 60 | Pins[PIN_DC]->write(1); |
infotech1 | 0:16926c5097c9 | 61 | if(Pins[PIN_SCE] > 0) |
infotech1 | 0:16926c5097c9 | 62 | Pins[PIN_SCE]->write(0); |
infotech1 | 0:16926c5097c9 | 63 | LcdSPI->write(c); |
infotech1 | 0:16926c5097c9 | 64 | //spiwrite(c); |
infotech1 | 0:16926c5097c9 | 65 | if(Pins[PIN_SCE] > 0) |
infotech1 | 0:16926c5097c9 | 66 | Pins[PIN_SCE]->write(1); |
infotech1 | 0:16926c5097c9 | 67 | LcdSPI->format(16,3); |
infotech1 | 0:16926c5097c9 | 68 | LcdSPI->setFormat(); |
infotech1 | 0:16926c5097c9 | 69 | } |
infotech1 | 0:16926c5097c9 | 70 | |
infotech1 | 0:16926c5097c9 | 71 | void Adafruit_ILI9341::commandList(uint8_t *addr) { |
infotech1 | 0:16926c5097c9 | 72 | |
infotech1 | 0:16926c5097c9 | 73 | /*uint8_t numCommands, numArgs; |
infotech1 | 0:16926c5097c9 | 74 | uint16_t ms; |
infotech1 | 0:16926c5097c9 | 75 | |
infotech1 | 0:16926c5097c9 | 76 | numCommands = pgm_read_byte(addr++); // Number of commands to follow |
infotech1 | 0:16926c5097c9 | 77 | while(numCommands--) { // For each command... |
infotech1 | 0:16926c5097c9 | 78 | writecommand(pgm_read_byte(addr++)); // Read, issue command |
infotech1 | 0:16926c5097c9 | 79 | numArgs = pgm_read_byte(addr++); // Number of args to follow |
infotech1 | 0:16926c5097c9 | 80 | ms = numArgs & DELAY; // If hibit set, delay follows args |
infotech1 | 0:16926c5097c9 | 81 | numArgs &= ~DELAY; // Mask out delay bit |
infotech1 | 0:16926c5097c9 | 82 | while(numArgs--) { // For each argument... |
infotech1 | 0:16926c5097c9 | 83 | writedata(pgm_read_byte(addr++)); // Read, issue argument |
infotech1 | 0:16926c5097c9 | 84 | } |
infotech1 | 0:16926c5097c9 | 85 | |
infotech1 | 0:16926c5097c9 | 86 | if(ms) { |
infotech1 | 0:16926c5097c9 | 87 | ms = pgm_read_byte(addr++); // Read post-command delay time (ms) |
infotech1 | 0:16926c5097c9 | 88 | if(ms == 255) ms = 500; // If 255, delay for 500 ms |
infotech1 | 0:16926c5097c9 | 89 | wait_ms(ms); |
infotech1 | 0:16926c5097c9 | 90 | } |
infotech1 | 0:16926c5097c9 | 91 | }*/ |
infotech1 | 0:16926c5097c9 | 92 | } |
infotech1 | 0:16926c5097c9 | 93 | void Adafruit_ILI9341::begin(void) { |
infotech1 | 0:16926c5097c9 | 94 | LcdSPI = new BurstSPI(_pins.mosi,_pins.miso,_pins.sclk); |
infotech1 | 0:16926c5097c9 | 95 | LcdSPI->format(LCD_SPI_BITS, LCD_SPI_MODE); |
infotech1 | 0:16926c5097c9 | 96 | LcdSPI->frequency(LCD_FREQ); |
infotech1 | 0:16926c5097c9 | 97 | LcdSPI->setFormat(); |
infotech1 | 0:16926c5097c9 | 98 | Pins = new DigitalOut*[3]; |
infotech1 | 0:16926c5097c9 | 99 | Pins[PIN_RST] = new DigitalOut(_pins.rst); |
infotech1 | 0:16926c5097c9 | 100 | Pins[PIN_SCE] = new DigitalOut(_pins.cs); |
infotech1 | 0:16926c5097c9 | 101 | Pins[PIN_DC] = new DigitalOut(_pins.dc); |
infotech1 | 0:16926c5097c9 | 102 | |
infotech1 | 0:16926c5097c9 | 103 | |
infotech1 | 0:16926c5097c9 | 104 | |
infotech1 | 0:16926c5097c9 | 105 | if (Pins[PIN_RST] > 0) { |
infotech1 | 0:16926c5097c9 | 106 | Pins[PIN_RST]->write(0); |
infotech1 | 0:16926c5097c9 | 107 | } |
infotech1 | 0:16926c5097c9 | 108 | Pins[PIN_DC]->write(0); |
infotech1 | 0:16926c5097c9 | 109 | Pins[PIN_SCE]->write(0); |
infotech1 | 0:16926c5097c9 | 110 | |
infotech1 | 0:16926c5097c9 | 111 | // toggle RST low to reset |
infotech1 | 0:16926c5097c9 | 112 | if (Pins[PIN_RST] > 0) { |
infotech1 | 0:16926c5097c9 | 113 | Pins[PIN_RST]->write(1); |
infotech1 | 0:16926c5097c9 | 114 | wait_ms(5); |
infotech1 | 0:16926c5097c9 | 115 | Pins[PIN_RST]->write(0); |
infotech1 | 0:16926c5097c9 | 116 | wait_ms(20); |
infotech1 | 0:16926c5097c9 | 117 | Pins[PIN_RST]->write(1); |
infotech1 | 0:16926c5097c9 | 118 | wait_ms(150); |
infotech1 | 0:16926c5097c9 | 119 | } |
infotech1 | 0:16926c5097c9 | 120 | |
infotech1 | 0:16926c5097c9 | 121 | /* |
infotech1 | 0:16926c5097c9 | 122 | uint8_t x = readcommand8(ILI9341_RDMODE); |
infotech1 | 0:16926c5097c9 | 123 | Serial.print("\nDisplay Power Mode: 0x"); Serial.println(x, HEX); |
infotech1 | 0:16926c5097c9 | 124 | x = readcommand8(ILI9341_RDMADCTL); |
infotech1 | 0:16926c5097c9 | 125 | Serial.print("\nMADCTL Mode: 0x"); Serial.println(x, HEX); |
infotech1 | 0:16926c5097c9 | 126 | x = readcommand8(ILI9341_RDPIXFMT); |
infotech1 | 0:16926c5097c9 | 127 | Serial.print("\nPixel Format: 0x"); Serial.println(x, HEX); |
infotech1 | 0:16926c5097c9 | 128 | x = readcommand8(ILI9341_RDIMGFMT); |
infotech1 | 0:16926c5097c9 | 129 | Serial.print("\nImage Format: 0x"); Serial.println(x, HEX); |
infotech1 | 0:16926c5097c9 | 130 | x = readcommand8(ILI9341_RDSELFDIAG); |
infotech1 | 0:16926c5097c9 | 131 | Serial.print("\nSelf Diagnostic: 0x"); Serial.println(x, HEX); |
infotech1 | 0:16926c5097c9 | 132 | */ |
infotech1 | 0:16926c5097c9 | 133 | //if(cmdList) commandList(cmdList); |
infotech1 | 0:16926c5097c9 | 134 | |
infotech1 | 0:16926c5097c9 | 135 | if (hwSPI) spi_begin(); |
infotech1 | 0:16926c5097c9 | 136 | writecommand(0xEF); |
infotech1 | 0:16926c5097c9 | 137 | writedata(0x03); |
infotech1 | 0:16926c5097c9 | 138 | writedata(0x80); |
infotech1 | 0:16926c5097c9 | 139 | writedata(0x02); |
infotech1 | 0:16926c5097c9 | 140 | |
infotech1 | 0:16926c5097c9 | 141 | writecommand(0xCF); |
infotech1 | 0:16926c5097c9 | 142 | writedata(0x00); |
infotech1 | 0:16926c5097c9 | 143 | writedata(0XC1); |
infotech1 | 0:16926c5097c9 | 144 | writedata(0X30); |
infotech1 | 0:16926c5097c9 | 145 | |
infotech1 | 0:16926c5097c9 | 146 | writecommand(0xED); |
infotech1 | 0:16926c5097c9 | 147 | writedata(0x64); |
infotech1 | 0:16926c5097c9 | 148 | writedata(0x03); |
infotech1 | 0:16926c5097c9 | 149 | writedata(0X12); |
infotech1 | 0:16926c5097c9 | 150 | writedata(0X81); |
infotech1 | 0:16926c5097c9 | 151 | |
infotech1 | 0:16926c5097c9 | 152 | writecommand(0xE8); |
infotech1 | 0:16926c5097c9 | 153 | writedata(0x85); |
infotech1 | 0:16926c5097c9 | 154 | writedata(0x00); |
infotech1 | 0:16926c5097c9 | 155 | writedata(0x78); |
infotech1 | 0:16926c5097c9 | 156 | |
infotech1 | 0:16926c5097c9 | 157 | writecommand(0xCB); |
infotech1 | 0:16926c5097c9 | 158 | writedata(0x39); |
infotech1 | 0:16926c5097c9 | 159 | writedata(0x2C); |
infotech1 | 0:16926c5097c9 | 160 | writedata(0x00); |
infotech1 | 0:16926c5097c9 | 161 | writedata(0x34); |
infotech1 | 0:16926c5097c9 | 162 | writedata(0x02); |
infotech1 | 0:16926c5097c9 | 163 | |
infotech1 | 0:16926c5097c9 | 164 | writecommand(0xF7); |
infotech1 | 0:16926c5097c9 | 165 | writedata(0x20); |
infotech1 | 0:16926c5097c9 | 166 | |
infotech1 | 0:16926c5097c9 | 167 | writecommand(0xEA); |
infotech1 | 0:16926c5097c9 | 168 | writedata(0x00); |
infotech1 | 0:16926c5097c9 | 169 | writedata(0x00); |
infotech1 | 0:16926c5097c9 | 170 | |
infotech1 | 0:16926c5097c9 | 171 | writecommand(ILI9341_PWCTR1); //Power control |
infotech1 | 0:16926c5097c9 | 172 | writedata(0x23); //VRH[5:0] |
infotech1 | 0:16926c5097c9 | 173 | |
infotech1 | 0:16926c5097c9 | 174 | writecommand(ILI9341_PWCTR2); //Power control |
infotech1 | 0:16926c5097c9 | 175 | writedata(0x10); //SAP[2:0];BT[3:0] |
infotech1 | 0:16926c5097c9 | 176 | |
infotech1 | 0:16926c5097c9 | 177 | writecommand(ILI9341_VMCTR1); //VCM control |
infotech1 | 0:16926c5097c9 | 178 | writedata(0x3e); //¶Ô±È¶Èµ÷½Ú |
infotech1 | 0:16926c5097c9 | 179 | writedata(0x28); |
infotech1 | 0:16926c5097c9 | 180 | |
infotech1 | 0:16926c5097c9 | 181 | writecommand(ILI9341_VMCTR2); //VCM control2 |
infotech1 | 0:16926c5097c9 | 182 | writedata(0x86); //-- |
infotech1 | 0:16926c5097c9 | 183 | |
infotech1 | 0:16926c5097c9 | 184 | writecommand(ILI9341_MADCTL); // Memory Access Control |
infotech1 | 0:16926c5097c9 | 185 | writedata(0x48); |
infotech1 | 0:16926c5097c9 | 186 | |
infotech1 | 0:16926c5097c9 | 187 | writecommand(ILI9341_PIXFMT); |
infotech1 | 0:16926c5097c9 | 188 | writedata(0x55); |
infotech1 | 0:16926c5097c9 | 189 | |
infotech1 | 0:16926c5097c9 | 190 | writecommand(ILI9341_FRMCTR1); |
infotech1 | 0:16926c5097c9 | 191 | writedata(0x00); |
infotech1 | 0:16926c5097c9 | 192 | writedata(0x18); |
infotech1 | 0:16926c5097c9 | 193 | |
infotech1 | 0:16926c5097c9 | 194 | writecommand(ILI9341_DFUNCTR); // Display Function Control |
infotech1 | 0:16926c5097c9 | 195 | writedata(0x08); |
infotech1 | 0:16926c5097c9 | 196 | writedata(0x82); |
infotech1 | 0:16926c5097c9 | 197 | writedata(0x27); |
infotech1 | 0:16926c5097c9 | 198 | |
infotech1 | 0:16926c5097c9 | 199 | writecommand(0xF2); // 3Gamma Function Disable |
infotech1 | 0:16926c5097c9 | 200 | writedata(0x00); |
infotech1 | 0:16926c5097c9 | 201 | |
infotech1 | 0:16926c5097c9 | 202 | writecommand(ILI9341_GAMMASET); //Gamma curve selected |
infotech1 | 0:16926c5097c9 | 203 | writedata(0x01); |
infotech1 | 0:16926c5097c9 | 204 | |
infotech1 | 0:16926c5097c9 | 205 | writecommand(ILI9341_GMCTRP1); //Set Gamma |
infotech1 | 0:16926c5097c9 | 206 | writedata(0x0F); |
infotech1 | 0:16926c5097c9 | 207 | writedata(0x31); |
infotech1 | 0:16926c5097c9 | 208 | writedata(0x2B); |
infotech1 | 0:16926c5097c9 | 209 | writedata(0x0C); |
infotech1 | 0:16926c5097c9 | 210 | writedata(0x0E); |
infotech1 | 0:16926c5097c9 | 211 | writedata(0x08); |
infotech1 | 0:16926c5097c9 | 212 | writedata(0x4E); |
infotech1 | 0:16926c5097c9 | 213 | writedata(0xF1); |
infotech1 | 0:16926c5097c9 | 214 | writedata(0x37); |
infotech1 | 0:16926c5097c9 | 215 | writedata(0x07); |
infotech1 | 0:16926c5097c9 | 216 | writedata(0x10); |
infotech1 | 0:16926c5097c9 | 217 | writedata(0x03); |
infotech1 | 0:16926c5097c9 | 218 | writedata(0x0E); |
infotech1 | 0:16926c5097c9 | 219 | writedata(0x09); |
infotech1 | 0:16926c5097c9 | 220 | writedata(0x00); |
infotech1 | 0:16926c5097c9 | 221 | |
infotech1 | 0:16926c5097c9 | 222 | writecommand(ILI9341_GMCTRN1); //Set Gamma |
infotech1 | 0:16926c5097c9 | 223 | writedata(0x00); |
infotech1 | 0:16926c5097c9 | 224 | writedata(0x0E); |
infotech1 | 0:16926c5097c9 | 225 | writedata(0x14); |
infotech1 | 0:16926c5097c9 | 226 | writedata(0x03); |
infotech1 | 0:16926c5097c9 | 227 | writedata(0x11); |
infotech1 | 0:16926c5097c9 | 228 | writedata(0x07); |
infotech1 | 0:16926c5097c9 | 229 | writedata(0x31); |
infotech1 | 0:16926c5097c9 | 230 | writedata(0xC1); |
infotech1 | 0:16926c5097c9 | 231 | writedata(0x48); |
infotech1 | 0:16926c5097c9 | 232 | writedata(0x08); |
infotech1 | 0:16926c5097c9 | 233 | writedata(0x0F); |
infotech1 | 0:16926c5097c9 | 234 | writedata(0x0C); |
infotech1 | 0:16926c5097c9 | 235 | writedata(0x31); |
infotech1 | 0:16926c5097c9 | 236 | writedata(0x36); |
infotech1 | 0:16926c5097c9 | 237 | writedata(0x0F); |
infotech1 | 0:16926c5097c9 | 238 | |
infotech1 | 0:16926c5097c9 | 239 | writecommand(ILI9341_SLPOUT); //Exit Sleep |
infotech1 | 0:16926c5097c9 | 240 | if (hwSPI) spi_end(); |
infotech1 | 0:16926c5097c9 | 241 | wait_ms(120); |
infotech1 | 0:16926c5097c9 | 242 | if (hwSPI) spi_begin(); |
infotech1 | 0:16926c5097c9 | 243 | writecommand(ILI9341_DISPON); //Display on |
infotech1 | 0:16926c5097c9 | 244 | if (hwSPI) spi_end(); |
infotech1 | 0:16926c5097c9 | 245 | |
infotech1 | 0:16926c5097c9 | 246 | } |
infotech1 | 0:16926c5097c9 | 247 | |
infotech1 | 0:16926c5097c9 | 248 | |
infotech1 | 0:16926c5097c9 | 249 | void Adafruit_ILI9341::setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, |
infotech1 | 0:16926c5097c9 | 250 | uint16_t y1) { |
infotech1 | 0:16926c5097c9 | 251 | writecommand(ILI9341_CASET); // Column addr set |
infotech1 | 0:16926c5097c9 | 252 | writedata(x0 >> 8); |
infotech1 | 0:16926c5097c9 | 253 | writedata(x0 & 0xFF); // XSTART |
infotech1 | 0:16926c5097c9 | 254 | writedata(x1 >> 8); |
infotech1 | 0:16926c5097c9 | 255 | writedata(x1 & 0xFF); // XEND |
infotech1 | 0:16926c5097c9 | 256 | |
infotech1 | 0:16926c5097c9 | 257 | writecommand(ILI9341_PASET); // Row addr set |
infotech1 | 0:16926c5097c9 | 258 | writedata(y0>>8); |
infotech1 | 0:16926c5097c9 | 259 | writedata(y0); // YSTART |
infotech1 | 0:16926c5097c9 | 260 | writedata(y1>>8); |
infotech1 | 0:16926c5097c9 | 261 | writedata(y1); // YEND |
infotech1 | 0:16926c5097c9 | 262 | |
infotech1 | 0:16926c5097c9 | 263 | writecommand(ILI9341_RAMWR); // write to RAM |
infotech1 | 0:16926c5097c9 | 264 | } |
infotech1 | 0:16926c5097c9 | 265 | |
infotech1 | 0:16926c5097c9 | 266 | |
infotech1 | 0:16926c5097c9 | 267 | void Adafruit_ILI9341::pushColor(uint16_t color) { |
infotech1 | 0:16926c5097c9 | 268 | if (hwSPI) spi_begin(); |
infotech1 | 0:16926c5097c9 | 269 | Pins[PIN_DC]->write(1); |
infotech1 | 0:16926c5097c9 | 270 | Pins[PIN_SCE]->write(0); |
infotech1 | 0:16926c5097c9 | 271 | //digitalWrite(_dc, HIGH); |
infotech1 | 0:16926c5097c9 | 272 | //*dcport |= dcpinmask; |
infotech1 | 0:16926c5097c9 | 273 | //digitalWrite(_cs, LOW); |
infotech1 | 0:16926c5097c9 | 274 | //*csport &= ~cspinmask; |
infotech1 | 0:16926c5097c9 | 275 | |
infotech1 | 0:16926c5097c9 | 276 | //spiwrite(color >> 8); |
infotech1 | 0:16926c5097c9 | 277 | spiwrite(color); |
infotech1 | 0:16926c5097c9 | 278 | Pins[PIN_SCE]->write(1); |
infotech1 | 0:16926c5097c9 | 279 | //*csport |= cspinmask; |
infotech1 | 0:16926c5097c9 | 280 | //digitalWrite(_cs, HIGH); |
infotech1 | 0:16926c5097c9 | 281 | if (hwSPI) spi_end(); |
infotech1 | 0:16926c5097c9 | 282 | } |
infotech1 | 0:16926c5097c9 | 283 | uint16_t buf[25][33]; |
infotech1 | 0:16926c5097c9 | 284 | void Adafruit_ILI9341::drawPixel(int16_t x, int16_t y, uint16_t color) { |
infotech1 | 0:16926c5097c9 | 285 | |
infotech1 | 0:16926c5097c9 | 286 | if((x < 0) ||(x >= _width) || (y < 0) || (y >= _height)) return; |
infotech1 | 0:16926c5097c9 | 287 | |
infotech1 | 0:16926c5097c9 | 288 | if (hwSPI) spi_begin(); |
infotech1 | 0:16926c5097c9 | 289 | setAddrWindow(x,y,x+1,y+1); |
infotech1 | 0:16926c5097c9 | 290 | |
infotech1 | 0:16926c5097c9 | 291 | //digitalWrite(_dc, HIGH); |
infotech1 | 0:16926c5097c9 | 292 | Pins[PIN_DC]->write(1); |
infotech1 | 0:16926c5097c9 | 293 | Pins[PIN_SCE]->write(0); |
infotech1 | 0:16926c5097c9 | 294 | |
infotech1 | 0:16926c5097c9 | 295 | //*dcport |= dcpinmask; |
infotech1 | 0:16926c5097c9 | 296 | //digitalWrite(_cs, LOW); |
infotech1 | 0:16926c5097c9 | 297 | //*csport &= ~cspinmask; |
infotech1 | 0:16926c5097c9 | 298 | |
infotech1 | 0:16926c5097c9 | 299 | //spiwrite(color >> 8); |
infotech1 | 0:16926c5097c9 | 300 | spiwrite(color); |
infotech1 | 0:16926c5097c9 | 301 | //LcdSPI->fastWrite(color); |
infotech1 | 0:16926c5097c9 | 302 | Pins[PIN_SCE]->write(1); |
infotech1 | 0:16926c5097c9 | 303 | //*csport |= cspinmask; |
infotech1 | 0:16926c5097c9 | 304 | //digitalWrite(_cs, HIGH); |
infotech1 | 0:16926c5097c9 | 305 | if (hwSPI) spi_end(); |
infotech1 | 0:16926c5097c9 | 306 | } |
infotech1 | 0:16926c5097c9 | 307 | |
infotech1 | 0:16926c5097c9 | 308 | |
infotech1 | 0:16926c5097c9 | 309 | void Adafruit_ILI9341::drawFastVLine(int16_t x, int16_t y, int16_t h, |
infotech1 | 0:16926c5097c9 | 310 | uint16_t color) { |
infotech1 | 0:16926c5097c9 | 311 | |
infotech1 | 0:16926c5097c9 | 312 | // Rudimentary clipping |
infotech1 | 0:16926c5097c9 | 313 | if((x >= _width) || (y >= _height)) return; |
infotech1 | 0:16926c5097c9 | 314 | |
infotech1 | 0:16926c5097c9 | 315 | if((y+h-1) >= _height) |
infotech1 | 0:16926c5097c9 | 316 | h = _height-y; |
infotech1 | 0:16926c5097c9 | 317 | |
infotech1 | 0:16926c5097c9 | 318 | if (hwSPI) spi_begin(); |
infotech1 | 0:16926c5097c9 | 319 | setAddrWindow(x, y, x, y+h-1); |
infotech1 | 0:16926c5097c9 | 320 | |
infotech1 | 0:16926c5097c9 | 321 | uint8_t hi = color >> 8, lo = color; |
infotech1 | 0:16926c5097c9 | 322 | |
infotech1 | 0:16926c5097c9 | 323 | //*dcport |= dcpinmask; |
infotech1 | 0:16926c5097c9 | 324 | //digitalWrite(_dc, HIGH); |
infotech1 | 0:16926c5097c9 | 325 | //*csport &= ~cspinmask; |
infotech1 | 0:16926c5097c9 | 326 | //digitalWrite(_cs, LOW); |
infotech1 | 0:16926c5097c9 | 327 | Pins[PIN_DC]->write(1); |
infotech1 | 0:16926c5097c9 | 328 | Pins[PIN_SCE]->write(0); |
infotech1 | 0:16926c5097c9 | 329 | |
infotech1 | 0:16926c5097c9 | 330 | while (h--) { |
infotech1 | 0:16926c5097c9 | 331 | LcdSPI->fastWrite(color); |
infotech1 | 0:16926c5097c9 | 332 | //spiwrite(color); |
infotech1 | 0:16926c5097c9 | 333 | //spiwrite(hi); |
infotech1 | 0:16926c5097c9 | 334 | //spiwrite(lo); |
infotech1 | 0:16926c5097c9 | 335 | } |
infotech1 | 0:16926c5097c9 | 336 | Pins[PIN_SCE]->write(1); |
infotech1 | 0:16926c5097c9 | 337 | //*csport |= cspinmask; |
infotech1 | 0:16926c5097c9 | 338 | //digitalWrite(_cs, HIGH); |
infotech1 | 0:16926c5097c9 | 339 | if (hwSPI) spi_end(); |
infotech1 | 0:16926c5097c9 | 340 | LcdSPI->clearRX(); |
infotech1 | 0:16926c5097c9 | 341 | } |
infotech1 | 0:16926c5097c9 | 342 | |
infotech1 | 0:16926c5097c9 | 343 | |
infotech1 | 0:16926c5097c9 | 344 | void Adafruit_ILI9341::drawFastHLine(int16_t x, int16_t y, int16_t w, |
infotech1 | 0:16926c5097c9 | 345 | uint16_t color) { |
infotech1 | 0:16926c5097c9 | 346 | |
infotech1 | 0:16926c5097c9 | 347 | // Rudimentary clipping |
infotech1 | 0:16926c5097c9 | 348 | if((x >= _width) || (y >= _height)) return; |
infotech1 | 0:16926c5097c9 | 349 | if((x+w-1) >= _width) w = _width-x; |
infotech1 | 0:16926c5097c9 | 350 | if (hwSPI) spi_begin(); |
infotech1 | 0:16926c5097c9 | 351 | setAddrWindow(x, y, x+w-1, y); |
infotech1 | 0:16926c5097c9 | 352 | |
infotech1 | 0:16926c5097c9 | 353 | uint8_t hi = color >> 8, lo = color; |
infotech1 | 0:16926c5097c9 | 354 | //*dcport |= dcpinmask; |
infotech1 | 0:16926c5097c9 | 355 | //*csport &= ~cspinmask; |
infotech1 | 0:16926c5097c9 | 356 | Pins[PIN_DC]->write(1); |
infotech1 | 0:16926c5097c9 | 357 | Pins[PIN_SCE]->write(0); |
infotech1 | 0:16926c5097c9 | 358 | //digitalWrite(_dc, HIGH); |
infotech1 | 0:16926c5097c9 | 359 | //digitalWrite(_cs, LOW); |
infotech1 | 0:16926c5097c9 | 360 | while (w--) { |
infotech1 | 0:16926c5097c9 | 361 | //spiwrite(color); |
infotech1 | 0:16926c5097c9 | 362 | LcdSPI->fastWrite(color); |
infotech1 | 0:16926c5097c9 | 363 | //spiwrite(hi); |
infotech1 | 0:16926c5097c9 | 364 | //spiwrite(lo); |
infotech1 | 0:16926c5097c9 | 365 | } |
infotech1 | 0:16926c5097c9 | 366 | Pins[PIN_SCE]->write(1); |
infotech1 | 0:16926c5097c9 | 367 | //*csport |= cspinmask; |
infotech1 | 0:16926c5097c9 | 368 | //digitalWrite(_cs, HIGH); |
infotech1 | 0:16926c5097c9 | 369 | if (hwSPI) spi_end(); |
infotech1 | 0:16926c5097c9 | 370 | LcdSPI->clearRX(); |
infotech1 | 0:16926c5097c9 | 371 | } |
infotech1 | 0:16926c5097c9 | 372 | |
infotech1 | 0:16926c5097c9 | 373 | void Adafruit_ILI9341::fillScreen(uint16_t color) { |
infotech1 | 0:16926c5097c9 | 374 | fillRect(0, 0, _width, _height, color); |
infotech1 | 0:16926c5097c9 | 375 | } |
infotech1 | 0:16926c5097c9 | 376 | |
infotech1 | 0:16926c5097c9 | 377 | |
infotech1 | 0:16926c5097c9 | 378 | |
infotech1 | 0:16926c5097c9 | 379 | // fill a rectangle |
infotech1 | 0:16926c5097c9 | 380 | void Adafruit_ILI9341::fillRect(int16_t x, int16_t y, int16_t w, int16_t h, |
infotech1 | 0:16926c5097c9 | 381 | uint16_t color) { |
infotech1 | 0:16926c5097c9 | 382 | |
infotech1 | 0:16926c5097c9 | 383 | // rudimentary clipping (drawChar w/big text requires this) |
infotech1 | 0:16926c5097c9 | 384 | if((x >= _width) || (y >= _height)) return; |
infotech1 | 0:16926c5097c9 | 385 | if((x + w - 1) >= _width) w = _width - x; |
infotech1 | 0:16926c5097c9 | 386 | if((y + h - 1) >= _height) h = _height - y; |
infotech1 | 0:16926c5097c9 | 387 | |
infotech1 | 0:16926c5097c9 | 388 | if (hwSPI) spi_begin(); |
infotech1 | 0:16926c5097c9 | 389 | setAddrWindow(x, y, x+w-1, y+h-1); |
infotech1 | 0:16926c5097c9 | 390 | |
infotech1 | 0:16926c5097c9 | 391 | uint8_t hi = color >> 8, lo = color; |
infotech1 | 0:16926c5097c9 | 392 | //LcdSPI->format(16, LCD_SPI_MODE); |
infotech1 | 0:16926c5097c9 | 393 | //LcdSPI->setFormat(); |
infotech1 | 0:16926c5097c9 | 394 | //*dcport |= dcpinmask; |
infotech1 | 0:16926c5097c9 | 395 | //digitalWrite(_dc, HIGH); |
infotech1 | 0:16926c5097c9 | 396 | //*csport &= ~cspinmask; |
infotech1 | 0:16926c5097c9 | 397 | //digitalWrite(_cs, LOW); |
infotech1 | 0:16926c5097c9 | 398 | Pins[PIN_DC]->write(1); |
infotech1 | 0:16926c5097c9 | 399 | Pins[PIN_SCE]->write(0); |
infotech1 | 0:16926c5097c9 | 400 | int32_t cnt; |
infotech1 | 0:16926c5097c9 | 401 | for(cnt = w*h;cnt > 0;cnt--){ |
infotech1 | 0:16926c5097c9 | 402 | LcdSPI->fastWrite(color); |
infotech1 | 0:16926c5097c9 | 403 | //spiwrite(hi); |
infotech1 | 0:16926c5097c9 | 404 | //spiwrite(color); |
infotech1 | 0:16926c5097c9 | 405 | //spiwrite(color); |
infotech1 | 0:16926c5097c9 | 406 | // LcdSPI->clearRX(); |
infotech1 | 0:16926c5097c9 | 407 | } |
infotech1 | 0:16926c5097c9 | 408 | /*for(y=h; y>0; y--) { |
infotech1 | 0:16926c5097c9 | 409 | for(x=w; x>0; x--) { |
infotech1 | 0:16926c5097c9 | 410 | //LcdSPI->fastWrite(color); |
infotech1 | 0:16926c5097c9 | 411 | //LcdSPI->write(hi); |
infotech1 | 0:16926c5097c9 | 412 | //LcdSPI->write(lo); |
infotech1 | 0:16926c5097c9 | 413 | spiwrite(hi); |
infotech1 | 0:16926c5097c9 | 414 | spiwrite(lo); |
infotech1 | 0:16926c5097c9 | 415 | } |
infotech1 | 0:16926c5097c9 | 416 | }*/ |
infotech1 | 0:16926c5097c9 | 417 | |
infotech1 | 0:16926c5097c9 | 418 | //digitalWrite(_cs, HIGH); |
infotech1 | 0:16926c5097c9 | 419 | Pins[PIN_SCE]->write(1); |
infotech1 | 0:16926c5097c9 | 420 | //*csport |= cspinmask; |
infotech1 | 0:16926c5097c9 | 421 | if (hwSPI) spi_end(); |
infotech1 | 0:16926c5097c9 | 422 | |
infotech1 | 0:16926c5097c9 | 423 | //wait_ms(30); |
infotech1 | 0:16926c5097c9 | 424 | LcdSPI->clearRX(); |
infotech1 | 0:16926c5097c9 | 425 | //LcdSPI->format(LCD_SPI_BITS, LCD_SPI_MODE); |
infotech1 | 0:16926c5097c9 | 426 | //LcdSPI->setFormat(); |
infotech1 | 0:16926c5097c9 | 427 | } |
infotech1 | 0:16926c5097c9 | 428 | |
infotech1 | 0:16926c5097c9 | 429 | |
infotech1 | 0:16926c5097c9 | 430 | // Pass 8-bit (each) R,G,B, get back 16-bit packed color |
infotech1 | 0:16926c5097c9 | 431 | uint16_t Adafruit_ILI9341::color565(uint8_t r, uint8_t g, uint8_t b) { |
infotech1 | 0:16926c5097c9 | 432 | return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3); |
infotech1 | 0:16926c5097c9 | 433 | } |
infotech1 | 0:16926c5097c9 | 434 | |
infotech1 | 0:16926c5097c9 | 435 | |
infotech1 | 0:16926c5097c9 | 436 | #define MADCTL_MY 0x80 |
infotech1 | 0:16926c5097c9 | 437 | #define MADCTL_MX 0x40 |
infotech1 | 0:16926c5097c9 | 438 | #define MADCTL_MV 0x20 |
infotech1 | 0:16926c5097c9 | 439 | #define MADCTL_ML 0x10 |
infotech1 | 0:16926c5097c9 | 440 | #define MADCTL_RGB 0x00 |
infotech1 | 0:16926c5097c9 | 441 | #define MADCTL_BGR 0x08 |
infotech1 | 0:16926c5097c9 | 442 | #define MADCTL_MH 0x04 |
infotech1 | 0:16926c5097c9 | 443 | |
infotech1 | 0:16926c5097c9 | 444 | void Adafruit_ILI9341::setRotation(uint8_t m) { |
infotech1 | 0:16926c5097c9 | 445 | |
infotech1 | 0:16926c5097c9 | 446 | if (hwSPI) spi_begin(); |
infotech1 | 0:16926c5097c9 | 447 | writecommand(ILI9341_MADCTL); |
infotech1 | 0:16926c5097c9 | 448 | rotation = m % 4; // can't be higher than 3 |
infotech1 | 0:16926c5097c9 | 449 | switch (rotation) { |
infotech1 | 0:16926c5097c9 | 450 | case 0: |
infotech1 | 0:16926c5097c9 | 451 | writedata(MADCTL_MX | MADCTL_BGR); |
infotech1 | 0:16926c5097c9 | 452 | _width = ILI9341_TFTWIDTH; |
infotech1 | 0:16926c5097c9 | 453 | _height = ILI9341_TFTHEIGHT; |
infotech1 | 0:16926c5097c9 | 454 | break; |
infotech1 | 0:16926c5097c9 | 455 | case 1: |
infotech1 | 0:16926c5097c9 | 456 | writedata(MADCTL_MV | MADCTL_BGR); |
infotech1 | 0:16926c5097c9 | 457 | _width = ILI9341_TFTHEIGHT; |
infotech1 | 0:16926c5097c9 | 458 | _height = ILI9341_TFTWIDTH; |
infotech1 | 0:16926c5097c9 | 459 | break; |
infotech1 | 0:16926c5097c9 | 460 | case 2: |
infotech1 | 0:16926c5097c9 | 461 | writedata(MADCTL_MY | MADCTL_BGR); |
infotech1 | 0:16926c5097c9 | 462 | _width = ILI9341_TFTWIDTH; |
infotech1 | 0:16926c5097c9 | 463 | _height = ILI9341_TFTHEIGHT; |
infotech1 | 0:16926c5097c9 | 464 | break; |
infotech1 | 0:16926c5097c9 | 465 | case 3: |
infotech1 | 0:16926c5097c9 | 466 | writedata(MADCTL_MX | MADCTL_MY | MADCTL_MV | MADCTL_BGR); |
infotech1 | 0:16926c5097c9 | 467 | _width = ILI9341_TFTHEIGHT; |
infotech1 | 0:16926c5097c9 | 468 | _height = ILI9341_TFTWIDTH; |
infotech1 | 0:16926c5097c9 | 469 | break; |
infotech1 | 0:16926c5097c9 | 470 | } |
infotech1 | 0:16926c5097c9 | 471 | if (hwSPI) spi_end(); |
infotech1 | 0:16926c5097c9 | 472 | } |
infotech1 | 0:16926c5097c9 | 473 | |
infotech1 | 0:16926c5097c9 | 474 | |
infotech1 | 0:16926c5097c9 | 475 | void Adafruit_ILI9341::invertDisplay(bool i) { |
infotech1 | 0:16926c5097c9 | 476 | if (hwSPI) spi_begin(); |
infotech1 | 0:16926c5097c9 | 477 | writecommand(i ? ILI9341_INVON : ILI9341_INVOFF); |
infotech1 | 0:16926c5097c9 | 478 | if (hwSPI) spi_end(); |
infotech1 | 0:16926c5097c9 | 479 | } |
infotech1 | 0:16926c5097c9 | 480 | |
infotech1 | 0:16926c5097c9 | 481 | |
infotech1 | 0:16926c5097c9 | 482 | |
infotech1 | 0:16926c5097c9 | 483 | uint8_t Adafruit_ILI9341::spiread(void) { |
infotech1 | 0:16926c5097c9 | 484 | //LcdSPI->clearRX(); |
infotech1 | 0:16926c5097c9 | 485 | uint8_t r = 0; |
infotech1 | 0:16926c5097c9 | 486 | r = LcdSPI->write(0); |
infotech1 | 0:16926c5097c9 | 487 | return r; |
infotech1 | 0:16926c5097c9 | 488 | |
infotech1 | 0:16926c5097c9 | 489 | } |
infotech1 | 0:16926c5097c9 | 490 | |
infotech1 | 0:16926c5097c9 | 491 | |
infotech1 | 0:16926c5097c9 | 492 | |
infotech1 | 0:16926c5097c9 | 493 | uint8_t Adafruit_ILI9341::readdata(void) { |
infotech1 | 0:16926c5097c9 | 494 | Pins[PIN_DC]->write(1); |
infotech1 | 0:16926c5097c9 | 495 | Pins[PIN_SCE]->write(0); |
infotech1 | 0:16926c5097c9 | 496 | //digitalWrite(_dc, HIGH); |
infotech1 | 0:16926c5097c9 | 497 | //digitalWrite(_cs, LOW); |
infotech1 | 0:16926c5097c9 | 498 | uint8_t r = spiread(); |
infotech1 | 0:16926c5097c9 | 499 | //digitalWrite(_cs, HIGH); |
infotech1 | 0:16926c5097c9 | 500 | Pins[PIN_SCE]->write(1); |
infotech1 | 0:16926c5097c9 | 501 | return r; |
infotech1 | 0:16926c5097c9 | 502 | } |
infotech1 | 0:16926c5097c9 | 503 | |
infotech1 | 0:16926c5097c9 | 504 | |
infotech1 | 0:16926c5097c9 | 505 | uint8_t Adafruit_ILI9341::readcommand8(uint8_t c, uint8_t index) { |
infotech1 | 0:16926c5097c9 | 506 | if (hwSPI) spi_begin(); |
infotech1 | 0:16926c5097c9 | 507 | LcdSPI->format(8, LCD_SPI_MODE); |
infotech1 | 0:16926c5097c9 | 508 | |
infotech1 | 0:16926c5097c9 | 509 | Pins[PIN_DC]->write(0); |
infotech1 | 0:16926c5097c9 | 510 | Pins[PIN_SCE]->write(0); |
infotech1 | 0:16926c5097c9 | 511 | //digitalWrite(_dc, LOW); // command |
infotech1 | 0:16926c5097c9 | 512 | //digitalWrite(_cs, LOW); |
infotech1 | 0:16926c5097c9 | 513 | spiwrite(0xD9); // woo sekret command? |
infotech1 | 0:16926c5097c9 | 514 | Pins[PIN_DC]->write(1); |
infotech1 | 0:16926c5097c9 | 515 | //digitalWrite(_dc, HIGH); // data |
infotech1 | 0:16926c5097c9 | 516 | spiwrite(0x10 + index); |
infotech1 | 0:16926c5097c9 | 517 | //digitalWrite(_cs, HIGH); |
infotech1 | 0:16926c5097c9 | 518 | Pins[PIN_SCE]->write(1); |
infotech1 | 0:16926c5097c9 | 519 | |
infotech1 | 0:16926c5097c9 | 520 | Pins[PIN_DC]->write(0); |
infotech1 | 0:16926c5097c9 | 521 | Pins[PIN_SCE]->write(0); |
infotech1 | 0:16926c5097c9 | 522 | //digitalWrite(_dc, LOW); |
infotech1 | 0:16926c5097c9 | 523 | //digitalWrite(_sclk, LOW); |
infotech1 | 0:16926c5097c9 | 524 | //digitalWrite(_cs, LOW); |
infotech1 | 0:16926c5097c9 | 525 | spiwrite(c); |
infotech1 | 0:16926c5097c9 | 526 | Pins[PIN_DC]->write(1); |
infotech1 | 0:16926c5097c9 | 527 | //digitalWrite(_dc, HIGH); |
infotech1 | 0:16926c5097c9 | 528 | uint8_t r = spiread(); |
infotech1 | 0:16926c5097c9 | 529 | Pins[PIN_SCE]->write(1); |
infotech1 | 0:16926c5097c9 | 530 | //digitalWrite(_cs, HIGH); |
infotech1 | 0:16926c5097c9 | 531 | if (hwSPI) spi_end(); |
infotech1 | 0:16926c5097c9 | 532 | |
infotech1 | 0:16926c5097c9 | 533 | LcdSPI->format(LCD_SPI_BITS, LCD_SPI_MODE); |
infotech1 | 0:16926c5097c9 | 534 | return r; |
infotech1 | 0:16926c5097c9 | 535 | } |