Sample TFT to KL25Z program.
Dependencies: SPI_STMPE610 TFT_fonts UniGraphic mbed
Fork of TFT_test_frdm-kl25z by
Revision 5:449dc35a29bd, committed 2018-02-13
- Comitter:
- Sustainable
- Date:
- Tue Feb 13 20:21:30 2018 +0000
- Parent:
- 4:8c3681dc676f
- Commit message:
- Basic TFT to KL25Z board. Shows how to use colors and circles to create buttons. Also displays the x,y data after each touch.
Changed in this revision
--- a/MMA8451Q.lib Sat Aug 08 12:27:57 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/emilmont/code/MMA8451Q/#c4d879a39775
--- a/SPI_STMPE610.lib Sat Aug 08 12:27:57 2015 +0000 +++ b/SPI_STMPE610.lib Tue Feb 13 20:21:30 2018 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/Rhyme/code/SPI_STMPE610/#a012c1710119 +http://mbed.org/users/Rhyme/code/SPI_STMPE610/#09fece599665
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TFT_fonts.lib Tue Feb 13 20:21:30 2018 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/Sustainable/code/TFT_fonts/#ff8951994cec
--- a/TSI.lib Sat Aug 08 12:27:57 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/emilmont/code/TSI/#507b1f67804b
--- a/UniGraphic.lib Sat Aug 08 12:27:57 2015 +0000 +++ b/UniGraphic.lib Tue Feb 13 20:21:30 2018 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/teams/GraphicsDisplay/code/UniGraphic/#1bdfb971b2c1 +http://mbed.org/teams/GraphicsDisplay/code/UniGraphic/#9cd6227dc7a6
--- a/main.cpp Sat Aug 08 12:27:57 2015 +0000
+++ b/main.cpp Tue Feb 13 20:21:30 2018 +0000
@@ -1,401 +1,194 @@
-/* mbed main.cpp to test adafruit 2.8" TFT LCD shiled w Touchscreen
- * Copyright (c) 2014 Motoo Tanaka @ Design Methodology Lab
+/*******************************************************************
+ * TFT Sample program for STEM project
+ * By Henry Foley, Feb 2017
*
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
+ * Hardware: Freedom KL25Z uC development board with a TFT 2.8"
+ * resistive touch display shield from Adafruit (1651).
+ *
*/
-
- /*
- * Note: This program is derived from the SeeeStudioTFTv2 program.
- * Although both program share same ILI9341 TFT driver,
- * the touch sensor was not same with the Display I purchased from Akizuki.
- * http://akizukidenshi.com/catalog/g/gM-07747/
- * The touch sensor on the display is STMPE610,
- * so I hacked the minimum spi driver for it (polling mode only).
- */
#include "mbed.h"
-#include "MMA8451Q.h"
-#include "TSISensor.h"
-#include <math.h>
#include "ILI9341.h"
#include "SPI_STMPE610.h"
#include "Arial12x12.h"
#include "Arial24x23.h"
#include "Arial28x28.h"
-#include "Arial43x48_numb.h"
-
+#include "Verdana22x21-16.h"
#define PIN_XP PTB3
#define PIN_XM PTB1
#define PIN_YP PTB2
#define PIN_YM PTB0
#define PIN_MOSI PTD2
-#define PIN_MISO PTD3
-#define PIN_SCLK PTD1
-#define PIN_CS_TFT PTD0
-#define PIN_DC_TFT PTD5
-#define PIN_BL_TFT PTC9
-#define PIN_CS_SD PTA4
+#define PIN_MISO PTD3
+#define PIN_SCLK PTD1
+#define PIN_CS_TFT PTD0
+#define PIN_DC_TFT PTD5
+#define PIN_BL_TFT PTC9
+#define PIN_CS_SD PTA4
#define PIN_CS_TSC PTA13
-#define PIN_TSC_INTR PTC9
-#define PIN_RESET_TFT PTB10
+#define PIN_RESET_TFT PTB10
-#define MMA8451_I2C_ADDRESS (0x1d<<1)
-MMA8451Q *acc = 0 ;
-TSISensor tsi;
+
DigitalOut backlight(PTA12) ;
-ILI9341 TFT(SPI_8, 10000000,
- PIN_MOSI, PIN_MISO, PIN_SCLK,
- PIN_CS_TFT, PIN_RESET_TFT, PIN_DC_TFT, "Adafruit2.8") ;
+
SPI_STMPE610 TSC(PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TSC) ;
-DigitalIn pinD7(PTC9) ;
+ILI9341 TFT(SPI_8, 10000000,
+ PIN_MOSI, PIN_MISO, PIN_SCLK,
+ PIN_CS_TFT, PIN_RESET_TFT, PIN_DC_TFT, "Adafruit2.8") ;
+
+void initTFT(void);
+void drawScreen(void);
+uint16_t map(uint16_t, uint16_t, uint16_t, uint16_t, uint16_t);
+void display_xy(void);
+
+int main()
+{
+ static uint16_t x, y, z ;
+ uint16_t temp_x;
+ int touched = 0;
+
+ initTFT() ;
-int page = 0 ;
-int numPage = 5 ;
-
-extern void doMaze(void) ;
+ drawScreen();
+
+ while(true)
+ {
+ touched = TSC.getRAWPoint(&x, &y, &z);
+
+ if(touched) // any touch
+ {
+
+ // scale the raw values to match the screen orentation and 320(x = horiz) 240(y = vert) size, 0,0 = upper left corner
+ temp_x = x;
+ x = map(y,180, 3800,0,320);
+ y = map(temp_x,3800,280,0,240);
+ if( (x > 0 && x < 320) && ( y > 0 && y < 240 )) // if valid values
+ {
+ drawScreen();
+
+ TFT.BusEnable(true) ;
+ TFT.background(Blue);
+ wait(0.2);
+ TFT.foreground(White);
+ TFT.locate(120, 160);
+ TFT.printf("x%3d", x);
+ TFT.locate(120, 190);
+ TFT.printf("y%3d", y);
+ TFT.BusEnable(false) ;
+ wait(0.5); // to reduce mutiple hits from one touch
+ }
+
+ }
+ }
+} // end of main()
void initTFT(void)
{
//Configure the display driver
TFT.BusEnable(true) ;
- TFT.FastWindow(true) ;
- TFT.background(Black);
+ TFT.set_orientation(1);
+ backlight = 1 ;
+ TFT.background(Blue);
+ wait(0.2);
TFT.foreground(White);
- wait(0.01) ;
+ wait(0.2);
TFT.cls();
+ wait(0.2);
+ TFT.set_font((unsigned char*) Arial28x28);
+ wait(0.2);
+ TFT.locate(15, 10);
+ TFT.printf(" TFT Test ");
+ TFT.locate(15, 100);
+ TFT.BusEnable(false);
+ wait(5);
+}
+
+
+void drawScreen(void)
+{
+ TFT.BusEnable(true);
+ TFT.foreground(White); // Set default text colors
+ TFT.background(Blue);
+ TFT.cls(); // wipe the screen clean and set bg colors
+ wait(0.1);
+
+ TFT.foreground(Black);
+ TFT.set_font((unsigned char*) Arial24x23);
+ TFT.fillcircle( 60, 175, 50, Yellow);
+ TFT.background(Yellow) ;
+ TFT.locate(25, 160);
+ TFT.printf(" A ");
+
+ TFT.fillcircle( 160, 105, 50, Cyan);
+ TFT.background(Cyan) ;
+ TFT.locate(127, 92);
+ TFT.printf(" B ");
+
+ TFT.fillcircle( 260, 175, 50, Green);
+ TFT.background(Green) ;
+ TFT.locate(222, 160);
+ TFT.printf(" C ");
+
+ TFT.fillcircle( 60, 60, 50, Orange);
+ TFT.background(Orange) ;
+ TFT.locate(15, 50);
+ TFT.printf(" D ");
+
+ TFT.fillcircle( 260, 60, 50, GreenYellow);
+ TFT.background(GreenYellow) ;
+ TFT.locate(225, 50);
+ TFT.printf(" E ");
+
TFT.BusEnable(false) ;
}
-void screen1(void) // Welcome Screen
-{
- TFT.BusEnable(true) ;
- backlight = 0 ;
- TFT.background(White) ;
- wait(0.1) ;
- TFT.cls() ;
- wait(0.1) ;
-
- TFT.set_font((unsigned char*) Arial24x23);
- TFT.foreground(Red) ;
- TFT.locate(80, 40) ;
- TFT.printf("MBED") ;
- TFT.foreground(Blue);
- TFT.locate(60, 80) ;
- TFT.printf("2.8\"TFT") ;
- TFT.locate(40, 120) ;
- TFT.printf("with touch") ;
- TFT.foreground(Black);
- TFT.set_font((unsigned char*) Arial12x12);
- TFT.foreground(Blue) ;
- TFT.locate(30, 180) ;
- TFT.printf("This program is running on") ;
- TFT.locate(30, 200) ;
- TFT.printf("freescale FRDM-KL25Z with") ;
- TFT.locate(30, 220) ;
- TFT.printf("a program developed in mbed") ;
- TFT.foreground(Green) ;
- TFT.locate(30, 260) ;
- TFT.printf("To advance demo page, touch") ;
- TFT.locate(30, 280) ;
- TFT.printf("and hold right side of screen") ;
- TFT.locate(30, 300) ;
- TFT.printf("until the next screen starts") ;
- TFT.BusEnable(false) ;
- backlight = 1 ;
-}
-
-void screen2(void) // Graphics
+// mapping function to change the orentation of the screen to horizontal
+uint16_t map(uint16_t x, uint16_t in_min, uint16_t in_max, uint16_t out_min, uint16_t out_max)
{
- //Draw some graphics
- int i, x[2], y[2] ;
- backlight = 0 ;
- TFT.BusEnable(true) ;
- TFT.background(Black);
- wait(0.1) ;
- TFT.foreground(White);
- wait(0.1) ;
- TFT.cls() ;
- wait(0.1) ;
- TFT.set_font((unsigned char*) Arial12x12);
- TFT.locate(90,0);
- TFT.printf("Graphics");
-
- x[0] = 25 ; x[1] = 224 ;
- y[0] = 20 ; y[1] = 219 ;
- for (i = 20 ; i < 220 ; i += 10) {
- TFT.line(i+5, y[0], i+5, y[1], Blue) ;
- TFT.line(x[0], i, x[1], i, Blue) ;
- }
- TFT.line(125, y[0], 125, y[1], Green) ;
- TFT.line(x[0], 120, x[1], 120, Green) ;
- TFT.rect(x[0],y[0], x[1], y[1], Green) ;
- TFT.locate(10, 20) ;
- TFT.printf("V") ;
- TFT.locate(0, 115) ;
- TFT.printf("0.0") ;
- TFT.locate(115, 225) ;
- TFT.printf("0.0") ;
- TFT.locate(215, 225) ;
- TFT.printf("T") ;
-
- double s;
- for (int i = x[0]; i < 225; i++) {
- s = 40 * sin((long double)i / 20);
- TFT.pixel(i, 120 + (int)s, White);
- }
-
- TFT.fillrect(10, 240, 229, 309, White) ;
- TFT.rect(10, 240, 229, 309, Red) ;
- TFT.rect(11, 241, 228, 308, Red) ;
-
- TFT.background(White) ;
- TFT.foreground(Black) ;
- TFT.locate(20, 250) ;
- TFT.printf("With QVGA resolution") ;
- TFT.locate(20, 270) ;
- TFT.printf("simple graphics drawing") ;
- TFT.locate(20, 290) ;
- TFT.printf("capability is provided") ;
- TFT.BusEnable(false) ;
- backlight = 1 ;
-}
-
-double clip(double src)
-{
- double value ;
- value = src ;
- if (value < 0.0) {
- value = 0.0 ;
- } else if (value > 2.0) {
- value = 2.0 ;
- }
- return( value ) ;
+ return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
-void screen3(void)
+// Code used to debug screen orentation and touch pad
+void display_xy(void)
{
- int t = 0 ;
- int pt = 0 ; // previous t
- int i, x, y ;
- unsigned int data[3] ; // for x, y, z
- unsigned int prev[3] ;
- unsigned short signalHeight = 39 ;
- unsigned short xoffset = 30 ;
- unsigned short yoffset = 120 ;
- unsigned short zoffset = 210 ;
- unsigned short paneX[2] = {20, 235} ;
- unsigned short paneH = 81 ;
-
- backlight = 1 ;
- TFT.BusEnable(true) ;
- TFT.background(Black) ;
- TFT.foreground(White) ;
-// TFT.cls() ;
+ static uint16_t x, y, z ;
+ uint16_t temp_x;
+ int touched = 0;
+
+ while(true) {
+ touched = TSC.getRAWPoint(&x, &y, &z);
+ x = x & 0x7f;
+ y = y & 0x7f;
+
+ if(x) { // any touch
+ wait(0.5);
+ TFT.BusEnable(true);
+ TFT.locate(5, 40);
+ TFT.printf("x= %5d x=%4X", x, x);
+ TFT.locate(5, 80);
+ TFT.printf("y= %5d y=%4X", y, y);
+ // TFT.locate(5, 120);
+ // TFT.printf("raw z%6d z%4X", z, z);
+
+ // scale the raw values to match the screen orentation and 320(x = horiz) 240(y = vert) size
+ // 0,0 = upper left corner
+ temp_x = x;
+ x = map(y,180, 3800,0,320); // TFT.set_orientation(1);
+ y = map(temp_x,3800,280,0,240);
+
+// x = map(y,3800, 180,0,320); // TFT.set_orientation(3);
+// y = map(temp_x,280,3800,0,240);
+
+ TFT.locate(5, 120);
+ TFT.printf("map x%5d y%5d", x, y);
- TFT.fillrect(paneX[0], xoffset, paneX[1], xoffset+paneH, Black) ;
- TFT.fillrect(paneX[0], yoffset, paneX[1], yoffset+paneH, Black) ;
- TFT.fillrect(paneX[0], zoffset, paneX[1], zoffset+paneH, Black) ;
- TFT.fillrect(paneX[0], xoffset, paneX[1], xoffset+paneH, Black) ;
- for (i = 0 ; i < 10 ; i++ ) {
- y = i * 8 ;
- TFT.line(paneX[0], xoffset + y, paneX[1], xoffset + y, Blue) ;
- TFT.line(paneX[0], yoffset + y, paneX[1], yoffset + y, Blue) ;
- TFT.line(paneX[0], zoffset + y, paneX[1], zoffset + y, Blue) ;
- }
- for (x = 30 ; x < paneX[1] ; x += 10 ) {
- TFT.line(x, xoffset, x, xoffset+paneH, Blue) ;
- TFT.line(x, yoffset, x, yoffset+paneH, Blue) ;
- TFT.line(x, zoffset, x, zoffset+paneH, Blue) ;
- }
- TFT.rect(paneX[0], xoffset, paneX[1], xoffset+paneH, White) ;
- TFT.rect(paneX[0], yoffset, paneX[1], yoffset+paneH, White) ;
- TFT.rect(paneX[0], zoffset, paneX[1], zoffset+paneH, White) ;
- TFT.set_font((unsigned char*) Arial12x12);
- TFT.locate(5, xoffset+30) ;
- TFT.printf("X") ;
- TFT.locate(5, yoffset+30) ;
- TFT.printf("Y") ;
- TFT.locate(5, zoffset+30) ;
- TFT.printf("Z") ;
- TFT.locate(50, 10) ;
- TFT.printf("Xtrinsic Accelerometer") ;
- TFT.locate(90, 300) ;
- TFT.printf("MMA8451Q") ;
-
- prev[0] = xoffset + (signalHeight * clip((1.0 + acc->getAccX()))) ;
- prev[1] = yoffset + (signalHeight * clip((1.0 + acc->getAccY()))) ;
- prev[2] = zoffset + (signalHeight * clip((1.0 + acc->getAccZ()))) ;
- pt = paneX[0] ;
- backlight = 1 ;
- for(t = 21 ; t < paneX[1] ; t++) {
- data[0] = xoffset + (signalHeight * clip((1.0 + acc->getAccX()))) ;
- data[1] = yoffset + (signalHeight * clip((1.0 + acc->getAccY()))) ;
- data[2] = zoffset + (signalHeight * clip((1.0 + acc->getAccZ()))) ;
- TFT.line(pt, prev[0], t, data[0], Red) ;
- TFT.line(pt, prev[1], t, data[1], Green) ;
- TFT.line(pt, prev[2], t, data[2], Yellow) ;
- prev[0] = data[0] ;
- prev[1] = data[1] ;
- prev[2] = data[2] ;
- pt = t ;
- wait(0.01) ;
- }
- TFT.BusEnable(false) ;
-}
-
-void screen4(void)
-{
- int dx, px ;
- float delta = 0.0 ;
- dx = 0 ;
- px = 0 ;
- backlight = 0 ;
- TFT.BusEnable(true) ;
- TFT.background(Black);
- wait(0.1) ;
- TFT.foreground(White);
- wait(0.1) ;
- TFT.cls() ;
- wait(0.1) ;
-
- TFT.set_font((unsigned char*) Arial12x12);
- TFT.foreground(Blue) ;
- TFT.locate(60, 10) ;
- TFT.printf("<< TSI demo >>") ;
- TFT.locate(30, 280) ;
- TFT.printf("Use FRDM touch slider") ;
- TFT.locate(30, 300) ;
- TFT.printf("Touch right edge to end") ;
-
- TFT.fillcircle(120, 160, 100, Green) ;
- TFT.fillcircle(60, 160, 50, Black) ;
- TFT.fillcircle(60, 160, 45, White) ;
- TFT.fillcircle(180, 160, 50, Black) ;
- TFT.fillcircle(180, 160, 45, White) ;
- TFT.fillcircle(60, 160, 5, Black) ;
- TFT.fillcircle(180, 160, 5, Black) ;
- backlight = 1 ;
-
- while(dx < 38) {
- delta = (80.0 * (tsi.readPercentage()-0.5)) ;
- dx = (int)(delta + 0.5) ;
- TFT.fillcircle(60+px, 160, 5, White) ;
- TFT.fillcircle(180+px, 160, 5, White) ;
- TFT.fillcircle(60+dx, 160, 5, Black) ;
- TFT.fillcircle(180+dx, 160, 5, Black) ;
- px = dx ;
- wait(0.1) ;
- }
- TFT.fillcircle(60+px, 160, 5, White) ;
- TFT.fillcircle(180+px, 160, 5, White) ;
- TFT.line(15, 160, 105, 160, Black) ;
- TFT.line(135, 160, 225, 160, Black) ;
- TFT.foreground(Yellow) ;
- TFT.locate(30, 300) ;
-// TFT.printf("Use FRDM touch slider") ;
- TFT.printf(" Wake Up! ") ;
- TFT.locate(5, 300) ;
- TFT.printf("<< Prev") ;
- TFT.locate(180, 300) ;
- TFT.printf("Next >>") ;
- TFT.BusEnable(false) ;
-}
-
-void incPage(void)
-{
- page++ ;
- if (page >= numPage) {
- page = 0 ;
+ TFT.BusEnable(false);
+ wait(0.5);
+ }
}
}
-
-void decPage(void)
-{
- page-- ;
- if (page < 0) {
- page = numPage - 1 ;
- }
-}
-
-int main()
-{
- uint16_t x, y, z ;
- int prevPage = 99 ;
- bool waitTouch = false ;
-
- acc = new MMA8451Q(PTE25, PTE24, MMA8451_I2C_ADDRESS) ;
-
- initTFT() ;
-
- // screen0() ;
-
- printf("Program Started!\n\r") ;
-
- for(;;) {
-// printf("TFT width = %d, height = %d\n\r", TFT.width(), TFT.height()) ;
- switch(page) {
- case 0:
- if (prevPage != page) {
- screen1() ;
- }
- waitTouch = true ;
- break ;
- case 1:
- if (prevPage != page) {
- screen2() ;
- }
- waitTouch = true ;
- break ;
- case 2:
- if (prevPage != page) {
- TFT.BusEnable(true) ;
- TFT.background(Black) ;
- TFT.foreground(White) ;
- TFT.cls() ;
- TFT.BusEnable(false) ;
- }
- screen3() ;
- waitTouch = false ;
- break ;
- case 3:
-// if (prevPage != page) {
- screen4() ;
- waitTouch = true ;
-// }
- break ;
- case 4:
-// if (prevPage != page) {
- doMaze() ;
- waitTouch = true ;
-// }
- break ;
- default:
- page = 0 ;
- break ;
- }
- prevPage = page ;
-
- do {
- TSC.getRAWPoint(&x, &y, &z) ;
- if ((x != 0)||(y != 0) || (z != 0)) {
- if (x < 1000) { // left
- decPage() ;
- } else if (x > 3000) { // right
- incPage() ;
- }
- waitTouch = false ;
- }
- } while(waitTouch != false) ;
-// wait(1) ;
- }
-}
+
\ No newline at end of file
--- a/maze.cpp Sat Aug 08 12:27:57 2015 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,255 +0,0 @@
-/** maze_vt100 a simple maze using UniGraphic and MMA8451Q libraries
- */
-#include "mbed.h"
-#include "MMA8451Q.h"
-#include <ILI9341.h>
-#include "Arial12x12.h"
-#include "Arial24x23.h"
-#include "Arial28x28.h"
-#include "Arial43x48_numb.h"
-#include "maze.h"
-
-#define CELL_W 10
-#define CELL_H 10
-#define CELL_S 5
-#define CELL_R 4
-#define LEFT_OFFSET 20
-#define TOP_OFFSET 20
-
-#define DIR_STAY 0
-#define DIR_UP 1
-#define DIR_DOWN 2
-#define DIR_RIGHT 3
-#define DIR_LEFT 4
-
-#define MMA8451_I2C_ADDRESS (0x1D<<1)
-
-typedef struct _pos {
- int x ;
- int y ;
-} pos_type ;
-
-extern MMA8451Q *acc ;
-extern ILI9341 TFT ;
-extern DigitalOut backlight ;
-float threshold = 0.2 ;
-
-/** Check if two pos_type values are same
- * @param pos_type a
- * @param pos_type b
- * @returns if a and b are same position
- */
-bool isSame(pos_type a, pos_type b)
-{
- return((a.x == b.x)&&(a.y == b.y)) ;
-}
-
-/** Draw the maze defined in the "maze.h"
- * @param pos_type *current actually the start point
- * @param pos_type *goal the position of the goal
- * @note those params are actually returned by this function
- */
-void drawTFTMaze(pos_type *current, pos_type *goal)
-{
- int x, y, i, j ;
- TFT.BusEnable(true) ;
-
- backlight = 0 ;
- TFT.foreground(White) ;
- TFT.background(Black) ;
- TFT.cls() ;
- for (j = 0 ; j < MAZE_H ; j++ ) {
- for (i = 0 ; i < MAZE_W ; i++ ) {
- x = i * CELL_W + LEFT_OFFSET ;
- y = j * CELL_H + TOP_OFFSET ;
- switch(maze[j][i]) {
- case 0: // path
- TFT.fillrect(x, y, x+CELL_W-1, y+CELL_H-1, Black) ;
- break ;
- case 1: // wall
- TFT.fillrect(x, y, x+CELL_W-1, y+CELL_H-1, DarkGrey) ;
- break ;
- case 2: // Start point
- TFT.fillcircle(x+CELL_S, y+CELL_S, CELL_R, Green) ;
- current->x = i ;
- current->y = j ;
- break ;
- case 3: // Goal
- TFT.fillcircle(x+CELL_S, y+CELL_S, CELL_R, Red) ;
- goal->x = i ;
- goal->y = j ;
- break ;
- default: // should not be here
- break ;
- }
- }
- }
- backlight = 1 ;
- TFT.BusEnable(false) ;
-}
-
-/** Filter out too little move
- * @param float in returned value from the acc
- * @returns float result filtered value of in
- */
-float filterVal(float in)
-{
- float result = 0.0 ;
- if ((-threshold > in)||(in > threshold)) {
- result = in ;
- }
- return( result ) ;
-}
-
-/** Decide which direction to go
- * @param float res[] acc value of x, y
- * @returns int direction to move
- */
-int getDirection(float res[])
-{
- float dx, dy ;
- int direction = DIR_STAY ;
- dx = filterVal(res[0]) ;
- dy = filterVal(res[1]) ;
-
- if ((dx*dx) > (dy*dy)) { // holizontal move
- if (dx > 0.0) {
- direction = DIR_DOWN ;
- } else if (dx < 0.0) {
- direction = DIR_UP ;
- }
- } else { // vertical move
- if (dy > 0.0) {
- direction = DIR_RIGHT ;
- } else if (dy < 0.0) {
- direction = DIR_LEFT ;
- }
- }
- return(direction) ;
-}
-
-/** Get next positon to move to
- * @param pos_type current where we are now
- * @param int direction which way we'd like to move
- * @returns the candidate positon for the next move
- */
-pos_type getNext(pos_type current, int direction)
-{
- pos_type next = current ;
- switch(direction) {
- case DIR_STAY:
- break ;
- case DIR_UP:
- if (next.y > 0) {
- next.y-- ;
- }
- break ;
- case DIR_DOWN:
- if (next.y < (MAZE_H - 1)) {
- next.y++ ;
- }
- break ;
- case DIR_RIGHT:
- if (next.x < (MAZE_W - 1)) {
- next.x++ ;
- }
- break ;
- case DIR_LEFT:
- if (next.x > 0) {
- next.x-- ;
- }
- break ;
- default:
- break ;
- }
- return( next ) ;
-}
-
-/** Notice of the goal
- */
-void showGoal(void)
-{
- int x0, y0, x1, y1 ;
- x0 = ((MAZE_W/2)-4) * CELL_W + LEFT_OFFSET ;
- y0 = ((MAZE_H/2)-1) * CELL_H + TOP_OFFSET ;
- x1 = ((MAZE_W/2)+4) * CELL_W + LEFT_OFFSET ;
- y1 = ((MAZE_H/2)+1) * CELL_H + TOP_OFFSET ;
-
- TFT.BusEnable(true) ;
-// TFT.set_font((unsigned char*) Arial12x12);
- TFT.set_font((unsigned char*) Terminal6x8) ;
- TFT.fillrect(x0, y0, x1, y1, Blue) ;
- TFT.rect(x0,y0,x1,y1, Yellow) ;
- TFT.foreground(Red) ;
- TFT.background(Blue) ;
- x0 = ((MAZE_W/2) - 2) * CELL_W + LEFT_OFFSET ;
- y0 = (MAZE_H/2) * CELL_H - 3 + TOP_OFFSET ;
- TFT.locate(x0, y0) ;
- TFT.printf("G O A L") ;
- TFT.foreground(Yellow) ;
- TFT.background(Black) ;
- TFT.locate(30, 300) ;
-// TFT.printf("Use FRDM touch slider") ;
- TFT.printf(" Retry! ") ;
- TFT.locate(5, 300) ;
- TFT.printf("<< Prev") ;
- TFT.locate(180, 300) ;
- TFT.printf("Next >>") ;
- TFT.BusEnable(false) ;
-}
-
-/** Check if we can move to the next position
- * @param pos_type next the position we'd like to move next
- * @returns if the position is empty (movable)
- */
-bool checkMove(pos_type next)
-{
- bool result = false ;
-
- switch(maze[next.y][next.x]) {
- case POS_PATH:
- case POS_GOAL:
- result = true ;
- break ;
- case POS_START:
- case POS_WALL:
- default:
- result = false ;
- break ;
- }
- return( result ) ;
-}
-
-/** main a simple maze program
- */
-void doMaze(void)
-{
- float res[3] ;
- pos_type current, next, goal ;
- int direction = DIR_STAY ;
- int x, y ;
-
- drawTFTMaze(¤t, &goal) ;
-
- for (;;) {
- acc->getAccAllAxis(res) ;
- direction = getDirection(res) ;
- next = getNext(current, direction) ;
- if ((!isSame(current, next)) && checkMove(next)) {
- TFT.BusEnable(true) ;
- x = current.x * CELL_W + LEFT_OFFSET ;
- y = current.y * CELL_H + TOP_OFFSET ;
- TFT.fillrect(x, y, x+CELL_W-1, y+CELL_H-1, Black) ;
- x = next.x * CELL_W + LEFT_OFFSET ;
- y = next.y * CELL_H + TOP_OFFSET ;
- TFT.fillcircle(x+CELL_S, y+CELL_S, CELL_R, Green) ;
- TFT.BusEnable(false) ;
- current = next ;
- if (isSame(next, goal)) {
- break ; // goal in!
- }
- }
- wait(0.1) ;
- }
- showGoal() ;
-}
--- a/maze.h Sat Aug 08 12:27:57 2015 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,36 +0,0 @@
-#ifndef _MAZE_H_
-#define _MAZE_H_ defined
-
-#define MAZE_W 20
-#define MAZE_H 20
-
-#define POS_PATH 0
-#define POS_WALL 1
-#define POS_START 2
-#define POS_GOAL 3
-
-uint8_t maze[MAZE_H][MAZE_W] = {
- {1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
- {1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,1,0,1,0,1,1,1,1,0,1,1,1,1,1,1,1,0,1},
- {1,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,1,1,0,1},
- {1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,0,1,1,0,1},
- {1,0,0,0,1,0,1,0,1,0,0,0,0,0,1,0,1,1,0,1},
- {1,0,1,1,1,0,1,0,1,1,0,1,0,1,1,0,1,1,0,1},
- {1,0,1,0,0,0,1,0,1,1,0,1,0,1,0,0,1,1,0,1},
- {1,0,1,0,1,1,1,0,0,1,0,1,0,1,0,1,1,1,0,1},
- {1,0,1,0,1,1,1,1,1,1,0,1,0,1,0,0,0,1,0,1},
- {1,0,0,0,0,0,0,0,0,0,0,1,0,1,1,1,0,1,0,1},
- {1,0,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1},
- {1,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1},
- {1,0,1,0,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1},
- {1,0,1,0,1,0,0,0,1,0,0,0,0,0,0,1,1,1,1,1},
- {1,0,1,0,1,1,1,0,1,0,1,1,1,1,1,1,1,1,1,1},
- {1,0,1,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,0,1},
- {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1},
- {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,3,1}
-} ;
-
-extern void doMaze(void) ;
-#endif // _MAZE_H_
\ No newline at end of file
