Library to control a QVGA TFT connected to SPI. You can use printf to print text The lib can handle different fonts, draw lines, circles, rect and bmp

Committer:
dreschpe
Date:
Fri Dec 30 11:48:57 2011 +0000
Revision:
10:1e868317ff49
Parent:
9:9dc5dfdda734
Child:
11:3cfa9bb9b070
fix cls bug

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dreschpe 4:e1e45f8a7664 1 /* mbed library for 240*320 pixel display TFT based on HX8347D LCD Controller
dreschpe 4:e1e45f8a7664 2 * Copyright (c) 2011 Peter Drescher - DC2PD
dreschpe 4:e1e45f8a7664 3 *
dreschpe 4:e1e45f8a7664 4 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
dreschpe 4:e1e45f8a7664 5 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
dreschpe 4:e1e45f8a7664 6 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
dreschpe 4:e1e45f8a7664 7 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
dreschpe 4:e1e45f8a7664 8 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
dreschpe 4:e1e45f8a7664 9 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
dreschpe 4:e1e45f8a7664 10 * THE SOFTWARE.
dreschpe 4:e1e45f8a7664 11 */
dreschpe 6:fc33e4a5713e 12
dreschpe 6:fc33e4a5713e 13
dreschpe 6:fc33e4a5713e 14 // fix bmp padding for Bitmap function
dreschpe 10:1e868317ff49 15 // speed up pixel
dreschpe 10:1e868317ff49 16 // 30.12.11 fix cls
dreschpe 6:fc33e4a5713e 17
dreschpe 4:e1e45f8a7664 18
dreschpe 4:e1e45f8a7664 19 #include "SPI_TFT.h"
dreschpe 4:e1e45f8a7664 20 #include "mbed.h"
dreschpe 4:e1e45f8a7664 21
dreschpe 4:e1e45f8a7664 22
dreschpe 4:e1e45f8a7664 23 #define BPP 16 // Bits per pixel
dreschpe 4:e1e45f8a7664 24
dreschpe 7:4781bb8eed45 25 //DigitalOut led(LED1);
dreschpe 4:e1e45f8a7664 26
dreschpe 4:e1e45f8a7664 27 SPI_TFT::SPI_TFT(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset, const char *name)
dreschpe 4:e1e45f8a7664 28 : _spi(mosi, miso, sclk), _cs(cs), _reset(reset),GraphicsDisplay(name) {
dreschpe 4:e1e45f8a7664 29 tft_reset();
dreschpe 4:e1e45f8a7664 30 orientation = 0;
dreschpe 4:e1e45f8a7664 31 char_x = 0;
dreschpe 4:e1e45f8a7664 32 }
dreschpe 4:e1e45f8a7664 33
dreschpe 4:e1e45f8a7664 34 int SPI_TFT::width() {
dreschpe 4:e1e45f8a7664 35 if (orientation == 0 || orientation == 2) return 240;
dreschpe 4:e1e45f8a7664 36 else return 320;
dreschpe 4:e1e45f8a7664 37 }
dreschpe 4:e1e45f8a7664 38
dreschpe 4:e1e45f8a7664 39
dreschpe 4:e1e45f8a7664 40 int SPI_TFT::height() {
dreschpe 4:e1e45f8a7664 41 if (orientation == 0 || orientation == 2) return 320;
dreschpe 4:e1e45f8a7664 42 else return 240;
dreschpe 4:e1e45f8a7664 43 }
dreschpe 4:e1e45f8a7664 44
dreschpe 4:e1e45f8a7664 45
dreschpe 4:e1e45f8a7664 46
dreschpe 4:e1e45f8a7664 47 void SPI_TFT::set_orientation(unsigned int o) {
dreschpe 4:e1e45f8a7664 48 orientation = o;
dreschpe 4:e1e45f8a7664 49 switch (orientation) {
dreschpe 4:e1e45f8a7664 50 case 0:
dreschpe 4:e1e45f8a7664 51 wr_reg(0x16, 0x0008);
dreschpe 4:e1e45f8a7664 52 break;
dreschpe 4:e1e45f8a7664 53 case 1:
dreschpe 4:e1e45f8a7664 54 wr_reg(0x16, 0x0068);
dreschpe 4:e1e45f8a7664 55 break;
dreschpe 4:e1e45f8a7664 56 case 2:
dreschpe 4:e1e45f8a7664 57 wr_reg(0x16, 0x00C8);
dreschpe 4:e1e45f8a7664 58 break;
dreschpe 4:e1e45f8a7664 59 case 3:
dreschpe 4:e1e45f8a7664 60 wr_reg(0x16, 0x00A8);
dreschpe 4:e1e45f8a7664 61 break;
dreschpe 4:e1e45f8a7664 62 }
dreschpe 4:e1e45f8a7664 63 }
dreschpe 4:e1e45f8a7664 64
dreschpe 4:e1e45f8a7664 65
dreschpe 4:e1e45f8a7664 66
dreschpe 4:e1e45f8a7664 67 void SPI_TFT::wr_cmd(int cmd) {
dreschpe 4:e1e45f8a7664 68 _cs = 0;
dreschpe 4:e1e45f8a7664 69 _spi.write(SPI_START | SPI_WR | SPI_INDEX); /* Write : RS = 0, RW = 0 */
dreschpe 4:e1e45f8a7664 70 _spi.write(cmd);
dreschpe 4:e1e45f8a7664 71 _cs = 1;
dreschpe 4:e1e45f8a7664 72 }
dreschpe 4:e1e45f8a7664 73
dreschpe 4:e1e45f8a7664 74
dreschpe 4:e1e45f8a7664 75
dreschpe 4:e1e45f8a7664 76 void SPI_TFT::wr_dat(int dat) {
dreschpe 4:e1e45f8a7664 77 _cs = 0;
dreschpe 4:e1e45f8a7664 78 _spi.write(SPI_START | SPI_WR | SPI_DATA); // Write : RS = 1, RW = 0
dreschpe 4:e1e45f8a7664 79 _spi.format(16,3); // switch to 16 bit Mode 3
dreschpe 4:e1e45f8a7664 80 _spi.write(dat); // Write D0..D15
dreschpe 4:e1e45f8a7664 81 _spi.format(8,3); // 8 bit Mode 3
dreschpe 4:e1e45f8a7664 82 _cs = 1;
dreschpe 4:e1e45f8a7664 83 }
dreschpe 4:e1e45f8a7664 84
dreschpe 4:e1e45f8a7664 85
dreschpe 4:e1e45f8a7664 86
dreschpe 4:e1e45f8a7664 87 void SPI_TFT::wr_dat_start(void) {
dreschpe 4:e1e45f8a7664 88 _cs = 0;
dreschpe 4:e1e45f8a7664 89 _spi.write(SPI_START | SPI_WR | SPI_DATA); /* Write : RS = 1, RW = 0 */
dreschpe 4:e1e45f8a7664 90 }
dreschpe 4:e1e45f8a7664 91
dreschpe 4:e1e45f8a7664 92
dreschpe 4:e1e45f8a7664 93
dreschpe 4:e1e45f8a7664 94 void SPI_TFT::wr_dat_stop (void) {
dreschpe 4:e1e45f8a7664 95 _cs = 1;
dreschpe 4:e1e45f8a7664 96 }
dreschpe 4:e1e45f8a7664 97
dreschpe 4:e1e45f8a7664 98
dreschpe 4:e1e45f8a7664 99
dreschpe 4:e1e45f8a7664 100 void SPI_TFT::wr_dat_only (unsigned short dat) {
dreschpe 4:e1e45f8a7664 101
dreschpe 4:e1e45f8a7664 102 _spi.format(16,3); // switch to 16 bit Mode 3
dreschpe 4:e1e45f8a7664 103 _spi.write(dat); // Write D0..D15
dreschpe 4:e1e45f8a7664 104 _spi.format(8,3); // 8 bit Mode 3
dreschpe 4:e1e45f8a7664 105 }
dreschpe 4:e1e45f8a7664 106
dreschpe 4:e1e45f8a7664 107
dreschpe 4:e1e45f8a7664 108
dreschpe 4:e1e45f8a7664 109 unsigned short SPI_TFT::rd_dat (void) {
dreschpe 4:e1e45f8a7664 110 unsigned short val = 0;
dreschpe 4:e1e45f8a7664 111
dreschpe 4:e1e45f8a7664 112 _cs = 0;
dreschpe 4:e1e45f8a7664 113 _spi.write(SPI_START | SPI_RD | SPI_DATA); /* Read: RS = 1, RW = 1 */
dreschpe 4:e1e45f8a7664 114 _spi.write(0); /* Dummy read 1 */
dreschpe 4:e1e45f8a7664 115 val = _spi.write(0); /* Read D8..D15 */
dreschpe 4:e1e45f8a7664 116 val <<= 8;
dreschpe 4:e1e45f8a7664 117 val |= _spi.write(0); /* Read D0..D7 */
dreschpe 4:e1e45f8a7664 118 _cs = 1;
dreschpe 4:e1e45f8a7664 119 return (val);
dreschpe 4:e1e45f8a7664 120 }
dreschpe 4:e1e45f8a7664 121
dreschpe 4:e1e45f8a7664 122
dreschpe 4:e1e45f8a7664 123
dreschpe 4:e1e45f8a7664 124 void SPI_TFT::wr_reg (unsigned char reg, unsigned short val) {
dreschpe 4:e1e45f8a7664 125
dreschpe 4:e1e45f8a7664 126 wr_cmd(reg);
dreschpe 4:e1e45f8a7664 127 wr_dat(val);
dreschpe 4:e1e45f8a7664 128 }
dreschpe 4:e1e45f8a7664 129
dreschpe 4:e1e45f8a7664 130
dreschpe 4:e1e45f8a7664 131
dreschpe 4:e1e45f8a7664 132 unsigned short SPI_TFT::rd_reg (unsigned char reg) {
dreschpe 4:e1e45f8a7664 133
dreschpe 4:e1e45f8a7664 134 wr_cmd(reg);
dreschpe 4:e1e45f8a7664 135 return(rd_dat());
dreschpe 4:e1e45f8a7664 136 }
dreschpe 4:e1e45f8a7664 137
dreschpe 4:e1e45f8a7664 138
dreschpe 4:e1e45f8a7664 139
dreschpe 4:e1e45f8a7664 140 void SPI_TFT::tft_reset() {
dreschpe 4:e1e45f8a7664 141 static unsigned short driverCode;
dreschpe 4:e1e45f8a7664 142 _spi.format(8,3); // 8 bit spi mode 3
dreschpe 4:e1e45f8a7664 143 _spi.frequency(48000000); // 48Mhz SPI clock
dreschpe 4:e1e45f8a7664 144 _reset = 0; // reset
dreschpe 4:e1e45f8a7664 145 _cs = 1;
dreschpe 4:e1e45f8a7664 146 wait_us(50);
dreschpe 4:e1e45f8a7664 147 _reset = 1; // end reset
dreschpe 4:e1e45f8a7664 148 wait_ms(5);
dreschpe 4:e1e45f8a7664 149
dreschpe 4:e1e45f8a7664 150 driverCode = rd_reg(0x00); // read controller ID
dreschpe 4:e1e45f8a7664 151 //printf("Disp_ID = %x",driverCode);
dreschpe 4:e1e45f8a7664 152
dreschpe 4:e1e45f8a7664 153 /* Start Initial Sequence ----------------------------------------------------*/
dreschpe 4:e1e45f8a7664 154 wr_reg(0xEA, 0x0000); /* Reset Power Control 1 */
dreschpe 4:e1e45f8a7664 155 wr_reg(0xEB, 0x0020); /* Power Control 2 */
dreschpe 4:e1e45f8a7664 156 wr_reg(0xEC, 0x000C); /* Power Control 3 */
dreschpe 4:e1e45f8a7664 157 wr_reg(0xED, 0x00C4); /* Power Control 4 */
dreschpe 4:e1e45f8a7664 158 wr_reg(0xE8, 0x0040); /* Source OPON_N */
dreschpe 4:e1e45f8a7664 159 wr_reg(0xE9, 0x0038); /* Source OPON_I */
dreschpe 4:e1e45f8a7664 160 wr_reg(0xF1, 0x0001); /* */
dreschpe 4:e1e45f8a7664 161 wr_reg(0xF2, 0x0010); /* */
dreschpe 4:e1e45f8a7664 162 wr_reg(0x27, 0x00A3); /* Display Control 2 */
dreschpe 4:e1e45f8a7664 163
dreschpe 4:e1e45f8a7664 164 /* Power On sequence ---------------------------------------------------------*/
dreschpe 4:e1e45f8a7664 165 wr_reg(0x1B, 0x001B); /* Power Control 2 */
dreschpe 4:e1e45f8a7664 166 wr_reg(0x1A, 0x0001); /* Power Control 1 */
dreschpe 4:e1e45f8a7664 167 wr_reg(0x24, 0x002F); /* Vcom Control 2 */
dreschpe 4:e1e45f8a7664 168 wr_reg(0x25, 0x0057); /* Vcom Control 3 */
dreschpe 4:e1e45f8a7664 169 wr_reg(0x23, 0x008D); /* Vcom Control 1 */
dreschpe 4:e1e45f8a7664 170
dreschpe 4:e1e45f8a7664 171 /* Gamma settings -----------------------------------------------------------*/
dreschpe 4:e1e45f8a7664 172 wr_reg(0x40,0x00); //
dreschpe 4:e1e45f8a7664 173 wr_reg(0x41,0x00); //
dreschpe 4:e1e45f8a7664 174 wr_reg(0x42,0x01); //
dreschpe 4:e1e45f8a7664 175 wr_reg(0x43,0x13); //
dreschpe 4:e1e45f8a7664 176 wr_reg(0x44,0x10); //
dreschpe 4:e1e45f8a7664 177 wr_reg(0x45,0x26); //
dreschpe 4:e1e45f8a7664 178 wr_reg(0x46,0x08); //
dreschpe 4:e1e45f8a7664 179 wr_reg(0x47,0x51); //
dreschpe 4:e1e45f8a7664 180 wr_reg(0x48,0x02); //
dreschpe 4:e1e45f8a7664 181 wr_reg(0x49,0x12); //
dreschpe 4:e1e45f8a7664 182 wr_reg(0x4A,0x18); //
dreschpe 4:e1e45f8a7664 183 wr_reg(0x4B,0x19); //
dreschpe 4:e1e45f8a7664 184 wr_reg(0x4C,0x14); //
dreschpe 4:e1e45f8a7664 185 wr_reg(0x50,0x19); //
dreschpe 4:e1e45f8a7664 186 wr_reg(0x51,0x2F); //
dreschpe 4:e1e45f8a7664 187 wr_reg(0x52,0x2C); //
dreschpe 4:e1e45f8a7664 188 wr_reg(0x53,0x3E); //
dreschpe 4:e1e45f8a7664 189 wr_reg(0x54,0x3F); //
dreschpe 4:e1e45f8a7664 190 wr_reg(0x55,0x3F); //
dreschpe 4:e1e45f8a7664 191 wr_reg(0x56,0x2E); //
dreschpe 4:e1e45f8a7664 192 wr_reg(0x57,0x77); //
dreschpe 4:e1e45f8a7664 193 wr_reg(0x58,0x0B); //
dreschpe 4:e1e45f8a7664 194 wr_reg(0x59,0x06); //
dreschpe 4:e1e45f8a7664 195 wr_reg(0x5A,0x07); //
dreschpe 4:e1e45f8a7664 196 wr_reg(0x5B,0x0D); //
dreschpe 4:e1e45f8a7664 197 wr_reg(0x5C,0x1D); //
dreschpe 4:e1e45f8a7664 198 wr_reg(0x5D,0xCC); //
dreschpe 4:e1e45f8a7664 199
dreschpe 4:e1e45f8a7664 200 /* Power + Osc ---------------------------------------------------------------*/
dreschpe 4:e1e45f8a7664 201 wr_reg(0x18, 0x0036); /* OSC Control 1 */
dreschpe 4:e1e45f8a7664 202 wr_reg(0x19, 0x0001); /* OSC Control 2 */
dreschpe 4:e1e45f8a7664 203 wr_reg(0x01, 0x0000); /* Display Mode Control */
dreschpe 4:e1e45f8a7664 204 wr_reg(0x1F, 0x0088); /* Power Control 6 */
dreschpe 4:e1e45f8a7664 205 wait_ms(5); /* Delay 5 ms */
dreschpe 4:e1e45f8a7664 206 wr_reg(0x1F, 0x0080); /* Power Control 6 */
dreschpe 4:e1e45f8a7664 207 wait_ms(5); /* Delay 5 ms */
dreschpe 4:e1e45f8a7664 208 wr_reg(0x1F, 0x0090); /* Power Control 6 */
dreschpe 4:e1e45f8a7664 209 wait_ms(5); /* Delay 5 ms */
dreschpe 4:e1e45f8a7664 210 wr_reg(0x1F, 0x00D0); /* Power Control 6 */
dreschpe 4:e1e45f8a7664 211 wait_ms(5); /* Delay 5 ms */
dreschpe 4:e1e45f8a7664 212
dreschpe 4:e1e45f8a7664 213 wr_reg(0x17, 0x0005); /* Colmod 16Bit/Pixel */
dreschpe 4:e1e45f8a7664 214
dreschpe 4:e1e45f8a7664 215 wr_reg(0x36, 0x0000); /* Panel Characteristic */
dreschpe 4:e1e45f8a7664 216 wr_reg(0x28, 0x0038); /* Display Control 3 */
dreschpe 4:e1e45f8a7664 217 wait_ms(40);
dreschpe 4:e1e45f8a7664 218 wr_reg(0x28, 0x003C); /* Display Control 3 */
dreschpe 4:e1e45f8a7664 219 switch (orientation) {
dreschpe 4:e1e45f8a7664 220 case 0:
dreschpe 4:e1e45f8a7664 221 wr_reg(0x16, 0x0008);
dreschpe 4:e1e45f8a7664 222 break;
dreschpe 4:e1e45f8a7664 223 case 1:
dreschpe 4:e1e45f8a7664 224 wr_reg(0x16, 0x0068);
dreschpe 4:e1e45f8a7664 225 break;
dreschpe 4:e1e45f8a7664 226 case 2:
dreschpe 4:e1e45f8a7664 227 wr_reg(0x16, 0x00C8);
dreschpe 4:e1e45f8a7664 228 break;
dreschpe 4:e1e45f8a7664 229 case 3:
dreschpe 4:e1e45f8a7664 230 wr_reg(0x16, 0x00A8);
dreschpe 4:e1e45f8a7664 231 break;
dreschpe 4:e1e45f8a7664 232 }
dreschpe 4:e1e45f8a7664 233
dreschpe 4:e1e45f8a7664 234 WindowMax ();
dreschpe 4:e1e45f8a7664 235 }
dreschpe 4:e1e45f8a7664 236
dreschpe 4:e1e45f8a7664 237
dreschpe 4:e1e45f8a7664 238
dreschpe 4:e1e45f8a7664 239
dreschpe 4:e1e45f8a7664 240 void SPI_TFT::pixel(int x, int y, int color) {
dreschpe 4:e1e45f8a7664 241 wr_reg(0x03, (x >> 0));
dreschpe 4:e1e45f8a7664 242 wr_reg(0x02, (x >> 8));
dreschpe 4:e1e45f8a7664 243 wr_reg(0x07, (y >> 0));
dreschpe 4:e1e45f8a7664 244 wr_reg(0x06, (y >> 8));
dreschpe 9:9dc5dfdda734 245 //wr_reg(0x05, (x+1 >> 0));
dreschpe 9:9dc5dfdda734 246 //wr_reg(0x04, (x+1 >> 8));
dreschpe 9:9dc5dfdda734 247 //wr_reg(0x09, (y+1 >> 0));
dreschpe 9:9dc5dfdda734 248 //wr_reg(0x08, (y+1 >> 8));
dreschpe 4:e1e45f8a7664 249 wr_cmd(0x22);
dreschpe 4:e1e45f8a7664 250 wr_dat(color);
dreschpe 4:e1e45f8a7664 251 }
dreschpe 4:e1e45f8a7664 252
dreschpe 4:e1e45f8a7664 253
dreschpe 4:e1e45f8a7664 254
dreschpe 4:e1e45f8a7664 255
dreschpe 4:e1e45f8a7664 256 void SPI_TFT::window (unsigned int x, unsigned int y, unsigned int w, unsigned int h) {
dreschpe 4:e1e45f8a7664 257 wr_reg(0x03, (x >> 0));
dreschpe 4:e1e45f8a7664 258 wr_reg(0x02, (x >> 8));
dreschpe 4:e1e45f8a7664 259 wr_reg(0x05, (x+w-1 >> 0));
dreschpe 4:e1e45f8a7664 260 wr_reg(0x04, (x+w-1 >> 8));
dreschpe 4:e1e45f8a7664 261 wr_reg(0x07, ( y >> 0));
dreschpe 4:e1e45f8a7664 262 wr_reg(0x06, ( y >> 8));
dreschpe 4:e1e45f8a7664 263 wr_reg(0x09, ( y+h-1 >> 0));
dreschpe 4:e1e45f8a7664 264 wr_reg(0x08, ( y+h-1 >> 8));
dreschpe 4:e1e45f8a7664 265 //wr_cmd(0x22);
dreschpe 4:e1e45f8a7664 266 }
dreschpe 4:e1e45f8a7664 267
dreschpe 4:e1e45f8a7664 268
dreschpe 4:e1e45f8a7664 269 void SPI_TFT::WindowMax (void) {
dreschpe 4:e1e45f8a7664 270 window (0, 0, width(), height());
dreschpe 4:e1e45f8a7664 271 }
dreschpe 4:e1e45f8a7664 272
dreschpe 4:e1e45f8a7664 273
dreschpe 4:e1e45f8a7664 274 void SPI_TFT::cls (void) {
dreschpe 4:e1e45f8a7664 275 unsigned int i;
dreschpe 10:1e868317ff49 276 WindowMax();
dreschpe 4:e1e45f8a7664 277 wr_cmd(0x22);
dreschpe 4:e1e45f8a7664 278 wr_dat_start();
dreschpe 4:e1e45f8a7664 279 _spi.format(16,3); // 16 bit Mode 3
dreschpe 4:e1e45f8a7664 280 for (i = 0; i < ( width() * height()); i++)
dreschpe 4:e1e45f8a7664 281 _spi.write(_background);
dreschpe 4:e1e45f8a7664 282 _spi.format(8,3); // 8 bit Mode 3
dreschpe 4:e1e45f8a7664 283 wr_dat_stop();
dreschpe 4:e1e45f8a7664 284 }
dreschpe 4:e1e45f8a7664 285
dreschpe 4:e1e45f8a7664 286
dreschpe 4:e1e45f8a7664 287 void SPI_TFT::circle(int x0, int y0, int r, int color) {
dreschpe 4:e1e45f8a7664 288
dreschpe 4:e1e45f8a7664 289 int draw_x0, draw_y0;
dreschpe 4:e1e45f8a7664 290 int draw_x1, draw_y1;
dreschpe 4:e1e45f8a7664 291 int draw_x2, draw_y2;
dreschpe 4:e1e45f8a7664 292 int draw_x3, draw_y3;
dreschpe 4:e1e45f8a7664 293 int draw_x4, draw_y4;
dreschpe 4:e1e45f8a7664 294 int draw_x5, draw_y5;
dreschpe 4:e1e45f8a7664 295 int draw_x6, draw_y6;
dreschpe 4:e1e45f8a7664 296 int draw_x7, draw_y7;
dreschpe 4:e1e45f8a7664 297 int xx, yy;
dreschpe 4:e1e45f8a7664 298 int di;
dreschpe 9:9dc5dfdda734 299 //WindowMax();
dreschpe 4:e1e45f8a7664 300 if (r == 0) { /* no radius */
dreschpe 4:e1e45f8a7664 301 return;
dreschpe 4:e1e45f8a7664 302 }
dreschpe 4:e1e45f8a7664 303
dreschpe 4:e1e45f8a7664 304 draw_x0 = draw_x1 = x0;
dreschpe 4:e1e45f8a7664 305 draw_y0 = draw_y1 = y0 + r;
dreschpe 4:e1e45f8a7664 306 if (draw_y0 < height()) {
dreschpe 4:e1e45f8a7664 307 pixel(draw_x0, draw_y0, color); /* 90 degree */
dreschpe 4:e1e45f8a7664 308 }
dreschpe 4:e1e45f8a7664 309
dreschpe 4:e1e45f8a7664 310 draw_x2 = draw_x3 = x0;
dreschpe 4:e1e45f8a7664 311 draw_y2 = draw_y3 = y0 - r;
dreschpe 4:e1e45f8a7664 312 if (draw_y2 >= 0) {
dreschpe 4:e1e45f8a7664 313 pixel(draw_x2, draw_y2, color); /* 270 degree */
dreschpe 4:e1e45f8a7664 314 }
dreschpe 4:e1e45f8a7664 315
dreschpe 4:e1e45f8a7664 316 draw_x4 = draw_x6 = x0 + r;
dreschpe 4:e1e45f8a7664 317 draw_y4 = draw_y6 = y0;
dreschpe 4:e1e45f8a7664 318 if (draw_x4 < width()) {
dreschpe 4:e1e45f8a7664 319 pixel(draw_x4, draw_y4, color); /* 0 degree */
dreschpe 4:e1e45f8a7664 320 }
dreschpe 4:e1e45f8a7664 321
dreschpe 4:e1e45f8a7664 322 draw_x5 = draw_x7 = x0 - r;
dreschpe 4:e1e45f8a7664 323 draw_y5 = draw_y7 = y0;
dreschpe 4:e1e45f8a7664 324 if (draw_x5>=0) {
dreschpe 4:e1e45f8a7664 325 pixel(draw_x5, draw_y5, color); /* 180 degree */
dreschpe 4:e1e45f8a7664 326 }
dreschpe 4:e1e45f8a7664 327
dreschpe 4:e1e45f8a7664 328 if (r == 1) {
dreschpe 4:e1e45f8a7664 329 return;
dreschpe 4:e1e45f8a7664 330 }
dreschpe 4:e1e45f8a7664 331
dreschpe 4:e1e45f8a7664 332 di = 3 - 2*r;
dreschpe 4:e1e45f8a7664 333 xx = 0;
dreschpe 4:e1e45f8a7664 334 yy = r;
dreschpe 4:e1e45f8a7664 335 while (xx < yy) {
dreschpe 4:e1e45f8a7664 336
dreschpe 4:e1e45f8a7664 337 if (di < 0) {
dreschpe 4:e1e45f8a7664 338 di += 4*xx + 6;
dreschpe 4:e1e45f8a7664 339 } else {
dreschpe 4:e1e45f8a7664 340 di += 4*(xx - yy) + 10;
dreschpe 4:e1e45f8a7664 341 yy--;
dreschpe 4:e1e45f8a7664 342 draw_y0--;
dreschpe 4:e1e45f8a7664 343 draw_y1--;
dreschpe 4:e1e45f8a7664 344 draw_y2++;
dreschpe 4:e1e45f8a7664 345 draw_y3++;
dreschpe 4:e1e45f8a7664 346 draw_x4--;
dreschpe 4:e1e45f8a7664 347 draw_x5++;
dreschpe 4:e1e45f8a7664 348 draw_x6--;
dreschpe 4:e1e45f8a7664 349 draw_x7++;
dreschpe 4:e1e45f8a7664 350 }
dreschpe 4:e1e45f8a7664 351 xx++;
dreschpe 4:e1e45f8a7664 352 draw_x0++;
dreschpe 4:e1e45f8a7664 353 draw_x1--;
dreschpe 4:e1e45f8a7664 354 draw_x2++;
dreschpe 4:e1e45f8a7664 355 draw_x3--;
dreschpe 4:e1e45f8a7664 356 draw_y4++;
dreschpe 4:e1e45f8a7664 357 draw_y5++;
dreschpe 4:e1e45f8a7664 358 draw_y6--;
dreschpe 4:e1e45f8a7664 359 draw_y7--;
dreschpe 4:e1e45f8a7664 360
dreschpe 4:e1e45f8a7664 361 if ( (draw_x0 <= width()) && (draw_y0>=0) ) {
dreschpe 4:e1e45f8a7664 362 pixel(draw_x0, draw_y0, color);
dreschpe 4:e1e45f8a7664 363 }
dreschpe 4:e1e45f8a7664 364
dreschpe 4:e1e45f8a7664 365 if ( (draw_x1 >= 0) && (draw_y1 >= 0) ) {
dreschpe 4:e1e45f8a7664 366 pixel(draw_x1, draw_y1, color);
dreschpe 4:e1e45f8a7664 367 }
dreschpe 4:e1e45f8a7664 368
dreschpe 4:e1e45f8a7664 369 if ( (draw_x2 <= width()) && (draw_y2 <= height()) ) {
dreschpe 4:e1e45f8a7664 370 pixel(draw_x2, draw_y2, color);
dreschpe 4:e1e45f8a7664 371 }
dreschpe 4:e1e45f8a7664 372
dreschpe 4:e1e45f8a7664 373 if ( (draw_x3 >=0 ) && (draw_y3 <= height()) ) {
dreschpe 4:e1e45f8a7664 374 pixel(draw_x3, draw_y3, color);
dreschpe 4:e1e45f8a7664 375 }
dreschpe 4:e1e45f8a7664 376
dreschpe 4:e1e45f8a7664 377 if ( (draw_x4 <= width()) && (draw_y4 >= 0) ) {
dreschpe 4:e1e45f8a7664 378 pixel(draw_x4, draw_y4, color);
dreschpe 4:e1e45f8a7664 379 }
dreschpe 4:e1e45f8a7664 380
dreschpe 4:e1e45f8a7664 381 if ( (draw_x5 >= 0) && (draw_y5 >= 0) ) {
dreschpe 4:e1e45f8a7664 382 pixel(draw_x5, draw_y5, color);
dreschpe 4:e1e45f8a7664 383 }
dreschpe 4:e1e45f8a7664 384 if ( (draw_x6 <=width()) && (draw_y6 <= height()) ) {
dreschpe 4:e1e45f8a7664 385 pixel(draw_x6, draw_y6, color);
dreschpe 4:e1e45f8a7664 386 }
dreschpe 4:e1e45f8a7664 387 if ( (draw_x7 >= 0) && (draw_y7 <= height()) ) {
dreschpe 4:e1e45f8a7664 388 pixel(draw_x7, draw_y7, color);
dreschpe 4:e1e45f8a7664 389 }
dreschpe 4:e1e45f8a7664 390 }
dreschpe 4:e1e45f8a7664 391 return;
dreschpe 4:e1e45f8a7664 392 }
dreschpe 4:e1e45f8a7664 393
dreschpe 5:2db1b8070d94 394 void SPI_TFT::fillcircle(int x, int y, int r, int color) {
dreschpe 4:e1e45f8a7664 395 int i;
dreschpe 5:2db1b8070d94 396 for (i = 0; i <= r; i++)
dreschpe 4:e1e45f8a7664 397 circle(x,y,i,color);
dreschpe 4:e1e45f8a7664 398 }
dreschpe 4:e1e45f8a7664 399
dreschpe 4:e1e45f8a7664 400
dreschpe 4:e1e45f8a7664 401
dreschpe 4:e1e45f8a7664 402 void SPI_TFT::hline(int x0, int x1, int y, int color) {
dreschpe 4:e1e45f8a7664 403 int w;
dreschpe 4:e1e45f8a7664 404 w = x1 - x0 + 1;
dreschpe 4:e1e45f8a7664 405 window(x0,y,w,1);
dreschpe 4:e1e45f8a7664 406 wr_cmd(0x22);
dreschpe 4:e1e45f8a7664 407 wr_dat_start();
dreschpe 4:e1e45f8a7664 408 _spi.format(16,3); // pixel are send in 16 bit mode to speed up
dreschpe 4:e1e45f8a7664 409 for (int x=0; x<w; x++) {
dreschpe 4:e1e45f8a7664 410 _spi.write(color);
dreschpe 4:e1e45f8a7664 411 }
dreschpe 4:e1e45f8a7664 412 _spi.format(8,3);
dreschpe 4:e1e45f8a7664 413 wr_dat_stop();
dreschpe 9:9dc5dfdda734 414 WindowMax();
dreschpe 4:e1e45f8a7664 415 return;
dreschpe 4:e1e45f8a7664 416 }
dreschpe 4:e1e45f8a7664 417
dreschpe 4:e1e45f8a7664 418
dreschpe 4:e1e45f8a7664 419
dreschpe 4:e1e45f8a7664 420 void SPI_TFT::vline(int x, int y0, int y1, int color) {
dreschpe 4:e1e45f8a7664 421 int h;
dreschpe 4:e1e45f8a7664 422 h = y1 - y0 + 1;
dreschpe 4:e1e45f8a7664 423 window(x,y0,1,h);
dreschpe 4:e1e45f8a7664 424 wr_cmd(0x22);
dreschpe 4:e1e45f8a7664 425 wr_dat_start();
dreschpe 4:e1e45f8a7664 426 _spi.format(16,3); // pixel are send in 16 bit mode to speed up
dreschpe 4:e1e45f8a7664 427 for (int y=0; y<h; y++) {
dreschpe 4:e1e45f8a7664 428 _spi.write(color);
dreschpe 4:e1e45f8a7664 429 }
dreschpe 4:e1e45f8a7664 430 _spi.format(8,3);
dreschpe 4:e1e45f8a7664 431 wr_dat_stop();
dreschpe 9:9dc5dfdda734 432 WindowMax();
dreschpe 4:e1e45f8a7664 433 return;
dreschpe 4:e1e45f8a7664 434 }
dreschpe 4:e1e45f8a7664 435
dreschpe 4:e1e45f8a7664 436
dreschpe 4:e1e45f8a7664 437
dreschpe 4:e1e45f8a7664 438 void SPI_TFT::line(int x0, int y0, int x1, int y1, int color) {
dreschpe 9:9dc5dfdda734 439 //WindowMax();
dreschpe 4:e1e45f8a7664 440 int dx = 0, dy = 0;
dreschpe 4:e1e45f8a7664 441 int dx_sym = 0, dy_sym = 0;
dreschpe 4:e1e45f8a7664 442 int dx_x2 = 0, dy_x2 = 0;
dreschpe 4:e1e45f8a7664 443 int di = 0;
dreschpe 4:e1e45f8a7664 444
dreschpe 4:e1e45f8a7664 445 dx = x1-x0;
dreschpe 4:e1e45f8a7664 446 dy = y1-y0;
dreschpe 4:e1e45f8a7664 447
dreschpe 4:e1e45f8a7664 448 if (dx == 0) { /* vertical line */
dreschpe 4:e1e45f8a7664 449 if (y1 > y0) vline(x0,y0,y1,color);
dreschpe 4:e1e45f8a7664 450 else vline(x0,y1,y0,color);
dreschpe 4:e1e45f8a7664 451 return;
dreschpe 4:e1e45f8a7664 452 }
dreschpe 4:e1e45f8a7664 453
dreschpe 4:e1e45f8a7664 454 if (dx > 0) {
dreschpe 4:e1e45f8a7664 455 dx_sym = 1;
dreschpe 4:e1e45f8a7664 456 } else {
dreschpe 4:e1e45f8a7664 457 dx_sym = -1;
dreschpe 4:e1e45f8a7664 458 }
dreschpe 4:e1e45f8a7664 459 if (dy == 0) { /* horizontal line */
dreschpe 4:e1e45f8a7664 460 if (x1 > x0) hline(x0,x1,y0,color);
dreschpe 4:e1e45f8a7664 461 else hline(x1,x0,y0,color);
dreschpe 4:e1e45f8a7664 462 return;
dreschpe 4:e1e45f8a7664 463 }
dreschpe 4:e1e45f8a7664 464
dreschpe 4:e1e45f8a7664 465 if (dy > 0) {
dreschpe 4:e1e45f8a7664 466 dy_sym = 1;
dreschpe 4:e1e45f8a7664 467 } else {
dreschpe 4:e1e45f8a7664 468 dy_sym = -1;
dreschpe 4:e1e45f8a7664 469 }
dreschpe 4:e1e45f8a7664 470
dreschpe 4:e1e45f8a7664 471 dx = dx_sym*dx;
dreschpe 4:e1e45f8a7664 472 dy = dy_sym*dy;
dreschpe 4:e1e45f8a7664 473
dreschpe 4:e1e45f8a7664 474 dx_x2 = dx*2;
dreschpe 4:e1e45f8a7664 475 dy_x2 = dy*2;
dreschpe 4:e1e45f8a7664 476
dreschpe 4:e1e45f8a7664 477 if (dx >= dy) {
dreschpe 4:e1e45f8a7664 478 di = dy_x2 - dx;
dreschpe 4:e1e45f8a7664 479 while (x0 != x1) {
dreschpe 4:e1e45f8a7664 480
dreschpe 4:e1e45f8a7664 481 pixel(x0, y0, color);
dreschpe 4:e1e45f8a7664 482 x0 += dx_sym;
dreschpe 4:e1e45f8a7664 483 if (di<0) {
dreschpe 4:e1e45f8a7664 484 di += dy_x2;
dreschpe 4:e1e45f8a7664 485 } else {
dreschpe 4:e1e45f8a7664 486 di += dy_x2 - dx_x2;
dreschpe 4:e1e45f8a7664 487 y0 += dy_sym;
dreschpe 4:e1e45f8a7664 488 }
dreschpe 4:e1e45f8a7664 489 }
dreschpe 4:e1e45f8a7664 490 pixel(x0, y0, color);
dreschpe 4:e1e45f8a7664 491 } else {
dreschpe 4:e1e45f8a7664 492 di = dx_x2 - dy;
dreschpe 4:e1e45f8a7664 493 while (y0 != y1) {
dreschpe 4:e1e45f8a7664 494 pixel(x0, y0, color);
dreschpe 4:e1e45f8a7664 495 y0 += dy_sym;
dreschpe 4:e1e45f8a7664 496 if (di < 0) {
dreschpe 4:e1e45f8a7664 497 di += dx_x2;
dreschpe 4:e1e45f8a7664 498 } else {
dreschpe 4:e1e45f8a7664 499 di += dx_x2 - dy_x2;
dreschpe 4:e1e45f8a7664 500 x0 += dx_sym;
dreschpe 4:e1e45f8a7664 501 }
dreschpe 4:e1e45f8a7664 502 }
dreschpe 4:e1e45f8a7664 503 pixel(x0, y0, color);
dreschpe 4:e1e45f8a7664 504 }
dreschpe 4:e1e45f8a7664 505 return;
dreschpe 4:e1e45f8a7664 506 }
dreschpe 4:e1e45f8a7664 507
dreschpe 4:e1e45f8a7664 508
dreschpe 4:e1e45f8a7664 509
dreschpe 4:e1e45f8a7664 510
dreschpe 4:e1e45f8a7664 511 void SPI_TFT::rect(int x0, int y0, int x1, int y1, int color) {
dreschpe 4:e1e45f8a7664 512
dreschpe 4:e1e45f8a7664 513 if (x1 > x0) hline(x0,x1,y0,color);
dreschpe 4:e1e45f8a7664 514 else hline(x1,x0,y0,color);
dreschpe 4:e1e45f8a7664 515
dreschpe 4:e1e45f8a7664 516 if (y1 > y0) vline(x0,y0,y1,color);
dreschpe 4:e1e45f8a7664 517 else vline(x0,y1,y0,color);
dreschpe 4:e1e45f8a7664 518
dreschpe 4:e1e45f8a7664 519 if (x1 > x0) hline(x0,x1,y1,color);
dreschpe 4:e1e45f8a7664 520 else hline(x1,x0,y1,color);
dreschpe 4:e1e45f8a7664 521
dreschpe 4:e1e45f8a7664 522 if (y1 > y0) vline(x1,y0,y1,color);
dreschpe 4:e1e45f8a7664 523 else vline(x1,y1,y0,color);
dreschpe 4:e1e45f8a7664 524
dreschpe 4:e1e45f8a7664 525 return;
dreschpe 4:e1e45f8a7664 526 }
dreschpe 4:e1e45f8a7664 527
dreschpe 4:e1e45f8a7664 528
dreschpe 4:e1e45f8a7664 529
dreschpe 4:e1e45f8a7664 530 void SPI_TFT::fillrect(int x0, int y0, int x1, int y1, int color) {
dreschpe 4:e1e45f8a7664 531
dreschpe 4:e1e45f8a7664 532 int h = y1 - y0 + 1;
dreschpe 4:e1e45f8a7664 533 int w = x1 - x0 + 1;
dreschpe 4:e1e45f8a7664 534 int pixel = h * w;
dreschpe 4:e1e45f8a7664 535 window(x0,y0,w,h);
dreschpe 4:e1e45f8a7664 536 wr_cmd(0x22);
dreschpe 4:e1e45f8a7664 537 wr_dat_start();
dreschpe 4:e1e45f8a7664 538 _spi.format(16,3); // pixel are send in 16 bit mode to speed up
dreschpe 4:e1e45f8a7664 539 for (int p=0; p<pixel; p++) {
dreschpe 4:e1e45f8a7664 540 _spi.write(color);
dreschpe 4:e1e45f8a7664 541 }
dreschpe 4:e1e45f8a7664 542 _spi.format(8,3);
dreschpe 4:e1e45f8a7664 543 wr_dat_stop();
dreschpe 9:9dc5dfdda734 544 WindowMax();
dreschpe 4:e1e45f8a7664 545 return;
dreschpe 4:e1e45f8a7664 546 }
dreschpe 4:e1e45f8a7664 547
dreschpe 4:e1e45f8a7664 548
dreschpe 4:e1e45f8a7664 549
dreschpe 6:fc33e4a5713e 550 void SPI_TFT::locate(int x, int y) {
dreschpe 6:fc33e4a5713e 551 char_x = x;
dreschpe 6:fc33e4a5713e 552 char_y = y;
dreschpe 4:e1e45f8a7664 553 }
dreschpe 4:e1e45f8a7664 554
dreschpe 4:e1e45f8a7664 555
dreschpe 4:e1e45f8a7664 556
dreschpe 4:e1e45f8a7664 557 int SPI_TFT::columns() {
dreschpe 4:e1e45f8a7664 558 return width() / font[1];
dreschpe 4:e1e45f8a7664 559 }
dreschpe 4:e1e45f8a7664 560
dreschpe 4:e1e45f8a7664 561
dreschpe 4:e1e45f8a7664 562
dreschpe 4:e1e45f8a7664 563 int SPI_TFT::rows() {
dreschpe 4:e1e45f8a7664 564 return height() / font[2];
dreschpe 4:e1e45f8a7664 565 }
dreschpe 4:e1e45f8a7664 566
dreschpe 4:e1e45f8a7664 567
dreschpe 4:e1e45f8a7664 568
dreschpe 4:e1e45f8a7664 569 int SPI_TFT::_putc(int value) {
dreschpe 6:fc33e4a5713e 570 if (value == '\n') { // new line
dreschpe 4:e1e45f8a7664 571 char_x = 0;
dreschpe 6:fc33e4a5713e 572 char_y = char_y + font[2];
dreschpe 6:fc33e4a5713e 573 if (char_y >= height() - font[2]) {
dreschpe 6:fc33e4a5713e 574 char_y = 0;
dreschpe 4:e1e45f8a7664 575 }
dreschpe 4:e1e45f8a7664 576 } else {
dreschpe 6:fc33e4a5713e 577 character(char_x, char_y, value);
dreschpe 6:fc33e4a5713e 578 }
dreschpe 4:e1e45f8a7664 579 return value;
dreschpe 4:e1e45f8a7664 580 }
dreschpe 4:e1e45f8a7664 581
dreschpe 4:e1e45f8a7664 582
dreschpe 4:e1e45f8a7664 583
dreschpe 4:e1e45f8a7664 584
dreschpe 6:fc33e4a5713e 585 void SPI_TFT::character(int x, int y, int c) {
dreschpe 4:e1e45f8a7664 586 unsigned int hor,vert,offset,bpl,j,i,b;
dreschpe 4:e1e45f8a7664 587 unsigned char* zeichen;
dreschpe 4:e1e45f8a7664 588 unsigned char z,w;
dreschpe 4:e1e45f8a7664 589
dreschpe 4:e1e45f8a7664 590 if ((c < 31) || (c > 127)) return; // test char range
dreschpe 4:e1e45f8a7664 591
dreschpe 4:e1e45f8a7664 592 // read font parameter from start of array
dreschpe 4:e1e45f8a7664 593 offset = font[0]; // bytes / char
dreschpe 4:e1e45f8a7664 594 hor = font[1]; // get hor size of font
dreschpe 4:e1e45f8a7664 595 vert = font[2]; // get vert size of font
dreschpe 4:e1e45f8a7664 596 bpl = font[3]; // bytes per line
dreschpe 4:e1e45f8a7664 597
dreschpe 4:e1e45f8a7664 598 if (char_x + hor > width()) {
dreschpe 4:e1e45f8a7664 599 char_x = 0;
dreschpe 6:fc33e4a5713e 600 char_y = char_y + vert;
dreschpe 6:fc33e4a5713e 601 if (char_y >= height() - font[2]) {
dreschpe 6:fc33e4a5713e 602 char_y = 0;
dreschpe 4:e1e45f8a7664 603 }
dreschpe 4:e1e45f8a7664 604 }
dreschpe 4:e1e45f8a7664 605
dreschpe 6:fc33e4a5713e 606 window(char_x, char_y,hor,vert); // char box
dreschpe 4:e1e45f8a7664 607 wr_cmd(0x22);
dreschpe 4:e1e45f8a7664 608 wr_dat_start();
dreschpe 4:e1e45f8a7664 609 zeichen = &font[((c -32) * offset) + 4]; // start of char bitmap
dreschpe 4:e1e45f8a7664 610 w = zeichen[0]; // width of actual char
dreschpe 4:e1e45f8a7664 611 _spi.format(16,3); // pixel are 16 bit
dreschpe 4:e1e45f8a7664 612
dreschpe 4:e1e45f8a7664 613 for (j=0; j<vert; j++) { // vert line
dreschpe 4:e1e45f8a7664 614 for (i=0; i<hor; i++) { // horz line
dreschpe 4:e1e45f8a7664 615 z = zeichen[bpl * i + ((j & 0xF8) >> 3)+1];
dreschpe 4:e1e45f8a7664 616 b = 1 << (j & 0x07);
dreschpe 4:e1e45f8a7664 617 if (( z & b ) == 0x00) {
dreschpe 4:e1e45f8a7664 618 _spi.write(_background);
dreschpe 4:e1e45f8a7664 619 } else {
dreschpe 4:e1e45f8a7664 620 _spi.write(_foreground);
dreschpe 4:e1e45f8a7664 621 }
dreschpe 4:e1e45f8a7664 622 }
dreschpe 4:e1e45f8a7664 623 }
dreschpe 4:e1e45f8a7664 624 _spi.format(8,3); // 8 bit
dreschpe 4:e1e45f8a7664 625 wr_dat_stop();
dreschpe 9:9dc5dfdda734 626 WindowMax();
dreschpe 4:e1e45f8a7664 627 if ((w + 2) < hor) { // x offset to next char
dreschpe 4:e1e45f8a7664 628 char_x += w + 2;
dreschpe 4:e1e45f8a7664 629 } else char_x += hor;
dreschpe 4:e1e45f8a7664 630 }
dreschpe 4:e1e45f8a7664 631
dreschpe 4:e1e45f8a7664 632
dreschpe 4:e1e45f8a7664 633
dreschpe 4:e1e45f8a7664 634
dreschpe 4:e1e45f8a7664 635
dreschpe 4:e1e45f8a7664 636 void SPI_TFT::set_font(unsigned char* f) {
dreschpe 4:e1e45f8a7664 637 font = f;
dreschpe 4:e1e45f8a7664 638 }
dreschpe 4:e1e45f8a7664 639
dreschpe 4:e1e45f8a7664 640
dreschpe 4:e1e45f8a7664 641
dreschpe 4:e1e45f8a7664 642 void SPI_TFT::Bitmap(unsigned int x, unsigned int y, unsigned int w, unsigned int h,unsigned char *bitmap) {
dreschpe 6:fc33e4a5713e 643 unsigned int i,j,padd;
dreschpe 4:e1e45f8a7664 644 unsigned short *bitmap_ptr = (unsigned short *)bitmap;
dreschpe 6:fc33e4a5713e 645 // the lines are padded to multiple of 4 bytes in a bitmap
dreschpe 6:fc33e4a5713e 646 padd = -1;
dreschpe 6:fc33e4a5713e 647 do {
dreschpe 6:fc33e4a5713e 648 padd ++;
dreschpe 6:fc33e4a5713e 649 } while (2*(w + padd)%4 != 0);
dreschpe 4:e1e45f8a7664 650 window(x, y, w, h);
dreschpe 4:e1e45f8a7664 651 wr_cmd(0x22);
dreschpe 4:e1e45f8a7664 652 wr_dat_start();
dreschpe 4:e1e45f8a7664 653 _spi.format(16,3);
dreschpe 6:fc33e4a5713e 654 bitmap_ptr += ((h - 1)* (w + padd));
dreschpe 6:fc33e4a5713e 655 //bitmap_ptr -= padd;
dreschpe 4:e1e45f8a7664 656 for (j = 0; j < h; j++) { //Lines
dreschpe 4:e1e45f8a7664 657 for (i = 0; i < w; i++) { // copy pixel data to TFT
dreschpe 4:e1e45f8a7664 658 _spi.write(*bitmap_ptr); // one line
dreschpe 4:e1e45f8a7664 659 bitmap_ptr++;
dreschpe 4:e1e45f8a7664 660 }
dreschpe 4:e1e45f8a7664 661 bitmap_ptr -= 2*w;
dreschpe 6:fc33e4a5713e 662 bitmap_ptr -= padd;
dreschpe 4:e1e45f8a7664 663 }
dreschpe 4:e1e45f8a7664 664 _spi.format(8,3);
dreschpe 4:e1e45f8a7664 665 wr_dat_stop();
dreschpe 9:9dc5dfdda734 666 WindowMax();
dreschpe 5:2db1b8070d94 667 }
dreschpe 5:2db1b8070d94 668
dreschpe 5:2db1b8070d94 669
dreschpe 5:2db1b8070d94 670 int SPI_TFT::BMP_16(unsigned int x, unsigned int y, const char *Name_BMP) {
dreschpe 5:2db1b8070d94 671
dreschpe 5:2db1b8070d94 672 #define OffsetPixelWidth 18
dreschpe 5:2db1b8070d94 673 #define OffsetPixelHeigh 22
dreschpe 5:2db1b8070d94 674 #define OffsetFileSize 34
dreschpe 5:2db1b8070d94 675 #define OffsetPixData 10
dreschpe 5:2db1b8070d94 676 #define OffsetBPP 28
dreschpe 5:2db1b8070d94 677
dreschpe 5:2db1b8070d94 678 char filename[50];
dreschpe 5:2db1b8070d94 679 unsigned char BMP_Header[54];
dreschpe 5:2db1b8070d94 680 unsigned short BPP_t;
dreschpe 5:2db1b8070d94 681 unsigned int PixelWidth,PixelHeigh,start_data;
dreschpe 5:2db1b8070d94 682 unsigned int i,off;
dreschpe 5:2db1b8070d94 683 int padd,j;
dreschpe 5:2db1b8070d94 684 unsigned short *line;
dreschpe 5:2db1b8070d94 685
dreschpe 5:2db1b8070d94 686 // get the filename
dreschpe 5:2db1b8070d94 687 LocalFileSystem local("local");
dreschpe 5:2db1b8070d94 688 sprintf(&filename[0],"/local/");
dreschpe 5:2db1b8070d94 689 i=7;
dreschpe 5:2db1b8070d94 690 while (*Name_BMP!='\0') {
dreschpe 5:2db1b8070d94 691 filename[i++]=*Name_BMP++;
dreschpe 5:2db1b8070d94 692 }
dreschpe 5:2db1b8070d94 693 FILE *Image = fopen((const char *)&filename[0], "r"); // open the bmp file
dreschpe 5:2db1b8070d94 694 if (!Image) {
dreschpe 5:2db1b8070d94 695 return(0); // error file not found !
dreschpe 5:2db1b8070d94 696 }
dreschpe 5:2db1b8070d94 697
dreschpe 5:2db1b8070d94 698 fread(&BMP_Header[0],1,54,Image); // get the BMP Header
dreschpe 5:2db1b8070d94 699
dreschpe 5:2db1b8070d94 700 if (BMP_Header[0] != 0x42 || BMP_Header[1] != 0x4D) { // check magic byte
dreschpe 5:2db1b8070d94 701 fclose(Image);
dreschpe 5:2db1b8070d94 702 return(-1); // error no BMP file
dreschpe 5:2db1b8070d94 703 }
dreschpe 5:2db1b8070d94 704
dreschpe 5:2db1b8070d94 705 BPP_t = BMP_Header[OffsetBPP] + (BMP_Header[OffsetBPP + 1] << 8);
dreschpe 5:2db1b8070d94 706 if (BPP_t != 0x0010) {
dreschpe 5:2db1b8070d94 707 fclose(Image);
dreschpe 5:2db1b8070d94 708 return(-2); // error no 16 bit BMP
dreschpe 5:2db1b8070d94 709 }
dreschpe 5:2db1b8070d94 710
dreschpe 5:2db1b8070d94 711 PixelHeigh = BMP_Header[OffsetPixelHeigh] + (BMP_Header[OffsetPixelHeigh + 1] << 8) + (BMP_Header[OffsetPixelHeigh + 2] << 16) + (BMP_Header[OffsetPixelHeigh + 3] << 24);
dreschpe 5:2db1b8070d94 712 PixelWidth = BMP_Header[OffsetPixelWidth] + (BMP_Header[OffsetPixelWidth + 1] << 8) + (BMP_Header[OffsetPixelWidth + 2] << 16) + (BMP_Header[OffsetPixelWidth + 3] << 24);
dreschpe 5:2db1b8070d94 713 if (PixelHeigh > height() + y || PixelWidth > width() + x) {
dreschpe 5:2db1b8070d94 714 fclose(Image);
dreschpe 5:2db1b8070d94 715 return(-3); // to big
dreschpe 5:2db1b8070d94 716 }
dreschpe 5:2db1b8070d94 717
dreschpe 5:2db1b8070d94 718 start_data = BMP_Header[OffsetPixData] + (BMP_Header[OffsetPixData + 1] << 8) + (BMP_Header[OffsetPixData + 2] << 16) + (BMP_Header[OffsetPixData + 3] << 24);
dreschpe 5:2db1b8070d94 719
dreschpe 5:2db1b8070d94 720 line = (unsigned short *) malloc (PixelWidth); // we need a buffer for a line
dreschpe 5:2db1b8070d94 721 if (line == NULL) {
dreschpe 5:2db1b8070d94 722 return(-4); // error no memory
dreschpe 5:2db1b8070d94 723 }
dreschpe 5:2db1b8070d94 724
dreschpe 5:2db1b8070d94 725 // the lines are padded to multiple of 4 bytes
dreschpe 5:2db1b8070d94 726 padd = -1;
dreschpe 5:2db1b8070d94 727 do {
dreschpe 5:2db1b8070d94 728 padd ++;
dreschpe 5:2db1b8070d94 729 } while ((PixelWidth * 2 + padd)%4 != 0);
dreschpe 5:2db1b8070d94 730
dreschpe 5:2db1b8070d94 731 window(x, y,PixelWidth,PixelHeigh);
dreschpe 5:2db1b8070d94 732 wr_cmd(0x22);
dreschpe 5:2db1b8070d94 733 wr_dat_start();
dreschpe 5:2db1b8070d94 734 _spi.format(16,3);
dreschpe 5:2db1b8070d94 735 for (j = PixelHeigh - 1; j >= 0; j--) { //Lines bottom up
dreschpe 5:2db1b8070d94 736 off = j * (PixelWidth * 2 + padd) + start_data; // start of line
dreschpe 5:2db1b8070d94 737 fseek(Image, off ,SEEK_SET);
dreschpe 5:2db1b8070d94 738 fread(line,1,PixelWidth * 2,Image); // read a line - slow !
dreschpe 5:2db1b8070d94 739 for (i = 0; i < PixelWidth; i++) { // copy pixel data to TFT
dreschpe 5:2db1b8070d94 740 _spi.write(line[i]); // one 16 bit pixel
dreschpe 5:2db1b8070d94 741 }
dreschpe 5:2db1b8070d94 742 }
dreschpe 5:2db1b8070d94 743 _spi.format(8,3);
dreschpe 5:2db1b8070d94 744 wr_dat_stop();
dreschpe 5:2db1b8070d94 745 free (line);
dreschpe 5:2db1b8070d94 746 fclose(Image);
dreschpe 9:9dc5dfdda734 747 WindowMax();
dreschpe 5:2db1b8070d94 748 return(1);
dreschpe 4:e1e45f8a7664 749 }