Test code for the touch display, should output the number 12.

Dependencies:   SPI_TFT_ILI9341 TFT_fonts mbed

Fork of TFT_Mikroelectronika_IL9341_sketchpad by Oxford CWM Team

Committer:
JHutchinson
Date:
Wed May 24 13:35:34 2017 +0000
Revision:
8:3ae0674601ec
Parent:
7:c2bc477a07da
Child:
9:6b60556b4cdf
Code tested and reduced slightly for future developement.;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cstevens 4:14043cafbec7 1 // example to test the TFT Display from Mikroelectronika
dreschpe 0:7c3b9bfd6ead 2
dreschpe 0:7c3b9bfd6ead 3 #include "stdio.h"
dreschpe 0:7c3b9bfd6ead 4 #include "mbed.h"
dreschpe 0:7c3b9bfd6ead 5 #include "SPI_TFT_ILI9341.h"
dreschpe 0:7c3b9bfd6ead 6 #include "string"
dreschpe 0:7c3b9bfd6ead 7 #include "Arial12x12.h"
dreschpe 0:7c3b9bfd6ead 8 #include "Arial24x23.h"
dreschpe 0:7c3b9bfd6ead 9 #include "Arial28x28.h"
dreschpe 0:7c3b9bfd6ead 10 #include "font_big.h"
cstevens 4:14043cafbec7 11
cstevens 6:18c7288b5e00 12
cstevens 6:18c7288b5e00 13 Serial pc (USBTX,USBRX);
cstevens 4:14043cafbec7 14
dreschpe 0:7c3b9bfd6ead 15
dreschpe 0:7c3b9bfd6ead 16
pegcjs 3:676adf84c914 17
cstevens 6:18c7288b5e00 18 // the display has a backlight switch on board
cstevens 6:18c7288b5e00 19 DigitalOut LCD_LED(PTA13); // may not be needed on mikroelectronika board
cstevens 5:424af4fa03ab 20 DigitalOut pwr(PTD7); // ditto
dreschpe 0:7c3b9bfd6ead 21
dreschpe 0:7c3b9bfd6ead 22 // the TFT is connected to SPI pin 5-7
pegcjs 1:e0479f60cd48 23 //SPI_TFT_ILI9341 TFT(p5, p6, p7, p8, p9, p10,"TFT"); // mosi, miso, sclk, cs, reset, dc for lpc1768
cstevens 5:424af4fa03ab 24 SPI_TFT_ILI9341 TFT(PTD2, PTD3, PTD1, PTD5, PTD0, PTA13,"TFT"); // mosi, miso, sclk, cs, reset, dc for frdmkl25z
cstevens 6:18c7288b5e00 25 //NB better combination to use a coherent 2x4 block for lcd
pegcjs 3:676adf84c914 26 // SPI_TFT_ILI9341 TFT(PTD2, PTD3, PTD1, PTA16, PTA17, PTC16,"TFT"); // mosi, miso, sclk, cs, reset, dc for frdmkl25z
cstevens 6:18c7288b5e00 27 // DigitalOut LCD_LED(PTC17);
cstevens 7:c2bc477a07da 28 int touching=0;
cstevens 6:18c7288b5e00 29
cstevens 6:18c7288b5e00 30 // Subroutine to read the x location of the touch point
cstevens 6:18c7288b5e00 31 // need to set x+ to 3V and ground x- then read analogue voltage on ym
cstevens 6:18c7288b5e00 32 //nb need to add a check for actual touch as opposed to random crap
cstevens 6:18c7288b5e00 33 int readX()
cstevens 6:18c7288b5e00 34 {
cstevens 7:c2bc477a07da 35 int delta=0,xv1=0,xv2=0,k=0;
cstevens 7:c2bc477a07da 36
cstevens 7:c2bc477a07da 37 AnalogIn yp(PTB3);
cstevens 7:c2bc477a07da 38 AnalogIn ym(PTB2);
cstevens 6:18c7288b5e00 39 DigitalOut xp(PTB0);
cstevens 6:18c7288b5e00 40 DigitalOut xm(PTB1);
cstevens 6:18c7288b5e00 41
cstevens 6:18c7288b5e00 42 xp=1; // set positive sdie of x high
cstevens 6:18c7288b5e00 43 xm=0;
cstevens 6:18c7288b5e00 44 // dont need to do anyhting to set low side as it should be fine.
cstevens 6:18c7288b5e00 45 // but do need to disconnect yp
cstevens 6:18c7288b5e00 46 //yp.PinMode(PullNone)
cstevens 7:c2bc477a07da 47 for(k=0; k<10; k++) { // make 10 readings to average
cstevens 7:c2bc477a07da 48 xv1+=(int)ym.read_u16(); // get value
cstevens 7:c2bc477a07da 49 xv2+=(int)yp.read_u16(); // get other value
cstevens 7:c2bc477a07da 50 }
cstevens 7:c2bc477a07da 51 delta=abs(xv2-xv1)/10;
cstevens 7:c2bc477a07da 52 if(delta<300) touching=1;
cstevens 7:c2bc477a07da 53 else touching=0;
cstevens 7:c2bc477a07da 54 pc.printf("delta=%d \t %d\n\r",delta,touching);
cstevens 7:c2bc477a07da 55 xp=0;
cstevens 7:c2bc477a07da 56 xm=0;
cstevens 7:c2bc477a07da 57 return(xv2/10); //maybe better to return the average of both....
cstevens 6:18c7288b5e00 58 }
cstevens 6:18c7288b5e00 59 // subroutine to read y values - has different pin functions ..
cstevens 6:18c7288b5e00 60 int readY()
cstevens 6:18c7288b5e00 61 {
cstevens 7:c2bc477a07da 62 DigitalOut yp(PTB3);
cstevens 7:c2bc477a07da 63 DigitalOut ym(PTB2);
cstevens 6:18c7288b5e00 64 AnalogIn xp(PTB0);
cstevens 6:18c7288b5e00 65 AnalogIn xm(PTB1);
cstevens 6:18c7288b5e00 66
cstevens 6:18c7288b5e00 67 yp=1; // set positive sdie of x high
cstevens 6:18c7288b5e00 68 ym=0;
cstevens 6:18c7288b5e00 69 // dont need to do anyhting to set low side as it should be fine.
cstevens 6:18c7288b5e00 70 // but do need to disconnect yp
cstevens 6:18c7288b5e00 71 //yp.PinMode(PullNone)
cstevens 6:18c7288b5e00 72 int yval=(int)xm.read_u16(); // get value
cstevens 7:c2bc477a07da 73
cstevens 7:c2bc477a07da 74 yp=0;
cstevens 7:c2bc477a07da 75 ym=0;
cstevens 6:18c7288b5e00 76 return(yval);
cstevens 7:c2bc477a07da 77
cstevens 6:18c7288b5e00 78 }
cstevens 6:18c7288b5e00 79
cstevens 7:c2bc477a07da 80 void drawbuttons()
cstevens 7:c2bc477a07da 81 {
cstevens 7:c2bc477a07da 82 TFT.fillrect(0,0,50,50,Red);
cstevens 7:c2bc477a07da 83 TFT.fillrect(50,0,100,50,Green);
cstevens 7:c2bc477a07da 84 TFT.fillrect(100,0,150,50,Blue);
cstevens 7:c2bc477a07da 85 TFT.fillrect(150,0,200,50,White);
cstevens 7:c2bc477a07da 86 TFT.fillrect(200,0,240,50,Black);
cstevens 7:c2bc477a07da 87
cstevens 7:c2bc477a07da 88 TFT.rect(0,0,50,50,White);
cstevens 7:c2bc477a07da 89 TFT.rect(50,0,100,50,White);
cstevens 7:c2bc477a07da 90 TFT.rect(100,0,150,50,White);
cstevens 7:c2bc477a07da 91 TFT.rect(150,0,200,50,White);
cstevens 7:c2bc477a07da 92 TFT.rect(200,0,240,50,White);
cstevens 7:c2bc477a07da 93
cstevens 7:c2bc477a07da 94 }
cstevens 6:18c7288b5e00 95
cstevens 4:14043cafbec7 96
dreschpe 0:7c3b9bfd6ead 97 int main()
dreschpe 0:7c3b9bfd6ead 98 {
cstevens 6:18c7288b5e00 99 pc.baud(115200);
pegcjs 3:676adf84c914 100 pwr=1;
pegcjs 3:676adf84c914 101 wait(0.2);
cstevens 6:18c7288b5e00 102
JHutchinson 8:3ae0674601ec 103
pegcjs 2:25bfb21253a6 104 LCD_LED = 1; // backlight on
cstevens 6:18c7288b5e00 105
dreschpe 0:7c3b9bfd6ead 106 TFT.claim(stdout); // send stdout to the TFT display
dreschpe 0:7c3b9bfd6ead 107 TFT.set_orientation(1);
dreschpe 0:7c3b9bfd6ead 108 TFT.background(Black); // set background to black
dreschpe 0:7c3b9bfd6ead 109 TFT.foreground(White); // set chars to white
dreschpe 0:7c3b9bfd6ead 110 TFT.cls(); // clear the screen
dreschpe 0:7c3b9bfd6ead 111
dreschpe 0:7c3b9bfd6ead 112 //first show the 4 directions
dreschpe 0:7c3b9bfd6ead 113 TFT.set_orientation(0);
dreschpe 0:7c3b9bfd6ead 114 TFT.background(Black);
dreschpe 0:7c3b9bfd6ead 115 TFT.cls();
dreschpe 0:7c3b9bfd6ead 116
dreschpe 0:7c3b9bfd6ead 117 TFT.set_font((unsigned char*) Arial12x12);
dreschpe 0:7c3b9bfd6ead 118 TFT.locate(0,0);
cstevens 6:18c7288b5e00 119 printf(" 0 Hello Mbed 0");
JHutchinson 8:3ae0674601ec 120
JHutchinson 8:3ae0674601ec 121 }