dhivya gopal / Mbed 2 deprecated TFT_sdcard

Dependencies:   MMA8451Q SDFileSystem SPI_TFT_ILI9341 TFT_fonts mbed

Fork of TFT_test_frdm-kl25z by Motoo Tanaka

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed main.cpp to test adafruit 2.8" TFT LCD shiled w Touchscreen
00002  * Copyright (c) 2014 Motoo Tanaka @ Design Methodology Lab
00003  *
00004  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00005  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00006  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00007  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00008  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00009  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00010  * THE SOFTWARE.
00011  */
00012  
00013  /* 
00014   * Note: This program is derived from the SeeeStudioTFTv2 program.
00015   * Although both program share same ILI9341 TFT driver,
00016   * the touch sensor was not same with the Display I purchased from Akizuki.
00017   * http://akizukidenshi.com/catalog/g/gM-07747/
00018   * The touch sensor on the display is STMPE610,
00019   * so I hacked the minimum spi driver for it (polling mode only).
00020   */
00021 
00022 #include "mbed.h"
00023 #include "MMA8451Q.h"
00024 #include <math.h>
00025 #include "SPI_TFT_ILI9341.h"
00026 #include "SPI_STMPE610.h"
00027 #include "Arial12x12.h"
00028 #include "Arial24x23.h"
00029 #include "Arial28x28.h"
00030 #include "font_big.h"
00031 
00032 #include "SDFileSystem.h"
00033 
00034 
00035 
00036 #define PIN_XP          PTB3
00037 #define PIN_XM          PTB1
00038 #define PIN_YP          PTB2
00039 #define PIN_YM          PTB0
00040 #define PIN_MOSI        PTD2
00041 #define PIN_MISO        PTD3 
00042 #define PIN_SCLK        PTD1 
00043 #define PIN_CS_TFT      PTD0 
00044 #define PIN_DC_TFT      PTD5 
00045 #define PIN_BL_TFT      PTC9 
00046 #define PIN_CS_SD       PTA4 
00047 #define PIN_CS_TSC      PTA13
00048 #define PIN_TSC_INTR    PTC9
00049 
00050 #define MMA8451_I2C_ADDRESS (0x1d<<1)
00051 MMA8451Q *acc = 0 ;
00052 
00053 //  SPI_TFT_ILI9341(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset, PinName dc, const char* name ="TFT");
00054 SPI_TFT_ILI9341 TFT(PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TFT, PIN_BL_TFT, PIN_DC_TFT) ;
00055 SPI_STMPE610 TSC(PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TSC) ;
00056 
00057 SDFileSystem sd(PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_SD,"sd");
00058 
00059 DigitalOut backlight(PTA12) ;
00060 DigitalIn pinD7(PTC9) ;
00061 
00062 int page = 0 ;
00063 int numPage = 3 ;
00064 
00065 void initTFT(void)
00066 {
00067     //Configure the display driver
00068     TFT.background(Black);
00069     TFT.foreground(White);
00070     wait(0.01) ;
00071     TFT.cls();
00072 }
00073 
00074 void screen1(void) // Welcome Screen
00075 {
00076     backlight = 0 ;
00077     TFT.background(White) ;
00078     TFT.cls() ;
00079 
00080     wait(0.1) ;
00081     TFT.set_font((unsigned char*) Arial24x23);
00082     TFT.foreground(Red) ;
00083     TFT.locate(80, 40) ;
00084     TFT.printf("MBED") ;
00085     TFT.foreground(Blue);
00086     TFT.locate(60, 80) ;
00087     TFT.printf("2.8\"TFT") ; 
00088     TFT.locate(40, 120) ;
00089     TFT.printf("with touch") ;
00090     TFT.foreground(Black);
00091     TFT.set_font((unsigned char*) Arial12x12);
00092     TFT.foreground(Blue) ;
00093     TFT.locate(10, 180) ;
00094     TFT.printf("This program is running on") ;
00095     TFT.locate(10, 200) ;
00096     TFT.printf("freescale FRDM-KL25Z with") ;
00097     TFT.locate(10, 220) ;
00098     TFT.printf("a program developed on mbed") ;
00099     TFT.foreground(Green) ;
00100     TFT.locate(10, 260) ;
00101     TFT.printf("To advance demo page, touch") ;
00102     TFT.locate(10, 280) ;
00103     TFT.printf("and hold right side of screen") ;
00104     TFT.locate(10, 300) ;
00105     TFT.printf("until the next screen starts") ;
00106     backlight = 1 ;
00107 }
00108 
00109 
00110 
00111 void screen4(void) // Welcome Screen
00112 {
00113   
00114     backlight = 0 ;
00115     TFT.background(White) ;
00116     TFT.cls() ;
00117     wait(0.1) ;
00118     TFT.set_font((unsigned char*) Arial24x23);
00119     TFT.foreground(Red) ;
00120     TFT.locate(10, 40) ;
00121      TFT.printf("Hello World");   
00122  
00123     mkdir("/sd/mydir", 0777);
00124     
00125     FILE *fp = fopen("/sd/mydir/sdtest.txt", "w");
00126     if(fp == NULL) {
00127         error("Could not open file for write\n");
00128     }
00129     fprintf(fp, "Hello fun SD Card World!");
00130     fclose(fp); 
00131  
00132     
00133     
00134     
00135     TFT.foreground(Blue);
00136     TFT.locate(0, 80) ;
00137     TFT.printf("Goodbye World");
00138 
00139 }
00140 
00141 
00142 
00143 
00144 
00145 void screen2(void) // Graphics
00146 {
00147     //Draw some graphics
00148     int i, x[2], y[2] ;
00149     backlight = 0 ;
00150 
00151     TFT.background(Black);
00152     TFT.foreground(White);
00153     TFT.cls() ;
00154     TFT.set_font((unsigned char*) Arial12x12);
00155     TFT.locate(90,0);
00156     TFT.printf("Graphics");
00157     
00158     x[0] = 25 ; x[1] = 224 ;
00159     y[0] = 20 ; y[1] = 219 ;
00160     for (i = 20 ; i < 220 ; i += 10) {
00161         TFT.line(i+5, y[0], i+5, y[1], Blue) ;
00162         TFT.line(x[0], i, x[1], i, Blue) ;
00163     }
00164     TFT.line(125, y[0], 125, y[1], Green) ;
00165     TFT.line(x[0], 120, x[1], 120, Green) ;
00166     TFT.rect(x[0],y[0], x[1], y[1], Green) ;
00167     TFT.locate(10, 20) ;
00168     TFT.printf("V") ;
00169     TFT.locate(0, 115) ;
00170     TFT.printf("0.0") ;
00171     TFT.locate(115, 225) ;
00172     TFT.printf("0.0") ;
00173     TFT.locate(215, 225) ;
00174     TFT.printf("T") ;
00175 
00176     double s;
00177     for (int i = x[0]; i < 225; i++) {
00178         s = 40 * sin((long double)i / 20);
00179         TFT.pixel(i, 120 + (int)s, White);
00180     }
00181     
00182     TFT.fillrect(10, 240, 229, 309, White) ;
00183     TFT.rect(10, 240, 229, 309, Red) ;
00184     TFT.rect(11, 241, 228, 308, Red) ;
00185     
00186     TFT.background(White) ;
00187     TFT.foreground(Black) ;
00188     TFT.locate(20, 250) ;
00189     TFT.printf("With QVGA resolution") ;
00190     TFT.locate(20, 270) ;
00191     TFT.printf("simple graphics drawing") ;
00192     TFT.locate(20, 290) ;
00193     TFT.printf("capability is provided") ;
00194     backlight = 1 ;
00195 }    
00196 
00197 double clip(double src)
00198 {
00199     double value ;
00200     value = src ;
00201     if (value < 0.0) {
00202         value = 0.0 ;
00203     } else if (value > 2.0) {
00204         value = 2.0 ;
00205     }
00206     return( value ) ;
00207 }
00208 
00209 void screen3(void)
00210 {
00211     int t = 0 ;
00212     int pt = 0 ; // previous t 
00213     int i, x, y ;
00214     unsigned int data[3] ; // for x, y, z 
00215     unsigned int prev[3] ;
00216     unsigned short signalHeight = 39 ;
00217     unsigned short xoffset = 30 ;
00218     unsigned short yoffset = 120 ;
00219     unsigned short zoffset = 210 ;
00220     unsigned short paneX[2] = {20, 235} ;
00221     unsigned short paneH = 81 ;
00222  
00223     backlight = 0 ;
00224     TFT.background(Black) ;
00225     TFT.foreground(White) ;
00226 //    TFT.cls() ;
00227 
00228  
00229     TFT.fillrect(paneX[0], xoffset, paneX[1], xoffset+paneH, Black) ;
00230     TFT.fillrect(paneX[0], yoffset, paneX[1], yoffset+paneH, Black) ;
00231     TFT.fillrect(paneX[0], zoffset, paneX[1], zoffset+paneH, Black) ;
00232     TFT.fillrect(paneX[0], xoffset, paneX[1], xoffset+paneH, Black) ;
00233     for (i = 0 ; i < 10 ; i++ ) {
00234         y = i * 8 ;
00235         TFT.line(paneX[0], xoffset + y, paneX[1], xoffset + y, Blue) ;
00236         TFT.line(paneX[0], yoffset + y, paneX[1], yoffset + y, Blue) ;
00237         TFT.line(paneX[0], zoffset + y, paneX[1], zoffset + y, Blue) ;
00238     }
00239     for (x = 30 ; x < paneX[1] ; x += 10 ) {
00240         TFT.line(x, xoffset, x, xoffset+paneH, Blue) ;
00241         TFT.line(x, yoffset, x, yoffset+paneH, Blue) ;
00242         TFT.line(x, zoffset, x, zoffset+paneH, Blue) ;
00243     } 
00244     TFT.rect(paneX[0], xoffset, paneX[1], xoffset+paneH, White) ;
00245     TFT.rect(paneX[0], yoffset, paneX[1], yoffset+paneH, White) ;
00246     TFT.rect(paneX[0], zoffset, paneX[1], zoffset+paneH, White) ;
00247     TFT.set_font((unsigned char*) Arial12x12);
00248     TFT.locate(5, xoffset+30) ;
00249     TFT.printf("X") ;
00250     TFT.locate(5, yoffset+30) ;
00251     TFT.printf("Y") ;
00252     TFT.locate(5, zoffset+30) ;
00253     TFT.printf("Z") ;
00254     TFT.locate(50, 10) ;
00255     TFT.printf("Xtrinsic Accelerometer") ;
00256     TFT.locate(90, 300) ;
00257     TFT.printf("MMA8451Q") ;
00258     
00259     prev[0] = xoffset + (signalHeight * clip((1.0 + acc->getAccX()))) ;
00260     prev[1] = yoffset + (signalHeight * clip((1.0 + acc->getAccY()))) ;
00261     prev[2] = zoffset + (signalHeight * clip((1.0 + acc->getAccZ()))) ;
00262     pt = paneX[0] ;
00263     backlight = 1 ;
00264     for(t = 21 ; t < paneX[1] ; t++) {
00265         data[0] = xoffset + (signalHeight * clip((1.0 + acc->getAccX()))) ;
00266         data[1] = yoffset + (signalHeight * clip((1.0 + acc->getAccY()))) ;
00267         data[2] = zoffset + (signalHeight * clip((1.0 + acc->getAccZ()))) ;
00268         TFT.line(pt, prev[0], t, data[0], Red) ;
00269         TFT.line(pt, prev[1], t, data[1], Green) ;
00270         TFT.line(pt, prev[2], t, data[2], Yellow) ;
00271         prev[0] = data[0] ;
00272         prev[1] = data[1] ;
00273         prev[2] = data[2] ;
00274         pt = t ;
00275         wait(0.1) ;
00276     }
00277 }
00278 
00279 void incPage(void)
00280 {
00281     page++ ;
00282     if (page >= numPage) {
00283         page = 0 ;
00284     }
00285 }
00286 
00287 void decPage(void) 
00288 {
00289     page-- ;
00290     if (page < 0) {
00291         page = numPage - 1 ;
00292     }
00293 }
00294     
00295 int main()
00296 {
00297   //  uint16_t x, y, z ;
00298  //   int prevPage = 0 ;
00299     
00300     acc = new MMA8451Q(PTE25, PTE24, MMA8451_I2C_ADDRESS) ;
00301     
00302     initTFT() ;
00303     
00304     while(1)
00305     {
00306      screen4(); 
00307      wait(10);
00308         }
00309    
00310     
00311  }