Erik -
/
Mini_DK_clk
Simple clock program for LPC1768 Mini-DK
Mini_DK/SPI_TFT/SPI_TFT.cpp@0:ab0aec01b38e, 2012-12-30 (annotated)
- Committer:
- Sissors
- Date:
- Sun Dec 30 21:31:56 2012 +0000
- Revision:
- 0:ab0aec01b38e
v0.1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sissors | 0:ab0aec01b38e | 1 | /* mbed library for 240*320 pixel TFT with ILI9320 LCD Controller |
Sissors | 0:ab0aec01b38e | 2 | * Rewrite from Peter Drescher code - http://mbed.org/cookbook/SPI-driven-QVGA-TFT |
Sissors | 0:ab0aec01b38e | 3 | * |
Sissors | 0:ab0aec01b38e | 4 | * TODO : BMP routine |
Sissors | 0:ab0aec01b38e | 5 | */ |
Sissors | 0:ab0aec01b38e | 6 | |
Sissors | 0:ab0aec01b38e | 7 | |
Sissors | 0:ab0aec01b38e | 8 | |
Sissors | 0:ab0aec01b38e | 9 | #include "SPI_TFT.h" |
Sissors | 0:ab0aec01b38e | 10 | #include "mbed.h" |
Sissors | 0:ab0aec01b38e | 11 | |
Sissors | 0:ab0aec01b38e | 12 | |
Sissors | 0:ab0aec01b38e | 13 | #define BPP 16 // Bits per pixel |
Sissors | 0:ab0aec01b38e | 14 | |
Sissors | 0:ab0aec01b38e | 15 | |
Sissors | 0:ab0aec01b38e | 16 | SPI_TFT::SPI_TFT(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset, const char *name) |
Sissors | 0:ab0aec01b38e | 17 | : GraphicsDisplay(name), _spi(mosi, miso, sclk), _cs(cs), _reset(reset) |
Sissors | 0:ab0aec01b38e | 18 | { |
Sissors | 0:ab0aec01b38e | 19 | char_x = 0; |
Sissors | 0:ab0aec01b38e | 20 | tft_reset(); |
Sissors | 0:ab0aec01b38e | 21 | set_orientation(0); |
Sissors | 0:ab0aec01b38e | 22 | } |
Sissors | 0:ab0aec01b38e | 23 | |
Sissors | 0:ab0aec01b38e | 24 | int SPI_TFT::width() |
Sissors | 0:ab0aec01b38e | 25 | { |
Sissors | 0:ab0aec01b38e | 26 | if (orientation == 0 || orientation == 2) return 240; |
Sissors | 0:ab0aec01b38e | 27 | else return 320; |
Sissors | 0:ab0aec01b38e | 28 | } |
Sissors | 0:ab0aec01b38e | 29 | |
Sissors | 0:ab0aec01b38e | 30 | int SPI_TFT::height() |
Sissors | 0:ab0aec01b38e | 31 | { |
Sissors | 0:ab0aec01b38e | 32 | if (orientation == 0 || orientation == 2) return 320; |
Sissors | 0:ab0aec01b38e | 33 | else return 240; |
Sissors | 0:ab0aec01b38e | 34 | } |
Sissors | 0:ab0aec01b38e | 35 | |
Sissors | 0:ab0aec01b38e | 36 | // |
Sissors | 0:ab0aec01b38e | 37 | void SPI_TFT::set_orientation(unsigned int o) |
Sissors | 0:ab0aec01b38e | 38 | { |
Sissors | 0:ab0aec01b38e | 39 | orientation = o; |
Sissors | 0:ab0aec01b38e | 40 | WindowMax(); |
Sissors | 0:ab0aec01b38e | 41 | } |
Sissors | 0:ab0aec01b38e | 42 | |
Sissors | 0:ab0aec01b38e | 43 | // ILI9320 |
Sissors | 0:ab0aec01b38e | 44 | // Orientation is only set before a window command (registers 0x50..0x53) |
Sissors | 0:ab0aec01b38e | 45 | // reg 03h (Entry Mode) : BGR = 1 - ORG = 1 - ID0, ID1 and AM are set according to the orientation variable. |
Sissors | 0:ab0aec01b38e | 46 | // IMPORTANT : when ORG = 1, the GRAM writing direction follows the orientation (ID0, ID1, AM bits) |
Sissors | 0:ab0aec01b38e | 47 | // AND we need to use the window command (reg 50h..53h) to write to an area on the display |
Sissors | 0:ab0aec01b38e | 48 | // because we cannot change reg 20h and 21h to set the GRAM address (they both remain at 00h). |
Sissors | 0:ab0aec01b38e | 49 | // This means that the pixel routine does not work when ORG = 1. |
Sissors | 0:ab0aec01b38e | 50 | // Routines relying on the pixel routine first need to set reg 03h = 0x1030 |
Sissors | 0:ab0aec01b38e | 51 | // (cls, circle and line do so) AND need to write the data according to the orientation variable. |
Sissors | 0:ab0aec01b38e | 52 | |
Sissors | 0:ab0aec01b38e | 53 | void SPI_TFT::mod_orientation(void) |
Sissors | 0:ab0aec01b38e | 54 | { |
Sissors | 0:ab0aec01b38e | 55 | switch (orientation) |
Sissors | 0:ab0aec01b38e | 56 | { |
Sissors | 0:ab0aec01b38e | 57 | case 0: |
Sissors | 0:ab0aec01b38e | 58 | wr_reg(0x03, 0x10b0); // ID1 = 1, ID0 = 1, AM = 0 - Portrait |
Sissors | 0:ab0aec01b38e | 59 | break; |
Sissors | 0:ab0aec01b38e | 60 | case 1: |
Sissors | 0:ab0aec01b38e | 61 | wr_reg(0x03, 0x10a8); // ID1 = 1, ID0 = 0, AM = 0 - Landscape |
Sissors | 0:ab0aec01b38e | 62 | break; |
Sissors | 0:ab0aec01b38e | 63 | case 2: |
Sissors | 0:ab0aec01b38e | 64 | wr_reg(0x03, 0x1080); // ID1 = 0, ID0 = 0, AM = 1 - Portrait upside down |
Sissors | 0:ab0aec01b38e | 65 | break; |
Sissors | 0:ab0aec01b38e | 66 | case 3: |
Sissors | 0:ab0aec01b38e | 67 | wr_reg(0x03, 0x1098); // ID1 = 0, ID0 = 1, AM = 1 - Landscape upside down |
Sissors | 0:ab0aec01b38e | 68 | break; |
Sissors | 0:ab0aec01b38e | 69 | } |
Sissors | 0:ab0aec01b38e | 70 | } |
Sissors | 0:ab0aec01b38e | 71 | |
Sissors | 0:ab0aec01b38e | 72 | void SPI_TFT::wr_cmd(unsigned char cmd) |
Sissors | 0:ab0aec01b38e | 73 | { |
Sissors | 0:ab0aec01b38e | 74 | _cs = 0; |
Sissors | 0:ab0aec01b38e | 75 | _spi.write(0x70); |
Sissors | 0:ab0aec01b38e | 76 | _spi.write(0x00); |
Sissors | 0:ab0aec01b38e | 77 | _spi.write(cmd); |
Sissors | 0:ab0aec01b38e | 78 | _cs = 1; |
Sissors | 0:ab0aec01b38e | 79 | } |
Sissors | 0:ab0aec01b38e | 80 | |
Sissors | 0:ab0aec01b38e | 81 | void SPI_TFT::wr_dat(unsigned short dat) |
Sissors | 0:ab0aec01b38e | 82 | { |
Sissors | 0:ab0aec01b38e | 83 | unsigned char u,l; |
Sissors | 0:ab0aec01b38e | 84 | u = (dat >> 0x08); |
Sissors | 0:ab0aec01b38e | 85 | l = (dat & 0xff); |
Sissors | 0:ab0aec01b38e | 86 | _cs = 0; |
Sissors | 0:ab0aec01b38e | 87 | _spi.write(0x72); |
Sissors | 0:ab0aec01b38e | 88 | _spi.write(u); |
Sissors | 0:ab0aec01b38e | 89 | _spi.write(l); |
Sissors | 0:ab0aec01b38e | 90 | _cs = 1; |
Sissors | 0:ab0aec01b38e | 91 | } |
Sissors | 0:ab0aec01b38e | 92 | |
Sissors | 0:ab0aec01b38e | 93 | void SPI_TFT::wr_dat_start(void) |
Sissors | 0:ab0aec01b38e | 94 | { |
Sissors | 0:ab0aec01b38e | 95 | _spi.write(0x72); |
Sissors | 0:ab0aec01b38e | 96 | } |
Sissors | 0:ab0aec01b38e | 97 | |
Sissors | 0:ab0aec01b38e | 98 | void SPI_TFT::wr_dat_only(unsigned short dat) |
Sissors | 0:ab0aec01b38e | 99 | { |
Sissors | 0:ab0aec01b38e | 100 | unsigned char u,l; |
Sissors | 0:ab0aec01b38e | 101 | u = (dat >> 0x08); |
Sissors | 0:ab0aec01b38e | 102 | l = (dat & 0xff); |
Sissors | 0:ab0aec01b38e | 103 | _spi.write(u); |
Sissors | 0:ab0aec01b38e | 104 | _spi.write(l); |
Sissors | 0:ab0aec01b38e | 105 | } |
Sissors | 0:ab0aec01b38e | 106 | |
Sissors | 0:ab0aec01b38e | 107 | unsigned short SPI_TFT::rd_dat(void) // SPI frequency needs to be lowered on read |
Sissors | 0:ab0aec01b38e | 108 | { |
Sissors | 0:ab0aec01b38e | 109 | unsigned short val = 0; |
Sissors | 0:ab0aec01b38e | 110 | _cs = 0; |
Sissors | 0:ab0aec01b38e | 111 | _spi.frequency(SPI_F_LO); |
Sissors | 0:ab0aec01b38e | 112 | _spi.write(0x73); |
Sissors | 0:ab0aec01b38e | 113 | _spi.write(0x00); |
Sissors | 0:ab0aec01b38e | 114 | val = _spi.write(0); // Dummy read |
Sissors | 0:ab0aec01b38e | 115 | val = _spi.write(0); // Read D8..D15 |
Sissors | 0:ab0aec01b38e | 116 | val <<= 8; |
Sissors | 0:ab0aec01b38e | 117 | val |= _spi.write(0); // Read D0..D7 |
Sissors | 0:ab0aec01b38e | 118 | _cs = 1; |
Sissors | 0:ab0aec01b38e | 119 | _spi.frequency(SPI_F_HI); |
Sissors | 0:ab0aec01b38e | 120 | return (val); |
Sissors | 0:ab0aec01b38e | 121 | } |
Sissors | 0:ab0aec01b38e | 122 | |
Sissors | 0:ab0aec01b38e | 123 | void SPI_TFT::wr_reg(unsigned char reg, unsigned short val) |
Sissors | 0:ab0aec01b38e | 124 | { |
Sissors | 0:ab0aec01b38e | 125 | wr_cmd(reg); |
Sissors | 0:ab0aec01b38e | 126 | wr_dat(val); |
Sissors | 0:ab0aec01b38e | 127 | } |
Sissors | 0:ab0aec01b38e | 128 | |
Sissors | 0:ab0aec01b38e | 129 | unsigned short SPI_TFT::rd_reg(unsigned char reg) |
Sissors | 0:ab0aec01b38e | 130 | { |
Sissors | 0:ab0aec01b38e | 131 | wr_cmd(reg); |
Sissors | 0:ab0aec01b38e | 132 | return(rd_dat()); |
Sissors | 0:ab0aec01b38e | 133 | } |
Sissors | 0:ab0aec01b38e | 134 | |
Sissors | 0:ab0aec01b38e | 135 | unsigned short SPI_TFT::Read_ID(void) // IMPORTANT : SPI frequency needs to be lowered when reading |
Sissors | 0:ab0aec01b38e | 136 | { |
Sissors | 0:ab0aec01b38e | 137 | unsigned short val = 0; |
Sissors | 0:ab0aec01b38e | 138 | _cs = 0; |
Sissors | 0:ab0aec01b38e | 139 | _spi.write(0x70); |
Sissors | 0:ab0aec01b38e | 140 | _spi.write(0x00); |
Sissors | 0:ab0aec01b38e | 141 | _spi.write(0X00); |
Sissors | 0:ab0aec01b38e | 142 | _cs = 1; |
Sissors | 0:ab0aec01b38e | 143 | _spi.frequency(SPI_F_LO); |
Sissors | 0:ab0aec01b38e | 144 | _cs = 0; |
Sissors | 0:ab0aec01b38e | 145 | _spi.write(0x73); |
Sissors | 0:ab0aec01b38e | 146 | val = _spi.write(0x00); // Dummy read |
Sissors | 0:ab0aec01b38e | 147 | val = _spi.write(0x00); // Read D8..D15 |
Sissors | 0:ab0aec01b38e | 148 | val <<= 8; |
Sissors | 0:ab0aec01b38e | 149 | val |= _spi.write(0x00); // Read D0..D7 |
Sissors | 0:ab0aec01b38e | 150 | _cs = 1; |
Sissors | 0:ab0aec01b38e | 151 | _spi.frequency(SPI_F_HI); |
Sissors | 0:ab0aec01b38e | 152 | return (val); |
Sissors | 0:ab0aec01b38e | 153 | } |
Sissors | 0:ab0aec01b38e | 154 | |
Sissors | 0:ab0aec01b38e | 155 | void SPI_TFT::SetCursor( unsigned short Xpos, unsigned short Ypos ) |
Sissors | 0:ab0aec01b38e | 156 | { |
Sissors | 0:ab0aec01b38e | 157 | wr_reg(0x20, Xpos ); |
Sissors | 0:ab0aec01b38e | 158 | wr_reg(0x21, Ypos ); |
Sissors | 0:ab0aec01b38e | 159 | } |
Sissors | 0:ab0aec01b38e | 160 | |
Sissors | 0:ab0aec01b38e | 161 | void SPI_TFT::tft_reset() |
Sissors | 0:ab0aec01b38e | 162 | { |
Sissors | 0:ab0aec01b38e | 163 | _spi.format(8,3); // 8 bit spi mode 3 |
Sissors | 0:ab0aec01b38e | 164 | _spi.frequency(SPI_F_HI); // 48 Mhz SPI clock |
Sissors | 0:ab0aec01b38e | 165 | |
Sissors | 0:ab0aec01b38e | 166 | wr_reg(0x00,0x0000); |
Sissors | 0:ab0aec01b38e | 167 | wr_reg(0x01,0x0100); // Driver Output Control |
Sissors | 0:ab0aec01b38e | 168 | wr_reg(0x02,0x0700); // LCD Driver Waveform Control |
Sissors | 0:ab0aec01b38e | 169 | wr_reg(0x03,0x1030); // Set the scan mode |
Sissors | 0:ab0aec01b38e | 170 | wr_reg(0x04,0x0000); // Scaling Control |
Sissors | 0:ab0aec01b38e | 171 | wr_reg(0x08,0x0202); // Display Control 2 |
Sissors | 0:ab0aec01b38e | 172 | wr_reg(0x09,0x0000); // Display Control 3 |
Sissors | 0:ab0aec01b38e | 173 | wr_reg(0x0a,0x0000); // Frame Cycle Contal |
Sissors | 0:ab0aec01b38e | 174 | wr_reg(0x0c,(1<<0)); // Extern Display Interface Control 1 |
Sissors | 0:ab0aec01b38e | 175 | wr_reg(0x0d,0x0000); // Frame Maker Position |
Sissors | 0:ab0aec01b38e | 176 | wr_reg(0x0f,0x0000); // Extern Display Interface Control 2 |
Sissors | 0:ab0aec01b38e | 177 | |
Sissors | 0:ab0aec01b38e | 178 | wait_ms(50); |
Sissors | 0:ab0aec01b38e | 179 | |
Sissors | 0:ab0aec01b38e | 180 | wr_reg(0x07,0x0101); // Display Control |
Sissors | 0:ab0aec01b38e | 181 | |
Sissors | 0:ab0aec01b38e | 182 | wait_ms(50); |
Sissors | 0:ab0aec01b38e | 183 | |
Sissors | 0:ab0aec01b38e | 184 | wr_reg(0x10,(1<<12)|(0<<8)|(1<<7)|(1<<6)|(0<<4)); // Power Control 1 |
Sissors | 0:ab0aec01b38e | 185 | wr_reg(0x11,0x0007); // Power Control 2 |
Sissors | 0:ab0aec01b38e | 186 | wr_reg(0x12,(1<<8)|(1<<4)|(0<<0)); // Power Control 3 |
Sissors | 0:ab0aec01b38e | 187 | wr_reg(0x13,0x0b00); // Power Control 4 |
Sissors | 0:ab0aec01b38e | 188 | wr_reg(0x29,0x0000); // Power Control 7 |
Sissors | 0:ab0aec01b38e | 189 | wr_reg(0x2b,(1<<14)|(1<<4)); |
Sissors | 0:ab0aec01b38e | 190 | |
Sissors | 0:ab0aec01b38e | 191 | wr_reg(0x50,0); // Set X Start |
Sissors | 0:ab0aec01b38e | 192 | wr_reg(0x51,239); // Set X End |
Sissors | 0:ab0aec01b38e | 193 | wr_reg(0x52,0); // Set Y Start |
Sissors | 0:ab0aec01b38e | 194 | wr_reg(0x53,319); // Set Y End |
Sissors | 0:ab0aec01b38e | 195 | |
Sissors | 0:ab0aec01b38e | 196 | wait_ms(50); |
Sissors | 0:ab0aec01b38e | 197 | |
Sissors | 0:ab0aec01b38e | 198 | wr_reg(0x60,0x2700); // Driver Output Control |
Sissors | 0:ab0aec01b38e | 199 | wr_reg(0x61,0x0001); // Driver Output Control |
Sissors | 0:ab0aec01b38e | 200 | wr_reg(0x6a,0x0000); // Vertical Srcoll Control |
Sissors | 0:ab0aec01b38e | 201 | |
Sissors | 0:ab0aec01b38e | 202 | wr_reg(0x80,0x0000); // Display Position Partial Display 1 |
Sissors | 0:ab0aec01b38e | 203 | wr_reg(0x81,0x0000); // RAM Address Start Partial Display 1 |
Sissors | 0:ab0aec01b38e | 204 | wr_reg(0x82,0x0000); // RAM Address End-Partial Display 1 |
Sissors | 0:ab0aec01b38e | 205 | wr_reg(0x83,0x0000); // Displsy Position Partial Display 2 |
Sissors | 0:ab0aec01b38e | 206 | wr_reg(0x84,0x0000); // RAM Address Start Partial Display 2 |
Sissors | 0:ab0aec01b38e | 207 | wr_reg(0x85,0x0000); // RAM Address End Partial Display 2 |
Sissors | 0:ab0aec01b38e | 208 | |
Sissors | 0:ab0aec01b38e | 209 | wr_reg(0x90,(0<<7)|(16<<0)); // Frame Cycle Control |
Sissors | 0:ab0aec01b38e | 210 | wr_reg(0x92,0x0000); // Panel Interface Control 2 |
Sissors | 0:ab0aec01b38e | 211 | wr_reg(0x93,0x0001); // Panel Interface Control 3 |
Sissors | 0:ab0aec01b38e | 212 | wr_reg(0x95,0x0110); // Frame Cycle Control |
Sissors | 0:ab0aec01b38e | 213 | wr_reg(0x97,(0<<8)); |
Sissors | 0:ab0aec01b38e | 214 | wr_reg(0x98,0x0000); // Frame Cycle Control |
Sissors | 0:ab0aec01b38e | 215 | wr_reg(0x07,0x0133); |
Sissors | 0:ab0aec01b38e | 216 | |
Sissors | 0:ab0aec01b38e | 217 | wait_ms(100); |
Sissors | 0:ab0aec01b38e | 218 | WindowMax(); |
Sissors | 0:ab0aec01b38e | 219 | } |
Sissors | 0:ab0aec01b38e | 220 | |
Sissors | 0:ab0aec01b38e | 221 | |
Sissors | 0:ab0aec01b38e | 222 | void SPI_TFT::pixel(int x, int y, int color) |
Sissors | 0:ab0aec01b38e | 223 | { |
Sissors | 0:ab0aec01b38e | 224 | switch (orientation) |
Sissors | 0:ab0aec01b38e | 225 | { |
Sissors | 0:ab0aec01b38e | 226 | case 0: |
Sissors | 0:ab0aec01b38e | 227 | wr_reg(0x20, x); |
Sissors | 0:ab0aec01b38e | 228 | wr_reg(0x21, y); |
Sissors | 0:ab0aec01b38e | 229 | break; |
Sissors | 0:ab0aec01b38e | 230 | case 1: |
Sissors | 0:ab0aec01b38e | 231 | wr_reg(0x20, 239-y); |
Sissors | 0:ab0aec01b38e | 232 | wr_reg(0x21, x); |
Sissors | 0:ab0aec01b38e | 233 | break; |
Sissors | 0:ab0aec01b38e | 234 | case 2: |
Sissors | 0:ab0aec01b38e | 235 | wr_reg(0x20, 239-x); |
Sissors | 0:ab0aec01b38e | 236 | wr_reg(0x21, 319-y); |
Sissors | 0:ab0aec01b38e | 237 | break; |
Sissors | 0:ab0aec01b38e | 238 | case 3: |
Sissors | 0:ab0aec01b38e | 239 | wr_reg(0x20, y); |
Sissors | 0:ab0aec01b38e | 240 | wr_reg(0x21, 319-x); |
Sissors | 0:ab0aec01b38e | 241 | break; |
Sissors | 0:ab0aec01b38e | 242 | } |
Sissors | 0:ab0aec01b38e | 243 | wr_cmd(0x22); |
Sissors | 0:ab0aec01b38e | 244 | wr_dat(color); |
Sissors | 0:ab0aec01b38e | 245 | } |
Sissors | 0:ab0aec01b38e | 246 | |
Sissors | 0:ab0aec01b38e | 247 | |
Sissors | 0:ab0aec01b38e | 248 | void SPI_TFT::window(int x, int y, int w, int h) |
Sissors | 0:ab0aec01b38e | 249 | { |
Sissors | 0:ab0aec01b38e | 250 | unsigned int xw1, yh1; |
Sissors | 0:ab0aec01b38e | 251 | xw1 = x + w - 1; |
Sissors | 0:ab0aec01b38e | 252 | yh1 = y + h - 1; |
Sissors | 0:ab0aec01b38e | 253 | wr_reg(0x20, x); |
Sissors | 0:ab0aec01b38e | 254 | wr_reg(0x21, y); |
Sissors | 0:ab0aec01b38e | 255 | switch (orientation) |
Sissors | 0:ab0aec01b38e | 256 | { |
Sissors | 0:ab0aec01b38e | 257 | case 0: |
Sissors | 0:ab0aec01b38e | 258 | wr_reg(0x50, x); |
Sissors | 0:ab0aec01b38e | 259 | wr_reg(0x51, xw1); |
Sissors | 0:ab0aec01b38e | 260 | wr_reg(0x52, y); |
Sissors | 0:ab0aec01b38e | 261 | wr_reg(0x53, yh1); |
Sissors | 0:ab0aec01b38e | 262 | break; |
Sissors | 0:ab0aec01b38e | 263 | case 1: |
Sissors | 0:ab0aec01b38e | 264 | wr_reg(0x50, 239 - yh1); |
Sissors | 0:ab0aec01b38e | 265 | wr_reg(0x51, 239 - y); |
Sissors | 0:ab0aec01b38e | 266 | wr_reg(0x52, x); |
Sissors | 0:ab0aec01b38e | 267 | wr_reg(0x53, xw1); |
Sissors | 0:ab0aec01b38e | 268 | break; |
Sissors | 0:ab0aec01b38e | 269 | case 2: |
Sissors | 0:ab0aec01b38e | 270 | wr_reg(0x50, 239 - xw1); |
Sissors | 0:ab0aec01b38e | 271 | wr_reg(0x51, 239 - x); |
Sissors | 0:ab0aec01b38e | 272 | wr_reg(0x52, 319 - yh1); |
Sissors | 0:ab0aec01b38e | 273 | wr_reg(0x53, 319 - y); |
Sissors | 0:ab0aec01b38e | 274 | break; |
Sissors | 0:ab0aec01b38e | 275 | case 3: |
Sissors | 0:ab0aec01b38e | 276 | wr_reg(0x50, y); |
Sissors | 0:ab0aec01b38e | 277 | wr_reg(0x51, yh1); |
Sissors | 0:ab0aec01b38e | 278 | wr_reg(0x52, 319 - xw1); |
Sissors | 0:ab0aec01b38e | 279 | wr_reg(0x53, 319 - x); |
Sissors | 0:ab0aec01b38e | 280 | break; |
Sissors | 0:ab0aec01b38e | 281 | } |
Sissors | 0:ab0aec01b38e | 282 | } |
Sissors | 0:ab0aec01b38e | 283 | |
Sissors | 0:ab0aec01b38e | 284 | |
Sissors | 0:ab0aec01b38e | 285 | void SPI_TFT::WindowMax(void) |
Sissors | 0:ab0aec01b38e | 286 | { |
Sissors | 0:ab0aec01b38e | 287 | window(0, 0, width(), height()); |
Sissors | 0:ab0aec01b38e | 288 | } |
Sissors | 0:ab0aec01b38e | 289 | |
Sissors | 0:ab0aec01b38e | 290 | |
Sissors | 0:ab0aec01b38e | 291 | void SPI_TFT::cls (void) |
Sissors | 0:ab0aec01b38e | 292 | { |
Sissors | 0:ab0aec01b38e | 293 | unsigned long int index=0; |
Sissors | 0:ab0aec01b38e | 294 | wr_reg(0x03, 0x1030); |
Sissors | 0:ab0aec01b38e | 295 | WindowMax(); |
Sissors | 0:ab0aec01b38e | 296 | SetCursor(0,0); |
Sissors | 0:ab0aec01b38e | 297 | wr_cmd(0x22); |
Sissors | 0:ab0aec01b38e | 298 | _cs = 0; |
Sissors | 0:ab0aec01b38e | 299 | wr_dat_start(); |
Sissors | 0:ab0aec01b38e | 300 | |
Sissors | 0:ab0aec01b38e | 301 | _spi.format(16,3); |
Sissors | 0:ab0aec01b38e | 302 | unsigned short dat = _background; |
Sissors | 0:ab0aec01b38e | 303 | |
Sissors | 0:ab0aec01b38e | 304 | for( index = 0; index<width()*height(); index++ ) |
Sissors | 0:ab0aec01b38e | 305 | { |
Sissors | 0:ab0aec01b38e | 306 | _spi.write(dat); |
Sissors | 0:ab0aec01b38e | 307 | } |
Sissors | 0:ab0aec01b38e | 308 | |
Sissors | 0:ab0aec01b38e | 309 | _spi.format(8,3); |
Sissors | 0:ab0aec01b38e | 310 | |
Sissors | 0:ab0aec01b38e | 311 | /*for( index = 0; index < width() * height(); index++ ) |
Sissors | 0:ab0aec01b38e | 312 | { |
Sissors | 0:ab0aec01b38e | 313 | wr_dat_only(color); |
Sissors | 0:ab0aec01b38e | 314 | }*/ |
Sissors | 0:ab0aec01b38e | 315 | _cs = 1; |
Sissors | 0:ab0aec01b38e | 316 | } |
Sissors | 0:ab0aec01b38e | 317 | |
Sissors | 0:ab0aec01b38e | 318 | void SPI_TFT::hline(int x0, int x1, int y, int color) |
Sissors | 0:ab0aec01b38e | 319 | { |
Sissors | 0:ab0aec01b38e | 320 | unsigned int index=0; |
Sissors | 0:ab0aec01b38e | 321 | int w; |
Sissors | 0:ab0aec01b38e | 322 | w = x1 - x0 + 1; |
Sissors | 0:ab0aec01b38e | 323 | mod_orientation(); |
Sissors | 0:ab0aec01b38e | 324 | window(x0,y,w,1); |
Sissors | 0:ab0aec01b38e | 325 | wr_cmd(0x22); |
Sissors | 0:ab0aec01b38e | 326 | _cs = 0; |
Sissors | 0:ab0aec01b38e | 327 | wr_dat_start(); |
Sissors | 0:ab0aec01b38e | 328 | for( index = 0; index < (x1 - x0); index++ ) |
Sissors | 0:ab0aec01b38e | 329 | { |
Sissors | 0:ab0aec01b38e | 330 | wr_dat_only(color); |
Sissors | 0:ab0aec01b38e | 331 | } |
Sissors | 0:ab0aec01b38e | 332 | _cs = 1; |
Sissors | 0:ab0aec01b38e | 333 | return; |
Sissors | 0:ab0aec01b38e | 334 | } |
Sissors | 0:ab0aec01b38e | 335 | |
Sissors | 0:ab0aec01b38e | 336 | void SPI_TFT::vline(int x, int y0, int y1, int color) |
Sissors | 0:ab0aec01b38e | 337 | { |
Sissors | 0:ab0aec01b38e | 338 | unsigned int index=0; |
Sissors | 0:ab0aec01b38e | 339 | int h; |
Sissors | 0:ab0aec01b38e | 340 | h = y1 - y0 + 1; |
Sissors | 0:ab0aec01b38e | 341 | mod_orientation(); |
Sissors | 0:ab0aec01b38e | 342 | window(x,y0,1,h); |
Sissors | 0:ab0aec01b38e | 343 | wr_cmd(0x22); |
Sissors | 0:ab0aec01b38e | 344 | _cs = 0; |
Sissors | 0:ab0aec01b38e | 345 | wr_dat_start(); |
Sissors | 0:ab0aec01b38e | 346 | for( index = 0; index < (y1 - y0); index++ ) |
Sissors | 0:ab0aec01b38e | 347 | { |
Sissors | 0:ab0aec01b38e | 348 | wr_dat_only(color); |
Sissors | 0:ab0aec01b38e | 349 | } |
Sissors | 0:ab0aec01b38e | 350 | _cs = 1; |
Sissors | 0:ab0aec01b38e | 351 | return; |
Sissors | 0:ab0aec01b38e | 352 | } |
Sissors | 0:ab0aec01b38e | 353 | |
Sissors | 0:ab0aec01b38e | 354 | void SPI_TFT::line(int x0, int y0, int x1, int y1, int color) |
Sissors | 0:ab0aec01b38e | 355 | { |
Sissors | 0:ab0aec01b38e | 356 | wr_reg(0x03, 0x1030); |
Sissors | 0:ab0aec01b38e | 357 | WindowMax(); |
Sissors | 0:ab0aec01b38e | 358 | int dx = 0, dy = 0; |
Sissors | 0:ab0aec01b38e | 359 | int dx_sym = 0, dy_sym = 0; |
Sissors | 0:ab0aec01b38e | 360 | int dx_x2 = 0, dy_x2 = 0; |
Sissors | 0:ab0aec01b38e | 361 | int di = 0; |
Sissors | 0:ab0aec01b38e | 362 | |
Sissors | 0:ab0aec01b38e | 363 | dx = x1-x0; |
Sissors | 0:ab0aec01b38e | 364 | dy = y1-y0; |
Sissors | 0:ab0aec01b38e | 365 | |
Sissors | 0:ab0aec01b38e | 366 | if (dx == 0) { /* vertical line */ |
Sissors | 0:ab0aec01b38e | 367 | if (y1 > y0) vline(x0,y0,y1,color); |
Sissors | 0:ab0aec01b38e | 368 | else vline(x0,y1,y0,color); |
Sissors | 0:ab0aec01b38e | 369 | return; |
Sissors | 0:ab0aec01b38e | 370 | } |
Sissors | 0:ab0aec01b38e | 371 | |
Sissors | 0:ab0aec01b38e | 372 | if (dx > 0) { |
Sissors | 0:ab0aec01b38e | 373 | dx_sym = 1; |
Sissors | 0:ab0aec01b38e | 374 | } else { |
Sissors | 0:ab0aec01b38e | 375 | dx_sym = -1; |
Sissors | 0:ab0aec01b38e | 376 | } |
Sissors | 0:ab0aec01b38e | 377 | if (dy == 0) { /* horizontal line */ |
Sissors | 0:ab0aec01b38e | 378 | if (x1 > x0) hline(x0,x1,y0,color); |
Sissors | 0:ab0aec01b38e | 379 | else hline(x1,x0,y0,color); |
Sissors | 0:ab0aec01b38e | 380 | return; |
Sissors | 0:ab0aec01b38e | 381 | } |
Sissors | 0:ab0aec01b38e | 382 | |
Sissors | 0:ab0aec01b38e | 383 | if (dy > 0) { |
Sissors | 0:ab0aec01b38e | 384 | dy_sym = 1; |
Sissors | 0:ab0aec01b38e | 385 | } else { |
Sissors | 0:ab0aec01b38e | 386 | dy_sym = -1; |
Sissors | 0:ab0aec01b38e | 387 | } |
Sissors | 0:ab0aec01b38e | 388 | |
Sissors | 0:ab0aec01b38e | 389 | dx = dx_sym*dx; |
Sissors | 0:ab0aec01b38e | 390 | dy = dy_sym*dy; |
Sissors | 0:ab0aec01b38e | 391 | |
Sissors | 0:ab0aec01b38e | 392 | dx_x2 = dx*2; |
Sissors | 0:ab0aec01b38e | 393 | dy_x2 = dy*2; |
Sissors | 0:ab0aec01b38e | 394 | |
Sissors | 0:ab0aec01b38e | 395 | if (dx >= dy) { |
Sissors | 0:ab0aec01b38e | 396 | di = dy_x2 - dx; |
Sissors | 0:ab0aec01b38e | 397 | while (x0 != x1) { |
Sissors | 0:ab0aec01b38e | 398 | |
Sissors | 0:ab0aec01b38e | 399 | pixel(x0, y0, color); |
Sissors | 0:ab0aec01b38e | 400 | x0 += dx_sym; |
Sissors | 0:ab0aec01b38e | 401 | if (di<0) { |
Sissors | 0:ab0aec01b38e | 402 | di += dy_x2; |
Sissors | 0:ab0aec01b38e | 403 | } else { |
Sissors | 0:ab0aec01b38e | 404 | di += dy_x2 - dx_x2; |
Sissors | 0:ab0aec01b38e | 405 | y0 += dy_sym; |
Sissors | 0:ab0aec01b38e | 406 | } |
Sissors | 0:ab0aec01b38e | 407 | } |
Sissors | 0:ab0aec01b38e | 408 | pixel(x0, y0, color); |
Sissors | 0:ab0aec01b38e | 409 | } else { |
Sissors | 0:ab0aec01b38e | 410 | di = dx_x2 - dy; |
Sissors | 0:ab0aec01b38e | 411 | while (y0 != y1) { |
Sissors | 0:ab0aec01b38e | 412 | pixel(x0, y0, color); |
Sissors | 0:ab0aec01b38e | 413 | y0 += dy_sym; |
Sissors | 0:ab0aec01b38e | 414 | if (di < 0) { |
Sissors | 0:ab0aec01b38e | 415 | di += dx_x2; |
Sissors | 0:ab0aec01b38e | 416 | } else { |
Sissors | 0:ab0aec01b38e | 417 | di += dx_x2 - dy_x2; |
Sissors | 0:ab0aec01b38e | 418 | x0 += dx_sym; |
Sissors | 0:ab0aec01b38e | 419 | } |
Sissors | 0:ab0aec01b38e | 420 | } |
Sissors | 0:ab0aec01b38e | 421 | pixel(x0, y0, color); |
Sissors | 0:ab0aec01b38e | 422 | } |
Sissors | 0:ab0aec01b38e | 423 | return; |
Sissors | 0:ab0aec01b38e | 424 | } |
Sissors | 0:ab0aec01b38e | 425 | |
Sissors | 0:ab0aec01b38e | 426 | |
Sissors | 0:ab0aec01b38e | 427 | void SPI_TFT::rect(int x0, int y0, int w, int h, int color) |
Sissors | 0:ab0aec01b38e | 428 | { |
Sissors | 0:ab0aec01b38e | 429 | hline(x0,x0+w,y0,color); |
Sissors | 0:ab0aec01b38e | 430 | vline(x0,y0,y0+h,color); |
Sissors | 0:ab0aec01b38e | 431 | hline(x0,x0+w,y0+h,color); |
Sissors | 0:ab0aec01b38e | 432 | vline(x0+w,y0,y0+h,color); |
Sissors | 0:ab0aec01b38e | 433 | |
Sissors | 0:ab0aec01b38e | 434 | return; |
Sissors | 0:ab0aec01b38e | 435 | } |
Sissors | 0:ab0aec01b38e | 436 | |
Sissors | 0:ab0aec01b38e | 437 | void SPI_TFT::fillrect(int x0, int y0, int w, int h, int color) |
Sissors | 0:ab0aec01b38e | 438 | { |
Sissors | 0:ab0aec01b38e | 439 | unsigned long int index=0; |
Sissors | 0:ab0aec01b38e | 440 | if (w < 0) |
Sissors | 0:ab0aec01b38e | 441 | { |
Sissors | 0:ab0aec01b38e | 442 | x0 = x0 + w; |
Sissors | 0:ab0aec01b38e | 443 | w = -w; |
Sissors | 0:ab0aec01b38e | 444 | } |
Sissors | 0:ab0aec01b38e | 445 | if (h < 0) |
Sissors | 0:ab0aec01b38e | 446 | { |
Sissors | 0:ab0aec01b38e | 447 | y0 = y0 + h; |
Sissors | 0:ab0aec01b38e | 448 | h = -h; |
Sissors | 0:ab0aec01b38e | 449 | } |
Sissors | 0:ab0aec01b38e | 450 | mod_orientation(); |
Sissors | 0:ab0aec01b38e | 451 | window(x0,y0,w,h); |
Sissors | 0:ab0aec01b38e | 452 | wr_cmd(0x22); |
Sissors | 0:ab0aec01b38e | 453 | _cs = 0; |
Sissors | 0:ab0aec01b38e | 454 | wr_dat_start(); |
Sissors | 0:ab0aec01b38e | 455 | |
Sissors | 0:ab0aec01b38e | 456 | for( index = 0; index < h * w; index++ ) |
Sissors | 0:ab0aec01b38e | 457 | { |
Sissors | 0:ab0aec01b38e | 458 | wr_dat_only(color); |
Sissors | 0:ab0aec01b38e | 459 | } |
Sissors | 0:ab0aec01b38e | 460 | _cs = 1; |
Sissors | 0:ab0aec01b38e | 461 | return; |
Sissors | 0:ab0aec01b38e | 462 | } |
Sissors | 0:ab0aec01b38e | 463 | |
Sissors | 0:ab0aec01b38e | 464 | void SPI_TFT::draw_ellipse(int xc, int yc, int a, int b, unsigned int color) |
Sissors | 0:ab0aec01b38e | 465 | { /* e(x,y) = b^2*x^2 + a^2*y^2 - a^2*b^2 */ |
Sissors | 0:ab0aec01b38e | 466 | wr_reg(0x03, 0x1030); |
Sissors | 0:ab0aec01b38e | 467 | WindowMax(); |
Sissors | 0:ab0aec01b38e | 468 | int x = 0, y = b; |
Sissors | 0:ab0aec01b38e | 469 | long a2 = (long)a*a, b2 = (long)b*b; |
Sissors | 0:ab0aec01b38e | 470 | long crit1 = -(a2/4 + a%2 + b2); |
Sissors | 0:ab0aec01b38e | 471 | long crit2 = -(b2/4 + b%2 + a2); |
Sissors | 0:ab0aec01b38e | 472 | long crit3 = -(b2/4 + b%2); |
Sissors | 0:ab0aec01b38e | 473 | long t = -a2*y; /* e(x+1/2,y-1/2) - (a^2+b^2)/4 */ |
Sissors | 0:ab0aec01b38e | 474 | long dxt = 2*b2*x, dyt = -2*a2*y; |
Sissors | 0:ab0aec01b38e | 475 | long d2xt = 2*b2, d2yt = 2*a2; |
Sissors | 0:ab0aec01b38e | 476 | |
Sissors | 0:ab0aec01b38e | 477 | while (y>=0 && x<=a) |
Sissors | 0:ab0aec01b38e | 478 | { |
Sissors | 0:ab0aec01b38e | 479 | pixel(xc+x, yc+y, color); |
Sissors | 0:ab0aec01b38e | 480 | if (x!=0 || y!=0) |
Sissors | 0:ab0aec01b38e | 481 | pixel(xc-x, yc-y, color); |
Sissors | 0:ab0aec01b38e | 482 | if (x!=0 && y!=0) |
Sissors | 0:ab0aec01b38e | 483 | { |
Sissors | 0:ab0aec01b38e | 484 | pixel(xc+x, yc-y, color); |
Sissors | 0:ab0aec01b38e | 485 | pixel(xc-x, yc+y, color); |
Sissors | 0:ab0aec01b38e | 486 | } |
Sissors | 0:ab0aec01b38e | 487 | if (t + b2*x <= crit1 || /* e(x+1,y-1/2) <= 0 */ |
Sissors | 0:ab0aec01b38e | 488 | t + a2*y <= crit3) /* e(x+1/2,y) <= 0 */ |
Sissors | 0:ab0aec01b38e | 489 | incx(); |
Sissors | 0:ab0aec01b38e | 490 | else if (t - a2*y > crit2) /* e(x+1/2,y-1) > 0 */ |
Sissors | 0:ab0aec01b38e | 491 | incy(); |
Sissors | 0:ab0aec01b38e | 492 | else |
Sissors | 0:ab0aec01b38e | 493 | { |
Sissors | 0:ab0aec01b38e | 494 | incx(); |
Sissors | 0:ab0aec01b38e | 495 | incy(); |
Sissors | 0:ab0aec01b38e | 496 | } |
Sissors | 0:ab0aec01b38e | 497 | } |
Sissors | 0:ab0aec01b38e | 498 | } |
Sissors | 0:ab0aec01b38e | 499 | |
Sissors | 0:ab0aec01b38e | 500 | void SPI_TFT::fill_ellipse(int xc, int yc, int a, int b, unsigned int color) |
Sissors | 0:ab0aec01b38e | 501 | { /* e(x,y) = b^2*x^2 + a^2*y^2 - a^2*b^2 */ |
Sissors | 0:ab0aec01b38e | 502 | int x = 0, y = b; |
Sissors | 0:ab0aec01b38e | 503 | int rx = x, ry = y; |
Sissors | 0:ab0aec01b38e | 504 | unsigned int width = 1; |
Sissors | 0:ab0aec01b38e | 505 | unsigned int height = 1; |
Sissors | 0:ab0aec01b38e | 506 | long a2 = (long)a*a, b2 = (long)b*b; |
Sissors | 0:ab0aec01b38e | 507 | long crit1 = -(a2/4 + a%2 + b2); |
Sissors | 0:ab0aec01b38e | 508 | long crit2 = -(b2/4 + b%2 + a2); |
Sissors | 0:ab0aec01b38e | 509 | long crit3 = -(b2/4 + b%2); |
Sissors | 0:ab0aec01b38e | 510 | long t = -a2*y; /* e(x+1/2,y-1/2) - (a^2+b^2)/4 */ |
Sissors | 0:ab0aec01b38e | 511 | long dxt = 2*b2*x, dyt = -2*a2*y; |
Sissors | 0:ab0aec01b38e | 512 | long d2xt = 2*b2, d2yt = 2*a2; |
Sissors | 0:ab0aec01b38e | 513 | |
Sissors | 0:ab0aec01b38e | 514 | if (b == 0) |
Sissors | 0:ab0aec01b38e | 515 | { |
Sissors | 0:ab0aec01b38e | 516 | fillrect(xc-a, yc, 2*a+1, 1, color); |
Sissors | 0:ab0aec01b38e | 517 | return; |
Sissors | 0:ab0aec01b38e | 518 | } |
Sissors | 0:ab0aec01b38e | 519 | |
Sissors | 0:ab0aec01b38e | 520 | while (y>=0 && x<=a) |
Sissors | 0:ab0aec01b38e | 521 | { |
Sissors | 0:ab0aec01b38e | 522 | if (t + b2*x <= crit1 || /* e(x+1,y-1/2) <= 0 */ |
Sissors | 0:ab0aec01b38e | 523 | t + a2*y <= crit3) /* e(x+1/2,y) <= 0 */ |
Sissors | 0:ab0aec01b38e | 524 | { |
Sissors | 0:ab0aec01b38e | 525 | if (height == 1) |
Sissors | 0:ab0aec01b38e | 526 | ; /* draw nothing */ |
Sissors | 0:ab0aec01b38e | 527 | else if (ry*2+1 > (height-1)*2) |
Sissors | 0:ab0aec01b38e | 528 | { |
Sissors | 0:ab0aec01b38e | 529 | fillrect(xc-rx, yc-ry, width, height-1, color); |
Sissors | 0:ab0aec01b38e | 530 | fillrect(xc-rx, yc+ry+1, width, 1-height, color); |
Sissors | 0:ab0aec01b38e | 531 | ry -= height-1; |
Sissors | 0:ab0aec01b38e | 532 | height = 1; |
Sissors | 0:ab0aec01b38e | 533 | } |
Sissors | 0:ab0aec01b38e | 534 | else |
Sissors | 0:ab0aec01b38e | 535 | { |
Sissors | 0:ab0aec01b38e | 536 | fillrect(xc-rx, yc-ry, width, ry*2+1, color); |
Sissors | 0:ab0aec01b38e | 537 | ry -= ry; |
Sissors | 0:ab0aec01b38e | 538 | height = 1; |
Sissors | 0:ab0aec01b38e | 539 | } |
Sissors | 0:ab0aec01b38e | 540 | incx(); |
Sissors | 0:ab0aec01b38e | 541 | rx++; |
Sissors | 0:ab0aec01b38e | 542 | width += 2; |
Sissors | 0:ab0aec01b38e | 543 | } |
Sissors | 0:ab0aec01b38e | 544 | else if (t - a2*y > crit2) /* e(x+1/2,y-1) > 0 */ |
Sissors | 0:ab0aec01b38e | 545 | { |
Sissors | 0:ab0aec01b38e | 546 | incy(); |
Sissors | 0:ab0aec01b38e | 547 | height++; |
Sissors | 0:ab0aec01b38e | 548 | } |
Sissors | 0:ab0aec01b38e | 549 | else |
Sissors | 0:ab0aec01b38e | 550 | { |
Sissors | 0:ab0aec01b38e | 551 | if (ry*2+1 > height*2) |
Sissors | 0:ab0aec01b38e | 552 | { |
Sissors | 0:ab0aec01b38e | 553 | fillrect(xc-rx, yc-ry, width, height, color); |
Sissors | 0:ab0aec01b38e | 554 | fillrect(xc-rx, yc+ry+1, width, -height, color); |
Sissors | 0:ab0aec01b38e | 555 | } |
Sissors | 0:ab0aec01b38e | 556 | else |
Sissors | 0:ab0aec01b38e | 557 | { |
Sissors | 0:ab0aec01b38e | 558 | fillrect(xc-rx, yc-ry, width, ry*2+1, color); |
Sissors | 0:ab0aec01b38e | 559 | } |
Sissors | 0:ab0aec01b38e | 560 | incx(); |
Sissors | 0:ab0aec01b38e | 561 | incy(); |
Sissors | 0:ab0aec01b38e | 562 | rx++; |
Sissors | 0:ab0aec01b38e | 563 | width += 2; |
Sissors | 0:ab0aec01b38e | 564 | ry -= height; |
Sissors | 0:ab0aec01b38e | 565 | height = 1; |
Sissors | 0:ab0aec01b38e | 566 | } |
Sissors | 0:ab0aec01b38e | 567 | } |
Sissors | 0:ab0aec01b38e | 568 | |
Sissors | 0:ab0aec01b38e | 569 | if (ry > height) |
Sissors | 0:ab0aec01b38e | 570 | { |
Sissors | 0:ab0aec01b38e | 571 | fillrect(xc-rx, yc-ry, width, height, color); |
Sissors | 0:ab0aec01b38e | 572 | fillrect(xc-rx, yc+ry+1, width, -height, color); |
Sissors | 0:ab0aec01b38e | 573 | } |
Sissors | 0:ab0aec01b38e | 574 | else |
Sissors | 0:ab0aec01b38e | 575 | { |
Sissors | 0:ab0aec01b38e | 576 | fillrect(xc-rx, yc-ry, width, ry*2+1, color); |
Sissors | 0:ab0aec01b38e | 577 | } |
Sissors | 0:ab0aec01b38e | 578 | } |
Sissors | 0:ab0aec01b38e | 579 | |
Sissors | 0:ab0aec01b38e | 580 | |
Sissors | 0:ab0aec01b38e | 581 | void SPI_TFT::locate(int x, int y) |
Sissors | 0:ab0aec01b38e | 582 | { |
Sissors | 0:ab0aec01b38e | 583 | char_x = x; |
Sissors | 0:ab0aec01b38e | 584 | char_y = y; |
Sissors | 0:ab0aec01b38e | 585 | } |
Sissors | 0:ab0aec01b38e | 586 | |
Sissors | 0:ab0aec01b38e | 587 | int SPI_TFT::columns() |
Sissors | 0:ab0aec01b38e | 588 | { |
Sissors | 0:ab0aec01b38e | 589 | return width() / font[1]; |
Sissors | 0:ab0aec01b38e | 590 | } |
Sissors | 0:ab0aec01b38e | 591 | |
Sissors | 0:ab0aec01b38e | 592 | int SPI_TFT::rows() |
Sissors | 0:ab0aec01b38e | 593 | { |
Sissors | 0:ab0aec01b38e | 594 | return height() / font[2]; |
Sissors | 0:ab0aec01b38e | 595 | } |
Sissors | 0:ab0aec01b38e | 596 | |
Sissors | 0:ab0aec01b38e | 597 | int SPI_TFT::_putc(int value) |
Sissors | 0:ab0aec01b38e | 598 | { |
Sissors | 0:ab0aec01b38e | 599 | if (value == '\n') // new line |
Sissors | 0:ab0aec01b38e | 600 | { |
Sissors | 0:ab0aec01b38e | 601 | char_x = 0; |
Sissors | 0:ab0aec01b38e | 602 | char_y = char_y + font[2]; |
Sissors | 0:ab0aec01b38e | 603 | if (char_y >= height() - font[2]) |
Sissors | 0:ab0aec01b38e | 604 | { |
Sissors | 0:ab0aec01b38e | 605 | char_y = 0; |
Sissors | 0:ab0aec01b38e | 606 | } |
Sissors | 0:ab0aec01b38e | 607 | } |
Sissors | 0:ab0aec01b38e | 608 | else |
Sissors | 0:ab0aec01b38e | 609 | { |
Sissors | 0:ab0aec01b38e | 610 | character(char_x, char_y, value); |
Sissors | 0:ab0aec01b38e | 611 | } |
Sissors | 0:ab0aec01b38e | 612 | return value; |
Sissors | 0:ab0aec01b38e | 613 | } |
Sissors | 0:ab0aec01b38e | 614 | |
Sissors | 0:ab0aec01b38e | 615 | void SPI_TFT::character(int x, int y, int c) |
Sissors | 0:ab0aec01b38e | 616 | { |
Sissors | 0:ab0aec01b38e | 617 | unsigned int hor,vert,offset,bpl,j,i,b; |
Sissors | 0:ab0aec01b38e | 618 | unsigned char* bitmap_char; |
Sissors | 0:ab0aec01b38e | 619 | unsigned char z,w; |
Sissors | 0:ab0aec01b38e | 620 | |
Sissors | 0:ab0aec01b38e | 621 | if ((c < 31) || (c > 127)) return; // test char range |
Sissors | 0:ab0aec01b38e | 622 | |
Sissors | 0:ab0aec01b38e | 623 | // read font parameter from start of array |
Sissors | 0:ab0aec01b38e | 624 | offset = font[0]; // bytes / char |
Sissors | 0:ab0aec01b38e | 625 | hor = font[1]; // get hor size of font |
Sissors | 0:ab0aec01b38e | 626 | vert = font[2]; // get vert size of font |
Sissors | 0:ab0aec01b38e | 627 | bpl = font[3]; // bytes per line |
Sissors | 0:ab0aec01b38e | 628 | |
Sissors | 0:ab0aec01b38e | 629 | if (char_x + hor > width()) |
Sissors | 0:ab0aec01b38e | 630 | { |
Sissors | 0:ab0aec01b38e | 631 | char_x = 0; |
Sissors | 0:ab0aec01b38e | 632 | char_y = char_y + vert; |
Sissors | 0:ab0aec01b38e | 633 | if (char_y >= height() - font[2]) |
Sissors | 0:ab0aec01b38e | 634 | { |
Sissors | 0:ab0aec01b38e | 635 | char_y = 0; |
Sissors | 0:ab0aec01b38e | 636 | } |
Sissors | 0:ab0aec01b38e | 637 | } |
Sissors | 0:ab0aec01b38e | 638 | mod_orientation(); |
Sissors | 0:ab0aec01b38e | 639 | |
Sissors | 0:ab0aec01b38e | 640 | bitmap_char = &font[((c -32) * offset) + 4]; // start of char bitmap |
Sissors | 0:ab0aec01b38e | 641 | w = bitmap_char[0]; // width of actual char |
Sissors | 0:ab0aec01b38e | 642 | window(char_x, char_y,w,vert); // char box |
Sissors | 0:ab0aec01b38e | 643 | wr_cmd(0x22); |
Sissors | 0:ab0aec01b38e | 644 | _cs = 0; |
Sissors | 0:ab0aec01b38e | 645 | wr_dat_start(); |
Sissors | 0:ab0aec01b38e | 646 | |
Sissors | 0:ab0aec01b38e | 647 | |
Sissors | 0:ab0aec01b38e | 648 | for (j=0; j<vert; j++) // vert line |
Sissors | 0:ab0aec01b38e | 649 | { |
Sissors | 0:ab0aec01b38e | 650 | for (i=0; i<w; i++) // horz line |
Sissors | 0:ab0aec01b38e | 651 | { |
Sissors | 0:ab0aec01b38e | 652 | z = bitmap_char[bpl * i + ((j & 0xF8) >> 3)+1]; |
Sissors | 0:ab0aec01b38e | 653 | b = 1 << (j & 0x07); |
Sissors | 0:ab0aec01b38e | 654 | if (( z & b ) == 0x00) |
Sissors | 0:ab0aec01b38e | 655 | { |
Sissors | 0:ab0aec01b38e | 656 | wr_dat_only(_background); |
Sissors | 0:ab0aec01b38e | 657 | } |
Sissors | 0:ab0aec01b38e | 658 | else |
Sissors | 0:ab0aec01b38e | 659 | { |
Sissors | 0:ab0aec01b38e | 660 | wr_dat_only(_foreground); |
Sissors | 0:ab0aec01b38e | 661 | } |
Sissors | 0:ab0aec01b38e | 662 | } |
Sissors | 0:ab0aec01b38e | 663 | } |
Sissors | 0:ab0aec01b38e | 664 | _cs = 1; |
Sissors | 0:ab0aec01b38e | 665 | if ((w + 2) < hor) // x offset to next char |
Sissors | 0:ab0aec01b38e | 666 | { |
Sissors | 0:ab0aec01b38e | 667 | char_x += w + 2; |
Sissors | 0:ab0aec01b38e | 668 | } |
Sissors | 0:ab0aec01b38e | 669 | else char_x += hor; |
Sissors | 0:ab0aec01b38e | 670 | } |
Sissors | 0:ab0aec01b38e | 671 | |
Sissors | 0:ab0aec01b38e | 672 | |
Sissors | 0:ab0aec01b38e | 673 | void SPI_TFT::set_font(unsigned char* f) |
Sissors | 0:ab0aec01b38e | 674 | { |
Sissors | 0:ab0aec01b38e | 675 | font = f; |
Sissors | 0:ab0aec01b38e | 676 | } |
Sissors | 0:ab0aec01b38e | 677 | |
Sissors | 0:ab0aec01b38e | 678 | |
Sissors | 0:ab0aec01b38e | 679 | void SPI_TFT::Bitmap(unsigned int x, unsigned int y, unsigned int w, unsigned int h,unsigned char *bitmap) |
Sissors | 0:ab0aec01b38e | 680 | { |
Sissors | 0:ab0aec01b38e | 681 | unsigned int i,j; |
Sissors | 0:ab0aec01b38e | 682 | unsigned short *bitmap_ptr = (unsigned short *)bitmap; |
Sissors | 0:ab0aec01b38e | 683 | mod_orientation(); |
Sissors | 0:ab0aec01b38e | 684 | window(x, y, w, h); |
Sissors | 0:ab0aec01b38e | 685 | wr_cmd(0x22); |
Sissors | 0:ab0aec01b38e | 686 | _cs = 0; |
Sissors | 0:ab0aec01b38e | 687 | wr_dat_start(); |
Sissors | 0:ab0aec01b38e | 688 | _spi.format(16,3); |
Sissors | 0:ab0aec01b38e | 689 | bitmap_ptr += ((h - 1)*w); |
Sissors | 0:ab0aec01b38e | 690 | for (j = 0; j < h; j++) //Lines |
Sissors | 0:ab0aec01b38e | 691 | { |
Sissors | 0:ab0aec01b38e | 692 | for (i = 0; i < w; i++) // copy pixel data to TFT |
Sissors | 0:ab0aec01b38e | 693 | { |
Sissors | 0:ab0aec01b38e | 694 | _spi.write(*bitmap_ptr); // one line |
Sissors | 0:ab0aec01b38e | 695 | bitmap_ptr++; |
Sissors | 0:ab0aec01b38e | 696 | } |
Sissors | 0:ab0aec01b38e | 697 | bitmap_ptr -= 2*w; |
Sissors | 0:ab0aec01b38e | 698 | } |
Sissors | 0:ab0aec01b38e | 699 | _spi.format(8,3); |
Sissors | 0:ab0aec01b38e | 700 | _cs = 1; |
Sissors | 0:ab0aec01b38e | 701 | } |
Sissors | 0:ab0aec01b38e | 702 | |
Sissors | 0:ab0aec01b38e | 703 | int SPI_TFT::BMP_16(unsigned int x, unsigned int y, const char *Name_BMP) |
Sissors | 0:ab0aec01b38e | 704 | { |
Sissors | 0:ab0aec01b38e | 705 | /* // Current code unusable : Rewrite without DMA is needed |
Sissors | 0:ab0aec01b38e | 706 | #define OffsetPixelWidth 18 |
Sissors | 0:ab0aec01b38e | 707 | #define OffsetPixelHeigh 22 |
Sissors | 0:ab0aec01b38e | 708 | #define OffsetFileSize 34 |
Sissors | 0:ab0aec01b38e | 709 | #define OffsetPixData 10 |
Sissors | 0:ab0aec01b38e | 710 | #define OffsetBPP 28 |
Sissors | 0:ab0aec01b38e | 711 | |
Sissors | 0:ab0aec01b38e | 712 | char filename[50]; |
Sissors | 0:ab0aec01b38e | 713 | unsigned char BMP_Header[54]; |
Sissors | 0:ab0aec01b38e | 714 | unsigned short BPP_t; |
Sissors | 0:ab0aec01b38e | 715 | unsigned int PixelWidth,PixelHeigh,start_data; |
Sissors | 0:ab0aec01b38e | 716 | unsigned int i,off; |
Sissors | 0:ab0aec01b38e | 717 | int padd,j; |
Sissors | 0:ab0aec01b38e | 718 | unsigned short *line; |
Sissors | 0:ab0aec01b38e | 719 | |
Sissors | 0:ab0aec01b38e | 720 | // get the filename |
Sissors | 0:ab0aec01b38e | 721 | LocalFileSystem local("local"); |
Sissors | 0:ab0aec01b38e | 722 | sprintf(&filename[0],"/local/"); |
Sissors | 0:ab0aec01b38e | 723 | i=7; |
Sissors | 0:ab0aec01b38e | 724 | while (*Name_BMP!='\0') { |
Sissors | 0:ab0aec01b38e | 725 | filename[i++]=*Name_BMP++; |
Sissors | 0:ab0aec01b38e | 726 | } |
Sissors | 0:ab0aec01b38e | 727 | |
Sissors | 0:ab0aec01b38e | 728 | fprintf(stderr, "filename : %s \n\r",filename); |
Sissors | 0:ab0aec01b38e | 729 | |
Sissors | 0:ab0aec01b38e | 730 | FILE *Image = fopen((const char *)&filename[0], "rb"); // open the bmp file |
Sissors | 0:ab0aec01b38e | 731 | if (!Image) { |
Sissors | 0:ab0aec01b38e | 732 | return(0); // error file not found ! |
Sissors | 0:ab0aec01b38e | 733 | } |
Sissors | 0:ab0aec01b38e | 734 | |
Sissors | 0:ab0aec01b38e | 735 | fread(&BMP_Header[0],1,54,Image); // get the BMP Header |
Sissors | 0:ab0aec01b38e | 736 | |
Sissors | 0:ab0aec01b38e | 737 | if (BMP_Header[0] != 0x42 || BMP_Header[1] != 0x4D) { // check magic byte |
Sissors | 0:ab0aec01b38e | 738 | fclose(Image); |
Sissors | 0:ab0aec01b38e | 739 | return(-1); // error no BMP file |
Sissors | 0:ab0aec01b38e | 740 | } |
Sissors | 0:ab0aec01b38e | 741 | |
Sissors | 0:ab0aec01b38e | 742 | BPP_t = BMP_Header[OffsetBPP] + (BMP_Header[OffsetBPP + 1] << 8); |
Sissors | 0:ab0aec01b38e | 743 | if (BPP_t != 0x0010) { |
Sissors | 0:ab0aec01b38e | 744 | fclose(Image); |
Sissors | 0:ab0aec01b38e | 745 | return(-2); // error no 16 bit BMP |
Sissors | 0:ab0aec01b38e | 746 | } |
Sissors | 0:ab0aec01b38e | 747 | |
Sissors | 0:ab0aec01b38e | 748 | PixelHeigh = BMP_Header[OffsetPixelHeigh] + (BMP_Header[OffsetPixelHeigh + 1] << 8) + (BMP_Header[OffsetPixelHeigh + 2] << 16) + (BMP_Header[OffsetPixelHeigh + 3] << 24); |
Sissors | 0:ab0aec01b38e | 749 | PixelWidth = BMP_Header[OffsetPixelWidth] + (BMP_Header[OffsetPixelWidth + 1] << 8) + (BMP_Header[OffsetPixelWidth + 2] << 16) + (BMP_Header[OffsetPixelWidth + 3] << 24); |
Sissors | 0:ab0aec01b38e | 750 | if (PixelHeigh > height() + y || PixelWidth > width() + x) { |
Sissors | 0:ab0aec01b38e | 751 | fclose(Image); |
Sissors | 0:ab0aec01b38e | 752 | return(-3); // to big |
Sissors | 0:ab0aec01b38e | 753 | } |
Sissors | 0:ab0aec01b38e | 754 | |
Sissors | 0:ab0aec01b38e | 755 | start_data = BMP_Header[OffsetPixData] + (BMP_Header[OffsetPixData + 1] << 8) + (BMP_Header[OffsetPixData + 2] << 16) + (BMP_Header[OffsetPixData + 3] << 24); |
Sissors | 0:ab0aec01b38e | 756 | |
Sissors | 0:ab0aec01b38e | 757 | line = (unsigned short *) malloc (2 * PixelWidth); // we need a buffer for a line |
Sissors | 0:ab0aec01b38e | 758 | if (line == NULL) { |
Sissors | 0:ab0aec01b38e | 759 | return(-4); // error no memory |
Sissors | 0:ab0aec01b38e | 760 | } |
Sissors | 0:ab0aec01b38e | 761 | |
Sissors | 0:ab0aec01b38e | 762 | // the bmp lines are padded to multiple of 4 bytes |
Sissors | 0:ab0aec01b38e | 763 | padd = -1; |
Sissors | 0:ab0aec01b38e | 764 | do { |
Sissors | 0:ab0aec01b38e | 765 | padd ++; |
Sissors | 0:ab0aec01b38e | 766 | } while ((PixelWidth * 2 + padd)%4 != 0); |
Sissors | 0:ab0aec01b38e | 767 | |
Sissors | 0:ab0aec01b38e | 768 | |
Sissors | 0:ab0aec01b38e | 769 | //fseek(Image, 70 ,SEEK_SET); |
Sissors | 0:ab0aec01b38e | 770 | window(x, y,PixelWidth ,PixelHeigh); |
Sissors | 0:ab0aec01b38e | 771 | wr_cmd(0x22); |
Sissors | 0:ab0aec01b38e | 772 | _cs = 0; |
Sissors | 0:ab0aec01b38e | 773 | |
Sissors | 0:ab0aec01b38e | 774 | if (spi_port == 0) { // TFT on SSP0 |
Sissors | 0:ab0aec01b38e | 775 | LPC_GPDMACH0->DMACCDestAddr = (uint32_t)&LPC_SSP0->DR; // we send to SSP0 |
Sissors | 0:ab0aec01b38e | 776 | // LPC_SSP0->CR0 &= ~(0x08UL); // set to 8 bit |
Sissors | 0:ab0aec01b38e | 777 | LPC_SSP0->DR = 0x72; // start Data |
Sissors | 0:ab0aec01b38e | 778 | LPC_SSP0->CR0 |= 0x08UL; // set to 16 bit |
Sissors | 0:ab0aec01b38e | 779 | // Enable SSP0 for DMA. |
Sissors | 0:ab0aec01b38e | 780 | LPC_SSP0->DMACR = 0x2; |
Sissors | 0:ab0aec01b38e | 781 | |
Sissors | 0:ab0aec01b38e | 782 | } else { |
Sissors | 0:ab0aec01b38e | 783 | LPC_GPDMACH0->DMACCDestAddr = (uint32_t)&LPC_SSP1->DR; // we send to SSP1 |
Sissors | 0:ab0aec01b38e | 784 | // LPC_SSP1->CR0 &= ~(0x08UL); // set to 8 bit |
Sissors | 0:ab0aec01b38e | 785 | LPC_SSP1->DR = 0x72; // start Data |
Sissors | 0:ab0aec01b38e | 786 | LPC_SSP1->CR0 |= 0x08UL; // set to 16 bit |
Sissors | 0:ab0aec01b38e | 787 | // Enable SSP1 for DMA. |
Sissors | 0:ab0aec01b38e | 788 | LPC_SSP1->DMACR = 0x2; |
Sissors | 0:ab0aec01b38e | 789 | } |
Sissors | 0:ab0aec01b38e | 790 | for (j = PixelHeigh - 1; j >= 0; j--) { //Lines bottom up |
Sissors | 0:ab0aec01b38e | 791 | off = j * (PixelWidth * 2 + padd) + start_data; // start of line |
Sissors | 0:ab0aec01b38e | 792 | fseek(Image, off ,SEEK_SET); |
Sissors | 0:ab0aec01b38e | 793 | fread(line,1,PixelWidth * 2,Image); // read a line - slow ! |
Sissors | 0:ab0aec01b38e | 794 | |
Sissors | 0:ab0aec01b38e | 795 | LPC_GPDMA->DMACIntTCClear = 0x1; |
Sissors | 0:ab0aec01b38e | 796 | LPC_GPDMA->DMACIntErrClr = 0x1; |
Sissors | 0:ab0aec01b38e | 797 | LPC_GPDMACH0->DMACCSrcAddr = (uint32_t)line; |
Sissors | 0:ab0aec01b38e | 798 | LPC_GPDMACH0->DMACCControl = PixelWidth | (0UL << 18) | (0UL << 21) | (1UL << 31) | DMA_CHANNEL_SRC_INC ; // 8 bit transfer , address increment, interrupt |
Sissors | 0:ab0aec01b38e | 799 | LPC_GPDMACH0->DMACCConfig = DMA_CHANNEL_ENABLE | DMA_TRANSFER_TYPE_M2P | (spi_port ? DMA_DEST_SSP1_TX : DMA_DEST_SSP0_TX); |
Sissors | 0:ab0aec01b38e | 800 | LPC_GPDMA->DMACSoftSReq = 0x1; |
Sissors | 0:ab0aec01b38e | 801 | do { |
Sissors | 0:ab0aec01b38e | 802 | } while ((LPC_GPDMA->DMACRawIntTCStat & 0x01) == 0); // DMA is running |
Sissors | 0:ab0aec01b38e | 803 | |
Sissors | 0:ab0aec01b38e | 804 | } |
Sissors | 0:ab0aec01b38e | 805 | |
Sissors | 0:ab0aec01b38e | 806 | if (spi_port == 0) { // TFT on SSP0 |
Sissors | 0:ab0aec01b38e | 807 | do { |
Sissors | 0:ab0aec01b38e | 808 | } while ((LPC_SSP0->SR & 0x10) == 0x10); // SPI FIFO not empty |
Sissors | 0:ab0aec01b38e | 809 | LPC_SSP0->CR0 &= ~(0x08UL); // set to 8 bit |
Sissors | 0:ab0aec01b38e | 810 | } else { |
Sissors | 0:ab0aec01b38e | 811 | do { |
Sissors | 0:ab0aec01b38e | 812 | } while ((LPC_SSP1->SR & 0x10) == 0x10); // SPI FIFO not empty |
Sissors | 0:ab0aec01b38e | 813 | LPC_SSP1->CR0 |= 0x08UL; // set to 16 bit |
Sissors | 0:ab0aec01b38e | 814 | } |
Sissors | 0:ab0aec01b38e | 815 | _cs = 1; |
Sissors | 0:ab0aec01b38e | 816 | free (line); |
Sissors | 0:ab0aec01b38e | 817 | fclose(Image); |
Sissors | 0:ab0aec01b38e | 818 | WindowMax(); |
Sissors | 0:ab0aec01b38e | 819 | */ |
Sissors | 0:ab0aec01b38e | 820 | return(1); |
Sissors | 0:ab0aec01b38e | 821 | } |