Chinese module HY-1.8 SPI TFT lcd Display library.
Dependents: KL25Z_DCF77_HY-1_8LCD
Fork of HY-1_8TFT_ST7735 by
ST7735_TFT.cpp@1:ef70eaf15b18, 2013-04-19 (annotated)
- Committer:
- star297
- Date:
- Fri Apr 19 20:46:47 2013 +0000
- Revision:
- 1:ef70eaf15b18
- Parent:
- 0:35a1964228b4
- Child:
- 2:26149011665e
Changed LED brightness control + text formating.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
star297 | 0:35a1964228b4 | 1 | // ST7735 8 Bit SPI Library |
star297 | 0:35a1964228b4 | 2 | |
star297 | 0:35a1964228b4 | 3 | |
star297 | 0:35a1964228b4 | 4 | #include "ST7735_TFT.h" |
star297 | 0:35a1964228b4 | 5 | #include "mbed.h" |
star297 | 0:35a1964228b4 | 6 | |
star297 | 0:35a1964228b4 | 7 | #define BPP 8 // Bits per pixel |
star297 | 0:35a1964228b4 | 8 | |
star297 | 0:35a1964228b4 | 9 | ST7735_TFT::ST7735_TFT(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName rs, PinName reset, const char *name) |
star297 | 0:35a1964228b4 | 10 | : _spi(mosi, miso, sclk), _cs(cs), _rs(rs), _reset(reset),GraphicsDisplay(name) |
star297 | 0:35a1964228b4 | 11 | { |
star297 | 0:35a1964228b4 | 12 | tft_reset(); |
star297 | 0:35a1964228b4 | 13 | orientation = 2; |
star297 | 0:35a1964228b4 | 14 | char_x = 0; |
star297 | 0:35a1964228b4 | 15 | } |
star297 | 0:35a1964228b4 | 16 | int ST7735_TFT::width() { |
star297 | 0:35a1964228b4 | 17 | if (orientation == 0 || orientation == 2) return 128; |
star297 | 0:35a1964228b4 | 18 | else return 160; |
star297 | 0:35a1964228b4 | 19 | } |
star297 | 0:35a1964228b4 | 20 | int ST7735_TFT::height() { |
star297 | 0:35a1964228b4 | 21 | if (orientation == 0 || orientation == 2) return 160; |
star297 | 0:35a1964228b4 | 22 | else return 128; |
star297 | 0:35a1964228b4 | 23 | } |
star297 | 0:35a1964228b4 | 24 | void ST7735_TFT::set_orientation(unsigned int o) { |
star297 | 0:35a1964228b4 | 25 | orientation = o; |
star297 | 0:35a1964228b4 | 26 | switch (orientation) { |
star297 | 0:35a1964228b4 | 27 | case 0: |
star297 | 0:35a1964228b4 | 28 | wr_reg(ST7735_MADCTL, 0x0008); |
star297 | 0:35a1964228b4 | 29 | break; |
star297 | 0:35a1964228b4 | 30 | case 1: |
star297 | 0:35a1964228b4 | 31 | wr_reg(ST7735_MADCTL, 0x0068); |
star297 | 0:35a1964228b4 | 32 | break; |
star297 | 0:35a1964228b4 | 33 | case 2: |
star297 | 0:35a1964228b4 | 34 | wr_reg(ST7735_MADCTL, 0x00C8); |
star297 | 0:35a1964228b4 | 35 | break; |
star297 | 0:35a1964228b4 | 36 | case 3: |
star297 | 0:35a1964228b4 | 37 | wr_reg(ST7735_MADCTL, 0x00A8); |
star297 | 0:35a1964228b4 | 38 | break; |
star297 | 0:35a1964228b4 | 39 | } |
star297 | 0:35a1964228b4 | 40 | } |
star297 | 0:35a1964228b4 | 41 | void ST7735_TFT::wr_cmd(int cmd) { |
star297 | 0:35a1964228b4 | 42 | _rs = 0; // rs low, cs low for transmitting command |
star297 | 0:35a1964228b4 | 43 | _cs = 0; |
star297 | 0:35a1964228b4 | 44 | _spi.write(cmd); |
star297 | 0:35a1964228b4 | 45 | _cs = 1; |
star297 | 0:35a1964228b4 | 46 | } |
star297 | 0:35a1964228b4 | 47 | void ST7735_TFT::wr_dat(int dat) { |
star297 | 0:35a1964228b4 | 48 | _rs = 1; // rs high, cs low for transmitting data |
star297 | 0:35a1964228b4 | 49 | _cs = 0; |
star297 | 0:35a1964228b4 | 50 | _spi.write(dat); |
star297 | 0:35a1964228b4 | 51 | _cs = 1; |
star297 | 0:35a1964228b4 | 52 | } |
star297 | 0:35a1964228b4 | 53 | void ST7735_TFT::wr_dat_start(void) { |
star297 | 0:35a1964228b4 | 54 | _rs = 1; // rs high, cs low for transmitting data |
star297 | 0:35a1964228b4 | 55 | _cs = 0; |
star297 | 0:35a1964228b4 | 56 | } |
star297 | 0:35a1964228b4 | 57 | void ST7735_TFT::wr_dat_stop (void) { |
star297 | 0:35a1964228b4 | 58 | _cs = 1; |
star297 | 0:35a1964228b4 | 59 | } |
star297 | 0:35a1964228b4 | 60 | void ST7735_TFT::wr_dat_only (unsigned short dat) { |
star297 | 0:35a1964228b4 | 61 | _spi.write(dat); |
star297 | 0:35a1964228b4 | 62 | } |
star297 | 0:35a1964228b4 | 63 | unsigned short ST7735_TFT::rd_dat (void) { |
star297 | 0:35a1964228b4 | 64 | unsigned short val = 0; |
star297 | 0:35a1964228b4 | 65 | _cs = 0; |
star297 | 0:35a1964228b4 | 66 | _spi.write(0); /* Dummy read 1 */ |
star297 | 0:35a1964228b4 | 67 | val = _spi.write(0); /* Read D8..D15 */ |
star297 | 0:35a1964228b4 | 68 | val <<= 8; |
star297 | 0:35a1964228b4 | 69 | val |= _spi.write(0); /* Read D0..D7 */ |
star297 | 0:35a1964228b4 | 70 | _cs = 1; |
star297 | 0:35a1964228b4 | 71 | return (val); |
star297 | 0:35a1964228b4 | 72 | } |
star297 | 0:35a1964228b4 | 73 | void ST7735_TFT::wr_reg (unsigned char reg, unsigned short val) { |
star297 | 0:35a1964228b4 | 74 | |
star297 | 0:35a1964228b4 | 75 | wr_cmd(reg); |
star297 | 0:35a1964228b4 | 76 | wr_dat(val); |
star297 | 0:35a1964228b4 | 77 | } |
star297 | 0:35a1964228b4 | 78 | unsigned short ST7735_TFT::rd_reg (unsigned char reg) { |
star297 | 0:35a1964228b4 | 79 | |
star297 | 0:35a1964228b4 | 80 | wr_cmd(reg); |
star297 | 0:35a1964228b4 | 81 | return(rd_dat()); |
star297 | 0:35a1964228b4 | 82 | } |
star297 | 0:35a1964228b4 | 83 | void ST7735_TFT::read_area(unsigned int x, unsigned int y, unsigned int w, unsigned int h,unsigned char *buffer) { |
star297 | 0:35a1964228b4 | 84 | // BEWARE ! |
star297 | 0:35a1964228b4 | 85 | // DOES NOT WORK CORRECTLY YET !!! |
star297 | 0:35a1964228b4 | 86 | int val; |
star297 | 0:35a1964228b4 | 87 | window(x,y,w,h); |
star297 | 0:35a1964228b4 | 88 | wr_cmd(ST7735_RAMRD); // write to RAM |
star297 | 0:35a1964228b4 | 89 | _cs = 0; |
star297 | 0:35a1964228b4 | 90 | _rs = 1; |
star297 | 0:35a1964228b4 | 91 | _spi.write(0); /* Dummy read 1 */ |
star297 | 0:35a1964228b4 | 92 | |
star297 | 0:35a1964228b4 | 93 | val = _spi.write(0); /* Read D8..D15 */ |
star297 | 0:35a1964228b4 | 94 | val <<= 8; |
star297 | 0:35a1964228b4 | 95 | val |= _spi.write(0); /* Read D0..D7 */ |
star297 | 0:35a1964228b4 | 96 | _cs = 1; |
star297 | 0:35a1964228b4 | 97 | } |
star297 | 0:35a1964228b4 | 98 | int ST7735_TFT::getpixel(unsigned int x, unsigned int y) { |
star297 | 0:35a1964228b4 | 99 | // BEWARE ! |
star297 | 0:35a1964228b4 | 100 | // DOES NOT WORK CORRECTLY YET !!! |
star297 | 0:35a1964228b4 | 101 | int val; |
star297 | 0:35a1964228b4 | 102 | // _spi.format(8,3); |
star297 | 0:35a1964228b4 | 103 | wr_cmd(ST7735_CASET); // column addr set |
star297 | 0:35a1964228b4 | 104 | wr_dat(0x00); |
star297 | 0:35a1964228b4 | 105 | wr_dat(x+2); // XSTART |
star297 | 0:35a1964228b4 | 106 | wr_dat(0x00); |
star297 | 0:35a1964228b4 | 107 | wr_dat(x+2+2); // XEND |
star297 | 0:35a1964228b4 | 108 | |
star297 | 0:35a1964228b4 | 109 | wr_cmd(ST7735_RASET); // row addr set |
star297 | 0:35a1964228b4 | 110 | wr_dat(0x00); |
star297 | 0:35a1964228b4 | 111 | wr_dat(y+1); // YSTART |
star297 | 0:35a1964228b4 | 112 | wr_dat(0x00); |
star297 | 0:35a1964228b4 | 113 | wr_dat(y+1+1); // YEND |
star297 | 0:35a1964228b4 | 114 | |
star297 | 0:35a1964228b4 | 115 | _rs = 0; // rs low, cs low for transmitting command |
star297 | 0:35a1964228b4 | 116 | _cs = 0; |
star297 | 0:35a1964228b4 | 117 | _spi.write(0x2E); |
star297 | 0:35a1964228b4 | 118 | _rs = 1; |
star297 | 0:35a1964228b4 | 119 | _spi.write(0x00); /* Dummy read 1 */ |
star297 | 0:35a1964228b4 | 120 | |
star297 | 0:35a1964228b4 | 121 | val = _spi.write(0x00); /* Read D8..D15 */ |
star297 | 0:35a1964228b4 | 122 | val <<= 8; |
star297 | 0:35a1964228b4 | 123 | val |= _spi.write(0x00); /* Read D0..D7 */ |
star297 | 0:35a1964228b4 | 124 | |
star297 | 0:35a1964228b4 | 125 | _cs = 1; |
star297 | 0:35a1964228b4 | 126 | return val; |
star297 | 0:35a1964228b4 | 127 | } |
star297 | 0:35a1964228b4 | 128 | void ST7735_TFT::tft_reset() { |
star297 | 0:35a1964228b4 | 129 | // init SPI |
star297 | 0:35a1964228b4 | 130 | _spi.format(8,3); // 8 bit spi mode 3 |
star297 | 0:35a1964228b4 | 131 | _spi.frequency(16000000); // 16Mhz SPI clock ... 15Mhz is maximum for display, but it seems to work |
star297 | 0:35a1964228b4 | 132 | _cs = 0; |
star297 | 0:35a1964228b4 | 133 | _reset = 1; // reset |
star297 | 1:ef70eaf15b18 | 134 | wait_ms(10); |
star297 | 0:35a1964228b4 | 135 | _reset = 0; // reset |
star297 | 1:ef70eaf15b18 | 136 | wait_ms(10); |
star297 | 0:35a1964228b4 | 137 | _reset = 1; // reset |
star297 | 1:ef70eaf15b18 | 138 | wait_ms(10); |
star297 | 0:35a1964228b4 | 139 | |
star297 | 0:35a1964228b4 | 140 | /* Start Initial Sequence ----------------------------------------------------*/ |
star297 | 0:35a1964228b4 | 141 | wr_cmd(ST7735_SWRESET); /* SW Reset */ |
star297 | 0:35a1964228b4 | 142 | wait_ms(150); |
star297 | 0:35a1964228b4 | 143 | wr_cmd(ST7735_SLPOUT); /* Out of sleepmode */ |
star297 | 0:35a1964228b4 | 144 | wait_ms(500); |
star297 | 0:35a1964228b4 | 145 | |
star297 | 0:35a1964228b4 | 146 | wr_cmd(ST7735_FRMCTR1); /* Frame rate in normal mode */ |
star297 | 0:35a1964228b4 | 147 | wr_dat(0x01); |
star297 | 0:35a1964228b4 | 148 | wr_dat(0x2C); |
star297 | 0:35a1964228b4 | 149 | wr_dat(0x2D); |
star297 | 0:35a1964228b4 | 150 | |
star297 | 0:35a1964228b4 | 151 | wr_cmd(ST7735_FRMCTR2); /* Frame rate in idle mode */ |
star297 | 0:35a1964228b4 | 152 | wr_dat(0x01); |
star297 | 0:35a1964228b4 | 153 | wr_dat(0x2C); |
star297 | 0:35a1964228b4 | 154 | wr_dat(0x2D); |
star297 | 0:35a1964228b4 | 155 | |
star297 | 0:35a1964228b4 | 156 | wr_cmd(ST7735_FRMCTR3); /* Frame rate in partial mode */ |
star297 | 0:35a1964228b4 | 157 | wr_dat(0x01); |
star297 | 0:35a1964228b4 | 158 | wr_dat(0x2C); |
star297 | 0:35a1964228b4 | 159 | wr_dat(0x2D); |
star297 | 0:35a1964228b4 | 160 | wr_dat(0x01); // inversion mode settings |
star297 | 0:35a1964228b4 | 161 | wr_dat(0x2C); |
star297 | 0:35a1964228b4 | 162 | wr_dat(0x2D); |
star297 | 0:35a1964228b4 | 163 | |
star297 | 0:35a1964228b4 | 164 | wr_cmd(ST7735_INVCTR); // Inverted mode off |
star297 | 0:35a1964228b4 | 165 | wr_dat(0x07); |
star297 | 0:35a1964228b4 | 166 | |
star297 | 0:35a1964228b4 | 167 | wr_cmd(ST7735_PWCTR1); // POWER CONTROL 1 |
star297 | 0:35a1964228b4 | 168 | wr_dat(0xA2); |
star297 | 0:35a1964228b4 | 169 | wr_dat(0x02); // -4.6V |
star297 | 0:35a1964228b4 | 170 | wr_dat(0x84); // AUTO mode |
star297 | 0:35a1964228b4 | 171 | |
star297 | 0:35a1964228b4 | 172 | wr_cmd(ST7735_PWCTR2); // POWER CONTROL 2 |
star297 | 0:35a1964228b4 | 173 | wr_dat(0xC5); // VGH25 = 2.4C VGSEL =-10 VGH = 3*AVDD |
star297 | 0:35a1964228b4 | 174 | |
star297 | 0:35a1964228b4 | 175 | wr_cmd(ST7735_PWCTR3); // POWER CONTROL 3 |
star297 | 0:35a1964228b4 | 176 | wr_dat(0x0A); // Opamp current small |
star297 | 0:35a1964228b4 | 177 | wr_dat(0x00); // Boost freq |
star297 | 0:35a1964228b4 | 178 | |
star297 | 0:35a1964228b4 | 179 | wr_cmd(ST7735_PWCTR4); // POWER CONTROL 4 |
star297 | 0:35a1964228b4 | 180 | wr_dat(0x8A); // BCLK/2, Opamp current small / medium low |
star297 | 0:35a1964228b4 | 181 | wr_dat(0x2A); // |
star297 | 0:35a1964228b4 | 182 | |
star297 | 0:35a1964228b4 | 183 | wr_cmd(ST7735_PWCTR5); // POWER CONTROL 5 |
star297 | 0:35a1964228b4 | 184 | wr_dat(0x8A); // BCLK/2, Opamp current small / medium low |
star297 | 0:35a1964228b4 | 185 | wr_dat(0xEE); // |
star297 | 0:35a1964228b4 | 186 | |
star297 | 0:35a1964228b4 | 187 | wr_cmd(ST7735_VMCTR1); // POWER CONTROL 6 |
star297 | 0:35a1964228b4 | 188 | wr_dat(0x0E); // |
star297 | 0:35a1964228b4 | 189 | |
star297 | 0:35a1964228b4 | 190 | wr_cmd(ST7735_INVOFF); // INVOFF |
star297 | 0:35a1964228b4 | 191 | |
star297 | 0:35a1964228b4 | 192 | wr_cmd(ST7735_MADCTL); // ORIENTATION |
star297 | 0:35a1964228b4 | 193 | wr_dat(0xC8); // |
star297 | 0:35a1964228b4 | 194 | |
star297 | 0:35a1964228b4 | 195 | wr_cmd(ST7735_COLMOD); // COLOR MODE |
star297 | 0:35a1964228b4 | 196 | wr_dat(0x05); // |
star297 | 0:35a1964228b4 | 197 | |
star297 | 0:35a1964228b4 | 198 | wr_cmd(ST7735_CASET); // COLUMN ADDR SET |
star297 | 0:35a1964228b4 | 199 | wr_dat(0x00); // |
star297 | 0:35a1964228b4 | 200 | wr_dat(0x00); // xstart = 0 |
star297 | 0:35a1964228b4 | 201 | wr_dat(0x00); // |
star297 | 0:35a1964228b4 | 202 | wr_dat(0x7F); // xend = 127 |
star297 | 0:35a1964228b4 | 203 | |
star297 | 0:35a1964228b4 | 204 | wr_cmd(ST7735_RASET); // ROW ADDR SET |
star297 | 0:35a1964228b4 | 205 | wr_dat(0x00); // |
star297 | 0:35a1964228b4 | 206 | wr_dat(0x00); // ystart = 0 |
star297 | 0:35a1964228b4 | 207 | wr_dat(0x00); // |
star297 | 0:35a1964228b4 | 208 | wr_dat(0x9F); // yend = 159 |
star297 | 0:35a1964228b4 | 209 | |
star297 | 0:35a1964228b4 | 210 | /* Gamma settings -----------------------------------------------------------*/ |
star297 | 0:35a1964228b4 | 211 | |
star297 | 0:35a1964228b4 | 212 | wr_cmd(0xE0); // GMCTRP1 |
star297 | 0:35a1964228b4 | 213 | wr_dat(0x02); |
star297 | 0:35a1964228b4 | 214 | wr_dat(0x1c); |
star297 | 0:35a1964228b4 | 215 | wr_dat(0x07); |
star297 | 0:35a1964228b4 | 216 | wr_dat(0x12); |
star297 | 0:35a1964228b4 | 217 | wr_dat(0x37); |
star297 | 0:35a1964228b4 | 218 | wr_dat(0x32); |
star297 | 0:35a1964228b4 | 219 | wr_dat(0x29); |
star297 | 0:35a1964228b4 | 220 | wr_dat(0x2d); |
star297 | 0:35a1964228b4 | 221 | wr_dat(0x29); |
star297 | 0:35a1964228b4 | 222 | wr_dat(0x25); |
star297 | 0:35a1964228b4 | 223 | wr_dat(0x2B); |
star297 | 0:35a1964228b4 | 224 | wr_dat(0x39); |
star297 | 0:35a1964228b4 | 225 | wr_dat(0x00); |
star297 | 0:35a1964228b4 | 226 | wr_dat(0x01); |
star297 | 0:35a1964228b4 | 227 | wr_dat(0x03); |
star297 | 0:35a1964228b4 | 228 | wr_dat(0x10); |
star297 | 0:35a1964228b4 | 229 | wr_cmd(0xE1); // GMCTRN1 |
star297 | 0:35a1964228b4 | 230 | wr_dat(0x03); |
star297 | 0:35a1964228b4 | 231 | wr_dat(0x1d); |
star297 | 0:35a1964228b4 | 232 | wr_dat(0x07); |
star297 | 0:35a1964228b4 | 233 | wr_dat(0x06); |
star297 | 0:35a1964228b4 | 234 | wr_dat(0x2E); |
star297 | 0:35a1964228b4 | 235 | wr_dat(0x2C); |
star297 | 0:35a1964228b4 | 236 | wr_dat(0x29); |
star297 | 0:35a1964228b4 | 237 | wr_dat(0x2D); |
star297 | 0:35a1964228b4 | 238 | wr_dat(0x2E); |
star297 | 0:35a1964228b4 | 239 | wr_dat(0x2E); |
star297 | 0:35a1964228b4 | 240 | wr_dat(0x37); |
star297 | 0:35a1964228b4 | 241 | wr_dat(0x3F); |
star297 | 0:35a1964228b4 | 242 | wr_dat(0x00); |
star297 | 0:35a1964228b4 | 243 | wr_dat(0x00); |
star297 | 0:35a1964228b4 | 244 | wr_dat(0x02); |
star297 | 0:35a1964228b4 | 245 | wr_dat(0x10); |
star297 | 0:35a1964228b4 | 246 | |
star297 | 0:35a1964228b4 | 247 | wr_cmd(ST7735_DISPON); // display ON |
star297 | 0:35a1964228b4 | 248 | wait_ms(100); |
star297 | 0:35a1964228b4 | 249 | |
star297 | 0:35a1964228b4 | 250 | wr_cmd(ST7735_NORON); // normal display on |
star297 | 0:35a1964228b4 | 251 | wait_ms(10); |
star297 | 0:35a1964228b4 | 252 | |
star297 | 0:35a1964228b4 | 253 | switch (orientation) { |
star297 | 0:35a1964228b4 | 254 | case 0: |
star297 | 0:35a1964228b4 | 255 | wr_reg(0xC8, 0x0008); |
star297 | 0:35a1964228b4 | 256 | break; |
star297 | 0:35a1964228b4 | 257 | case 1: |
star297 | 0:35a1964228b4 | 258 | wr_reg(0xC8, 0x0068); |
star297 | 0:35a1964228b4 | 259 | break; |
star297 | 0:35a1964228b4 | 260 | case 2: |
star297 | 0:35a1964228b4 | 261 | wr_reg(0xC8, 0x00C8); |
star297 | 0:35a1964228b4 | 262 | break; |
star297 | 0:35a1964228b4 | 263 | case 3: |
star297 | 0:35a1964228b4 | 264 | wr_reg(0xC8, 0x00A8); |
star297 | 0:35a1964228b4 | 265 | break; |
star297 | 0:35a1964228b4 | 266 | } |
star297 | 0:35a1964228b4 | 267 | WindowMax (); |
star297 | 0:35a1964228b4 | 268 | } |
star297 | 0:35a1964228b4 | 269 | void ST7735_TFT::pixel(int x, int y, int color) { |
star297 | 0:35a1964228b4 | 270 | if ((x >= width()) || (y >= height())) return; |
star297 | 0:35a1964228b4 | 271 | window(x,y,x+1,y+1); |
star297 | 0:35a1964228b4 | 272 | _rs = 1; |
star297 | 0:35a1964228b4 | 273 | _cs = 0; |
star297 | 0:35a1964228b4 | 274 | _spi.write(color); |
star297 | 0:35a1964228b4 | 275 | _spi.write(color>>8); |
star297 | 0:35a1964228b4 | 276 | _cs = 1; |
star297 | 0:35a1964228b4 | 277 | } |
star297 | 0:35a1964228b4 | 278 | void ST7735_TFT::window (unsigned int x, unsigned int y, unsigned int w, unsigned int h) { |
star297 | 0:35a1964228b4 | 279 | wr_cmd(ST7735_CASET); // column addr set |
star297 | 0:35a1964228b4 | 280 | wr_dat(0x00); |
star297 | 0:35a1964228b4 | 281 | wr_dat(x); // XSTART |
star297 | 0:35a1964228b4 | 282 | wr_dat(0x00); |
star297 | 0:35a1964228b4 | 283 | wr_dat(x+w-1); // XEND |
star297 | 0:35a1964228b4 | 284 | wr_cmd(ST7735_RASET); // row addr set |
star297 | 0:35a1964228b4 | 285 | wr_dat(0x00); |
star297 | 0:35a1964228b4 | 286 | wr_dat(y); // YSTART |
star297 | 0:35a1964228b4 | 287 | wr_dat(0x00); |
star297 | 0:35a1964228b4 | 288 | wr_dat(y+h-1); // YEND |
star297 | 0:35a1964228b4 | 289 | wr_cmd(ST7735_RAMWR); // write to RAM |
star297 | 0:35a1964228b4 | 290 | } |
star297 | 0:35a1964228b4 | 291 | void ST7735_TFT::WindowMax (void) { |
star297 | 0:35a1964228b4 | 292 | window (0, 0, width(), height()); |
star297 | 0:35a1964228b4 | 293 | } |
star297 | 0:35a1964228b4 | 294 | |
star297 | 0:35a1964228b4 | 295 | void ST7735_TFT::cls (void) { |
star297 | 0:35a1964228b4 | 296 | unsigned int i; |
star297 | 0:35a1964228b4 | 297 | WindowMax(); |
star297 | 0:35a1964228b4 | 298 | wr_dat_start(); |
star297 | 0:35a1964228b4 | 299 | for (i = 0; i < ( (width()) * (height())); i++) { |
star297 | 0:35a1964228b4 | 300 | _spi.write(_background); |
star297 | 0:35a1964228b4 | 301 | _spi.write(_background>>8); |
star297 | 0:35a1964228b4 | 302 | } |
star297 | 0:35a1964228b4 | 303 | wr_dat_stop(); |
star297 | 0:35a1964228b4 | 304 | } |
star297 | 0:35a1964228b4 | 305 | void ST7735_TFT::circle(int x0, int y0, int r, int color) { |
star297 | 0:35a1964228b4 | 306 | int draw_x0, draw_y0; |
star297 | 0:35a1964228b4 | 307 | int draw_x1, draw_y1; |
star297 | 0:35a1964228b4 | 308 | int draw_x2, draw_y2; |
star297 | 0:35a1964228b4 | 309 | int draw_x3, draw_y3; |
star297 | 0:35a1964228b4 | 310 | int draw_x4, draw_y4; |
star297 | 0:35a1964228b4 | 311 | int draw_x5, draw_y5; |
star297 | 0:35a1964228b4 | 312 | int draw_x6, draw_y6; |
star297 | 0:35a1964228b4 | 313 | int draw_x7, draw_y7; |
star297 | 0:35a1964228b4 | 314 | int xx, yy; |
star297 | 0:35a1964228b4 | 315 | int di; |
star297 | 0:35a1964228b4 | 316 | WindowMax(); |
star297 | 0:35a1964228b4 | 317 | if (r == 0) { /* no radius */ |
star297 | 0:35a1964228b4 | 318 | return; |
star297 | 0:35a1964228b4 | 319 | } |
star297 | 0:35a1964228b4 | 320 | draw_x0 = draw_x1 = x0; |
star297 | 0:35a1964228b4 | 321 | draw_y0 = draw_y1 = y0 + r; |
star297 | 0:35a1964228b4 | 322 | if (draw_y0 < height()) { |
star297 | 0:35a1964228b4 | 323 | pixel(draw_x0, draw_y0, color); /* 90 degree */ |
star297 | 0:35a1964228b4 | 324 | } |
star297 | 0:35a1964228b4 | 325 | draw_x2 = draw_x3 = x0; |
star297 | 0:35a1964228b4 | 326 | draw_y2 = draw_y3 = y0 - r; |
star297 | 0:35a1964228b4 | 327 | if (draw_y2 >= 0) { |
star297 | 0:35a1964228b4 | 328 | pixel(draw_x2, draw_y2, color); /* 270 degree */ |
star297 | 0:35a1964228b4 | 329 | } |
star297 | 0:35a1964228b4 | 330 | draw_x4 = draw_x6 = x0 + r; |
star297 | 0:35a1964228b4 | 331 | draw_y4 = draw_y6 = y0; |
star297 | 0:35a1964228b4 | 332 | if (draw_x4 < width()) { |
star297 | 0:35a1964228b4 | 333 | pixel(draw_x4, draw_y4, color); /* 0 degree */ |
star297 | 0:35a1964228b4 | 334 | } |
star297 | 0:35a1964228b4 | 335 | draw_x5 = draw_x7 = x0 - r; |
star297 | 0:35a1964228b4 | 336 | draw_y5 = draw_y7 = y0; |
star297 | 0:35a1964228b4 | 337 | if (draw_x5>=0) { |
star297 | 0:35a1964228b4 | 338 | pixel(draw_x5, draw_y5, color); /* 180 degree */ |
star297 | 0:35a1964228b4 | 339 | } |
star297 | 0:35a1964228b4 | 340 | if (r == 1) { |
star297 | 0:35a1964228b4 | 341 | return; |
star297 | 0:35a1964228b4 | 342 | } |
star297 | 0:35a1964228b4 | 343 | di = 3 - 2*r; |
star297 | 0:35a1964228b4 | 344 | xx = 0; |
star297 | 0:35a1964228b4 | 345 | yy = r; |
star297 | 0:35a1964228b4 | 346 | while (xx < yy) { |
star297 | 0:35a1964228b4 | 347 | |
star297 | 0:35a1964228b4 | 348 | if (di < 0) { |
star297 | 0:35a1964228b4 | 349 | di += 4*xx + 6; |
star297 | 0:35a1964228b4 | 350 | } else { |
star297 | 0:35a1964228b4 | 351 | di += 4*(xx - yy) + 10; |
star297 | 0:35a1964228b4 | 352 | yy--; |
star297 | 0:35a1964228b4 | 353 | draw_y0--; |
star297 | 0:35a1964228b4 | 354 | draw_y1--; |
star297 | 0:35a1964228b4 | 355 | draw_y2++; |
star297 | 0:35a1964228b4 | 356 | draw_y3++; |
star297 | 0:35a1964228b4 | 357 | draw_x4--; |
star297 | 0:35a1964228b4 | 358 | draw_x5++; |
star297 | 0:35a1964228b4 | 359 | draw_x6--; |
star297 | 0:35a1964228b4 | 360 | draw_x7++; |
star297 | 0:35a1964228b4 | 361 | } |
star297 | 0:35a1964228b4 | 362 | xx++; |
star297 | 0:35a1964228b4 | 363 | draw_x0++; |
star297 | 0:35a1964228b4 | 364 | draw_x1--; |
star297 | 0:35a1964228b4 | 365 | draw_x2++; |
star297 | 0:35a1964228b4 | 366 | draw_x3--; |
star297 | 0:35a1964228b4 | 367 | draw_y4++; |
star297 | 0:35a1964228b4 | 368 | draw_y5++; |
star297 | 0:35a1964228b4 | 369 | draw_y6--; |
star297 | 0:35a1964228b4 | 370 | draw_y7--; |
star297 | 0:35a1964228b4 | 371 | |
star297 | 0:35a1964228b4 | 372 | if ( (draw_x0 <= width()) && (draw_y0>=0) ) { |
star297 | 0:35a1964228b4 | 373 | pixel(draw_x0, draw_y0, color); |
star297 | 0:35a1964228b4 | 374 | } |
star297 | 0:35a1964228b4 | 375 | if ( (draw_x1 >= 0) && (draw_y1 >= 0) ) { |
star297 | 0:35a1964228b4 | 376 | pixel(draw_x1, draw_y1, color); |
star297 | 0:35a1964228b4 | 377 | } |
star297 | 0:35a1964228b4 | 378 | if ( (draw_x2 <= width()) && (draw_y2 <= height()) ) { |
star297 | 0:35a1964228b4 | 379 | pixel(draw_x2, draw_y2, color); |
star297 | 0:35a1964228b4 | 380 | } |
star297 | 0:35a1964228b4 | 381 | if ( (draw_x3 >=0 ) && (draw_y3 <= height()) ) { |
star297 | 0:35a1964228b4 | 382 | pixel(draw_x3, draw_y3, color); |
star297 | 0:35a1964228b4 | 383 | } |
star297 | 0:35a1964228b4 | 384 | if ( (draw_x4 <= width()) && (draw_y4 >= 0) ) { |
star297 | 0:35a1964228b4 | 385 | pixel(draw_x4, draw_y4, color); |
star297 | 0:35a1964228b4 | 386 | } |
star297 | 0:35a1964228b4 | 387 | if ( (draw_x5 >= 0) && (draw_y5 >= 0) ) { |
star297 | 0:35a1964228b4 | 388 | pixel(draw_x5, draw_y5, color); |
star297 | 0:35a1964228b4 | 389 | } |
star297 | 0:35a1964228b4 | 390 | if ( (draw_x6 <=width()) && (draw_y6 <= height()) ) { |
star297 | 0:35a1964228b4 | 391 | pixel(draw_x6, draw_y6, color); |
star297 | 0:35a1964228b4 | 392 | } |
star297 | 0:35a1964228b4 | 393 | if ( (draw_x7 >= 0) && (draw_y7 <= height()) ) { |
star297 | 0:35a1964228b4 | 394 | pixel(draw_x7, draw_y7, color); |
star297 | 0:35a1964228b4 | 395 | } |
star297 | 0:35a1964228b4 | 396 | } |
star297 | 0:35a1964228b4 | 397 | return; |
star297 | 0:35a1964228b4 | 398 | } |
star297 | 0:35a1964228b4 | 399 | void ST7735_TFT::fillcircle(int x, int y, int r, int color) { |
star297 | 0:35a1964228b4 | 400 | int i; |
star297 | 0:35a1964228b4 | 401 | for (i = 0; i <= r; i++) |
star297 | 0:35a1964228b4 | 402 | circle(x,y,i,color); |
star297 | 0:35a1964228b4 | 403 | } |
star297 | 0:35a1964228b4 | 404 | void ST7735_TFT::hline(int x0, int x1, int y, int color) { |
star297 | 0:35a1964228b4 | 405 | int w; |
star297 | 0:35a1964228b4 | 406 | w = x1 - x0 + 1; |
star297 | 0:35a1964228b4 | 407 | window(x0,y,w,1); |
star297 | 0:35a1964228b4 | 408 | wr_cmd(0x2C); |
star297 | 0:35a1964228b4 | 409 | wr_dat_start(); |
star297 | 0:35a1964228b4 | 410 | for (int x=0; x<w; x++) { |
star297 | 0:35a1964228b4 | 411 | _spi.write(color); |
star297 | 0:35a1964228b4 | 412 | _spi.write(color >> 8); |
star297 | 0:35a1964228b4 | 413 | } |
star297 | 0:35a1964228b4 | 414 | wr_dat_stop(); |
star297 | 0:35a1964228b4 | 415 | return; |
star297 | 0:35a1964228b4 | 416 | } |
star297 | 0:35a1964228b4 | 417 | void ST7735_TFT::vline(int x, int y0, int y1, int color) { |
star297 | 0:35a1964228b4 | 418 | int h; |
star297 | 0:35a1964228b4 | 419 | h = y1 - y0 + 1; |
star297 | 0:35a1964228b4 | 420 | window(x,y0,1,h); |
star297 | 0:35a1964228b4 | 421 | wr_cmd(0x2C); |
star297 | 0:35a1964228b4 | 422 | wr_dat_start(); |
star297 | 0:35a1964228b4 | 423 | for (int y=0; y<h; y++) { |
star297 | 0:35a1964228b4 | 424 | _spi.write(color); |
star297 | 0:35a1964228b4 | 425 | _spi.write(color >> 8); |
star297 | 0:35a1964228b4 | 426 | } |
star297 | 0:35a1964228b4 | 427 | wr_dat_stop(); |
star297 | 0:35a1964228b4 | 428 | return; |
star297 | 0:35a1964228b4 | 429 | } |
star297 | 0:35a1964228b4 | 430 | void ST7735_TFT::line(int x0, int y0, int x1, int y1, int color) { |
star297 | 0:35a1964228b4 | 431 | WindowMax(); |
star297 | 0:35a1964228b4 | 432 | int dx = 0, dy = 0; |
star297 | 0:35a1964228b4 | 433 | int dx_sym = 0, dy_sym = 0; |
star297 | 0:35a1964228b4 | 434 | int dx_x2 = 0, dy_x2 = 0; |
star297 | 0:35a1964228b4 | 435 | int di = 0; |
star297 | 0:35a1964228b4 | 436 | dx = x1-x0; |
star297 | 0:35a1964228b4 | 437 | dy = y1-y0; |
star297 | 0:35a1964228b4 | 438 | |
star297 | 0:35a1964228b4 | 439 | if (dx == 0) { /* vertical line */ |
star297 | 0:35a1964228b4 | 440 | if (y1 > y0) vline(x0,y0,y1,color); |
star297 | 0:35a1964228b4 | 441 | else vline(x0,y1,y0,color); |
star297 | 0:35a1964228b4 | 442 | return; |
star297 | 0:35a1964228b4 | 443 | } |
star297 | 0:35a1964228b4 | 444 | if (dx > 0) { |
star297 | 0:35a1964228b4 | 445 | dx_sym = 1; |
star297 | 0:35a1964228b4 | 446 | } else { |
star297 | 0:35a1964228b4 | 447 | dx_sym = -1; |
star297 | 0:35a1964228b4 | 448 | } |
star297 | 0:35a1964228b4 | 449 | if (dy == 0) { /* horizontal line */ |
star297 | 0:35a1964228b4 | 450 | if (x1 > x0) hline(x0,x1,y0,color); |
star297 | 0:35a1964228b4 | 451 | else hline(x1,x0,y0,color); |
star297 | 0:35a1964228b4 | 452 | return; |
star297 | 0:35a1964228b4 | 453 | } |
star297 | 0:35a1964228b4 | 454 | if (dy > 0) { |
star297 | 0:35a1964228b4 | 455 | dy_sym = 1; |
star297 | 0:35a1964228b4 | 456 | } else { |
star297 | 0:35a1964228b4 | 457 | dy_sym = -1; |
star297 | 0:35a1964228b4 | 458 | } |
star297 | 0:35a1964228b4 | 459 | dx = dx_sym*dx; |
star297 | 0:35a1964228b4 | 460 | dy = dy_sym*dy; |
star297 | 0:35a1964228b4 | 461 | dx_x2 = dx*2; |
star297 | 0:35a1964228b4 | 462 | dy_x2 = dy*2; |
star297 | 0:35a1964228b4 | 463 | if (dx >= dy) { |
star297 | 0:35a1964228b4 | 464 | di = dy_x2 - dx; |
star297 | 0:35a1964228b4 | 465 | while (x0 != x1) { |
star297 | 0:35a1964228b4 | 466 | |
star297 | 0:35a1964228b4 | 467 | pixel(x0, y0, color); |
star297 | 0:35a1964228b4 | 468 | x0 += dx_sym; |
star297 | 0:35a1964228b4 | 469 | if (di<0) { |
star297 | 0:35a1964228b4 | 470 | di += dy_x2; |
star297 | 0:35a1964228b4 | 471 | } else { |
star297 | 0:35a1964228b4 | 472 | di += dy_x2 - dx_x2; |
star297 | 0:35a1964228b4 | 473 | y0 += dy_sym; |
star297 | 0:35a1964228b4 | 474 | } |
star297 | 0:35a1964228b4 | 475 | } |
star297 | 0:35a1964228b4 | 476 | pixel(x0, y0, color); |
star297 | 0:35a1964228b4 | 477 | } else { |
star297 | 0:35a1964228b4 | 478 | di = dx_x2 - dy; |
star297 | 0:35a1964228b4 | 479 | while (y0 != y1) { |
star297 | 0:35a1964228b4 | 480 | pixel(x0, y0, color); |
star297 | 0:35a1964228b4 | 481 | y0 += dy_sym; |
star297 | 0:35a1964228b4 | 482 | if (di < 0) { |
star297 | 0:35a1964228b4 | 483 | di += dx_x2; |
star297 | 0:35a1964228b4 | 484 | } else { |
star297 | 0:35a1964228b4 | 485 | di += dx_x2 - dy_x2; |
star297 | 0:35a1964228b4 | 486 | x0 += dx_sym; |
star297 | 0:35a1964228b4 | 487 | } |
star297 | 0:35a1964228b4 | 488 | } |
star297 | 0:35a1964228b4 | 489 | pixel(x0, y0, color); |
star297 | 0:35a1964228b4 | 490 | } |
star297 | 0:35a1964228b4 | 491 | return; |
star297 | 0:35a1964228b4 | 492 | } |
star297 | 0:35a1964228b4 | 493 | void ST7735_TFT::rect(int x0, int y0, int x1, int y1, int color) { |
star297 | 0:35a1964228b4 | 494 | if (x1 > x0) hline(x0,x1,y0,color); |
star297 | 0:35a1964228b4 | 495 | else hline(x1,x0,y0,color); |
star297 | 0:35a1964228b4 | 496 | if (y1 > y0) vline(x0,y0,y1,color); |
star297 | 0:35a1964228b4 | 497 | else vline(x0,y1,y0,color); |
star297 | 0:35a1964228b4 | 498 | if (x1 > x0) hline(x0,x1,y1,color); |
star297 | 0:35a1964228b4 | 499 | else hline(x1,x0,y1,color); |
star297 | 0:35a1964228b4 | 500 | if (y1 > y0) vline(x1,y0,y1,color); |
star297 | 0:35a1964228b4 | 501 | else vline(x1,y1,y0,color); |
star297 | 0:35a1964228b4 | 502 | return; |
star297 | 0:35a1964228b4 | 503 | } |
star297 | 0:35a1964228b4 | 504 | void ST7735_TFT::fillrect(int x0, int y0, int x1, int y1, int color) { |
star297 | 0:35a1964228b4 | 505 | int h = y1 - y0 ; |
star297 | 0:35a1964228b4 | 506 | int w = x1 - x0 ; |
star297 | 0:35a1964228b4 | 507 | int pixel = h * w; |
star297 | 0:35a1964228b4 | 508 | window(x0,y0,w,h); |
star297 | 0:35a1964228b4 | 509 | wr_cmd(0x2C); |
star297 | 0:35a1964228b4 | 510 | wr_dat_start(); |
star297 | 0:35a1964228b4 | 511 | for (int p=0; p<pixel; p++) { |
star297 | 0:35a1964228b4 | 512 | _spi.write(color); |
star297 | 0:35a1964228b4 | 513 | _spi.write(color >> 8); |
star297 | 0:35a1964228b4 | 514 | } |
star297 | 0:35a1964228b4 | 515 | wr_dat_stop(); |
star297 | 0:35a1964228b4 | 516 | return; |
star297 | 0:35a1964228b4 | 517 | } |
star297 | 0:35a1964228b4 | 518 | void ST7735_TFT::locate(int x, int y) { |
star297 | 0:35a1964228b4 | 519 | char_x = x; |
star297 | 0:35a1964228b4 | 520 | char_y = y; |
star297 | 0:35a1964228b4 | 521 | } |
star297 | 0:35a1964228b4 | 522 | int ST7735_TFT::columns() { |
star297 | 0:35a1964228b4 | 523 | return width() / font[1]; |
star297 | 0:35a1964228b4 | 524 | } |
star297 | 0:35a1964228b4 | 525 | int ST7735_TFT::rows() { |
star297 | 0:35a1964228b4 | 526 | return height() / font[2]; |
star297 | 0:35a1964228b4 | 527 | } |
star297 | 0:35a1964228b4 | 528 | int ST7735_TFT::_putc(int value) { |
star297 | 0:35a1964228b4 | 529 | if (value == '\n') { // new line |
star297 | 0:35a1964228b4 | 530 | char_x = 0; |
star297 | 0:35a1964228b4 | 531 | char_y = char_y + font[2]; |
star297 | 0:35a1964228b4 | 532 | if (char_y >= height() - font[2]) { |
star297 | 0:35a1964228b4 | 533 | char_y = 0; |
star297 | 0:35a1964228b4 | 534 | } |
star297 | 0:35a1964228b4 | 535 | } else { |
star297 | 0:35a1964228b4 | 536 | character(char_x, char_y, value); |
star297 | 0:35a1964228b4 | 537 | } |
star297 | 0:35a1964228b4 | 538 | return value; |
star297 | 0:35a1964228b4 | 539 | } |
star297 | 0:35a1964228b4 | 540 | void ST7735_TFT::character(int x, int y, int c) { |
star297 | 0:35a1964228b4 | 541 | unsigned int hor,vert,offset,bpl,j,i,b; |
star297 | 0:35a1964228b4 | 542 | unsigned char* sign; |
star297 | 0:35a1964228b4 | 543 | unsigned char z,w; |
star297 | 0:35a1964228b4 | 544 | if ((c < 31) || (c > 127)) return; // test char range |
star297 | 0:35a1964228b4 | 545 | // read font parameter from start of array |
star297 | 0:35a1964228b4 | 546 | offset = font[0]; // bytes / char |
star297 | 0:35a1964228b4 | 547 | hor = font[1]; // get hor size of font |
star297 | 0:35a1964228b4 | 548 | vert = font[2]; // get vert size of font |
star297 | 0:35a1964228b4 | 549 | bpl = font[3]; // bytes per line |
star297 | 0:35a1964228b4 | 550 | if (char_x + hor > width()) { |
star297 | 0:35a1964228b4 | 551 | char_x = 0; |
star297 | 0:35a1964228b4 | 552 | char_y = char_y + vert; |
star297 | 0:35a1964228b4 | 553 | if (char_y >= height() - font[2]) { |
star297 | 0:35a1964228b4 | 554 | char_y = 0; |
star297 | 0:35a1964228b4 | 555 | } |
star297 | 0:35a1964228b4 | 556 | } |
star297 | 0:35a1964228b4 | 557 | window(char_x, char_y,hor,vert); // char box |
star297 | 0:35a1964228b4 | 558 | wr_cmd(0x2C); |
star297 | 0:35a1964228b4 | 559 | wr_dat_start(); |
star297 | 0:35a1964228b4 | 560 | sign = &font[((c -32) * offset) + 4]; // start of char bitmap |
star297 | 0:35a1964228b4 | 561 | w = sign[0]; // width of actual char |
star297 | 0:35a1964228b4 | 562 | for (j=0; j<vert; j++) { // vert line |
star297 | 0:35a1964228b4 | 563 | for (i=0; i<hor; i++) { // horz line |
star297 | 0:35a1964228b4 | 564 | z = sign[bpl * i + ((j & 0xF8) >> 3)+1]; |
star297 | 0:35a1964228b4 | 565 | b = 1 << (j & 0x07); |
star297 | 0:35a1964228b4 | 566 | if (( z & b ) == 0x00) { |
star297 | 0:35a1964228b4 | 567 | _spi.write(_background); |
star297 | 0:35a1964228b4 | 568 | _spi.write(_background>>8); |
star297 | 0:35a1964228b4 | 569 | } else { |
star297 | 0:35a1964228b4 | 570 | _spi.write(_foreground); |
star297 | 0:35a1964228b4 | 571 | _spi.write(_foreground>>8); |
star297 | 0:35a1964228b4 | 572 | } |
star297 | 0:35a1964228b4 | 573 | } |
star297 | 0:35a1964228b4 | 574 | } |
star297 | 0:35a1964228b4 | 575 | wr_dat_stop(); |
star297 | 0:35a1964228b4 | 576 | if ((w + 2) < hor) { // x offset to next char |
star297 | 0:35a1964228b4 | 577 | char_x += w + 2; |
star297 | 0:35a1964228b4 | 578 | } else char_x += hor; |
star297 | 0:35a1964228b4 | 579 | } |
star297 | 0:35a1964228b4 | 580 | void ST7735_TFT::set_font(unsigned char* f) { |
star297 | 0:35a1964228b4 | 581 | font = f; |
star297 | 0:35a1964228b4 | 582 | } |
star297 | 0:35a1964228b4 | 583 | void ST7735_TFT::Bitmap(unsigned int x, unsigned int y, unsigned int w, unsigned int h,unsigned char *bitmap) { |
star297 | 0:35a1964228b4 | 584 | unsigned int i,j; //value; |
star297 | 0:35a1964228b4 | 585 | unsigned short *bitmap_ptr = (unsigned short *)bitmap; |
star297 | 0:35a1964228b4 | 586 | window(x, y, w, h); |
star297 | 0:35a1964228b4 | 587 | wr_cmd(0x2C); |
star297 | 0:35a1964228b4 | 588 | wr_dat_start(); |
star297 | 0:35a1964228b4 | 589 | for (j = 0; j < h; j++) { //Lines |
star297 | 0:35a1964228b4 | 590 | for (i = 0; i < w; i++) { // copy pixel data to TFT |
star297 | 0:35a1964228b4 | 591 | _spi.write(*bitmap_ptr); // one line |
star297 | 0:35a1964228b4 | 592 | _spi.write(*bitmap_ptr >> 8); |
star297 | 0:35a1964228b4 | 593 | bitmap_ptr++; |
star297 | 0:35a1964228b4 | 594 | } |
star297 | 0:35a1964228b4 | 595 | } |
star297 | 0:35a1964228b4 | 596 | wr_dat_stop(); |
star297 | 0:35a1964228b4 | 597 | } |
star297 | 0:35a1964228b4 | 598 | int ST7735_TFT::BMP_16(unsigned int x, unsigned int y, const char *Name_BMP) { |
star297 | 0:35a1964228b4 | 599 | // BEWARE ! |
star297 | 0:35a1964228b4 | 600 | // NOT TESTED |
star297 | 0:35a1964228b4 | 601 | #define OffsetPixelWidth 18 |
star297 | 0:35a1964228b4 | 602 | #define OffsetPixelHeigh 22 |
star297 | 0:35a1964228b4 | 603 | #define OffsetFileSize 34 |
star297 | 0:35a1964228b4 | 604 | #define OffsetPixData 10 |
star297 | 0:35a1964228b4 | 605 | #define OffsetBPP 28 |
star297 | 0:35a1964228b4 | 606 | char filename[50]; |
star297 | 0:35a1964228b4 | 607 | unsigned char BMP_Header[54]; |
star297 | 0:35a1964228b4 | 608 | unsigned short BPP_t; |
star297 | 0:35a1964228b4 | 609 | unsigned int PixelWidth,PixelHeigh,start_data; |
star297 | 0:35a1964228b4 | 610 | unsigned int i,off; |
star297 | 0:35a1964228b4 | 611 | int padd,j; |
star297 | 0:35a1964228b4 | 612 | unsigned short *line; |
star297 | 0:35a1964228b4 | 613 | // get the filename |
star297 | 0:35a1964228b4 | 614 | //LocalFileSystem local("local"); not working on FRDM-KL25Z |
star297 | 0:35a1964228b4 | 615 | sprintf(&filename[0],"/local/"); |
star297 | 0:35a1964228b4 | 616 | i=7; |
star297 | 0:35a1964228b4 | 617 | while (*Name_BMP!='\0') { |
star297 | 0:35a1964228b4 | 618 | filename[i++]=*Name_BMP++; |
star297 | 0:35a1964228b4 | 619 | } |
star297 | 0:35a1964228b4 | 620 | FILE *Image = fopen((const char *)&filename[0], "r"); // open the bmp file |
star297 | 0:35a1964228b4 | 621 | if (!Image) { |
star297 | 0:35a1964228b4 | 622 | return(0); // error file not found ! |
star297 | 0:35a1964228b4 | 623 | } |
star297 | 0:35a1964228b4 | 624 | fread(&BMP_Header[0],1,54,Image); // get the BMP Header |
star297 | 0:35a1964228b4 | 625 | |
star297 | 0:35a1964228b4 | 626 | if (BMP_Header[0] != 0x42 || BMP_Header[1] != 0x4D) { // check magic byte |
star297 | 0:35a1964228b4 | 627 | fclose(Image); |
star297 | 0:35a1964228b4 | 628 | return(-1); // error no BMP file |
star297 | 0:35a1964228b4 | 629 | } |
star297 | 0:35a1964228b4 | 630 | BPP_t = BMP_Header[OffsetBPP] + (BMP_Header[OffsetBPP + 1] << 8); |
star297 | 0:35a1964228b4 | 631 | if (BPP_t != 0x0010) { |
star297 | 0:35a1964228b4 | 632 | fclose(Image); |
star297 | 0:35a1964228b4 | 633 | return(-2); // error no 16 bit BMP |
star297 | 0:35a1964228b4 | 634 | } |
star297 | 0:35a1964228b4 | 635 | PixelHeigh = BMP_Header[OffsetPixelHeigh] + (BMP_Header[OffsetPixelHeigh + 1] << 8) + (BMP_Header[OffsetPixelHeigh + 2] << 16) + (BMP_Header[OffsetPixelHeigh + 3] << 24); |
star297 | 0:35a1964228b4 | 636 | PixelWidth = BMP_Header[OffsetPixelWidth] + (BMP_Header[OffsetPixelWidth + 1] << 8) + (BMP_Header[OffsetPixelWidth + 2] << 16) + (BMP_Header[OffsetPixelWidth + 3] << 24); |
star297 | 0:35a1964228b4 | 637 | if (PixelHeigh > height() + y || PixelWidth > width() + x) { |
star297 | 0:35a1964228b4 | 638 | fclose(Image); |
star297 | 0:35a1964228b4 | 639 | return(-3); // to big |
star297 | 0:35a1964228b4 | 640 | } |
star297 | 0:35a1964228b4 | 641 | start_data = BMP_Header[OffsetPixData] + (BMP_Header[OffsetPixData + 1] << 8) + (BMP_Header[OffsetPixData + 2] << 16) + (BMP_Header[OffsetPixData + 3] << 24); |
star297 | 0:35a1964228b4 | 642 | |
star297 | 0:35a1964228b4 | 643 | line = (unsigned short *) malloc (PixelWidth); // we need a buffer for a line |
star297 | 0:35a1964228b4 | 644 | if (line == NULL) { |
star297 | 0:35a1964228b4 | 645 | return(-4); // error no memory |
star297 | 0:35a1964228b4 | 646 | } |
star297 | 0:35a1964228b4 | 647 | // the lines are padded to multiple of 4 bytes |
star297 | 0:35a1964228b4 | 648 | padd = -1; |
star297 | 0:35a1964228b4 | 649 | do { |
star297 | 0:35a1964228b4 | 650 | padd ++; |
star297 | 0:35a1964228b4 | 651 | } while ((PixelWidth * 2 + padd)%4 != 0); |
star297 | 0:35a1964228b4 | 652 | window(x, y,PixelWidth,PixelHeigh); |
star297 | 0:35a1964228b4 | 653 | wr_cmd(0x2C); |
star297 | 0:35a1964228b4 | 654 | wr_dat_start(); |
star297 | 0:35a1964228b4 | 655 | for (j = PixelHeigh - 1; j >= 0; j--) { //Lines bottom up |
star297 | 0:35a1964228b4 | 656 | off = j * (PixelWidth * 2 + padd) + start_data; // start of line |
star297 | 0:35a1964228b4 | 657 | fseek(Image, off ,SEEK_SET); |
star297 | 0:35a1964228b4 | 658 | fread(line,1,PixelWidth * 2,Image); // read a line - slow ! |
star297 | 0:35a1964228b4 | 659 | for (i = 0; i < PixelWidth; i++) { // copy pixel data to TFT |
star297 | 0:35a1964228b4 | 660 | _spi.write(line[i]); // one 16 bit pixel |
star297 | 0:35a1964228b4 | 661 | _spi.write(line[i]>>8); |
star297 | 0:35a1964228b4 | 662 | } |
star297 | 0:35a1964228b4 | 663 | } |
star297 | 0:35a1964228b4 | 664 | wr_dat_stop(); |
star297 | 0:35a1964228b4 | 665 | free (line); |
star297 | 0:35a1964228b4 | 666 | fclose(Image); |
star297 | 0:35a1964228b4 | 667 | return(1); |
star297 | 0:35a1964228b4 | 668 | } |