Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MMA8451Q SPI_STMPE610 TSI UniGraphic mbed
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 "TSISensor.h" 00025 #include <math.h> 00026 #include "ILI9341.h" 00027 #include "SPI_STMPE610.h" 00028 #include "Arial12x12.h" 00029 #include "Arial24x23.h" 00030 #include "Arial28x28.h" 00031 #include "Arial43x48_numb.h" 00032 00033 00034 #define PIN_XP PTB3 00035 #define PIN_XM PTB1 00036 #define PIN_YP PTB2 00037 #define PIN_YM PTB0 00038 #define PIN_MOSI PTD2 00039 #define PIN_MISO PTD3 00040 #define PIN_SCLK PTD1 00041 #define PIN_CS_TFT PTD0 00042 #define PIN_DC_TFT PTD5 00043 #define PIN_BL_TFT PTC9 00044 #define PIN_CS_SD PTA4 00045 #define PIN_CS_TSC PTA13 00046 #define PIN_TSC_INTR PTC9 00047 #define PIN_RESET_TFT PTB10 00048 00049 #define MMA8451_I2C_ADDRESS (0x1d<<1) 00050 MMA8451Q *acc = 0 ; 00051 TSISensor tsi; 00052 00053 DigitalOut backlight(PTA12) ; 00054 ILI9341 TFT(SPI_8, 10000000, 00055 PIN_MOSI, PIN_MISO, PIN_SCLK, 00056 PIN_CS_TFT, PIN_RESET_TFT, PIN_DC_TFT, "Adafruit2.8") ; 00057 SPI_STMPE610 TSC(PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TSC) ; 00058 00059 DigitalIn pinD7(PTC9) ; 00060 00061 int page = 0 ; 00062 int numPage = 5 ; 00063 00064 extern void doMaze(void) ; 00065 00066 void initTFT(void) 00067 { 00068 //Configure the display driver 00069 TFT.BusEnable(true) ; 00070 TFT.FastWindow(true) ; 00071 TFT.background(Black); 00072 TFT.foreground(White); 00073 wait(0.01) ; 00074 TFT.cls(); 00075 TFT.BusEnable(false) ; 00076 } 00077 00078 void screen1(void) // Welcome Screen 00079 { 00080 TFT.BusEnable(true) ; 00081 backlight = 0 ; 00082 TFT.background(White) ; 00083 wait(0.1) ; 00084 TFT.cls() ; 00085 wait(0.1) ; 00086 00087 TFT.set_font((unsigned char*) Arial24x23); 00088 TFT.foreground(Red) ; 00089 TFT.locate(80, 40) ; 00090 TFT.printf("MBED") ; 00091 TFT.foreground(Blue); 00092 TFT.locate(60, 80) ; 00093 TFT.printf("2.8\"TFT") ; 00094 TFT.locate(40, 120) ; 00095 TFT.printf("with touch") ; 00096 TFT.foreground(Black); 00097 TFT.set_font((unsigned char*) Arial12x12); 00098 TFT.foreground(Blue) ; 00099 TFT.locate(30, 180) ; 00100 TFT.printf("This program is running on") ; 00101 TFT.locate(30, 200) ; 00102 TFT.printf("freescale FRDM-KL25Z with") ; 00103 TFT.locate(30, 220) ; 00104 TFT.printf("a program developed in mbed") ; 00105 TFT.foreground(Green) ; 00106 TFT.locate(30, 260) ; 00107 TFT.printf("To advance demo page, touch") ; 00108 TFT.locate(30, 280) ; 00109 TFT.printf("and hold right side of screen") ; 00110 TFT.locate(30, 300) ; 00111 TFT.printf("until the next screen starts") ; 00112 TFT.BusEnable(false) ; 00113 backlight = 1 ; 00114 } 00115 00116 void screen2(void) // Graphics 00117 { 00118 //Draw some graphics 00119 int i, x[2], y[2] ; 00120 backlight = 0 ; 00121 TFT.BusEnable(true) ; 00122 TFT.background(Black); 00123 wait(0.1) ; 00124 TFT.foreground(White); 00125 wait(0.1) ; 00126 TFT.cls() ; 00127 wait(0.1) ; 00128 TFT.set_font((unsigned char*) Arial12x12); 00129 TFT.locate(90,0); 00130 TFT.printf("Graphics"); 00131 00132 x[0] = 25 ; x[1] = 224 ; 00133 y[0] = 20 ; y[1] = 219 ; 00134 for (i = 20 ; i < 220 ; i += 10) { 00135 TFT.line(i+5, y[0], i+5, y[1], Blue) ; 00136 TFT.line(x[0], i, x[1], i, Blue) ; 00137 } 00138 TFT.line(125, y[0], 125, y[1], Green) ; 00139 TFT.line(x[0], 120, x[1], 120, Green) ; 00140 TFT.rect(x[0],y[0], x[1], y[1], Green) ; 00141 TFT.locate(10, 20) ; 00142 TFT.printf("V") ; 00143 TFT.locate(0, 115) ; 00144 TFT.printf("0.0") ; 00145 TFT.locate(115, 225) ; 00146 TFT.printf("0.0") ; 00147 TFT.locate(215, 225) ; 00148 TFT.printf("T") ; 00149 00150 double s; 00151 for (int i = x[0]; i < 225; i++) { 00152 s = 40 * sin((long double)i / 20); 00153 TFT.pixel(i, 120 + (int)s, White); 00154 } 00155 00156 TFT.fillrect(10, 240, 229, 309, White) ; 00157 TFT.rect(10, 240, 229, 309, Red) ; 00158 TFT.rect(11, 241, 228, 308, Red) ; 00159 00160 TFT.background(White) ; 00161 TFT.foreground(Black) ; 00162 TFT.locate(20, 250) ; 00163 TFT.printf("With QVGA resolution") ; 00164 TFT.locate(20, 270) ; 00165 TFT.printf("simple graphics drawing") ; 00166 TFT.locate(20, 290) ; 00167 TFT.printf("capability is provided") ; 00168 TFT.BusEnable(false) ; 00169 backlight = 1 ; 00170 } 00171 00172 double clip(double src) 00173 { 00174 double value ; 00175 value = src ; 00176 if (value < 0.0) { 00177 value = 0.0 ; 00178 } else if (value > 2.0) { 00179 value = 2.0 ; 00180 } 00181 return( value ) ; 00182 } 00183 00184 void screen3(void) 00185 { 00186 int t = 0 ; 00187 int pt = 0 ; // previous t 00188 int i, x, y ; 00189 unsigned int data[3] ; // for x, y, z 00190 unsigned int prev[3] ; 00191 unsigned short signalHeight = 39 ; 00192 unsigned short xoffset = 30 ; 00193 unsigned short yoffset = 120 ; 00194 unsigned short zoffset = 210 ; 00195 unsigned short paneX[2] = {20, 235} ; 00196 unsigned short paneH = 81 ; 00197 00198 backlight = 1 ; 00199 TFT.BusEnable(true) ; 00200 TFT.background(Black) ; 00201 TFT.foreground(White) ; 00202 // TFT.cls() ; 00203 00204 00205 TFT.fillrect(paneX[0], xoffset, paneX[1], xoffset+paneH, Black) ; 00206 TFT.fillrect(paneX[0], yoffset, paneX[1], yoffset+paneH, Black) ; 00207 TFT.fillrect(paneX[0], zoffset, paneX[1], zoffset+paneH, Black) ; 00208 TFT.fillrect(paneX[0], xoffset, paneX[1], xoffset+paneH, Black) ; 00209 for (i = 0 ; i < 10 ; i++ ) { 00210 y = i * 8 ; 00211 TFT.line(paneX[0], xoffset + y, paneX[1], xoffset + y, Blue) ; 00212 TFT.line(paneX[0], yoffset + y, paneX[1], yoffset + y, Blue) ; 00213 TFT.line(paneX[0], zoffset + y, paneX[1], zoffset + y, Blue) ; 00214 } 00215 for (x = 30 ; x < paneX[1] ; x += 10 ) { 00216 TFT.line(x, xoffset, x, xoffset+paneH, Blue) ; 00217 TFT.line(x, yoffset, x, yoffset+paneH, Blue) ; 00218 TFT.line(x, zoffset, x, zoffset+paneH, Blue) ; 00219 } 00220 TFT.rect(paneX[0], xoffset, paneX[1], xoffset+paneH, White) ; 00221 TFT.rect(paneX[0], yoffset, paneX[1], yoffset+paneH, White) ; 00222 TFT.rect(paneX[0], zoffset, paneX[1], zoffset+paneH, White) ; 00223 TFT.set_font((unsigned char*) Arial12x12); 00224 TFT.locate(5, xoffset+30) ; 00225 TFT.printf("X") ; 00226 TFT.locate(5, yoffset+30) ; 00227 TFT.printf("Y") ; 00228 TFT.locate(5, zoffset+30) ; 00229 TFT.printf("Z") ; 00230 TFT.locate(50, 10) ; 00231 TFT.printf("Xtrinsic Accelerometer") ; 00232 TFT.locate(90, 300) ; 00233 TFT.printf("MMA8451Q") ; 00234 00235 prev[0] = xoffset + (signalHeight * clip((1.0 + acc->getAccX()))) ; 00236 prev[1] = yoffset + (signalHeight * clip((1.0 + acc->getAccY()))) ; 00237 prev[2] = zoffset + (signalHeight * clip((1.0 + acc->getAccZ()))) ; 00238 pt = paneX[0] ; 00239 backlight = 1 ; 00240 for(t = 21 ; t < paneX[1] ; t++) { 00241 data[0] = xoffset + (signalHeight * clip((1.0 + acc->getAccX()))) ; 00242 data[1] = yoffset + (signalHeight * clip((1.0 + acc->getAccY()))) ; 00243 data[2] = zoffset + (signalHeight * clip((1.0 + acc->getAccZ()))) ; 00244 TFT.line(pt, prev[0], t, data[0], Red) ; 00245 TFT.line(pt, prev[1], t, data[1], Green) ; 00246 TFT.line(pt, prev[2], t, data[2], Yellow) ; 00247 prev[0] = data[0] ; 00248 prev[1] = data[1] ; 00249 prev[2] = data[2] ; 00250 pt = t ; 00251 wait(0.01) ; 00252 } 00253 TFT.BusEnable(false) ; 00254 } 00255 00256 void screen4(void) 00257 { 00258 int dx, px ; 00259 float delta = 0.0 ; 00260 dx = 0 ; 00261 px = 0 ; 00262 backlight = 0 ; 00263 TFT.BusEnable(true) ; 00264 TFT.background(Black); 00265 wait(0.1) ; 00266 TFT.foreground(White); 00267 wait(0.1) ; 00268 TFT.cls() ; 00269 wait(0.1) ; 00270 00271 TFT.set_font((unsigned char*) Arial12x12); 00272 TFT.foreground(Blue) ; 00273 TFT.locate(60, 10) ; 00274 TFT.printf("<< TSI demo >>") ; 00275 TFT.locate(30, 280) ; 00276 TFT.printf("Use FRDM touch slider") ; 00277 TFT.locate(30, 300) ; 00278 TFT.printf("Touch right edge to end") ; 00279 00280 TFT.fillcircle(120, 160, 100, Green) ; 00281 TFT.fillcircle(60, 160, 50, Black) ; 00282 TFT.fillcircle(60, 160, 45, White) ; 00283 TFT.fillcircle(180, 160, 50, Black) ; 00284 TFT.fillcircle(180, 160, 45, White) ; 00285 TFT.fillcircle(60, 160, 5, Black) ; 00286 TFT.fillcircle(180, 160, 5, Black) ; 00287 backlight = 1 ; 00288 00289 while(dx < 38) { 00290 delta = (80.0 * (tsi.readPercentage()-0.5)) ; 00291 dx = (int)(delta + 0.5) ; 00292 TFT.fillcircle(60+px, 160, 5, White) ; 00293 TFT.fillcircle(180+px, 160, 5, White) ; 00294 TFT.fillcircle(60+dx, 160, 5, Black) ; 00295 TFT.fillcircle(180+dx, 160, 5, Black) ; 00296 px = dx ; 00297 wait(0.1) ; 00298 } 00299 TFT.fillcircle(60+px, 160, 5, White) ; 00300 TFT.fillcircle(180+px, 160, 5, White) ; 00301 TFT.line(15, 160, 105, 160, Black) ; 00302 TFT.line(135, 160, 225, 160, Black) ; 00303 TFT.foreground(Yellow) ; 00304 TFT.locate(30, 300) ; 00305 // TFT.printf("Use FRDM touch slider") ; 00306 TFT.printf(" Wake Up! ") ; 00307 TFT.locate(5, 300) ; 00308 TFT.printf("<< Prev") ; 00309 TFT.locate(180, 300) ; 00310 TFT.printf("Next >>") ; 00311 TFT.BusEnable(false) ; 00312 } 00313 00314 void incPage(void) 00315 { 00316 page++ ; 00317 if (page >= numPage) { 00318 page = 0 ; 00319 } 00320 } 00321 00322 void decPage(void) 00323 { 00324 page-- ; 00325 if (page < 0) { 00326 page = numPage - 1 ; 00327 } 00328 } 00329 00330 int main() 00331 { 00332 uint16_t x, y, z ; 00333 int prevPage = 99 ; 00334 bool waitTouch = false ; 00335 00336 acc = new MMA8451Q(PTE25, PTE24, MMA8451_I2C_ADDRESS) ; 00337 00338 initTFT() ; 00339 00340 // screen0() ; 00341 00342 printf("Program Started!\n\r") ; 00343 00344 for(;;) { 00345 // printf("TFT width = %d, height = %d\n\r", TFT.width(), TFT.height()) ; 00346 switch(page) { 00347 case 0: 00348 if (prevPage != page) { 00349 screen1() ; 00350 } 00351 waitTouch = true ; 00352 break ; 00353 case 1: 00354 if (prevPage != page) { 00355 screen2() ; 00356 } 00357 waitTouch = true ; 00358 break ; 00359 case 2: 00360 if (prevPage != page) { 00361 TFT.BusEnable(true) ; 00362 TFT.background(Black) ; 00363 TFT.foreground(White) ; 00364 TFT.cls() ; 00365 TFT.BusEnable(false) ; 00366 } 00367 screen3() ; 00368 waitTouch = false ; 00369 break ; 00370 case 3: 00371 // if (prevPage != page) { 00372 screen4() ; 00373 waitTouch = true ; 00374 // } 00375 break ; 00376 case 4: 00377 // if (prevPage != page) { 00378 doMaze() ; 00379 waitTouch = true ; 00380 // } 00381 break ; 00382 default: 00383 page = 0 ; 00384 break ; 00385 } 00386 prevPage = page ; 00387 00388 do { 00389 TSC.getRAWPoint(&x, &y, &z) ; 00390 if ((x != 0)||(y != 0) || (z != 0)) { 00391 if (x < 1000) { // left 00392 decPage() ; 00393 } else if (x > 3000) { // right 00394 incPage() ; 00395 } 00396 waitTouch = false ; 00397 } 00398 } while(waitTouch != false) ; 00399 // wait(1) ; 00400 } 00401 }
Generated on Wed Jul 13 2022 05:26:59 by
1.7.2