Motoo Tanaka / Mbed 2 deprecated TFT_test_frdm-k64f

Dependencies:   MMA8451Q SPI_STMPE610 SPI_TFT_ILI9341 TFT_fonts mbed

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   * @note Although both program share same ILI9341 TFT driver,
00016   * @note the touch sensor was not same with the Display I purchased from Akizuki.
00017   * @note http://akizukidenshi.com/catalog/g/gM-07747/
00018   * @note The touch sensor on the display is STMPE610,
00019   * @note so I hacked the minimum spi driver for it (polling mode only).
00020   */
00021 /**
00022  * @note To make this work with FRDM-K64F
00023  * @note PTA0 must be disconnected from the swd clk by cutting J11.
00024  * @note But to re-active SWD you need to put jumper header to J11
00025  * @note so that it can be re-connected by a jumper.
00026  */
00027 
00028 #include "mbed.h"
00029 #include "MMA8451Q.h"
00030 #include <math.h>
00031 #include "SPI_TFT_ILI9341.h"
00032 #include "SPI_STMPE610.h"
00033 #include "Arial12x12.h"
00034 #include "Arial24x23.h"
00035 #include "Arial28x28.h"
00036 #include "font_big.h"
00037 
00038 #if 0 
00039 /* 
00040 // For FRDM-KL25Z
00041 #define PIN_XP          PTB3
00042 #define PIN_XM          PTB1
00043 #define PIN_YP          PTB2
00044 #define PIN_YM          PTB0
00045 #define PIN_MOSI        PTD2
00046 #define PIN_MISO        PTD3 
00047 #define PIN_SCLK        PTD1 
00048 #define PIN_CS_TFT      PTD0 
00049 #define PIN_DC_TFT      PTD5 
00050 #define PIN_BL_TFT      PTC9 
00051 #define PIN_CS_SD       PTA4 
00052 #define PIN_CS_TSC      PTA13
00053 #define PIN_TSC_INTR    PTC9
00054 #define PIN_BACKLIGHT   PTA12
00055 */
00056 #endif
00057 
00058 #if 1
00059 // For FRDM-K64F
00060 #define PIN_XP          PTB11
00061 #define PIN_XM          PTB3
00062 #define PIN_YP          PTB10
00063 #define PIN_YM          PTB2
00064 #define PIN_MOSI        PTD2
00065 #define PIN_MISO        PTD3 
00066 #define PIN_SCLK        PTD1 
00067 #define PIN_CS_TFT      PTD0 
00068 #define PIN_DC_TFT      PTC4 
00069 #define PIN_BL_TFT      PTC3 
00070 #define PIN_CS_SD       PTB23 
00071 // for board rev E or later
00072 #define PIN_CS_TSC      PTC12
00073 // for earlier boards use following line
00074 // #define PIN_CS_TSC      PTA0
00075 #define PIN_TSC_INTR    PTC3
00076 #define PIN_BACKLIGHT   PTA1
00077 #endif
00078 
00079 
00080 #define MMA8451_I2C_ADDRESS (0x1d<<1)
00081 MMA8451Q *acc = 0 ;
00082 
00083 // SeeedStudioTFTv2 TFT(PIN_XP, PIN_XM, PIN_YP, PIN_YM, PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TFT, PIN_DC_TFT, PIN_BL_TFT, PIN_CS_SD);
00084 //  SPI_TFT_ILI9341(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset, PinName dc, const char* name ="TFT");
00085 SPI_TFT_ILI9341 TFT(PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TFT, PIN_BL_TFT, PIN_DC_TFT) ;
00086 SPI_STMPE610 TSC(PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TSC) ;
00087 
00088 DigitalOut backlight(PIN_BACKLIGHT) ;
00089 DigitalIn pinD7(PIN_TSC_INTR) ;
00090 
00091 int page = 0 ;
00092 int numPage = 3 ;
00093 
00094 void initTFT(void)
00095 {
00096     //Configure the display driver
00097     TFT.background(Black);
00098     TFT.foreground(White);
00099     wait(0.01) ;
00100     TFT.cls();
00101 }
00102 
00103 void screen1(void) // Welcome Screen
00104 {
00105     backlight = 0 ;
00106     TFT.background(White) ;
00107     TFT.cls() ;
00108 
00109     wait(0.1) ;
00110     TFT.set_font((unsigned char*) Arial24x23);
00111     TFT.foreground(Red) ;
00112     TFT.locate(80, 40) ;
00113     TFT.printf("mbed") ;
00114     TFT.foreground(Blue);
00115     TFT.locate(60, 80) ;
00116     TFT.printf("TFT 2.8\"") ; 
00117     TFT.locate(40, 120) ;
00118     TFT.printf("with touch") ;
00119     TFT.foreground(Black);
00120     TFT.set_font((unsigned char*) Arial12x12);
00121     TFT.foreground(Blue) ;
00122     TFT.locate(10, 180) ;
00123     TFT.printf("This program is running on") ;
00124     TFT.locate(10, 200) ;
00125     TFT.printf("freescale FRDM-K64F with") ;
00126     TFT.locate(10, 220) ;
00127     TFT.printf("a program developed on mbed") ;
00128     TFT.foreground(Green) ;
00129     TFT.locate(10, 260) ;
00130     TFT.printf("To advance demo page, touch") ;
00131     TFT.locate(10, 280) ;
00132     TFT.printf("and hold right side of screen") ;
00133     TFT.locate(10, 300) ;
00134     TFT.printf("until the next screen starts") ;
00135     backlight = 1 ;
00136 }
00137 
00138 void screen2(void) // Graphics
00139 {
00140     //Draw some graphics
00141     int i, x[2], y[2] ;
00142     backlight = 0 ;
00143 
00144     TFT.background(Black);
00145     TFT.foreground(White);
00146     TFT.cls() ;
00147     TFT.set_font((unsigned char*) Arial12x12);
00148     TFT.locate(90,0);
00149     TFT.printf("Graphics");
00150     
00151     x[0] = 25 ; x[1] = 224 ;
00152     y[0] = 20 ; y[1] = 219 ;
00153     for (i = 20 ; i < 220 ; i += 10) {
00154         TFT.line(i+5, y[0], i+5, y[1], Blue) ;
00155         TFT.line(x[0], i, x[1], i, Blue) ;
00156     }
00157     TFT.line(125, y[0], 125, y[1], Green) ;
00158     TFT.line(x[0], 120, x[1], 120, Green) ;
00159     TFT.rect(x[0],y[0], x[1], y[1], Green) ;
00160     TFT.locate(10, 20) ;
00161     TFT.printf("V") ;
00162     TFT.locate(0, 115) ;
00163     TFT.printf("0.0") ;
00164     TFT.locate(115, 225) ;
00165     TFT.printf("0.0") ;
00166     TFT.locate(215, 225) ;
00167     TFT.printf("T") ;
00168 
00169     double s;
00170     for (int i = x[0]; i < 225; i++) {
00171         s = 40 * sin((long double)i / 20);
00172         TFT.pixel(i, 120 + (int)s, White);
00173     }
00174     
00175     TFT.fillrect(10, 240, 229, 309, White) ;
00176     TFT.rect(10, 240, 229, 309, Red) ;
00177     TFT.rect(11, 241, 228, 308, Red) ;
00178     
00179     TFT.background(White) ;
00180     TFT.foreground(Black) ;
00181     TFT.locate(20, 250) ;
00182     TFT.printf("With QVGA resolution") ;
00183     TFT.locate(20, 270) ;
00184     TFT.printf("simple graphics drawing") ;
00185     TFT.locate(20, 290) ;
00186     TFT.printf("capability is provided") ;
00187     backlight = 1 ;
00188 }    
00189 
00190 double clip(double src)
00191 {
00192     double value ;
00193     value = src ;
00194     if (value < 0.0) {
00195         value = 0.0 ;
00196     } else if (value > 2.0) {
00197         value = 2.0 ;
00198     }
00199     return( value ) ;
00200 }
00201 
00202 void screen3(void)
00203 {
00204     int t = 0 ;
00205     int pt = 0 ; // previous t 
00206     int i, x, y ;
00207     unsigned int data[3] ; // for x, y, z 
00208     unsigned int prev[3] ;
00209     unsigned short signalHeight = 39 ;
00210     unsigned short xoffset = 30 ;
00211     unsigned short yoffset = 120 ;
00212     unsigned short zoffset = 210 ;
00213     unsigned short paneX[2] = {20, 235} ;
00214     unsigned short paneH = 81 ;
00215  
00216     backlight = 0 ;
00217     TFT.background(Black) ;
00218     TFT.foreground(White) ;
00219 //    TFT.cls() ;
00220 
00221  
00222     TFT.fillrect(paneX[0], xoffset, paneX[1], xoffset+paneH, Black) ;
00223     TFT.fillrect(paneX[0], yoffset, paneX[1], yoffset+paneH, Black) ;
00224     TFT.fillrect(paneX[0], zoffset, paneX[1], zoffset+paneH, Black) ;
00225     TFT.fillrect(paneX[0], xoffset, paneX[1], xoffset+paneH, Black) ;
00226     for (i = 0 ; i < 10 ; i++ ) {
00227         y = i * 8 ;
00228         TFT.line(paneX[0], xoffset + y, paneX[1], xoffset + y, Blue) ;
00229         TFT.line(paneX[0], yoffset + y, paneX[1], yoffset + y, Blue) ;
00230         TFT.line(paneX[0], zoffset + y, paneX[1], zoffset + y, Blue) ;
00231     }
00232     for (x = 30 ; x < paneX[1] ; x += 10 ) {
00233         TFT.line(x, xoffset, x, xoffset+paneH, Blue) ;
00234         TFT.line(x, yoffset, x, yoffset+paneH, Blue) ;
00235         TFT.line(x, zoffset, x, zoffset+paneH, Blue) ;
00236     } 
00237     TFT.rect(paneX[0], xoffset, paneX[1], xoffset+paneH, White) ;
00238     TFT.rect(paneX[0], yoffset, paneX[1], yoffset+paneH, White) ;
00239     TFT.rect(paneX[0], zoffset, paneX[1], zoffset+paneH, White) ;
00240     TFT.set_font((unsigned char*) Arial12x12);
00241     TFT.locate(5, xoffset+30) ;
00242     TFT.printf("X") ;
00243     TFT.locate(5, yoffset+30) ;
00244     TFT.printf("Y") ;
00245     TFT.locate(5, zoffset+30) ;
00246     TFT.printf("Z") ;
00247     TFT.locate(50, 10) ;
00248     TFT.printf("Xtrinsic Accelerometer") ;
00249     TFT.locate(90, 300) ;
00250     TFT.printf("MMA8451Q") ;
00251     
00252     prev[0] = xoffset + (signalHeight * clip((1.0 + acc->getAccX()))) ;
00253     prev[1] = yoffset + (signalHeight * clip((1.0 + acc->getAccY()))) ;
00254     prev[2] = zoffset + (signalHeight * clip((1.0 + acc->getAccZ()))) ;
00255     pt = paneX[0] ;
00256     backlight = 1 ;
00257     for(t = 21 ; t < paneX[1] ; t++) {
00258         data[0] = xoffset + (signalHeight * clip((1.0 + acc->getAccX()))) ;
00259         data[1] = yoffset + (signalHeight * clip((1.0 + acc->getAccY()))) ;
00260         data[2] = zoffset + (signalHeight * clip((1.0 + acc->getAccZ()))) ;
00261         TFT.line(pt, prev[0], t, data[0], Red) ;
00262         TFT.line(pt, prev[1], t, data[1], Green) ;
00263         TFT.line(pt, prev[2], t, data[2], Yellow) ;
00264         prev[0] = data[0] ;
00265         prev[1] = data[1] ;
00266         prev[2] = data[2] ;
00267         pt = t ;
00268         wait(0.01) ;
00269     }
00270 }
00271 
00272 void incPage(void)
00273 {
00274     page++ ;
00275     if (page >= numPage) {
00276         page = 0 ;
00277     }
00278 }
00279 
00280 void decPage(void) 
00281 {
00282     page-- ;
00283     if (page < 0) {
00284         page = numPage - 1 ;
00285     }
00286 }
00287     
00288 int main()
00289 {
00290     uint16_t x, y, z ;
00291     int prevPage = 0 ;
00292     int touched = 0 ;
00293     
00294  
00295     
00296     initTFT() ;
00297     
00298     screen1() ;
00299         
00300     printf("Program Started!\n\r") ;
00301     
00302     acc = new MMA8451Q(PTE25, PTE24, MMA8451_I2C_ADDRESS) ;
00303     
00304     for(;;) {
00305 //        printf("TFT width = %d, height = %d\n\r", TFT.width(), TFT.height()) ;
00306         switch(page) {
00307         case 0:
00308             if (prevPage != page) {
00309                 screen1() ;
00310             }
00311             wait(1) ;
00312             break ;
00313         case 1:
00314             if (prevPage != page) {
00315                 screen2() ; 
00316             }
00317             wait(1) ;
00318             break ;
00319         case 2:
00320             if (prevPage != page) {
00321                 TFT.background(Black) ;
00322                 TFT.foreground(White) ;
00323                 TFT.cls() ;
00324             }
00325             screen3() ; 
00326             wait(2) ;
00327             break ;
00328         default:
00329             page = 0 ; 
00330             break ;
00331         }
00332         prevPage = page ;
00333         printf("Screen Printed\n\r") ;
00334 
00335         x = 0 ; y = 0 ; z = 0 ;
00336         touched = TSC.getRAWPoint(&x, &y, &z) ;
00337         printf("touched = %d x = %d, y = %d, z = %d\n\r",touched, x,y,z) ;
00338         if ((x != 0)||(y != 0) || (z != 0)) {
00339             if (x < 1000) { // left
00340                 decPage() ;
00341             } else if (x > 3000) { // right
00342                 incPage() ;
00343             }
00344         }
00345 //        wait(1) ;
00346     }
00347 }