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

Committer:
dreschpe
Date:
Tue Jul 12 08:27:34 2011 +0000
Revision:
0:cccc5726bdf3
Child:
1:aa3356b16080
0.8

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dreschpe 0:cccc5726bdf3 1 /* mbed library for 240*320 pixel display TFT based on HX8347D LCD Controller
dreschpe 0:cccc5726bdf3 2 * Copyright (c) 2011 Peter Drescher - DC2PD
dreschpe 0:cccc5726bdf3 3 *
dreschpe 0:cccc5726bdf3 4 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
dreschpe 0:cccc5726bdf3 5 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
dreschpe 0:cccc5726bdf3 6 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
dreschpe 0:cccc5726bdf3 7 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
dreschpe 0:cccc5726bdf3 8 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
dreschpe 0:cccc5726bdf3 9 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
dreschpe 0:cccc5726bdf3 10 * THE SOFTWARE.
dreschpe 0:cccc5726bdf3 11 */
dreschpe 0:cccc5726bdf3 12
dreschpe 0:cccc5726bdf3 13 #include "SPI_TFT.h"
dreschpe 0:cccc5726bdf3 14 #include "mbed.h"
dreschpe 0:cccc5726bdf3 15
dreschpe 0:cccc5726bdf3 16 /** Display control class, based on GraphicsDisplay and TextDisplay
dreschpe 0:cccc5726bdf3 17 *
dreschpe 0:cccc5726bdf3 18 * Example:
dreschpe 0:cccc5726bdf3 19 * @code
dreschpe 0:cccc5726bdf3 20 * #include "stdio.h"
dreschpe 0:cccc5726bdf3 21 * #include "mbed.h"
dreschpe 0:cccc5726bdf3 22 * #include "SPI_TFT.h"
dreschpe 0:cccc5726bdf3 23 * #include "string"
dreschpe 0:cccc5726bdf3 24 * #include "Arial12x12.h"
dreschpe 0:cccc5726bdf3 25 * #include "Arial24x23.h"
dreschpe 0:cccc5726bdf3 26 *
dreschpe 0:cccc5726bdf3 27 *
dreschpe 0:cccc5726bdf3 28 *
dreschpe 0:cccc5726bdf3 29 * // the TFT is connected to SPI pin 5-7
dreschpe 0:cccc5726bdf3 30 * SPI_TFT TFT(p5, p6, p7, p8, p15,"TFT"); // mosi, miso, sclk, cs, reset
dreschpe 0:cccc5726bdf3 31 *
dreschpe 0:cccc5726bdf3 32 * int main() {
dreschpe 0:cccc5726bdf3 33 * TFT.claim(stdout); // send stdout to the TFT display
dreschpe 0:cccc5726bdf3 34 * //TFT.claim(stderr); // send stderr to the TFT display
dreschpe 0:cccc5726bdf3 35 *
dreschpe 0:cccc5726bdf3 36 * TFT.background(Black); // set background to black
dreschpe 0:cccc5726bdf3 37 * TFT.foreground(White); // set chars to white
dreschpe 0:cccc5726bdf3 38 * TFT.cls(); // clear the screen
dreschpe 0:cccc5726bdf3 39 * TFT.set_font((unsigned char*) Arial12x12); // select the font
dreschpe 0:cccc5726bdf3 40 *
dreschpe 0:cccc5726bdf3 41 * TFT.set_orientation(0);
dreschpe 0:cccc5726bdf3 42 * TFT.locate(0,0);
dreschpe 0:cccc5726bdf3 43 * printf(" Hello Mbed 0");
dreschpe 0:cccc5726bdf3 44 * TFT.set_font((unsigned char*) Arial24x23); // select font 2
dreschpe 0:cccc5726bdf3 45 * TFT.locate(2,5);
dreschpe 0:cccc5726bdf3 46 * TFT.printf("Bigger Font");
dreschpe 0:cccc5726bdf3 47 * }
dreschpe 0:cccc5726bdf3 48 * @endcode
dreschpe 0:cccc5726bdf3 49 */
dreschpe 0:cccc5726bdf3 50
dreschpe 0:cccc5726bdf3 51 #define BPP 16 /* Bits per pixel
dreschpe 0:cccc5726bdf3 52
dreschpe 0:cccc5726bdf3 53
dreschpe 0:cccc5726bdf3 54 /** Create a SPI_TFT object connected to SPI and two pins
dreschpe 0:cccc5726bdf3 55 *
dreschpe 0:cccc5726bdf3 56 * @param mosi,miso,sclk SPI
dreschpe 0:cccc5726bdf3 57 * @param cs pin connected to CS of display
dreschpe 0:cccc5726bdf3 58 * @param reset pin connected to RESET of display
dreschpe 0:cccc5726bdf3 59 *
dreschpe 0:cccc5726bdf3 60 */
dreschpe 0:cccc5726bdf3 61 SPI_TFT::SPI_TFT(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset, const char *name)
dreschpe 0:cccc5726bdf3 62 : _spi(mosi, miso, sclk), _cs(cs), _reset(reset),GraphicsDisplay(name) {
dreschpe 0:cccc5726bdf3 63 tft_reset();
dreschpe 0:cccc5726bdf3 64 orientation = 0;
dreschpe 0:cccc5726bdf3 65 char_x = 0;
dreschpe 0:cccc5726bdf3 66 }
dreschpe 0:cccc5726bdf3 67
dreschpe 0:cccc5726bdf3 68
dreschpe 0:cccc5726bdf3 69 /** Get the width of the screen in pixel
dreschpe 0:cccc5726bdf3 70 *
dreschpe 0:cccc5726bdf3 71 * @param
dreschpe 0:cccc5726bdf3 72 * @returns width of screen in pixel
dreschpe 0:cccc5726bdf3 73 *
dreschpe 0:cccc5726bdf3 74 */
dreschpe 0:cccc5726bdf3 75
dreschpe 0:cccc5726bdf3 76 int SPI_TFT::width() {
dreschpe 0:cccc5726bdf3 77 if(orientation == 0 || orientation == 2) return 240;
dreschpe 0:cccc5726bdf3 78 else return 320;
dreschpe 0:cccc5726bdf3 79 }
dreschpe 0:cccc5726bdf3 80
dreschpe 0:cccc5726bdf3 81
dreschpe 0:cccc5726bdf3 82 /** Get the height of the screen in pixel
dreschpe 0:cccc5726bdf3 83 *
dreschpe 0:cccc5726bdf3 84 * @param
dreschpe 0:cccc5726bdf3 85 * @returns height of screen in pixel
dreschpe 0:cccc5726bdf3 86 *
dreschpe 0:cccc5726bdf3 87 */
dreschpe 0:cccc5726bdf3 88
dreschpe 0:cccc5726bdf3 89 int SPI_TFT::height() {
dreschpe 0:cccc5726bdf3 90 if(orientation == 0 || orientation == 2) return 320;
dreschpe 0:cccc5726bdf3 91 else return 240;
dreschpe 0:cccc5726bdf3 92 }
dreschpe 0:cccc5726bdf3 93
dreschpe 0:cccc5726bdf3 94 /** Set the orientation of the screen
dreschpe 0:cccc5726bdf3 95 * x,y: 0,0 is always top left
dreschpe 0:cccc5726bdf3 96 *
dreschpe 0:cccc5726bdf3 97 * @param o direction to use the screen 1-4
dreschpe 0:cccc5726bdf3 98 *
dreschpe 0:cccc5726bdf3 99 */
dreschpe 0:cccc5726bdf3 100
dreschpe 0:cccc5726bdf3 101 void SPI_TFT::set_orientation(unsigned int o){
dreschpe 0:cccc5726bdf3 102 orientation = o;
dreschpe 0:cccc5726bdf3 103 switch(orientation){
dreschpe 0:cccc5726bdf3 104 case 0:
dreschpe 0:cccc5726bdf3 105 wr_reg(0x16, 0x0008);
dreschpe 0:cccc5726bdf3 106 break;
dreschpe 0:cccc5726bdf3 107 case 1:
dreschpe 0:cccc5726bdf3 108 wr_reg(0x16, 0x0068);
dreschpe 0:cccc5726bdf3 109 break;
dreschpe 0:cccc5726bdf3 110 case 2:
dreschpe 0:cccc5726bdf3 111 wr_reg(0x16, 0x00C8);
dreschpe 0:cccc5726bdf3 112 break;
dreschpe 0:cccc5726bdf3 113 case 3:
dreschpe 0:cccc5726bdf3 114 wr_reg(0x16, 0x00A8);
dreschpe 0:cccc5726bdf3 115 break;
dreschpe 0:cccc5726bdf3 116 }
dreschpe 0:cccc5726bdf3 117 }
dreschpe 0:cccc5726bdf3 118
dreschpe 0:cccc5726bdf3 119 /** Write a command the LCD controller
dreschpe 0:cccc5726bdf3 120 *
dreschpe 0:cccc5726bdf3 121 * @param cmd: command to be written
dreschpe 0:cccc5726bdf3 122 *
dreschpe 0:cccc5726bdf3 123 */
dreschpe 0:cccc5726bdf3 124
dreschpe 0:cccc5726bdf3 125 void SPI_TFT::wr_cmd(int cmd) {
dreschpe 0:cccc5726bdf3 126 _cs = 0;
dreschpe 0:cccc5726bdf3 127 _spi.write(SPI_START | SPI_WR | SPI_INDEX); /* Write : RS = 0, RW = 0 */
dreschpe 0:cccc5726bdf3 128 _spi.write(cmd);
dreschpe 0:cccc5726bdf3 129 _cs = 1;
dreschpe 0:cccc5726bdf3 130 }
dreschpe 0:cccc5726bdf3 131
dreschpe 0:cccc5726bdf3 132 /** Write data to the LCD controller
dreschpe 0:cccc5726bdf3 133 *
dreschpe 0:cccc5726bdf3 134 * @param dat data written to LCD controller
dreschpe 0:cccc5726bdf3 135 *
dreschpe 0:cccc5726bdf3 136 */
dreschpe 0:cccc5726bdf3 137
dreschpe 0:cccc5726bdf3 138 void SPI_TFT::wr_dat(int dat) {
dreschpe 0:cccc5726bdf3 139 _cs = 0;
dreschpe 0:cccc5726bdf3 140 _spi.write(SPI_START | SPI_WR | SPI_DATA); // Write : RS = 1, RW = 0
dreschpe 0:cccc5726bdf3 141 _spi.format(16,3); // switch to 16 bit Mode 3
dreschpe 0:cccc5726bdf3 142 _spi.write(dat); // Write D0..D15
dreschpe 0:cccc5726bdf3 143 _spi.format(8,3); // 8 bit Mode 3
dreschpe 0:cccc5726bdf3 144 _cs = 1;
dreschpe 0:cccc5726bdf3 145 }
dreschpe 0:cccc5726bdf3 146
dreschpe 0:cccc5726bdf3 147 /** Start data sequence to the LCD controller
dreschpe 0:cccc5726bdf3 148 *
dreschpe 0:cccc5726bdf3 149 */
dreschpe 0:cccc5726bdf3 150
dreschpe 0:cccc5726bdf3 151 void SPI_TFT::wr_dat_start(void){
dreschpe 0:cccc5726bdf3 152 _cs = 0;
dreschpe 0:cccc5726bdf3 153 _spi.write(SPI_START | SPI_WR | SPI_DATA); /* Write : RS = 1, RW = 0 */
dreschpe 0:cccc5726bdf3 154 }
dreschpe 0:cccc5726bdf3 155
dreschpe 0:cccc5726bdf3 156 /** Stop of data writing to the LCD controller
dreschpe 0:cccc5726bdf3 157 *
dreschpe 0:cccc5726bdf3 158 */
dreschpe 0:cccc5726bdf3 159
dreschpe 0:cccc5726bdf3 160 void SPI_TFT::wr_dat_stop (void){
dreschpe 0:cccc5726bdf3 161 _cs = 1;
dreschpe 0:cccc5726bdf3 162 }
dreschpe 0:cccc5726bdf3 163
dreschpe 0:cccc5726bdf3 164 /** write data to the LCD controller
dreschpe 0:cccc5726bdf3 165 *
dreschpe 0:cccc5726bdf3 166 * @param data to be written
dreschpe 0:cccc5726bdf3 167 * *
dreschpe 0:cccc5726bdf3 168 */
dreschpe 0:cccc5726bdf3 169
dreschpe 0:cccc5726bdf3 170 void SPI_TFT::wr_dat_only (unsigned short dat) {
dreschpe 0:cccc5726bdf3 171
dreschpe 0:cccc5726bdf3 172 _spi.format(16,3); // switch to 16 bit Mode 3
dreschpe 0:cccc5726bdf3 173 _spi.write(dat); // Write D0..D15
dreschpe 0:cccc5726bdf3 174 _spi.format(8,3); // 8 bit Mode 3
dreschpe 0:cccc5726bdf3 175 }
dreschpe 0:cccc5726bdf3 176
dreschpe 0:cccc5726bdf3 177 /** Read data from the LCD controller
dreschpe 0:cccc5726bdf3 178 *
dreschpe 0:cccc5726bdf3 179 * @returns data from LCD controller
dreschpe 0:cccc5726bdf3 180 *
dreschpe 0:cccc5726bdf3 181 */
dreschpe 0:cccc5726bdf3 182
dreschpe 0:cccc5726bdf3 183 unsigned short SPI_TFT::rd_dat (void) {
dreschpe 0:cccc5726bdf3 184 unsigned short val = 0;
dreschpe 0:cccc5726bdf3 185
dreschpe 0:cccc5726bdf3 186 _cs = 0;
dreschpe 0:cccc5726bdf3 187 _spi.write(SPI_START | SPI_RD | SPI_DATA); /* Read: RS = 1, RW = 1 */
dreschpe 0:cccc5726bdf3 188 _spi.write(0); /* Dummy read 1 */
dreschpe 0:cccc5726bdf3 189 val = _spi.write(0); /* Read D8..D15 */
dreschpe 0:cccc5726bdf3 190 val <<= 8;
dreschpe 0:cccc5726bdf3 191 val |= _spi.write(0); /* Read D0..D7 */
dreschpe 0:cccc5726bdf3 192 _cs = 1;
dreschpe 0:cccc5726bdf3 193 return (val);
dreschpe 0:cccc5726bdf3 194 }
dreschpe 0:cccc5726bdf3 195
dreschpe 0:cccc5726bdf3 196 /** Write a value to the to a LCD register
dreschpe 0:cccc5726bdf3 197 *
dreschpe 0:cccc5726bdf3 198 * @param reg register to be written
dreschpe 0:cccc5726bdf3 199 * @param val data to be written
dreschpe 0:cccc5726bdf3 200 */
dreschpe 0:cccc5726bdf3 201
dreschpe 0:cccc5726bdf3 202 void SPI_TFT::wr_reg (unsigned char reg, unsigned short val) {
dreschpe 0:cccc5726bdf3 203
dreschpe 0:cccc5726bdf3 204 wr_cmd(reg);
dreschpe 0:cccc5726bdf3 205 wr_dat(val);
dreschpe 0:cccc5726bdf3 206 }
dreschpe 0:cccc5726bdf3 207
dreschpe 0:cccc5726bdf3 208 /** Read a LCD register
dreschpe 0:cccc5726bdf3 209 *
dreschpe 0:cccc5726bdf3 210 * @param reg register to be read
dreschpe 0:cccc5726bdf3 211 * @returns value of the register
dreschpe 0:cccc5726bdf3 212 */
dreschpe 0:cccc5726bdf3 213
dreschpe 0:cccc5726bdf3 214 unsigned short SPI_TFT::rd_reg (unsigned char reg) {
dreschpe 0:cccc5726bdf3 215
dreschpe 0:cccc5726bdf3 216 wr_cmd(reg);
dreschpe 0:cccc5726bdf3 217 return(rd_dat());
dreschpe 0:cccc5726bdf3 218 }
dreschpe 0:cccc5726bdf3 219
dreschpe 0:cccc5726bdf3 220 /** Init the HX8347D controller
dreschpe 0:cccc5726bdf3 221 *
dreschpe 0:cccc5726bdf3 222 */
dreschpe 0:cccc5726bdf3 223
dreschpe 0:cccc5726bdf3 224 void SPI_TFT::tft_reset() {
dreschpe 0:cccc5726bdf3 225 static unsigned short driverCode;
dreschpe 0:cccc5726bdf3 226 _spi.format(8,3); // 8 bit spi mode 3
dreschpe 0:cccc5726bdf3 227 _spi.frequency(48000000); // 48Mhz SPI clock
dreschpe 0:cccc5726bdf3 228 _reset = 0; // reset
dreschpe 0:cccc5726bdf3 229 _cs = 1;
dreschpe 0:cccc5726bdf3 230 wait_us(50);
dreschpe 0:cccc5726bdf3 231 _reset = 1; // end reset
dreschpe 0:cccc5726bdf3 232 wait_ms(5);
dreschpe 0:cccc5726bdf3 233
dreschpe 0:cccc5726bdf3 234 driverCode = rd_reg(0x00); // read controller ID
dreschpe 0:cccc5726bdf3 235 //printf("Disp_ID = %x",driverCode);
dreschpe 0:cccc5726bdf3 236
dreschpe 0:cccc5726bdf3 237 /* Start Initial Sequence ----------------------------------------------------*/
dreschpe 0:cccc5726bdf3 238 wr_reg(0xEA, 0x0000); /* Reset Power Control 1 */
dreschpe 0:cccc5726bdf3 239 wr_reg(0xEB, 0x0020); /* Power Control 2 */
dreschpe 0:cccc5726bdf3 240 wr_reg(0xEC, 0x000C); /* Power Control 3 */
dreschpe 0:cccc5726bdf3 241 wr_reg(0xED, 0x00C4); /* Power Control 4 */
dreschpe 0:cccc5726bdf3 242 wr_reg(0xE8, 0x0040); /* Source OPON_N */
dreschpe 0:cccc5726bdf3 243 wr_reg(0xE9, 0x0038); /* Source OPON_I */
dreschpe 0:cccc5726bdf3 244 wr_reg(0xF1, 0x0001); /* */
dreschpe 0:cccc5726bdf3 245 wr_reg(0xF2, 0x0010); /* */
dreschpe 0:cccc5726bdf3 246 wr_reg(0x27, 0x00A3); /* Display Control 2 */
dreschpe 0:cccc5726bdf3 247
dreschpe 0:cccc5726bdf3 248 /* Power On sequence ---------------------------------------------------------*/
dreschpe 0:cccc5726bdf3 249 wr_reg(0x1B, 0x001B); /* Power Control 2 */
dreschpe 0:cccc5726bdf3 250 wr_reg(0x1A, 0x0001); /* Power Control 1 */
dreschpe 0:cccc5726bdf3 251 wr_reg(0x24, 0x002F); /* Vcom Control 2 */
dreschpe 0:cccc5726bdf3 252 wr_reg(0x25, 0x0057); /* Vcom Control 3 */
dreschpe 0:cccc5726bdf3 253 wr_reg(0x23, 0x008D); /* Vcom Control 1 */
dreschpe 0:cccc5726bdf3 254
dreschpe 0:cccc5726bdf3 255 /* Gamma settings -----------------------------------------------------------*/
dreschpe 0:cccc5726bdf3 256 wr_reg(0x40,0x00); //
dreschpe 0:cccc5726bdf3 257 wr_reg(0x41,0x00); //
dreschpe 0:cccc5726bdf3 258 wr_reg(0x42,0x01); //
dreschpe 0:cccc5726bdf3 259 wr_reg(0x43,0x13); //
dreschpe 0:cccc5726bdf3 260 wr_reg(0x44,0x10); //
dreschpe 0:cccc5726bdf3 261 wr_reg(0x45,0x26); //
dreschpe 0:cccc5726bdf3 262 wr_reg(0x46,0x08); //
dreschpe 0:cccc5726bdf3 263 wr_reg(0x47,0x51); //
dreschpe 0:cccc5726bdf3 264 wr_reg(0x48,0x02); //
dreschpe 0:cccc5726bdf3 265 wr_reg(0x49,0x12); //
dreschpe 0:cccc5726bdf3 266 wr_reg(0x4A,0x18); //
dreschpe 0:cccc5726bdf3 267 wr_reg(0x4B,0x19); //
dreschpe 0:cccc5726bdf3 268 wr_reg(0x4C,0x14); //
dreschpe 0:cccc5726bdf3 269 wr_reg(0x50,0x19); //
dreschpe 0:cccc5726bdf3 270 wr_reg(0x51,0x2F); //
dreschpe 0:cccc5726bdf3 271 wr_reg(0x52,0x2C); //
dreschpe 0:cccc5726bdf3 272 wr_reg(0x53,0x3E); //
dreschpe 0:cccc5726bdf3 273 wr_reg(0x54,0x3F); //
dreschpe 0:cccc5726bdf3 274 wr_reg(0x55,0x3F); //
dreschpe 0:cccc5726bdf3 275 wr_reg(0x56,0x2E); //
dreschpe 0:cccc5726bdf3 276 wr_reg(0x57,0x77); //
dreschpe 0:cccc5726bdf3 277 wr_reg(0x58,0x0B); //
dreschpe 0:cccc5726bdf3 278 wr_reg(0x59,0x06); //
dreschpe 0:cccc5726bdf3 279 wr_reg(0x5A,0x07); //
dreschpe 0:cccc5726bdf3 280 wr_reg(0x5B,0x0D); //
dreschpe 0:cccc5726bdf3 281 wr_reg(0x5C,0x1D); //
dreschpe 0:cccc5726bdf3 282 wr_reg(0x5D,0xCC); //
dreschpe 0:cccc5726bdf3 283
dreschpe 0:cccc5726bdf3 284 /* Power + Osc ---------------------------------------------------------------*/
dreschpe 0:cccc5726bdf3 285 wr_reg(0x18, 0x0036); /* OSC Control 1 */
dreschpe 0:cccc5726bdf3 286 wr_reg(0x19, 0x0001); /* OSC Control 2 */
dreschpe 0:cccc5726bdf3 287 wr_reg(0x01, 0x0000); /* Display Mode Control */
dreschpe 0:cccc5726bdf3 288 wr_reg(0x1F, 0x0088); /* Power Control 6 */
dreschpe 0:cccc5726bdf3 289 wait_ms(5); /* Delay 5 ms */
dreschpe 0:cccc5726bdf3 290 wr_reg(0x1F, 0x0080); /* Power Control 6 */
dreschpe 0:cccc5726bdf3 291 wait_ms(5); /* Delay 5 ms */
dreschpe 0:cccc5726bdf3 292 wr_reg(0x1F, 0x0090); /* Power Control 6 */
dreschpe 0:cccc5726bdf3 293 wait_ms(5); /* Delay 5 ms */
dreschpe 0:cccc5726bdf3 294 wr_reg(0x1F, 0x00D0); /* Power Control 6 */
dreschpe 0:cccc5726bdf3 295 wait_ms(5); /* Delay 5 ms */
dreschpe 0:cccc5726bdf3 296
dreschpe 0:cccc5726bdf3 297 wr_reg(0x17, 0x0005); /* Colmod 16Bit/Pixel */
dreschpe 0:cccc5726bdf3 298
dreschpe 0:cccc5726bdf3 299 wr_reg(0x36, 0x0000); /* Panel Characteristic */
dreschpe 0:cccc5726bdf3 300 wr_reg(0x28, 0x0038); /* Display Control 3 */
dreschpe 0:cccc5726bdf3 301 wait_ms(40);
dreschpe 0:cccc5726bdf3 302 wr_reg(0x28, 0x003C); /* Display Control 3 */
dreschpe 0:cccc5726bdf3 303 switch(orientation){
dreschpe 0:cccc5726bdf3 304 case 0:
dreschpe 0:cccc5726bdf3 305 wr_reg(0x16, 0x0008);
dreschpe 0:cccc5726bdf3 306 break;
dreschpe 0:cccc5726bdf3 307 case 1:
dreschpe 0:cccc5726bdf3 308 wr_reg(0x16, 0x0068);
dreschpe 0:cccc5726bdf3 309 break;
dreschpe 0:cccc5726bdf3 310 case 2:
dreschpe 0:cccc5726bdf3 311 wr_reg(0x16, 0x00C8);
dreschpe 0:cccc5726bdf3 312 break;
dreschpe 0:cccc5726bdf3 313 case 3:
dreschpe 0:cccc5726bdf3 314 wr_reg(0x16, 0x00A8);
dreschpe 0:cccc5726bdf3 315 break;
dreschpe 0:cccc5726bdf3 316 }
dreschpe 0:cccc5726bdf3 317
dreschpe 0:cccc5726bdf3 318 WindowMax ();
dreschpe 0:cccc5726bdf3 319 }
dreschpe 0:cccc5726bdf3 320
dreschpe 0:cccc5726bdf3 321
dreschpe 0:cccc5726bdf3 322 /** Draw a pixel at x,y with color
dreschpe 0:cccc5726bdf3 323 *
dreschpe 0:cccc5726bdf3 324 * @param x horizontal position
dreschpe 0:cccc5726bdf3 325 * @param y vertical position
dreschpe 0:cccc5726bdf3 326 * @param color 16 bit pixel color
dreschpe 0:cccc5726bdf3 327 **/
dreschpe 0:cccc5726bdf3 328
dreschpe 0:cccc5726bdf3 329 void SPI_TFT::pixel(int x, int y, int color) {
dreschpe 0:cccc5726bdf3 330 wr_reg(0x03, (x >> 0));
dreschpe 0:cccc5726bdf3 331 wr_reg(0x02, (x >> 8));
dreschpe 0:cccc5726bdf3 332 wr_reg(0x07, (y >> 0));
dreschpe 0:cccc5726bdf3 333 wr_reg(0x06, (y >> 8));
dreschpe 0:cccc5726bdf3 334 wr_cmd(0x22);
dreschpe 0:cccc5726bdf3 335 wr_dat(color);
dreschpe 0:cccc5726bdf3 336 }
dreschpe 0:cccc5726bdf3 337
dreschpe 0:cccc5726bdf3 338
dreschpe 0:cccc5726bdf3 339 /** Set draw window region
dreschpe 0:cccc5726bdf3 340 *
dreschpe 0:cccc5726bdf3 341 * @param x horizontal position
dreschpe 0:cccc5726bdf3 342 * @param y vertical position
dreschpe 0:cccc5726bdf3 343 * @param w window width in pixel
dreschpe 0:cccc5726bdf3 344 * @param h window height in pixels
dreschpe 0:cccc5726bdf3 345 **/
dreschpe 0:cccc5726bdf3 346
dreschpe 0:cccc5726bdf3 347 void SPI_TFT::window (unsigned int x, unsigned int y, unsigned int w, unsigned int h) {
dreschpe 0:cccc5726bdf3 348 wr_reg(0x03, (x >> 0));
dreschpe 0:cccc5726bdf3 349 wr_reg(0x02, (x >> 8));
dreschpe 0:cccc5726bdf3 350 wr_reg(0x05, (x+w-1 >> 0));
dreschpe 0:cccc5726bdf3 351 wr_reg(0x04, (x+w-1 >> 8));
dreschpe 0:cccc5726bdf3 352 wr_reg(0x07, ( y >> 0));
dreschpe 0:cccc5726bdf3 353 wr_reg(0x06, ( y >> 8));
dreschpe 0:cccc5726bdf3 354 wr_reg(0x09, ( y+h-1 >> 0));
dreschpe 0:cccc5726bdf3 355 wr_reg(0x08, ( y+h-1 >> 8));
dreschpe 0:cccc5726bdf3 356 //wr_cmd(0x22);
dreschpe 0:cccc5726bdf3 357 }
dreschpe 0:cccc5726bdf3 358
dreschpe 0:cccc5726bdf3 359
dreschpe 0:cccc5726bdf3 360
dreschpe 0:cccc5726bdf3 361 /** Set draw window region to whole screen
dreschpe 0:cccc5726bdf3 362 *
dreschpe 0:cccc5726bdf3 363 **/
dreschpe 0:cccc5726bdf3 364
dreschpe 0:cccc5726bdf3 365 void SPI_TFT::WindowMax (void) {
dreschpe 0:cccc5726bdf3 366 window (0, 0, width(), height());
dreschpe 0:cccc5726bdf3 367 }
dreschpe 0:cccc5726bdf3 368
dreschpe 0:cccc5726bdf3 369
dreschpe 0:cccc5726bdf3 370 /** Fill the screen with _backgroun color
dreschpe 0:cccc5726bdf3 371 *
dreschpe 0:cccc5726bdf3 372 **/
dreschpe 0:cccc5726bdf3 373 void SPI_TFT::cls (void) {
dreschpe 0:cccc5726bdf3 374 unsigned int i;
dreschpe 0:cccc5726bdf3 375 WindowMax();
dreschpe 0:cccc5726bdf3 376 wr_cmd(0x22);
dreschpe 0:cccc5726bdf3 377 wr_dat_start();
dreschpe 0:cccc5726bdf3 378 _spi.format(16,3); // 16 bit Mode 3
dreschpe 0:cccc5726bdf3 379 for(i = 0; i < ( width() * height()); i++)
dreschpe 0:cccc5726bdf3 380 _spi.write(_background);
dreschpe 0:cccc5726bdf3 381 _spi.format(8,3); // 8 bit Mode 3
dreschpe 0:cccc5726bdf3 382 wr_dat_stop();
dreschpe 0:cccc5726bdf3 383 }
dreschpe 0:cccc5726bdf3 384
dreschpe 0:cccc5726bdf3 385
dreschpe 0:cccc5726bdf3 386 /** draw a circle
dreschpe 0:cccc5726bdf3 387 *
dreschpe 0:cccc5726bdf3 388 * @param x0,y0 center
dreschpe 0:cccc5726bdf3 389 * @param r radius
dreschpe 0:cccc5726bdf3 390 * @param color 16 bit color *
dreschpe 0:cccc5726bdf3 391 *
dreschpe 0:cccc5726bdf3 392 */
dreschpe 0:cccc5726bdf3 393 void SPI_TFT::circle(int x0, int y0, int r, int color) {
dreschpe 0:cccc5726bdf3 394
dreschpe 0:cccc5726bdf3 395 int draw_x0, draw_y0;
dreschpe 0:cccc5726bdf3 396 int draw_x1, draw_y1;
dreschpe 0:cccc5726bdf3 397 int draw_x2, draw_y2;
dreschpe 0:cccc5726bdf3 398 int draw_x3, draw_y3;
dreschpe 0:cccc5726bdf3 399 int draw_x4, draw_y4;
dreschpe 0:cccc5726bdf3 400 int draw_x5, draw_y5;
dreschpe 0:cccc5726bdf3 401 int draw_x6, draw_y6;
dreschpe 0:cccc5726bdf3 402 int draw_x7, draw_y7;
dreschpe 0:cccc5726bdf3 403 int xx, yy;
dreschpe 0:cccc5726bdf3 404 int di;
dreschpe 0:cccc5726bdf3 405 WindowMax();
dreschpe 0:cccc5726bdf3 406 if(r == 0) /* no radius */
dreschpe 0:cccc5726bdf3 407 {
dreschpe 0:cccc5726bdf3 408 return;
dreschpe 0:cccc5726bdf3 409 }
dreschpe 0:cccc5726bdf3 410
dreschpe 0:cccc5726bdf3 411 draw_x0 = draw_x1 = x0;
dreschpe 0:cccc5726bdf3 412 draw_y0 = draw_y1 = y0 + r;
dreschpe 0:cccc5726bdf3 413 if(draw_y0 < height())
dreschpe 0:cccc5726bdf3 414 {
dreschpe 0:cccc5726bdf3 415 pixel(draw_x0, draw_y0, color); /* 90 degree */
dreschpe 0:cccc5726bdf3 416 }
dreschpe 0:cccc5726bdf3 417
dreschpe 0:cccc5726bdf3 418 draw_x2 = draw_x3 = x0;
dreschpe 0:cccc5726bdf3 419 draw_y2 = draw_y3 = y0 - r;
dreschpe 0:cccc5726bdf3 420 if(draw_y2 >= 0)
dreschpe 0:cccc5726bdf3 421 {
dreschpe 0:cccc5726bdf3 422 pixel(draw_x2, draw_y2, color); /* 270 degree */
dreschpe 0:cccc5726bdf3 423 }
dreschpe 0:cccc5726bdf3 424
dreschpe 0:cccc5726bdf3 425 draw_x4 = draw_x6 = x0 + r;
dreschpe 0:cccc5726bdf3 426 draw_y4 = draw_y6 = y0;
dreschpe 0:cccc5726bdf3 427 if(draw_x4 < width())
dreschpe 0:cccc5726bdf3 428 {
dreschpe 0:cccc5726bdf3 429 pixel(draw_x4, draw_y4, color); /* 0 degree */
dreschpe 0:cccc5726bdf3 430 }
dreschpe 0:cccc5726bdf3 431
dreschpe 0:cccc5726bdf3 432 draw_x5 = draw_x7 = x0 - r;
dreschpe 0:cccc5726bdf3 433 draw_y5 = draw_y7 = y0;
dreschpe 0:cccc5726bdf3 434 if(draw_x5>=0)
dreschpe 0:cccc5726bdf3 435 {
dreschpe 0:cccc5726bdf3 436 pixel(draw_x5, draw_y5, color); /* 180 degree */
dreschpe 0:cccc5726bdf3 437 }
dreschpe 0:cccc5726bdf3 438
dreschpe 0:cccc5726bdf3 439 if(r == 1)
dreschpe 0:cccc5726bdf3 440 {
dreschpe 0:cccc5726bdf3 441 return;
dreschpe 0:cccc5726bdf3 442 }
dreschpe 0:cccc5726bdf3 443
dreschpe 0:cccc5726bdf3 444 di = 3 - 2*r;
dreschpe 0:cccc5726bdf3 445 xx = 0;
dreschpe 0:cccc5726bdf3 446 yy = r;
dreschpe 0:cccc5726bdf3 447 while(xx < yy)
dreschpe 0:cccc5726bdf3 448 {
dreschpe 0:cccc5726bdf3 449
dreschpe 0:cccc5726bdf3 450 if(di < 0)
dreschpe 0:cccc5726bdf3 451 {
dreschpe 0:cccc5726bdf3 452 di += 4*xx + 6;
dreschpe 0:cccc5726bdf3 453 }
dreschpe 0:cccc5726bdf3 454 else
dreschpe 0:cccc5726bdf3 455 {
dreschpe 0:cccc5726bdf3 456 di += 4*(xx - yy) + 10;
dreschpe 0:cccc5726bdf3 457 yy--;
dreschpe 0:cccc5726bdf3 458 draw_y0--;
dreschpe 0:cccc5726bdf3 459 draw_y1--;
dreschpe 0:cccc5726bdf3 460 draw_y2++;
dreschpe 0:cccc5726bdf3 461 draw_y3++;
dreschpe 0:cccc5726bdf3 462 draw_x4--;
dreschpe 0:cccc5726bdf3 463 draw_x5++;
dreschpe 0:cccc5726bdf3 464 draw_x6--;
dreschpe 0:cccc5726bdf3 465 draw_x7++;
dreschpe 0:cccc5726bdf3 466 }
dreschpe 0:cccc5726bdf3 467 xx++;
dreschpe 0:cccc5726bdf3 468 draw_x0++;
dreschpe 0:cccc5726bdf3 469 draw_x1--;
dreschpe 0:cccc5726bdf3 470 draw_x2++;
dreschpe 0:cccc5726bdf3 471 draw_x3--;
dreschpe 0:cccc5726bdf3 472 draw_y4++;
dreschpe 0:cccc5726bdf3 473 draw_y5++;
dreschpe 0:cccc5726bdf3 474 draw_y6--;
dreschpe 0:cccc5726bdf3 475 draw_y7--;
dreschpe 0:cccc5726bdf3 476
dreschpe 0:cccc5726bdf3 477 if( (draw_x0 <= width()) && (draw_y0>=0) )
dreschpe 0:cccc5726bdf3 478 {
dreschpe 0:cccc5726bdf3 479 pixel(draw_x0, draw_y0, color);
dreschpe 0:cccc5726bdf3 480 }
dreschpe 0:cccc5726bdf3 481
dreschpe 0:cccc5726bdf3 482 if( (draw_x1 >= 0) && (draw_y1 >= 0) )
dreschpe 0:cccc5726bdf3 483 {
dreschpe 0:cccc5726bdf3 484 pixel(draw_x1, draw_y1, color);
dreschpe 0:cccc5726bdf3 485 }
dreschpe 0:cccc5726bdf3 486
dreschpe 0:cccc5726bdf3 487 if( (draw_x2 <= width()) && (draw_y2 <= height()) )
dreschpe 0:cccc5726bdf3 488 {
dreschpe 0:cccc5726bdf3 489 pixel(draw_x2, draw_y2, color);
dreschpe 0:cccc5726bdf3 490 }
dreschpe 0:cccc5726bdf3 491
dreschpe 0:cccc5726bdf3 492 if( (draw_x3 >=0 ) && (draw_y3 <= height()) )
dreschpe 0:cccc5726bdf3 493 {
dreschpe 0:cccc5726bdf3 494 pixel(draw_x3, draw_y3, color);
dreschpe 0:cccc5726bdf3 495 }
dreschpe 0:cccc5726bdf3 496
dreschpe 0:cccc5726bdf3 497 if( (draw_x4 <= width()) && (draw_y4 >= 0) )
dreschpe 0:cccc5726bdf3 498 {
dreschpe 0:cccc5726bdf3 499 pixel(draw_x4, draw_y4, color);
dreschpe 0:cccc5726bdf3 500 }
dreschpe 0:cccc5726bdf3 501
dreschpe 0:cccc5726bdf3 502 if( (draw_x5 >= 0) && (draw_y5 >= 0) )
dreschpe 0:cccc5726bdf3 503 {
dreschpe 0:cccc5726bdf3 504 pixel(draw_x5, draw_y5, color);
dreschpe 0:cccc5726bdf3 505 }
dreschpe 0:cccc5726bdf3 506 if( (draw_x6 <=width()) && (draw_y6 <= height()) )
dreschpe 0:cccc5726bdf3 507 {
dreschpe 0:cccc5726bdf3 508 pixel(draw_x6, draw_y6, color);
dreschpe 0:cccc5726bdf3 509 }
dreschpe 0:cccc5726bdf3 510 if( (draw_x7 >= 0) && (draw_y7 <= height()) )
dreschpe 0:cccc5726bdf3 511 {
dreschpe 0:cccc5726bdf3 512 pixel(draw_x7, draw_y7, color);
dreschpe 0:cccc5726bdf3 513 }
dreschpe 0:cccc5726bdf3 514 }
dreschpe 0:cccc5726bdf3 515 return;
dreschpe 0:cccc5726bdf3 516 }
dreschpe 0:cccc5726bdf3 517
dreschpe 0:cccc5726bdf3 518 /** draw a horizontal line
dreschpe 0:cccc5726bdf3 519 *
dreschpe 0:cccc5726bdf3 520 * @param x0 horizontal start
dreschpe 0:cccc5726bdf3 521 * @param x1 horizontal stop
dreschpe 0:cccc5726bdf3 522 * @param y vertical position
dreschpe 0:cccc5726bdf3 523 * @param color 16 bit color
dreschpe 0:cccc5726bdf3 524 *
dreschpe 0:cccc5726bdf3 525 **/
dreschpe 0:cccc5726bdf3 526 void SPI_TFT::hline(int x0, int x1, int y, int color) {
dreschpe 0:cccc5726bdf3 527 int w;
dreschpe 0:cccc5726bdf3 528 w = x1 - x0 + 1;
dreschpe 0:cccc5726bdf3 529 window(x0,y,w,1);
dreschpe 0:cccc5726bdf3 530 wr_cmd(0x22);
dreschpe 0:cccc5726bdf3 531 wr_dat_start();
dreschpe 0:cccc5726bdf3 532 _spi.format(16,3); // pixel are send in 16 bit mode to speed up
dreschpe 0:cccc5726bdf3 533 for(int x=0; x<w; x++){
dreschpe 0:cccc5726bdf3 534 _spi.write(color);
dreschpe 0:cccc5726bdf3 535 }
dreschpe 0:cccc5726bdf3 536 _spi.format(8,3);
dreschpe 0:cccc5726bdf3 537 wr_dat_stop();
dreschpe 0:cccc5726bdf3 538 return;
dreschpe 0:cccc5726bdf3 539 }
dreschpe 0:cccc5726bdf3 540
dreschpe 0:cccc5726bdf3 541 /** draw a vertical line
dreschpe 0:cccc5726bdf3 542 *
dreschpe 0:cccc5726bdf3 543 * @param x horizontal position
dreschpe 0:cccc5726bdf3 544 * @param y0 vertical start
dreschpe 0:cccc5726bdf3 545 * @param y1 vertical stop
dreschpe 0:cccc5726bdf3 546 * @param color 16 bit color
dreschpe 0:cccc5726bdf3 547 *
dreschpe 0:cccc5726bdf3 548 **/
dreschpe 0:cccc5726bdf3 549
dreschpe 0:cccc5726bdf3 550 void SPI_TFT::vline(int x, int y0, int y1, int color) {
dreschpe 0:cccc5726bdf3 551 int h;
dreschpe 0:cccc5726bdf3 552 h = y1 - y0 + 1;
dreschpe 0:cccc5726bdf3 553 window(x,y0,1,h);
dreschpe 0:cccc5726bdf3 554 wr_cmd(0x22);
dreschpe 0:cccc5726bdf3 555 wr_dat_start();
dreschpe 0:cccc5726bdf3 556 _spi.format(16,3); // pixel are send in 16 bit mode to speed up
dreschpe 0:cccc5726bdf3 557 for(int y=0; y<h; y++){
dreschpe 0:cccc5726bdf3 558 _spi.write(color);
dreschpe 0:cccc5726bdf3 559 }
dreschpe 0:cccc5726bdf3 560 _spi.format(8,3);
dreschpe 0:cccc5726bdf3 561 wr_dat_stop();
dreschpe 0:cccc5726bdf3 562 return;
dreschpe 0:cccc5726bdf3 563 }
dreschpe 0:cccc5726bdf3 564
dreschpe 0:cccc5726bdf3 565 /** draw a 1 pixel line
dreschpe 0:cccc5726bdf3 566 *
dreschpe 0:cccc5726bdf3 567 * @param x0,y0 start point
dreschpe 0:cccc5726bdf3 568 * @param x1,y1 stop point
dreschpe 0:cccc5726bdf3 569 * @param color 16 bit color
dreschpe 0:cccc5726bdf3 570 *
dreschpe 0:cccc5726bdf3 571 **/
dreschpe 0:cccc5726bdf3 572
dreschpe 0:cccc5726bdf3 573 void SPI_TFT::line(int x0, int y0, int x1, int y1, int color) {
dreschpe 0:cccc5726bdf3 574 WindowMax();
dreschpe 0:cccc5726bdf3 575 int dx = 0, dy = 0;
dreschpe 0:cccc5726bdf3 576 int dx_sym = 0, dy_sym = 0;
dreschpe 0:cccc5726bdf3 577 int dx_x2 = 0, dy_x2 = 0;
dreschpe 0:cccc5726bdf3 578 int di = 0;
dreschpe 0:cccc5726bdf3 579
dreschpe 0:cccc5726bdf3 580 dx = x1-x0;
dreschpe 0:cccc5726bdf3 581 dy = y1-y0;
dreschpe 0:cccc5726bdf3 582
dreschpe 0:cccc5726bdf3 583 if(dx == 0) /* vertical line */
dreschpe 0:cccc5726bdf3 584 {
dreschpe 0:cccc5726bdf3 585 if(y1 > y0) vline(x0,y0,y1,color);
dreschpe 0:cccc5726bdf3 586 else vline(x0,y1,y0,color);
dreschpe 0:cccc5726bdf3 587 return;
dreschpe 0:cccc5726bdf3 588 }
dreschpe 0:cccc5726bdf3 589
dreschpe 0:cccc5726bdf3 590 if(dx > 0)
dreschpe 0:cccc5726bdf3 591 {
dreschpe 0:cccc5726bdf3 592 dx_sym = 1;
dreschpe 0:cccc5726bdf3 593 }
dreschpe 0:cccc5726bdf3 594 else
dreschpe 0:cccc5726bdf3 595 {
dreschpe 0:cccc5726bdf3 596 dx_sym = -1;
dreschpe 0:cccc5726bdf3 597 }
dreschpe 0:cccc5726bdf3 598 if(dy == 0) /* horizontal line */
dreschpe 0:cccc5726bdf3 599 {
dreschpe 0:cccc5726bdf3 600 if(x1 > x0) hline(x0,x1,y0,color);
dreschpe 0:cccc5726bdf3 601 else hline(x1,x0,y0,color);
dreschpe 0:cccc5726bdf3 602 return;
dreschpe 0:cccc5726bdf3 603 }
dreschpe 0:cccc5726bdf3 604
dreschpe 0:cccc5726bdf3 605 if(dy > 0)
dreschpe 0:cccc5726bdf3 606 {
dreschpe 0:cccc5726bdf3 607 dy_sym = 1;
dreschpe 0:cccc5726bdf3 608 }
dreschpe 0:cccc5726bdf3 609 else
dreschpe 0:cccc5726bdf3 610 {
dreschpe 0:cccc5726bdf3 611 dy_sym = -1;
dreschpe 0:cccc5726bdf3 612 }
dreschpe 0:cccc5726bdf3 613
dreschpe 0:cccc5726bdf3 614 dx = dx_sym*dx;
dreschpe 0:cccc5726bdf3 615 dy = dy_sym*dy;
dreschpe 0:cccc5726bdf3 616
dreschpe 0:cccc5726bdf3 617 dx_x2 = dx*2;
dreschpe 0:cccc5726bdf3 618 dy_x2 = dy*2;
dreschpe 0:cccc5726bdf3 619
dreschpe 0:cccc5726bdf3 620 if(dx >= dy)
dreschpe 0:cccc5726bdf3 621 {
dreschpe 0:cccc5726bdf3 622 di = dy_x2 - dx;
dreschpe 0:cccc5726bdf3 623 while(x0 != x1)
dreschpe 0:cccc5726bdf3 624 {
dreschpe 0:cccc5726bdf3 625
dreschpe 0:cccc5726bdf3 626 pixel(x0, y0, color);
dreschpe 0:cccc5726bdf3 627 x0 += dx_sym;
dreschpe 0:cccc5726bdf3 628 if(di<0)
dreschpe 0:cccc5726bdf3 629 {
dreschpe 0:cccc5726bdf3 630 di += dy_x2;
dreschpe 0:cccc5726bdf3 631 }
dreschpe 0:cccc5726bdf3 632 else
dreschpe 0:cccc5726bdf3 633 {
dreschpe 0:cccc5726bdf3 634 di += dy_x2 - dx_x2;
dreschpe 0:cccc5726bdf3 635 y0 += dy_sym;
dreschpe 0:cccc5726bdf3 636 }
dreschpe 0:cccc5726bdf3 637 }
dreschpe 0:cccc5726bdf3 638 pixel(x0, y0, color);
dreschpe 0:cccc5726bdf3 639 }
dreschpe 0:cccc5726bdf3 640 else
dreschpe 0:cccc5726bdf3 641 {
dreschpe 0:cccc5726bdf3 642 di = dx_x2 - dy;
dreschpe 0:cccc5726bdf3 643 while(y0 != y1)
dreschpe 0:cccc5726bdf3 644 {
dreschpe 0:cccc5726bdf3 645 pixel(x0, y0, color);
dreschpe 0:cccc5726bdf3 646 y0 += dy_sym;
dreschpe 0:cccc5726bdf3 647 if(di < 0)
dreschpe 0:cccc5726bdf3 648 {
dreschpe 0:cccc5726bdf3 649 di += dx_x2;
dreschpe 0:cccc5726bdf3 650 }
dreschpe 0:cccc5726bdf3 651 else
dreschpe 0:cccc5726bdf3 652 {
dreschpe 0:cccc5726bdf3 653 di += dx_x2 - dy_x2;
dreschpe 0:cccc5726bdf3 654 x0 += dx_sym;
dreschpe 0:cccc5726bdf3 655 }
dreschpe 0:cccc5726bdf3 656 }
dreschpe 0:cccc5726bdf3 657 pixel(x0, y0, color);
dreschpe 0:cccc5726bdf3 658 }
dreschpe 0:cccc5726bdf3 659 return;
dreschpe 0:cccc5726bdf3 660 }
dreschpe 0:cccc5726bdf3 661
dreschpe 0:cccc5726bdf3 662
dreschpe 0:cccc5726bdf3 663 /** draw a rect
dreschpe 0:cccc5726bdf3 664 *
dreschpe 0:cccc5726bdf3 665 * @param x0,y0 top left corner
dreschpe 0:cccc5726bdf3 666 * @param x1,y1 down right corner
dreschpe 0:cccc5726bdf3 667 * @param color 16 bit color
dreschpe 0:cccc5726bdf3 668 * *
dreschpe 0:cccc5726bdf3 669 **/
dreschpe 0:cccc5726bdf3 670
dreschpe 0:cccc5726bdf3 671 void SPI_TFT::rect(int x0, int y0, int x1, int y1, int color) {
dreschpe 0:cccc5726bdf3 672
dreschpe 0:cccc5726bdf3 673 if(x1 > x0) hline(x0,x1,y0,color);
dreschpe 0:cccc5726bdf3 674 else hline(x1,x0,y0,color);
dreschpe 0:cccc5726bdf3 675
dreschpe 0:cccc5726bdf3 676 if(y1 > y0) vline(x0,y0,y1,color);
dreschpe 0:cccc5726bdf3 677 else vline(x0,y1,y0,color);
dreschpe 0:cccc5726bdf3 678
dreschpe 0:cccc5726bdf3 679 if(x1 > x0) hline(x0,x1,y1,color);
dreschpe 0:cccc5726bdf3 680 else hline(x1,x0,y1,color);
dreschpe 0:cccc5726bdf3 681
dreschpe 0:cccc5726bdf3 682 if(y1 > y0) vline(x1,y0,y1,color);
dreschpe 0:cccc5726bdf3 683 else vline(x1,y1,y0,color);
dreschpe 0:cccc5726bdf3 684
dreschpe 0:cccc5726bdf3 685 return;
dreschpe 0:cccc5726bdf3 686 }
dreschpe 0:cccc5726bdf3 687
dreschpe 0:cccc5726bdf3 688 /** draw a filled rect
dreschpe 0:cccc5726bdf3 689 *
dreschpe 0:cccc5726bdf3 690 * @param x0,y0 top left corner
dreschpe 0:cccc5726bdf3 691 * @param x1,y1 down right corner
dreschpe 0:cccc5726bdf3 692 * @param color 16 bit color
dreschpe 0:cccc5726bdf3 693 *
dreschpe 0:cccc5726bdf3 694 **/
dreschpe 0:cccc5726bdf3 695
dreschpe 0:cccc5726bdf3 696 void SPI_TFT::fillrect(int x0, int y0, int x1, int y1, int color) {
dreschpe 0:cccc5726bdf3 697
dreschpe 0:cccc5726bdf3 698 int h = y1 - y0 + 1;
dreschpe 0:cccc5726bdf3 699 int w = x1 - x0 + 1;
dreschpe 0:cccc5726bdf3 700 int pixel = h * w;
dreschpe 0:cccc5726bdf3 701 window(x0,y0,w,h);
dreschpe 0:cccc5726bdf3 702 wr_cmd(0x22);
dreschpe 0:cccc5726bdf3 703 wr_dat_start();
dreschpe 0:cccc5726bdf3 704 _spi.format(16,3); // pixel are send in 16 bit mode to speed up
dreschpe 0:cccc5726bdf3 705 for(int p=0; p<pixel; p++){
dreschpe 0:cccc5726bdf3 706 _spi.write(color);
dreschpe 0:cccc5726bdf3 707 }
dreschpe 0:cccc5726bdf3 708 _spi.format(8,3);
dreschpe 0:cccc5726bdf3 709 wr_dat_stop();
dreschpe 0:cccc5726bdf3 710 return;
dreschpe 0:cccc5726bdf3 711 }
dreschpe 0:cccc5726bdf3 712
dreschpe 0:cccc5726bdf3 713
dreschpe 0:cccc5726bdf3 714 /** setup cursor position
dreschpe 0:cccc5726bdf3 715 *
dreschpe 0:cccc5726bdf3 716 * @param column 0 to max
dreschpe 0:cccc5726bdf3 717 * @param row 0 to max
dreschpe 0:cccc5726bdf3 718 * max depend on font size
dreschpe 0:cccc5726bdf3 719 **/
dreschpe 0:cccc5726bdf3 720 void SPI_TFT::locate(int column, int row) {
dreschpe 0:cccc5726bdf3 721 _column = column;
dreschpe 0:cccc5726bdf3 722 char_x = font[1] * column; // get the horz. size of the actual font
dreschpe 0:cccc5726bdf3 723 _row = row;
dreschpe 0:cccc5726bdf3 724 }
dreschpe 0:cccc5726bdf3 725
dreschpe 0:cccc5726bdf3 726 /** calculate the max number of char in a line
dreschpe 0:cccc5726bdf3 727 *
dreschpe 0:cccc5726bdf3 728 * @returns max columns
dreschpe 0:cccc5726bdf3 729 * depends on actual font size
dreschpe 0:cccc5726bdf3 730 *
dreschpe 0:cccc5726bdf3 731 */
dreschpe 0:cccc5726bdf3 732
dreschpe 0:cccc5726bdf3 733 int SPI_TFT::columns() {
dreschpe 0:cccc5726bdf3 734 return width() / font[1];
dreschpe 0:cccc5726bdf3 735 }
dreschpe 0:cccc5726bdf3 736
dreschpe 0:cccc5726bdf3 737 /** calculate the max number of columns
dreschpe 0:cccc5726bdf3 738 *
dreschpe 0:cccc5726bdf3 739 * @returns max column
dreschpe 0:cccc5726bdf3 740 * depends on actual font size
dreschpe 0:cccc5726bdf3 741 *
dreschpe 0:cccc5726bdf3 742 */
dreschpe 0:cccc5726bdf3 743
dreschpe 0:cccc5726bdf3 744 int SPI_TFT::rows() {
dreschpe 0:cccc5726bdf3 745 return height() / font[2];
dreschpe 0:cccc5726bdf3 746 }
dreschpe 0:cccc5726bdf3 747
dreschpe 0:cccc5726bdf3 748 /** put a char on the screen
dreschpe 0:cccc5726bdf3 749 *
dreschpe 0:cccc5726bdf3 750 * @param value char to print
dreschpe 0:cccc5726bdf3 751 * @returns printed char
dreschpe 0:cccc5726bdf3 752 *
dreschpe 0:cccc5726bdf3 753 */
dreschpe 0:cccc5726bdf3 754
dreschpe 0:cccc5726bdf3 755 int SPI_TFT::_putc(int value) {
dreschpe 0:cccc5726bdf3 756 if(value == '\n') {
dreschpe 0:cccc5726bdf3 757 _column = 0;
dreschpe 0:cccc5726bdf3 758 char_x = 0;
dreschpe 0:cccc5726bdf3 759 _row++;
dreschpe 0:cccc5726bdf3 760 if(_row >= rows()) {
dreschpe 0:cccc5726bdf3 761 _row = 0;
dreschpe 0:cccc5726bdf3 762 }
dreschpe 0:cccc5726bdf3 763 } else {
dreschpe 0:cccc5726bdf3 764 character(_column, _row, value);
dreschpe 0:cccc5726bdf3 765 _column++;
dreschpe 0:cccc5726bdf3 766 }
dreschpe 0:cccc5726bdf3 767 return value;
dreschpe 0:cccc5726bdf3 768 }
dreschpe 0:cccc5726bdf3 769
dreschpe 0:cccc5726bdf3 770
dreschpe 0:cccc5726bdf3 771 /** draw a character on given position out of the active font to the TFT
dreschpe 0:cccc5726bdf3 772 *
dreschpe 0:cccc5726bdf3 773 * @param col column
dreschpe 0:cccc5726bdf3 774 * @param row row
dreschpe 0:cccc5726bdf3 775 * @param c char to print
dreschpe 0:cccc5726bdf3 776 *
dreschpe 0:cccc5726bdf3 777 */
dreschpe 0:cccc5726bdf3 778
dreschpe 0:cccc5726bdf3 779 void SPI_TFT::character(int col, int row, int c){
dreschpe 0:cccc5726bdf3 780 unsigned int hor,vert,offset,bpl,j,i,b;
dreschpe 0:cccc5726bdf3 781 unsigned char* zeichen;
dreschpe 0:cccc5726bdf3 782 unsigned char z,w;
dreschpe 0:cccc5726bdf3 783
dreschpe 0:cccc5726bdf3 784 if((c < 31) || (c > 127)) return; // test char range
dreschpe 0:cccc5726bdf3 785
dreschpe 0:cccc5726bdf3 786 // read font parameter from start of array
dreschpe 0:cccc5726bdf3 787 offset = font[0]; // bytes / char
dreschpe 0:cccc5726bdf3 788 hor = font[1]; // get hor size of font
dreschpe 0:cccc5726bdf3 789 vert = font[2]; // get vert size of font
dreschpe 0:cccc5726bdf3 790 bpl = font[3]; // bytes per line
dreschpe 0:cccc5726bdf3 791
dreschpe 0:cccc5726bdf3 792 if(char_x + hor > width()){
dreschpe 0:cccc5726bdf3 793 char_x = 0;
dreschpe 0:cccc5726bdf3 794 _column = 0;
dreschpe 0:cccc5726bdf3 795 _row ++;
dreschpe 0:cccc5726bdf3 796 row++;
dreschpe 0:cccc5726bdf3 797 if(_row >= rows()) {
dreschpe 0:cccc5726bdf3 798 _row = 0;
dreschpe 0:cccc5726bdf3 799 row=0;
dreschpe 0:cccc5726bdf3 800 }
dreschpe 0:cccc5726bdf3 801 }
dreschpe 0:cccc5726bdf3 802
dreschpe 0:cccc5726bdf3 803 window(char_x, row * vert,hor,vert); // char box
dreschpe 0:cccc5726bdf3 804 wr_cmd(0x22);
dreschpe 0:cccc5726bdf3 805 wr_dat_start();
dreschpe 0:cccc5726bdf3 806 zeichen = &font[((c -32) * offset) + 4]; // start of char bitmap
dreschpe 0:cccc5726bdf3 807 w = zeichen[0]; // width of actual char
dreschpe 0:cccc5726bdf3 808 _spi.format(16,3); // pixel are 16 bit
dreschpe 0:cccc5726bdf3 809
dreschpe 0:cccc5726bdf3 810 for(j=0;j<vert;j++){ // vert line
dreschpe 0:cccc5726bdf3 811 for(i=0;i<hor;i++){ // horz line
dreschpe 0:cccc5726bdf3 812 z = zeichen[bpl * i + ((j & 0xF8) >> 3)+1];
dreschpe 0:cccc5726bdf3 813 b = 1 << (j & 0x07);
dreschpe 0:cccc5726bdf3 814 if (( z & b ) == 0x00){
dreschpe 0:cccc5726bdf3 815 _spi.write(_background);
dreschpe 0:cccc5726bdf3 816 }
dreschpe 0:cccc5726bdf3 817 else {
dreschpe 0:cccc5726bdf3 818 _spi.write(_foreground);
dreschpe 0:cccc5726bdf3 819 }
dreschpe 0:cccc5726bdf3 820 }
dreschpe 0:cccc5726bdf3 821 }
dreschpe 0:cccc5726bdf3 822 _spi.format(8,3); // 8 bit
dreschpe 0:cccc5726bdf3 823 wr_dat_stop();
dreschpe 0:cccc5726bdf3 824 if ((w + 2) < hor) { // x offset to next char
dreschpe 0:cccc5726bdf3 825 char_x += w + 2;
dreschpe 0:cccc5726bdf3 826 }
dreschpe 0:cccc5726bdf3 827 else char_x += hor;
dreschpe 0:cccc5726bdf3 828 }
dreschpe 0:cccc5726bdf3 829
dreschpe 0:cccc5726bdf3 830
dreschpe 0:cccc5726bdf3 831 /** select the font to use
dreschpe 0:cccc5726bdf3 832 *
dreschpe 0:cccc5726bdf3 833 * @param f pointer to font array
dreschpe 0:cccc5726bdf3 834 *
dreschpe 0:cccc5726bdf3 835 * font array can created with GLCD Font Creator from http://www.mikroe.com
dreschpe 0:cccc5726bdf3 836 * you have to add 4 parameter at the beginning of the font array to use:
dreschpe 0:cccc5726bdf3 837 * - the number of byte / char
dreschpe 0:cccc5726bdf3 838 * - the vertial size in pixel
dreschpe 0:cccc5726bdf3 839 * - the horizontal size in pixel
dreschpe 0:cccc5726bdf3 840 * - the number of byte per vertical line
dreschpe 0:cccc5726bdf3 841 * you also have to change the array to char[]
dreschpe 0:cccc5726bdf3 842 *
dreschpe 0:cccc5726bdf3 843 */
dreschpe 0:cccc5726bdf3 844
dreschpe 0:cccc5726bdf3 845
dreschpe 0:cccc5726bdf3 846 void SPI_TFT::set_font(unsigned char* f){
dreschpe 0:cccc5726bdf3 847 font = f;
dreschpe 0:cccc5726bdf3 848 }
dreschpe 0:cccc5726bdf3 849
dreschpe 0:cccc5726bdf3 850 /** paint a bitmap on the TFT
dreschpe 0:cccc5726bdf3 851 *
dreschpe 0:cccc5726bdf3 852 * @param x,y : upper left corner
dreschpe 0:cccc5726bdf3 853 * @param w width of bitmap
dreschpe 0:cccc5726bdf3 854 * @param h high of bitmap
dreschpe 0:cccc5726bdf3 855 * @param *bitmap pointer to the bitmap data
dreschpe 0:cccc5726bdf3 856 *
dreschpe 0:cccc5726bdf3 857 * bitmap format: 16 bit R5 G6 B5
dreschpe 0:cccc5726bdf3 858 *
dreschpe 0:cccc5726bdf3 859 * use Gimp to create / load , save as BMP, option 16 bit R5 G6 B5
dreschpe 0:cccc5726bdf3 860 * use winhex to load this file and mark data stating at offset 0x46 to end
dreschpe 0:cccc5726bdf3 861 * use edit -> copy block -> C Source to export C array
dreschpe 0:cccc5726bdf3 862 * paste this array into your program
dreschpe 0:cccc5726bdf3 863 *
dreschpe 0:cccc5726bdf3 864 */
dreschpe 0:cccc5726bdf3 865
dreschpe 0:cccc5726bdf3 866 void SPI_TFT::Bitmap(unsigned int x, unsigned int y, unsigned int w, unsigned int h,unsigned char *bitmap){
dreschpe 0:cccc5726bdf3 867 unsigned int i,j;
dreschpe 0:cccc5726bdf3 868 unsigned short *bitmap_ptr = (unsigned short *)bitmap;
dreschpe 0:cccc5726bdf3 869 window(x, y, w, h);
dreschpe 0:cccc5726bdf3 870 wr_cmd(0x22);
dreschpe 0:cccc5726bdf3 871 wr_dat_start();
dreschpe 0:cccc5726bdf3 872 _spi.format(16,3);
dreschpe 0:cccc5726bdf3 873 bitmap_ptr += ((h - 1)*w);
dreschpe 0:cccc5726bdf3 874 for (j = 0; j < h; j++){ //Lines
dreschpe 0:cccc5726bdf3 875 for (i = 0; i < w; i++) { // copy pixel data to TFT
dreschpe 0:cccc5726bdf3 876 _spi.write(*bitmap_ptr); // one line
dreschpe 0:cccc5726bdf3 877 bitmap_ptr++;
dreschpe 0:cccc5726bdf3 878 }
dreschpe 0:cccc5726bdf3 879 bitmap_ptr -= 2*w;
dreschpe 0:cccc5726bdf3 880 }
dreschpe 0:cccc5726bdf3 881 _spi.format(8,3);
dreschpe 0:cccc5726bdf3 882 wr_dat_stop();
dreschpe 0:cccc5726bdf3 883 }