This is a simple program using the Mikroelectronika TFT resistive touchscreen to create a sketchpad application.

Dependencies:   SPI_TFT_ILI9341 TFT_fonts mbed

Fork of TFT_Mikroelectronika_IL9341 by Oxford CWM Team

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // example to test the TFT Display from Mikroelectronika
00002 
00003 #include "stdio.h"
00004 #include "mbed.h"
00005 #include "SPI_TFT_ILI9341.h"
00006 #include "string"
00007 #include "Arial12x12.h"
00008 #include "Arial24x23.h"
00009 #include "Arial28x28.h"
00010 #include "font_big.h"
00011 
00012 
00013 Serial pc (USBTX,USBRX);
00014 
00015 
00016 
00017 
00018 // the display has a backlight switch on board
00019 DigitalOut LCD_LED(PTA13);   // may not be needed on mikroelectronika board
00020 DigitalOut pwr(PTD7); // ditto
00021 
00022 // the TFT is connected to SPI pin 5-7
00023 //SPI_TFT_ILI9341 TFT(p5, p6, p7, p8, p9, p10,"TFT"); // mosi, miso, sclk, cs, reset, dc for lpc1768
00024 SPI_TFT_ILI9341 TFT(PTD2, PTD3, PTD1, PTD5, PTD0, PTA13,"TFT"); // mosi, miso, sclk, cs, reset, dc for frdmkl25z
00025 //NB better combination to use a coherent 2x4 block for lcd
00026 //   SPI_TFT_ILI9341 TFT(PTD2, PTD3, PTD1, PTA16, PTA17, PTC16,"TFT"); // mosi, miso, sclk, cs, reset, dc for frdmkl25z
00027 //   DigitalOut LCD_LED(PTC17);
00028 int touching=0;
00029 
00030 // Subroutine to read the x location of the touch point
00031 // need to set x+ to 3V and ground x- then read analogue voltage on ym
00032 //nb need to add a check for actual touch as opposed to random crap
00033 int readX()
00034 {
00035     int delta=0,xv1=0,xv2=0,k=0;
00036 
00037     AnalogIn yp(PTB3);
00038     AnalogIn ym(PTB2);
00039     DigitalOut xp(PTB0);
00040     DigitalOut xm(PTB1);
00041 
00042     xp=1; // set positive sdie of x high
00043     xm=0;
00044     // dont need to do anyhting to set low side as it should be fine.
00045     // but do need to disconnect yp
00046     //yp.PinMode(PullNone)
00047     for(k=0; k<10; k++) { // make 10 readings to average
00048         xv1+=(int)ym.read_u16();  // get value
00049         xv2+=(int)yp.read_u16(); // get other value
00050     }
00051     delta=abs(xv2-xv1)/10;
00052     if(delta<300) touching=1;
00053     else touching=0;
00054     pc.printf("delta=%d \t %d\n\r",delta,touching);
00055     xp=0;
00056     xm=0;
00057     return(xv2/10); //maybe better to return the average of both....
00058 }
00059 // subroutine to read y values - has different pin functions ..
00060 int readY()
00061 {
00062     DigitalOut yp(PTB3);
00063     DigitalOut ym(PTB2);
00064     AnalogIn xp(PTB0);
00065     AnalogIn xm(PTB1);
00066 
00067     yp=1; // set positive sdie of x high
00068     ym=0;
00069     // dont need to do anyhting to set low side as it should be fine.
00070     // but do need to disconnect yp
00071     //yp.PinMode(PullNone)
00072     int yval=(int)xm.read_u16();  // get value
00073 
00074     yp=0;
00075     ym=0;
00076     return(yval);
00077 
00078 }
00079 
00080 void drawbuttons()
00081 {
00082     TFT.fillrect(0,0,50,50,Red);
00083     TFT.fillrect(50,0,100,50,Green);
00084     TFT.fillrect(100,0,150,50,Blue);
00085     TFT.fillrect(150,0,200,50,White);
00086     TFT.fillrect(200,0,240,50,Black);
00087 
00088     TFT.rect(0,0,50,50,White);
00089     TFT.rect(50,0,100,50,White);
00090     TFT.rect(100,0,150,50,White);
00091     TFT.rect(150,0,200,50,White);
00092     TFT.rect(200,0,240,50,White);
00093 
00094 }
00095 
00096 
00097 int main()
00098 {
00099     pc.baud(115200);
00100     int color=0;
00101     int xpos=0,ypos=0,xp=0,yp=0,sw=0;;
00102     pwr=1;
00103     wait(0.2);
00104 
00105     int i;
00106     LCD_LED = 1;            // backlight on
00107 
00108     TFT.claim(stdout);        // send stdout to the TFT display
00109     TFT.set_orientation(1);
00110     TFT.background(Black);    // set background to black
00111     TFT.foreground(White);    // set chars to white
00112     TFT.cls();                // clear the screen
00113 
00114     //first show the 4 directions
00115     TFT.set_orientation(0);
00116     TFT.background(Black);
00117     TFT.cls();
00118 
00119     TFT.set_font((unsigned char*) Arial12x12);
00120     TFT.locate(0,0);
00121     printf(" 0 Hello Mbed 0");
00122     TFT.set_orientation(1);
00123     TFT.locate(0,0);
00124     printf(" 1 Hello Mbed 1");
00125     TFT.set_orientation(2);
00126     TFT.locate(0,0);
00127     printf(" 2 Hello Mbed 2");
00128     TFT.set_orientation(3);
00129     TFT.locate(0,0);
00130     printf(" 3 Hello Mbed 3");
00131     TFT.set_orientation(3);
00132     TFT.set_font((unsigned char*) Arial24x23);
00133     TFT.locate(50,100);
00134     TFT.printf("TFT orientation 3");
00135     TFT.set_orientation(0);
00136 
00137     for(i=0; i<10; i++) {
00138         wait(0.2);        // wait one seconds
00139         TFT.locate(50,160);
00140         TFT.printf("count %d",i);
00141     }
00142     TFT.set_orientation(0);
00143     TFT.cls();
00144     //   LCD_LED = 1;
00145     //cornwer markers
00146     //TFT.fillcircle(10,10,5,0xffff);
00147     //TFT.fillcircle(230,10,5,0xffff);
00148     //TFT.fillcircle(230,310,5,0xffff);
00149     //TFT.fillcircle(10,310,5,0xffff);
00150     drawbuttons();
00151     while(1==1) {
00152 
00153         xpos=readX();
00154         ypos=readY();
00155         // top chunk of the screen is the button area //
00156         // 0<y<50 is palette area //
00157 
00158         //pc.printf("xpos=%d\t,\typo=%d",xpos,ypos);
00159         xp=(240*(xpos-5800))/51200;
00160         yp=320-(320*(ypos-3000))/58300;
00161         if(touching==1) pc.printf("\txp=%d\t,\typo=%d\n\r",xp,yp);
00162         if(xp>5 && yp>50 && touching==1) TFT.fillcircle(xp,yp,2,color);
00163         if(yp<50) { // color buttons
00164             sw=(int)xp/50;
00165             switch(sw) {
00166                 case 0:
00167                     color=0xf800;
00168                     break;
00169                 case 1:
00170                     color=0x07e0;
00171                     break;
00172                 case 2:
00173                     color=0x001f;
00174                     break;
00175                 case 3:
00176                     color=0xffff;
00177                     break;
00178                 case 4:
00179                     color=0x0000;
00180                     TFT.cls();
00181                     drawbuttons();
00182                     break;
00183             }
00184             //  if(xp<50) color=0xF800;
00185             //   if(50<xp && xp<100) color=0x07e0;
00186             // if(xp>100 && xp<150) color=0x001f;
00187         }
00188 
00189         wait(0.1);
00190 
00191     }
00192 
00193 }