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