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