Library for Adafruit ST7735, for use with the NCS36510 mbed board

Dependents:   mbed-TFT-example-NCS36510

Fork of Adafruit_ST7735 by Jacob Johnson

Committer:
jacobjohnson
Date:
Thu Feb 23 16:30:19 2017 +0000
Revision:
8:196b625fe857
Parent:
7:5842518d1feb
working library ;

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"
jacobjohnson 7:5842518d1feb 21
jacobjohnson 7:5842518d1feb 22 //#include <string>
jacobjohnson 7:5842518d1feb 23
jacobjohnson 7:5842518d1feb 24
SomeRandomBloke 0:99ee879ef5a9 25
SomeRandomBloke 0:99ee879ef5a9 26 // Constructor
SomeRandomBloke 0:99ee879ef5a9 27 Adafruit_ST7735::Adafruit_ST7735(PinName mosi, PinName miso, PinName sck, PinName cs, PinName rs, PinName rst)
SomeRandomBloke 1:226fd3534967 28 : lcdPort(mosi, miso, sck), _cs(cs), _rs(rs), _rst(rst), Adafruit_GFX(ST7735_TFTWIDTH, ST7735_TFTHEIGHT)
SomeRandomBloke 0:99ee879ef5a9 29 { }
SomeRandomBloke 0:99ee879ef5a9 30
justinkim 4:80075830f886 31 Serial pc(USBTX, USBRX);
SomeRandomBloke 0:99ee879ef5a9 32
SomeRandomBloke 0:99ee879ef5a9 33 void Adafruit_ST7735::writecommand(uint8_t c)
SomeRandomBloke 0:99ee879ef5a9 34 {
SomeRandomBloke 0:99ee879ef5a9 35 _rs = 0;
SomeRandomBloke 0:99ee879ef5a9 36 _cs = 0;
SomeRandomBloke 0:99ee879ef5a9 37 lcdPort.write( c );
SomeRandomBloke 0:99ee879ef5a9 38 _cs = 1;
SomeRandomBloke 0:99ee879ef5a9 39 }
SomeRandomBloke 0:99ee879ef5a9 40
SomeRandomBloke 0:99ee879ef5a9 41
SomeRandomBloke 0:99ee879ef5a9 42 void Adafruit_ST7735::writedata(uint8_t c)
SomeRandomBloke 0:99ee879ef5a9 43 {
SomeRandomBloke 0:99ee879ef5a9 44 _rs = 1;
SomeRandomBloke 0:99ee879ef5a9 45 _cs = 0;
SomeRandomBloke 0:99ee879ef5a9 46 lcdPort.write( c );
SomeRandomBloke 0:99ee879ef5a9 47
SomeRandomBloke 0:99ee879ef5a9 48 _cs = 1;
SomeRandomBloke 0:99ee879ef5a9 49 }
SomeRandomBloke 0:99ee879ef5a9 50
SomeRandomBloke 0:99ee879ef5a9 51
SomeRandomBloke 0:99ee879ef5a9 52 // Rather than a bazillion writecommand() and writedata() calls, screen
SomeRandomBloke 0:99ee879ef5a9 53 // initialization commands and arguments are organized in these tables
SomeRandomBloke 0:99ee879ef5a9 54 // stored in PROGMEM. The table may look bulky, but that's mostly the
SomeRandomBloke 0:99ee879ef5a9 55 // formatting -- storage-wise this is hundreds of bytes more compact
SomeRandomBloke 0:99ee879ef5a9 56 // than the equivalent code. Companion function follows.
SomeRandomBloke 0:99ee879ef5a9 57 #define DELAY 0x80
SomeRandomBloke 0:99ee879ef5a9 58 static unsigned char
SomeRandomBloke 0:99ee879ef5a9 59 Bcmd[] = { // Initialization commands for 7735B screens
SomeRandomBloke 0:99ee879ef5a9 60 18, // 18 commands in list:
SomeRandomBloke 0:99ee879ef5a9 61 ST7735_SWRESET, DELAY, // 1: Software reset, no args, w/delay
SomeRandomBloke 0:99ee879ef5a9 62 50, // 50 ms delay
SomeRandomBloke 0:99ee879ef5a9 63 ST7735_SLPOUT , DELAY, // 2: Out of sleep mode, no args, w/delay
SomeRandomBloke 0:99ee879ef5a9 64 255, // 255 = 500 ms delay
SomeRandomBloke 0:99ee879ef5a9 65 ST7735_COLMOD , 1+DELAY, // 3: Set color mode, 1 arg + delay:
SomeRandomBloke 0:99ee879ef5a9 66 0x05, // 16-bit color
SomeRandomBloke 0:99ee879ef5a9 67 10, // 10 ms delay
SomeRandomBloke 0:99ee879ef5a9 68 ST7735_FRMCTR1, 3+DELAY, // 4: Frame rate control, 3 args + delay:
SomeRandomBloke 0:99ee879ef5a9 69 0x00, // fastest refresh
SomeRandomBloke 0:99ee879ef5a9 70 0x06, // 6 lines front porch
SomeRandomBloke 0:99ee879ef5a9 71 0x03, // 3 lines back porch
SomeRandomBloke 0:99ee879ef5a9 72 10, // 10 ms delay
SomeRandomBloke 0:99ee879ef5a9 73 ST7735_MADCTL , 1 , // 5: Memory access ctrl (directions), 1 arg:
SomeRandomBloke 0:99ee879ef5a9 74 0x08, // Row addr/col addr, bottom to top refresh
SomeRandomBloke 0:99ee879ef5a9 75 ST7735_DISSET5, 2 , // 6: Display settings #5, 2 args, no delay:
SomeRandomBloke 0:99ee879ef5a9 76 0x15, // 1 clk cycle nonoverlap, 2 cycle gate
SomeRandomBloke 0:99ee879ef5a9 77 // rise, 3 cycle osc equalize
SomeRandomBloke 0:99ee879ef5a9 78 0x02, // Fix on VTL
SomeRandomBloke 0:99ee879ef5a9 79 ST7735_INVCTR , 1 , // 7: Display inversion control, 1 arg:
SomeRandomBloke 0:99ee879ef5a9 80 0x0, // Line inversion
SomeRandomBloke 0:99ee879ef5a9 81 ST7735_PWCTR1 , 2+DELAY, // 8: Power control, 2 args + delay:
SomeRandomBloke 0:99ee879ef5a9 82 0x02, // GVDD = 4.7V
SomeRandomBloke 0:99ee879ef5a9 83 0x70, // 1.0uA
SomeRandomBloke 0:99ee879ef5a9 84 10, // 10 ms delay
SomeRandomBloke 0:99ee879ef5a9 85 ST7735_PWCTR2 , 1 , // 9: Power control, 1 arg, no delay:
SomeRandomBloke 0:99ee879ef5a9 86 0x05, // VGH = 14.7V, VGL = -7.35V
SomeRandomBloke 0:99ee879ef5a9 87 ST7735_PWCTR3 , 2 , // 10: Power control, 2 args, no delay:
SomeRandomBloke 0:99ee879ef5a9 88 0x01, // Opamp current small
SomeRandomBloke 0:99ee879ef5a9 89 0x02, // Boost frequency
SomeRandomBloke 0:99ee879ef5a9 90 ST7735_VMCTR1 , 2+DELAY, // 11: Power control, 2 args + delay:
SomeRandomBloke 0:99ee879ef5a9 91 0x3C, // VCOMH = 4V
SomeRandomBloke 0:99ee879ef5a9 92 0x38, // VCOML = -1.1V
SomeRandomBloke 0:99ee879ef5a9 93 10, // 10 ms delay
SomeRandomBloke 0:99ee879ef5a9 94 ST7735_PWCTR6 , 2 , // 12: Power control, 2 args, no delay:
SomeRandomBloke 0:99ee879ef5a9 95 0x11, 0x15,
SomeRandomBloke 0:99ee879ef5a9 96 ST7735_GMCTRP1,16 , // 13: Magical unicorn dust, 16 args, no delay:
SomeRandomBloke 0:99ee879ef5a9 97 0x09, 0x16, 0x09, 0x20, // (seriously though, not sure what
SomeRandomBloke 0:99ee879ef5a9 98 0x21, 0x1B, 0x13, 0x19, // these config values represent)
SomeRandomBloke 0:99ee879ef5a9 99 0x17, 0x15, 0x1E, 0x2B,
SomeRandomBloke 0:99ee879ef5a9 100 0x04, 0x05, 0x02, 0x0E,
SomeRandomBloke 0:99ee879ef5a9 101 ST7735_GMCTRN1,16+DELAY, // 14: Sparkles and rainbows, 16 args + delay:
SomeRandomBloke 0:99ee879ef5a9 102 0x0B, 0x14, 0x08, 0x1E, // (ditto)
SomeRandomBloke 0:99ee879ef5a9 103 0x22, 0x1D, 0x18, 0x1E,
SomeRandomBloke 0:99ee879ef5a9 104 0x1B, 0x1A, 0x24, 0x2B,
SomeRandomBloke 0:99ee879ef5a9 105 0x06, 0x06, 0x02, 0x0F,
SomeRandomBloke 0:99ee879ef5a9 106 10, // 10 ms delay
SomeRandomBloke 0:99ee879ef5a9 107 ST7735_CASET , 4 , // 15: Column addr set, 4 args, no delay:
SomeRandomBloke 0:99ee879ef5a9 108 0x00, 0x02, // XSTART = 2
SomeRandomBloke 0:99ee879ef5a9 109 0x00, 0x81, // XEND = 129
SomeRandomBloke 0:99ee879ef5a9 110 ST7735_RASET , 4 , // 16: Row addr set, 4 args, no delay:
SomeRandomBloke 0:99ee879ef5a9 111 0x00, 0x02, // XSTART = 1
SomeRandomBloke 0:99ee879ef5a9 112 0x00, 0x81, // XEND = 160
SomeRandomBloke 0:99ee879ef5a9 113 ST7735_NORON , DELAY, // 17: Normal display on, no args, w/delay
SomeRandomBloke 0:99ee879ef5a9 114 10, // 10 ms delay
SomeRandomBloke 0:99ee879ef5a9 115 ST7735_DISPON , DELAY, // 18: Main screen turn on, no args, w/delay
SomeRandomBloke 0:99ee879ef5a9 116 255
SomeRandomBloke 0:99ee879ef5a9 117 }, // 255 = 500 ms delay
SomeRandomBloke 0:99ee879ef5a9 118
SomeRandomBloke 0:99ee879ef5a9 119 Rcmd1[] = { // Init for 7735R, part 1 (red or green tab)
justinkim 5:156b180d2eda 120 15, // 16 commands in list:
SomeRandomBloke 0:99ee879ef5a9 121 ST7735_SWRESET, DELAY, // 1: Software reset, 0 args, w/delay
SomeRandomBloke 0:99ee879ef5a9 122 150, // 150 ms delay
SomeRandomBloke 0:99ee879ef5a9 123 ST7735_SLPOUT , DELAY, // 2: Out of sleep mode, 0 args, w/delay
SomeRandomBloke 0:99ee879ef5a9 124 255, // 500 ms delay
SomeRandomBloke 0:99ee879ef5a9 125 ST7735_FRMCTR1, 3 , // 3: Frame rate ctrl - normal mode, 3 args:
SomeRandomBloke 0:99ee879ef5a9 126 0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D)
SomeRandomBloke 0:99ee879ef5a9 127 ST7735_FRMCTR2, 3 , // 4: Frame rate control - idle mode, 3 args:
SomeRandomBloke 0:99ee879ef5a9 128 0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D)
SomeRandomBloke 0:99ee879ef5a9 129 ST7735_FRMCTR3, 6 , // 5: Frame rate ctrl - partial mode, 6 args:
SomeRandomBloke 0:99ee879ef5a9 130 0x01, 0x2C, 0x2D, // Dot inversion mode
SomeRandomBloke 0:99ee879ef5a9 131 0x01, 0x2C, 0x2D, // Line inversion mode
SomeRandomBloke 0:99ee879ef5a9 132 ST7735_INVCTR , 1 , // 6: Display inversion ctrl, 1 arg, no delay:
SomeRandomBloke 0:99ee879ef5a9 133 0x07, // No inversion
SomeRandomBloke 0:99ee879ef5a9 134 ST7735_PWCTR1 , 3 , // 7: Power control, 3 args, no delay:
SomeRandomBloke 0:99ee879ef5a9 135 0xA2,
SomeRandomBloke 0:99ee879ef5a9 136 0x02, // -4.6V
SomeRandomBloke 0:99ee879ef5a9 137 0x84, // AUTO mode
SomeRandomBloke 0:99ee879ef5a9 138 ST7735_PWCTR2 , 1 , // 8: Power control, 1 arg, no delay:
SomeRandomBloke 0:99ee879ef5a9 139 0xC5, // VGH25 = 2.4C VGSEL = -10 VGH = 3 * AVDD
SomeRandomBloke 0:99ee879ef5a9 140 ST7735_PWCTR3 , 2 , // 9: Power control, 2 args, no delay:
SomeRandomBloke 0:99ee879ef5a9 141 0x0A, // Opamp current small
SomeRandomBloke 0:99ee879ef5a9 142 0x00, // Boost frequency
SomeRandomBloke 0:99ee879ef5a9 143 ST7735_PWCTR4 , 2 , // 10: Power control, 2 args, no delay:
SomeRandomBloke 0:99ee879ef5a9 144 0x8A, // BCLK/2, Opamp current small & Medium low
SomeRandomBloke 0:99ee879ef5a9 145 0x2A,
SomeRandomBloke 0:99ee879ef5a9 146 ST7735_PWCTR5 , 2 , // 11: Power control, 2 args, no delay:
SomeRandomBloke 0:99ee879ef5a9 147 0x8A, 0xEE,
SomeRandomBloke 0:99ee879ef5a9 148 ST7735_VMCTR1 , 1 , // 12: Power control, 1 arg, no delay:
SomeRandomBloke 0:99ee879ef5a9 149 0x0E,
SomeRandomBloke 0:99ee879ef5a9 150 ST7735_INVOFF , 0 , // 13: Don't invert display, no args, no delay
SomeRandomBloke 0:99ee879ef5a9 151 ST7735_MADCTL , 1 , // 14: Memory access control (directions), 1 arg:
justinkim 5:156b180d2eda 152 0xC0, // row addr/col addr, bottom to top refresh
SomeRandomBloke 0:99ee879ef5a9 153 ST7735_COLMOD , 1 , // 15: set color mode, 1 arg, no delay:
SomeRandomBloke 0:99ee879ef5a9 154 0x05
SomeRandomBloke 0:99ee879ef5a9 155 }, // 16-bit color
SomeRandomBloke 0:99ee879ef5a9 156
SomeRandomBloke 0:99ee879ef5a9 157 Rcmd2green[] = { // Init for 7735R, part 2 (green tab only)
SomeRandomBloke 0:99ee879ef5a9 158 2, // 2 commands in list:
SomeRandomBloke 0:99ee879ef5a9 159 ST7735_CASET , 4 , // 1: Column addr set, 4 args, no delay:
SomeRandomBloke 0:99ee879ef5a9 160 0x00, 0x02, // XSTART = 0
SomeRandomBloke 0:99ee879ef5a9 161 0x00, 0x7F+0x02, // XEND = 127
SomeRandomBloke 0:99ee879ef5a9 162 ST7735_RASET , 4 , // 2: Row addr set, 4 args, no delay:
SomeRandomBloke 0:99ee879ef5a9 163 0x00, 0x01, // XSTART = 0
SomeRandomBloke 0:99ee879ef5a9 164 0x00, 0x9F+0x01
SomeRandomBloke 0:99ee879ef5a9 165 }, // XEND = 159
SomeRandomBloke 0:99ee879ef5a9 166 Rcmd2red[] = { // Init for 7735R, part 2 (red tab only)
SomeRandomBloke 0:99ee879ef5a9 167 2, // 2 commands in list:
SomeRandomBloke 0:99ee879ef5a9 168 ST7735_CASET , 4 , // 1: Column addr set, 4 args, no delay:
SomeRandomBloke 0:99ee879ef5a9 169 0x00, 0x00, // XSTART = 0
SomeRandomBloke 0:99ee879ef5a9 170 0x00, 0x7F, // XEND = 127
SomeRandomBloke 0:99ee879ef5a9 171 ST7735_RASET , 4 , // 2: Row addr set, 4 args, no delay:
SomeRandomBloke 0:99ee879ef5a9 172 0x00, 0x00, // XSTART = 0
SomeRandomBloke 0:99ee879ef5a9 173 0x00, 0x9F
SomeRandomBloke 0:99ee879ef5a9 174 }, // XEND = 159
SomeRandomBloke 0:99ee879ef5a9 175
SomeRandomBloke 0:99ee879ef5a9 176 Rcmd3[] = { // Init for 7735R, part 3 (red or green tab)
SomeRandomBloke 0:99ee879ef5a9 177 4, // 4 commands in list:
SomeRandomBloke 0:99ee879ef5a9 178 ST7735_GMCTRP1, 16 , // 1: Magical unicorn dust, 16 args, no delay:
SomeRandomBloke 0:99ee879ef5a9 179 0x02, 0x1c, 0x07, 0x12,
SomeRandomBloke 0:99ee879ef5a9 180 0x37, 0x32, 0x29, 0x2d,
SomeRandomBloke 0:99ee879ef5a9 181 0x29, 0x25, 0x2B, 0x39,
SomeRandomBloke 0:99ee879ef5a9 182 0x00, 0x01, 0x03, 0x10,
SomeRandomBloke 0:99ee879ef5a9 183 ST7735_GMCTRN1, 16 , // 2: Sparkles and rainbows, 16 args, no delay:
SomeRandomBloke 0:99ee879ef5a9 184 0x03, 0x1d, 0x07, 0x06,
SomeRandomBloke 0:99ee879ef5a9 185 0x2E, 0x2C, 0x29, 0x2D,
SomeRandomBloke 0:99ee879ef5a9 186 0x2E, 0x2E, 0x37, 0x3F,
SomeRandomBloke 0:99ee879ef5a9 187 0x00, 0x00, 0x02, 0x10,
SomeRandomBloke 0:99ee879ef5a9 188 ST7735_NORON , DELAY, // 3: Normal display on, no args, w/delay
SomeRandomBloke 0:99ee879ef5a9 189 10, // 10 ms delay
SomeRandomBloke 0:99ee879ef5a9 190 ST7735_DISPON , DELAY, // 4: Main screen turn on, no args w/delay
SomeRandomBloke 0:99ee879ef5a9 191 100
SomeRandomBloke 0:99ee879ef5a9 192 }; // 100 ms delay
SomeRandomBloke 0:99ee879ef5a9 193
SomeRandomBloke 0:99ee879ef5a9 194
SomeRandomBloke 0:99ee879ef5a9 195 // Companion code to the above tables. Reads and issues
SomeRandomBloke 0:99ee879ef5a9 196 // a series of LCD commands stored in byte array.
SomeRandomBloke 0:99ee879ef5a9 197 void Adafruit_ST7735::commandList(uint8_t *addr)
SomeRandomBloke 0:99ee879ef5a9 198 {
SomeRandomBloke 0:99ee879ef5a9 199
SomeRandomBloke 0:99ee879ef5a9 200 uint8_t numCommands, numArgs;
SomeRandomBloke 0:99ee879ef5a9 201 uint16_t ms;
SomeRandomBloke 0:99ee879ef5a9 202
SomeRandomBloke 0:99ee879ef5a9 203 numCommands = *addr++; // Number of commands to follow
SomeRandomBloke 0:99ee879ef5a9 204 while(numCommands--) { // For each command...
SomeRandomBloke 0:99ee879ef5a9 205 writecommand(*addr++); // Read, issue command
SomeRandomBloke 0:99ee879ef5a9 206 numArgs = *addr++; // Number of args to follow
SomeRandomBloke 0:99ee879ef5a9 207 ms = numArgs & DELAY; // If hibit set, delay follows args
SomeRandomBloke 0:99ee879ef5a9 208 numArgs &= ~DELAY; // Mask out delay bit
SomeRandomBloke 0:99ee879ef5a9 209 while(numArgs--) { // For each argument...
SomeRandomBloke 0:99ee879ef5a9 210 writedata(*addr++); // Read, issue argument
SomeRandomBloke 0:99ee879ef5a9 211 }
SomeRandomBloke 0:99ee879ef5a9 212
SomeRandomBloke 0:99ee879ef5a9 213 if(ms) {
SomeRandomBloke 0:99ee879ef5a9 214 ms = *addr++; // Read post-command delay time (ms)
SomeRandomBloke 0:99ee879ef5a9 215 if(ms == 255) ms = 500; // If 255, delay for 500 ms
SomeRandomBloke 0:99ee879ef5a9 216 wait_ms(ms);
SomeRandomBloke 0:99ee879ef5a9 217 }
SomeRandomBloke 0:99ee879ef5a9 218 }
SomeRandomBloke 0:99ee879ef5a9 219 }
SomeRandomBloke 0:99ee879ef5a9 220
SomeRandomBloke 0:99ee879ef5a9 221
SomeRandomBloke 0:99ee879ef5a9 222 // Initialization code common to both 'B' and 'R' type displays
SomeRandomBloke 0:99ee879ef5a9 223 void Adafruit_ST7735::commonInit(uint8_t *cmdList)
SomeRandomBloke 0:99ee879ef5a9 224 {
SomeRandomBloke 0:99ee879ef5a9 225
SomeRandomBloke 0:99ee879ef5a9 226 colstart = rowstart = 0; // May be overridden in init func
SomeRandomBloke 0:99ee879ef5a9 227
SomeRandomBloke 0:99ee879ef5a9 228 _rs = 1;
SomeRandomBloke 0:99ee879ef5a9 229 _cs = 1;
SomeRandomBloke 0:99ee879ef5a9 230
SomeRandomBloke 0:99ee879ef5a9 231 // use default SPI format
SomeRandomBloke 0:99ee879ef5a9 232 lcdPort.format(8,0);
SomeRandomBloke 2:18ecda534e06 233 lcdPort.frequency(4000000); // Lets try 4MHz
SomeRandomBloke 0:99ee879ef5a9 234
SomeRandomBloke 0:99ee879ef5a9 235 // toggle RST low to reset; CS low so it'll listen to us
SomeRandomBloke 0:99ee879ef5a9 236 _cs = 0;
SomeRandomBloke 0:99ee879ef5a9 237 _rst = 1;
SomeRandomBloke 0:99ee879ef5a9 238 wait_ms(500);
SomeRandomBloke 0:99ee879ef5a9 239 _rst = 0;
SomeRandomBloke 0:99ee879ef5a9 240 wait_ms(500);
SomeRandomBloke 0:99ee879ef5a9 241 _rst = 1;
SomeRandomBloke 0:99ee879ef5a9 242 wait_ms(500);
SomeRandomBloke 0:99ee879ef5a9 243
SomeRandomBloke 0:99ee879ef5a9 244 if(cmdList) commandList(cmdList);
SomeRandomBloke 0:99ee879ef5a9 245 }
SomeRandomBloke 0:99ee879ef5a9 246
SomeRandomBloke 0:99ee879ef5a9 247
SomeRandomBloke 0:99ee879ef5a9 248 // Initialization for ST7735B screens
SomeRandomBloke 0:99ee879ef5a9 249 void Adafruit_ST7735::initB(void)
SomeRandomBloke 0:99ee879ef5a9 250 {
SomeRandomBloke 0:99ee879ef5a9 251 commonInit(Bcmd);
SomeRandomBloke 0:99ee879ef5a9 252 }
SomeRandomBloke 0:99ee879ef5a9 253
SomeRandomBloke 0:99ee879ef5a9 254
SomeRandomBloke 0:99ee879ef5a9 255 // Initialization for ST7735R screens (green or red tabs)
SomeRandomBloke 0:99ee879ef5a9 256 void Adafruit_ST7735::initR(uint8_t options)
SomeRandomBloke 0:99ee879ef5a9 257 {
SomeRandomBloke 0:99ee879ef5a9 258 commonInit(Rcmd1);
SomeRandomBloke 0:99ee879ef5a9 259 if(options == INITR_GREENTAB) {
SomeRandomBloke 0:99ee879ef5a9 260 commandList(Rcmd2green);
SomeRandomBloke 0:99ee879ef5a9 261 colstart = 2;
SomeRandomBloke 0:99ee879ef5a9 262 rowstart = 1;
SomeRandomBloke 0:99ee879ef5a9 263 } else {
SomeRandomBloke 0:99ee879ef5a9 264 // colstart, rowstart left at default '0' values
SomeRandomBloke 0:99ee879ef5a9 265 commandList(Rcmd2red);
SomeRandomBloke 0:99ee879ef5a9 266 }
SomeRandomBloke 0:99ee879ef5a9 267 commandList(Rcmd3);
SomeRandomBloke 0:99ee879ef5a9 268 }
SomeRandomBloke 0:99ee879ef5a9 269
SomeRandomBloke 0:99ee879ef5a9 270
SomeRandomBloke 0:99ee879ef5a9 271 void Adafruit_ST7735::setAddrWindow(uint8_t x0, uint8_t y0, uint8_t x1,
SomeRandomBloke 0:99ee879ef5a9 272 uint8_t y1)
SomeRandomBloke 0:99ee879ef5a9 273 {
SomeRandomBloke 0:99ee879ef5a9 274
SomeRandomBloke 0:99ee879ef5a9 275 writecommand(ST7735_CASET); // Column addr set
SomeRandomBloke 0:99ee879ef5a9 276 writedata(0x00);
SomeRandomBloke 0:99ee879ef5a9 277 writedata(x0+colstart); // XSTART
SomeRandomBloke 0:99ee879ef5a9 278 writedata(0x00);
SomeRandomBloke 0:99ee879ef5a9 279 writedata(x1+colstart); // XEND
SomeRandomBloke 0:99ee879ef5a9 280
SomeRandomBloke 0:99ee879ef5a9 281 writecommand(ST7735_RASET); // Row addr set
SomeRandomBloke 0:99ee879ef5a9 282 writedata(0x00);
SomeRandomBloke 0:99ee879ef5a9 283 writedata(y0+rowstart); // YSTART
SomeRandomBloke 0:99ee879ef5a9 284 writedata(0x00);
SomeRandomBloke 0:99ee879ef5a9 285 writedata(y1+rowstart); // YEND
SomeRandomBloke 0:99ee879ef5a9 286
SomeRandomBloke 0:99ee879ef5a9 287 writecommand(ST7735_RAMWR); // write to RAM
SomeRandomBloke 0:99ee879ef5a9 288 }
SomeRandomBloke 0:99ee879ef5a9 289
SomeRandomBloke 0:99ee879ef5a9 290
SomeRandomBloke 0:99ee879ef5a9 291 void Adafruit_ST7735::fillScreen(uint16_t color)
SomeRandomBloke 0:99ee879ef5a9 292 {
SomeRandomBloke 0:99ee879ef5a9 293
SomeRandomBloke 0:99ee879ef5a9 294 uint8_t x, y, hi = color >> 8, lo = color;
SomeRandomBloke 0:99ee879ef5a9 295
SomeRandomBloke 0:99ee879ef5a9 296 setAddrWindow(0, 0, _width-1, _height-1);
SomeRandomBloke 0:99ee879ef5a9 297
SomeRandomBloke 0:99ee879ef5a9 298 _rs = 1;
SomeRandomBloke 0:99ee879ef5a9 299 _cs = 0;
SomeRandomBloke 0:99ee879ef5a9 300
SomeRandomBloke 0:99ee879ef5a9 301 for(y=_height; y>0; y--) {
SomeRandomBloke 0:99ee879ef5a9 302 for(x=_width; x>0; x--) {
SomeRandomBloke 0:99ee879ef5a9 303 lcdPort.write( hi );
SomeRandomBloke 0:99ee879ef5a9 304 lcdPort.write( lo );
SomeRandomBloke 0:99ee879ef5a9 305 }
SomeRandomBloke 0:99ee879ef5a9 306 }
SomeRandomBloke 0:99ee879ef5a9 307
SomeRandomBloke 0:99ee879ef5a9 308 _cs = 1;
SomeRandomBloke 0:99ee879ef5a9 309 }
SomeRandomBloke 0:99ee879ef5a9 310
SomeRandomBloke 0:99ee879ef5a9 311
SomeRandomBloke 0:99ee879ef5a9 312 void Adafruit_ST7735::pushColor(uint16_t color)
SomeRandomBloke 0:99ee879ef5a9 313 {
SomeRandomBloke 0:99ee879ef5a9 314 _rs = 1;
SomeRandomBloke 0:99ee879ef5a9 315 _cs = 0;
SomeRandomBloke 0:99ee879ef5a9 316
SomeRandomBloke 0:99ee879ef5a9 317 lcdPort.write( color >> 8 );
SomeRandomBloke 0:99ee879ef5a9 318 lcdPort.write( color );
SomeRandomBloke 0:99ee879ef5a9 319 _cs = 1;
SomeRandomBloke 0:99ee879ef5a9 320 }
SomeRandomBloke 0:99ee879ef5a9 321
SomeRandomBloke 0:99ee879ef5a9 322
SomeRandomBloke 0:99ee879ef5a9 323 void Adafruit_ST7735::drawPixel(int16_t x, int16_t y, uint16_t color)
SomeRandomBloke 0:99ee879ef5a9 324 {
SomeRandomBloke 0:99ee879ef5a9 325
SomeRandomBloke 0:99ee879ef5a9 326 if((x < 0) ||(x >= _width) || (y < 0) || (y >= _height)) return;
SomeRandomBloke 0:99ee879ef5a9 327
SomeRandomBloke 0:99ee879ef5a9 328 setAddrWindow(x,y,x+1,y+1);
SomeRandomBloke 0:99ee879ef5a9 329
SomeRandomBloke 0:99ee879ef5a9 330 _rs = 1;
SomeRandomBloke 0:99ee879ef5a9 331 _cs = 0;
justinkim 5:156b180d2eda 332
justinkim 5:156b180d2eda 333 lcdPort.write( color >> 8 );
SomeRandomBloke 0:99ee879ef5a9 334 lcdPort.write( color );
SomeRandomBloke 0:99ee879ef5a9 335
SomeRandomBloke 0:99ee879ef5a9 336 _cs = 1;
SomeRandomBloke 0:99ee879ef5a9 337 }
SomeRandomBloke 0:99ee879ef5a9 338
SomeRandomBloke 0:99ee879ef5a9 339
SomeRandomBloke 0:99ee879ef5a9 340 void Adafruit_ST7735::drawFastVLine(int16_t x, int16_t y, int16_t h,
SomeRandomBloke 0:99ee879ef5a9 341 uint16_t color)
SomeRandomBloke 0:99ee879ef5a9 342 {
SomeRandomBloke 0:99ee879ef5a9 343
SomeRandomBloke 0:99ee879ef5a9 344 // Rudimentary clipping
SomeRandomBloke 0:99ee879ef5a9 345 if((x >= _width) || (y >= _height)) return;
SomeRandomBloke 0:99ee879ef5a9 346 if((y+h-1) >= _height) h = _height-y;
SomeRandomBloke 0:99ee879ef5a9 347 setAddrWindow(x, y, x, y+h-1);
SomeRandomBloke 0:99ee879ef5a9 348
SomeRandomBloke 0:99ee879ef5a9 349 uint8_t hi = color >> 8, lo = color;
SomeRandomBloke 0:99ee879ef5a9 350 _rs = 1;
SomeRandomBloke 0:99ee879ef5a9 351 _cs = 0;
SomeRandomBloke 0:99ee879ef5a9 352 while (h--) {
SomeRandomBloke 0:99ee879ef5a9 353 lcdPort.write( hi );
SomeRandomBloke 0:99ee879ef5a9 354 lcdPort.write( lo );
SomeRandomBloke 0:99ee879ef5a9 355 }
SomeRandomBloke 0:99ee879ef5a9 356 _cs = 1;
SomeRandomBloke 0:99ee879ef5a9 357 }
SomeRandomBloke 0:99ee879ef5a9 358
SomeRandomBloke 0:99ee879ef5a9 359
SomeRandomBloke 0:99ee879ef5a9 360 void Adafruit_ST7735::drawFastHLine(int16_t x, int16_t y, int16_t w,
SomeRandomBloke 0:99ee879ef5a9 361 uint16_t color)
SomeRandomBloke 0:99ee879ef5a9 362 {
SomeRandomBloke 0:99ee879ef5a9 363
SomeRandomBloke 0:99ee879ef5a9 364 // Rudimentary clipping
SomeRandomBloke 0:99ee879ef5a9 365 if((x >= _width) || (y >= _height)) return;
SomeRandomBloke 0:99ee879ef5a9 366 if((x+w-1) >= _width) w = _width-x;
SomeRandomBloke 0:99ee879ef5a9 367 setAddrWindow(x, y, x+w-1, y);
SomeRandomBloke 0:99ee879ef5a9 368
SomeRandomBloke 0:99ee879ef5a9 369 uint8_t hi = color >> 8, lo = color;
SomeRandomBloke 0:99ee879ef5a9 370 _rs = 1;
SomeRandomBloke 0:99ee879ef5a9 371 _cs = 0;
SomeRandomBloke 0:99ee879ef5a9 372 while (w--) {
SomeRandomBloke 0:99ee879ef5a9 373 lcdPort.write( hi );
SomeRandomBloke 0:99ee879ef5a9 374 lcdPort.write( lo );
SomeRandomBloke 0:99ee879ef5a9 375 }
SomeRandomBloke 0:99ee879ef5a9 376 _cs = 1;
SomeRandomBloke 0:99ee879ef5a9 377 }
SomeRandomBloke 0:99ee879ef5a9 378
SomeRandomBloke 0:99ee879ef5a9 379
SomeRandomBloke 0:99ee879ef5a9 380 // fill a rectangle
SomeRandomBloke 0:99ee879ef5a9 381 void Adafruit_ST7735::fillRect(int16_t x, int16_t y, int16_t w, int16_t h,
SomeRandomBloke 0:99ee879ef5a9 382 uint16_t color)
SomeRandomBloke 0:99ee879ef5a9 383 {
SomeRandomBloke 0:99ee879ef5a9 384
SomeRandomBloke 0:99ee879ef5a9 385 // rudimentary clipping (drawChar w/big text requires this)
SomeRandomBloke 0:99ee879ef5a9 386 if((x >= _width) || (y >= _height)) return;
SomeRandomBloke 0:99ee879ef5a9 387 if((x + w - 1) >= _width) w = _width - x;
SomeRandomBloke 0:99ee879ef5a9 388 if((y + h - 1) >= _height) h = _height - y;
SomeRandomBloke 0:99ee879ef5a9 389
SomeRandomBloke 0:99ee879ef5a9 390 setAddrWindow(x, y, x+w-1, y+h-1);
SomeRandomBloke 0:99ee879ef5a9 391
SomeRandomBloke 0:99ee879ef5a9 392 uint8_t hi = color >> 8, lo = color;
SomeRandomBloke 0:99ee879ef5a9 393 _rs = 1;
SomeRandomBloke 0:99ee879ef5a9 394 _cs = 0;
SomeRandomBloke 0:99ee879ef5a9 395 for(y=h; y>0; y--) {
SomeRandomBloke 0:99ee879ef5a9 396 for(x=w; x>0; x--) {
SomeRandomBloke 0:99ee879ef5a9 397 lcdPort.write( hi );
SomeRandomBloke 0:99ee879ef5a9 398 lcdPort.write( lo );
SomeRandomBloke 0:99ee879ef5a9 399 }
SomeRandomBloke 0:99ee879ef5a9 400 }
SomeRandomBloke 0:99ee879ef5a9 401
SomeRandomBloke 0:99ee879ef5a9 402 _cs = 1;
SomeRandomBloke 0:99ee879ef5a9 403 }
SomeRandomBloke 0:99ee879ef5a9 404
SomeRandomBloke 0:99ee879ef5a9 405
SomeRandomBloke 0:99ee879ef5a9 406 // Pass 8-bit (each) R,G,B, get back 16-bit packed color
SomeRandomBloke 0:99ee879ef5a9 407 uint16_t Adafruit_ST7735::Color565(uint8_t r, uint8_t g, uint8_t b)
SomeRandomBloke 0:99ee879ef5a9 408 {
SomeRandomBloke 0:99ee879ef5a9 409 return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
SomeRandomBloke 0:99ee879ef5a9 410 }
SomeRandomBloke 0:99ee879ef5a9 411
SomeRandomBloke 0:99ee879ef5a9 412
SomeRandomBloke 0:99ee879ef5a9 413 #define MADCTL_MY 0x80
SomeRandomBloke 0:99ee879ef5a9 414 #define MADCTL_MX 0x40
SomeRandomBloke 0:99ee879ef5a9 415 #define MADCTL_MV 0x20
SomeRandomBloke 0:99ee879ef5a9 416 #define MADCTL_ML 0x10
justinkim 6:7d45da201480 417 #define MADCTL_RGB 0x00
SomeRandomBloke 0:99ee879ef5a9 418 #define MADCTL_MH 0x04
SomeRandomBloke 0:99ee879ef5a9 419
SomeRandomBloke 0:99ee879ef5a9 420 void Adafruit_ST7735::setRotation(uint8_t m)
SomeRandomBloke 0:99ee879ef5a9 421 {
SomeRandomBloke 0:99ee879ef5a9 422
SomeRandomBloke 0:99ee879ef5a9 423 writecommand(ST7735_MADCTL);
SomeRandomBloke 0:99ee879ef5a9 424 rotation = m % 4; // can't be higher than 3
SomeRandomBloke 0:99ee879ef5a9 425 switch (rotation) {
SomeRandomBloke 0:99ee879ef5a9 426 case 0:
SomeRandomBloke 0:99ee879ef5a9 427 writedata(MADCTL_MX | MADCTL_MY | MADCTL_RGB);
SomeRandomBloke 0:99ee879ef5a9 428 _width = ST7735_TFTWIDTH;
SomeRandomBloke 0:99ee879ef5a9 429 _height = ST7735_TFTHEIGHT;
SomeRandomBloke 0:99ee879ef5a9 430 break;
SomeRandomBloke 0:99ee879ef5a9 431 case 1:
SomeRandomBloke 0:99ee879ef5a9 432 writedata(MADCTL_MY | MADCTL_MV | MADCTL_RGB);
SomeRandomBloke 0:99ee879ef5a9 433 _width = ST7735_TFTHEIGHT;
SomeRandomBloke 0:99ee879ef5a9 434 _height = ST7735_TFTWIDTH;
SomeRandomBloke 0:99ee879ef5a9 435 break;
SomeRandomBloke 0:99ee879ef5a9 436 case 2:
SomeRandomBloke 0:99ee879ef5a9 437 writedata(MADCTL_RGB);
SomeRandomBloke 0:99ee879ef5a9 438 _width = ST7735_TFTWIDTH;
SomeRandomBloke 0:99ee879ef5a9 439 _height = ST7735_TFTHEIGHT;
SomeRandomBloke 0:99ee879ef5a9 440 break;
SomeRandomBloke 0:99ee879ef5a9 441 case 3:
SomeRandomBloke 0:99ee879ef5a9 442 writedata(MADCTL_MX | MADCTL_MV | MADCTL_RGB);
SomeRandomBloke 0:99ee879ef5a9 443 _width = ST7735_TFTHEIGHT;
SomeRandomBloke 0:99ee879ef5a9 444 _height = ST7735_TFTWIDTH;
SomeRandomBloke 0:99ee879ef5a9 445 break;
SomeRandomBloke 0:99ee879ef5a9 446 }
SomeRandomBloke 0:99ee879ef5a9 447 }
SomeRandomBloke 0:99ee879ef5a9 448
SomeRandomBloke 0:99ee879ef5a9 449
SomeRandomBloke 0:99ee879ef5a9 450 void Adafruit_ST7735::invertDisplay(boolean i)
SomeRandomBloke 0:99ee879ef5a9 451 {
SomeRandomBloke 0:99ee879ef5a9 452 writecommand(i ? ST7735_INVON : ST7735_INVOFF);
SomeRandomBloke 0:99ee879ef5a9 453 }
SomeRandomBloke 0:99ee879ef5a9 454
SomeRandomBloke 0:99ee879ef5a9 455
justinkim 4:80075830f886 456 int Adafruit_ST7735::BMP_16(unsigned int x, unsigned int y, const char *Name_BMP)
justinkim 4:80075830f886 457 {
justinkim 4:80075830f886 458
justinkim 4:80075830f886 459
justinkim 4:80075830f886 460
justinkim 4:80075830f886 461 #define OffsetPixelWidth 18
justinkim 4:80075830f886 462 #define OffsetPixelHeigh 22
justinkim 4:80075830f886 463 #define OffsetFileSize 34
justinkim 4:80075830f886 464 #define OffsetPixData 10
justinkim 4:80075830f886 465 #define OffsetBPP 28
justinkim 4:80075830f886 466 pc.printf("TEST START\r\n");
justinkim 4:80075830f886 467 char filename[50];
justinkim 4:80075830f886 468 unsigned char BMP_Header[54];
justinkim 4:80075830f886 469 unsigned short BPP_t;
justinkim 4:80075830f886 470 unsigned int PixelWidth,PixelHeigh,start_data;
justinkim 4:80075830f886 471 unsigned int i,off;
justinkim 4:80075830f886 472 int padd,j;
justinkim 4:80075830f886 473 unsigned short *line;
justinkim 4:80075830f886 474
justinkim 4:80075830f886 475 pc.printf("TEST START1\r\n");
justinkim 4:80075830f886 476 // get the filename
justinkim 4:80075830f886 477 i=0;
justinkim 4:80075830f886 478 while (*Name_BMP!='\0') {
justinkim 4:80075830f886 479 filename[i++]=*Name_BMP++;
justinkim 4:80075830f886 480 }
justinkim 4:80075830f886 481 pc.printf("TEST START2\r\n");
justinkim 4:80075830f886 482 filename[i] = 0;
justinkim 4:80075830f886 483
justinkim 4:80075830f886 484 pc.printf("TEST START3\r\n");
justinkim 4:80075830f886 485 FILE *Image = fopen((const char *)&filename[0], "rb"); // open the bmp file
justinkim 4:80075830f886 486 pc.printf("TEST FILEOPEN\r\n");
justinkim 4:80075830f886 487 if (!Image) {
justinkim 4:80075830f886 488 pc.printf("TEST error file not found");
justinkim 4:80075830f886 489 return(0); // error file not found !
justinkim 4:80075830f886 490 }
justinkim 4:80075830f886 491
justinkim 4:80075830f886 492 fread(&BMP_Header[0],1,54,Image); // get the BMP Header
justinkim 4:80075830f886 493
justinkim 4:80075830f886 494 if (BMP_Header[0] != 0x42 || BMP_Header[1] != 0x4D) { // check magic byte
justinkim 4:80075830f886 495 pc.printf("TEST error no BMP file");
justinkim 4:80075830f886 496 fclose(Image);
justinkim 4:80075830f886 497 return(-1); // error no BMP file
justinkim 4:80075830f886 498 }
justinkim 4:80075830f886 499
justinkim 4:80075830f886 500 BPP_t = BMP_Header[OffsetBPP] + (BMP_Header[OffsetBPP + 1] << 8);
justinkim 4:80075830f886 501 if (BPP_t != 0x0010) {
justinkim 4:80075830f886 502 pc.printf("TEST error no 16 bit BMP\r\n");
justinkim 4:80075830f886 503 fclose(Image);
justinkim 4:80075830f886 504 return(-2); // error no 16 bit BMP
justinkim 4:80075830f886 505 }
justinkim 4:80075830f886 506
justinkim 4:80075830f886 507 PixelHeigh = BMP_Header[OffsetPixelHeigh] + (BMP_Header[OffsetPixelHeigh + 1] << 8) + (BMP_Header[OffsetPixelHeigh + 2] << 16) + (BMP_Header[OffsetPixelHeigh + 3] << 24);
justinkim 4:80075830f886 508 PixelWidth = BMP_Header[OffsetPixelWidth] + (BMP_Header[OffsetPixelWidth + 1] << 8) + (BMP_Header[OffsetPixelWidth + 2] << 16) + (BMP_Header[OffsetPixelWidth + 3] << 24);
justinkim 4:80075830f886 509 if (PixelHeigh > _height + y || PixelWidth > _width + x) {
justinkim 4:80075830f886 510 pc.printf("TEST to big\r\n");
justinkim 4:80075830f886 511 fclose(Image);
justinkim 4:80075830f886 512 return(-3); // to big
justinkim 4:80075830f886 513 }
justinkim 4:80075830f886 514
justinkim 4:80075830f886 515 start_data = BMP_Header[OffsetPixData] + (BMP_Header[OffsetPixData + 1] << 8) + (BMP_Header[OffsetPixData + 2] << 16) + (BMP_Header[OffsetPixData + 3] << 24);
justinkim 4:80075830f886 516
justinkim 4:80075830f886 517 line = (unsigned short *) malloc (2 * PixelHeigh); // we need a buffer for a line
justinkim 4:80075830f886 518 if (line == NULL) {
justinkim 4:80075830f886 519 pc.printf("TEST error no memory\r\n");
justinkim 4:80075830f886 520 return(-4); // error no memory
justinkim 4:80075830f886 521 }
justinkim 4:80075830f886 522 pc.printf("TEST 506");
justinkim 4:80075830f886 523 // the bmp lines are padded to multiple of 4 bytes
justinkim 4:80075830f886 524 padd = -1;
justinkim 4:80075830f886 525 do {pc.printf("TEST padd : %d\r\n",padd);
justinkim 4:80075830f886 526 padd ++;
justinkim 4:80075830f886 527 } while ((PixelHeigh * 2 + padd)%4 != 0);
jacobjohnson 8:196b625fe857 528
jacobjohnson 8:196b625fe857 529 pc.printf("Information: \n\r Height: %d\r\n Width: %d\r\n\ BPP: %d\r\n", PixelHeigh, PixelWidth, BPP_t);
justinkim 4:80075830f886 530
justinkim 4:80075830f886 531 setAddrWindow(x, y,PixelWidth ,PixelHeigh);
justinkim 4:80075830f886 532 writecommand(0x2C); // send pixel
justinkim 4:80075830f886 533
jacobjohnson 8:196b625fe857 534
justinkim 4:80075830f886 535 pc.printf("TEST 518");
jacobjohnson 8:196b625fe857 536 for (j = PixelHeigh- 1; j >= 0; j--) { //Lines bottom up
jacobjohnson 8:196b625fe857 537 off = j * (PixelWidth * 2 + padd) + start_data; // start of line
justinkim 4:80075830f886 538 fseek(Image, off ,SEEK_SET);
jacobjohnson 8:196b625fe857 539 fread(line,1,PixelWidth * 2,Image); // read a line - slow
jacobjohnson 8:196b625fe857 540 for (i = 0; i < PixelWidth; i++) { // copy pixel data to TFT
justinkim 4:80075830f886 541 _rs = 1;
justinkim 4:80075830f886 542 _cs = 0;
justinkim 4:80075830f886 543 lcdPort.write(line[i]);
justinkim 4:80075830f886 544 }
justinkim 4:80075830f886 545 }
justinkim 4:80075830f886 546 _cs = 1;
justinkim 4:80075830f886 547 lcdPort.format(8,3);
justinkim 4:80075830f886 548 free (line);
justinkim 4:80075830f886 549 fclose(Image);
justinkim 4:80075830f886 550 setAddrWindow(0,0,_width,_height);
justinkim 4:80075830f886 551 pc.printf("TEST END");
justinkim 4:80075830f886 552 return(1);
justinkim 4:80075830f886 553 }
justinkim 4:80075830f886 554
jacobjohnson 8:196b625fe857 555 #define RGB(r,g,b) (((r&0xF8)<<8)|((g&0xF8)<<3)|((b)>>3)) //5 red | 6 green | 5 blue
justinkim 4:80075830f886 556 #define TFT_DEBUG
justinkim 4:80075830f886 557
justinkim 4:80075830f886 558 int Adafruit_ST7735::DrawBitmapFile(const char *Name_BMP)
justinkim 4:80075830f886 559 {
justinkim 4:80075830f886 560
jacobjohnson 8:196b625fe857 561 char img[5*240];
justinkim 4:80075830f886 562 uint32_t imgsize = 0;
justinkim 4:80075830f886 563 uint32_t offset = 0;
justinkim 4:80075830f886 564 uint32_t imgw = 0;
justinkim 4:80075830f886 565 uint32_t imgh = 0;
justinkim 4:80075830f886 566 char colbits = 0;
justinkim 4:80075830f886 567 char compress = 0;
justinkim 4:80075830f886 568 uint16_t col;
justinkim 4:80075830f886 569
justinkim 4:80075830f886 570 int i, j;
justinkim 4:80075830f886 571
justinkim 4:80075830f886 572 char filename[50];
jacobjohnson 8:196b625fe857 573 lcdPort.format(8,0);
justinkim 4:80075830f886 574
justinkim 4:80075830f886 575 pc.printf("TEST START1\r\n");
justinkim 4:80075830f886 576 // get the filename
justinkim 4:80075830f886 577 i=0;
justinkim 4:80075830f886 578 while (*Name_BMP!='\0') {
justinkim 4:80075830f886 579 filename[i++]=*Name_BMP++;
justinkim 4:80075830f886 580 }
justinkim 4:80075830f886 581 pc.printf("TEST START2\r\n");
justinkim 4:80075830f886 582 filename[i] = 0;
justinkim 4:80075830f886 583
justinkim 4:80075830f886 584 pc.printf("TEST START3\r\n");
justinkim 4:80075830f886 585 FILE *Image = fopen((const char *)&filename[0], "rb"); // open the bmp file
jacobjohnson 7:5842518d1feb 586
jacobjohnson 7:5842518d1feb 587
justinkim 4:80075830f886 588 if(Image == NULL) return -1;
justinkim 4:80075830f886 589 if(fgetc(Image) != 0x42) return -2;
justinkim 4:80075830f886 590 if(fgetc(Image) != 0x4D) return -2;
justinkim 4:80075830f886 591
justinkim 4:80075830f886 592 for(i = 0; i < 4; i++)
justinkim 4:80075830f886 593 {
justinkim 4:80075830f886 594 imgsize += (((uint32_t)fgetc(Image)) << i*8);
justinkim 4:80075830f886 595 }
justinkim 4:80075830f886 596 #ifdef TFT_DEBUG
justinkim 4:80075830f886 597 pc.printf("BMP SIZE:%d\r\n",imgsize);
justinkim 4:80075830f886 598 #endif
justinkim 4:80075830f886 599 fseek(Image,4,SEEK_CUR);
justinkim 4:80075830f886 600 for(i = 0; i < 4; i++)
justinkim 4:80075830f886 601 {
justinkim 4:80075830f886 602 offset += (((uint32_t)fgetc(Image)) << i*8);
justinkim 4:80075830f886 603 }
justinkim 4:80075830f886 604 #ifdef TFT_DEBUG
justinkim 4:80075830f886 605 pc.printf("BMP OFFSET:%d\r\n",offset);
justinkim 4:80075830f886 606 #endif
justinkim 4:80075830f886 607 fseek(Image,4,SEEK_CUR);
justinkim 4:80075830f886 608 for(i = 0; i < 4; i++)
justinkim 4:80075830f886 609 {
justinkim 4:80075830f886 610 imgw += (((uint32_t)fgetc(Image)) << i*8);
justinkim 4:80075830f886 611 }
justinkim 4:80075830f886 612 if(imgw > 240) return -3;
justinkim 4:80075830f886 613
justinkim 4:80075830f886 614 for(i = 0; i < 4; i++)
justinkim 4:80075830f886 615 {
justinkim 4:80075830f886 616 imgh += (((uint32_t)fgetc(Image)) << i*8);
justinkim 4:80075830f886 617 }
justinkim 4:80075830f886 618 if(imgh > 320) return -3;
justinkim 4:80075830f886 619
justinkim 4:80075830f886 620 fseek(Image,2,SEEK_CUR);
justinkim 4:80075830f886 621 colbits = fgetc(Image);
justinkim 4:80075830f886 622 //if(colbits != 16 || colbits != 24) return -4;
justinkim 4:80075830f886 623 fgetc(Image);
justinkim 4:80075830f886 624 if((compress=fgetc(Image)) != 0)
justinkim 4:80075830f886 625 {
justinkim 4:80075830f886 626 #ifdef TFT_DEBUG
justinkim 4:80075830f886 627 pc.printf("Not supported compress : %d\r\n",compress);
justinkim 4:80075830f886 628 #endif
justinkim 4:80075830f886 629 return -4;
justinkim 4:80075830f886 630 }
justinkim 4:80075830f886 631
justinkim 4:80075830f886 632
justinkim 4:80075830f886 633 #ifdef TFT_DEBUG
justinkim 4:80075830f886 634 pc.printf("RESOL : %d col, %d X %d",colbits,imgw,imgh);
justinkim 4:80075830f886 635 #endif
justinkim 4:80075830f886 636
justinkim 4:80075830f886 637 fseek(Image, offset, SEEK_SET);
justinkim 4:80075830f886 638 for (j = imgh; j >= 0; j--) //Lines
justinkim 4:80075830f886 639 {
jacobjohnson 8:196b625fe857 640 //fread(img,sizeof(char),imgw*3,Image);
jacobjohnson 8:196b625fe857 641 fread(img,sizeof(char),imgw * 3,Image);
justinkim 4:80075830f886 642 _cs = 1;
justinkim 4:80075830f886 643 setAddrWindow(0, j, imgw ,1);
justinkim 4:80075830f886 644 writecommand(0x2C); // send pixel
justinkim 4:80075830f886 645 #ifdef TARGET_WIZWIKI_W7500
jacobjohnson 8:196b625fe857 646 lcdPort.format(8,3);
justinkim 4:80075830f886 647 #endif
justinkim 4:80075830f886 648
justinkim 4:80075830f886 649 for(i = 0; i < imgw; i++)
justinkim 4:80075830f886 650 {
jacobjohnson 8:196b625fe857 651 if(colbits == 16)
justinkim 4:80075830f886 652 {
justinkim 4:80075830f886 653 col = (uint16_t)img[2*i+1];
justinkim 4:80075830f886 654 col <<= 8;
justinkim 4:80075830f886 655 col += (uint16_t)img[2*i];
justinkim 4:80075830f886 656 }
jacobjohnson 8:196b625fe857 657 else if(colbits == 24)
jacobjohnson 8:196b625fe857 658 {
jacobjohnson 8:196b625fe857 659 col = RGB((uint16_t)img[(3*i-3)],(uint16_t)img[(3*i)+1-3], (uint16_t)img[(3*i)+2-3]);
jacobjohnson 8:196b625fe857 660 //col = RGB((uint16_t)img[i],(uint16_t)img[i], (uint16_t)img[i]);
jacobjohnson 8:196b625fe857 661 //pc.printf("R: %h\n\r G: %h\n\r B: &h\n\r", img[3*i], img[3*i+
jacobjohnson 8:196b625fe857 662 }
justinkim 4:80075830f886 663 #ifdef TFT_DEBUG
justinkim 4:80075830f886 664 /*
justinkim 4:80075830f886 665
justinkim 4:80075830f886 666 pc.printf("RGB(%d): ",i);
justinkim 4:80075830f886 667 pc.printf("(%d,",img[3*i+2]);
justinkim 4:80075830f886 668 pc.printf("%d,", img[3*i+1]);
justinkim 4:80075830f886 669 pc.printf("%d),->", img[3*i]);
justinkim 4:80075830f886 670 pc.printf("%04x\r\n",col);
justinkim 4:80075830f886 671 */
justinkim 4:80075830f886 672 //pc.printf("RGB(%d): (%d,%d,%d) -> %04X\r\n ",i,img[3*i+2],img[3*i+1],img[3*i],col);
justinkim 4:80075830f886 673 #endif
jacobjohnson 8:196b625fe857 674 /* _rs = 1;
jacobjohnson 8:196b625fe857 675 _cs = 0;
jacobjohnson 8:196b625fe857 676
jacobjohnson 8:196b625fe857 677 lcdPort.write(col); */
justinkim 4:80075830f886 678 _rs = 1;
justinkim 4:80075830f886 679 _cs = 0;
justinkim 4:80075830f886 680
jacobjohnson 8:196b625fe857 681 lcdPort.write( col >> 8 );
jacobjohnson 8:196b625fe857 682 lcdPort.write( col );
jacobjohnson 8:196b625fe857 683 //_cs = 1;
justinkim 4:80075830f886 684 }
justinkim 4:80075830f886 685 _cs = 1;
justinkim 4:80075830f886 686 #ifdef TARGET_WIZWIKI_W7500
justinkim 4:80075830f886 687 lcdPort.format(8,3);
justinkim 4:80075830f886 688 #endif
justinkim 4:80075830f886 689 }
justinkim 4:80075830f886 690 setAddrWindow(0,0,_width,_height);
justinkim 4:80075830f886 691
justinkim 4:80075830f886 692 return 0;
justinkim 4:80075830f886 693 }
justinkim 4:80075830f886 694
justinkim 5:156b180d2eda 695