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:
Sat Jul 30 22:15:35 2011 +0000
Revision:
5:2db1b8070d94
Parent:
4:e1e45f8a7664
Child:
6:fc33e4a5713e
0.2

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