My simple maze program with UniGraphic library version.

Dependencies:   MMA8451Q UniGraphic mbed

This is basically a same program with my maze_vt100_MMA8451Q
but this time I used UniGraphic library and Adafruit 2.8" TFT (with touch).

http://developer.mbed.org/users/Rhyme/code/maze_vt100_MMA8451Q/

You can tailor the maze by editing "maze.h."

先にパブリッシュした簡単な迷路プログラムで UniGraphic ライブラリを使用してみました。

“maze.h” を編集することで独自の迷路を作成していただけます。

/media/uploads/Rhyme/maze_tft_1.jpg

After download and reset, a green circle is located at the start point.
Goal is the red circle.

ダウンロードしてリセットすると、画面のスタート位置に緑の●が置かれます。
ゴールは赤色の●です。

Move the FRDM-KL25Z board to let the green circle go.

FRDM-KL25Z を動かして、緑●を移動させます。

/media/uploads/Rhyme/maze_tft_2.jpg

When the green circle arrives at the goal with the "goal" banner, game ends.

緑●がゴールにたどり着くと“goal”というバナーが出てゲーム終了です。

/media/uploads/Rhyme/maze_tft_3.jpg

Since I did not use TSC (Touch Sensor) in this proram,
it should be easy to use other TFT panel(s), thanks for the "UniGraphic" library.
(let me know when you succeed with other TFT panel ;-)

このプログラムではタッチセンサーは使用していませんので、
“UniGraphic” ライブラリの恩恵として、他のTFTパネルへの移植も
容易だと思います。 (上手く行った人いたら知らせてくださいね・・・)

Committer:
Rhyme
Date:
Sat Mar 07 00:11:50 2015 +0000
Revision:
0:572a79c1c040
First commit for UniGraphic version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Rhyme 0:572a79c1c040 1 /** maze_vt100 a simple maze using UniGraphic and MMA8451Q libraries
Rhyme 0:572a79c1c040 2 */
Rhyme 0:572a79c1c040 3 #include "mbed.h"
Rhyme 0:572a79c1c040 4 #include "MMA8451Q.h"
Rhyme 0:572a79c1c040 5 #include <ILI9341.h>
Rhyme 0:572a79c1c040 6 #include "Arial12x12.h"
Rhyme 0:572a79c1c040 7 #include "Arial24x23.h"
Rhyme 0:572a79c1c040 8 #include "Arial28x28.h"
Rhyme 0:572a79c1c040 9 #include "Arial43x48_numb.h"
Rhyme 0:572a79c1c040 10 #include "maze.h"
Rhyme 0:572a79c1c040 11
Rhyme 0:572a79c1c040 12 #if defined (TARGET_KL25Z)
Rhyme 0:572a79c1c040 13 #define PIN_MOSI PTD2
Rhyme 0:572a79c1c040 14 #define PIN_MISO PTD3
Rhyme 0:572a79c1c040 15 #define PIN_SCLK PTD1
Rhyme 0:572a79c1c040 16 #define PIN_CS_TFT PTD0
Rhyme 0:572a79c1c040 17 #define PIN_DC_TFT PTD5
Rhyme 0:572a79c1c040 18 #define PIN_BL_TFT PTC9
Rhyme 0:572a79c1c040 19 #define PIN_CS_SD PTA4
Rhyme 0:572a79c1c040 20 #define PIN_CS_TSC PTA13
Rhyme 0:572a79c1c040 21 #define PIN_TSC_INTR PTC9
Rhyme 0:572a79c1c040 22 #define PIN_RESET_TFT PTB10
Rhyme 0:572a79c1c040 23 #else
Rhyme 0:572a79c1c040 24 #error TARGET NOT DEFINED
Rhyme 0:572a79c1c040 25 #endif
Rhyme 0:572a79c1c040 26
Rhyme 0:572a79c1c040 27 #define DIR_STAY 0
Rhyme 0:572a79c1c040 28 #define DIR_UP 1
Rhyme 0:572a79c1c040 29 #define DIR_DOWN 2
Rhyme 0:572a79c1c040 30 #define DIR_RIGHT 3
Rhyme 0:572a79c1c040 31 #define DIR_LEFT 4
Rhyme 0:572a79c1c040 32
Rhyme 0:572a79c1c040 33 #define MMA8451_I2C_ADDRESS (0x1D<<1)
Rhyme 0:572a79c1c040 34
Rhyme 0:572a79c1c040 35 typedef struct _pos {
Rhyme 0:572a79c1c040 36 int x ;
Rhyme 0:572a79c1c040 37 int y ;
Rhyme 0:572a79c1c040 38 } pos_type ;
Rhyme 0:572a79c1c040 39
Rhyme 0:572a79c1c040 40 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS) ;
Rhyme 0:572a79c1c040 41 float threshold = 0.2 ;
Rhyme 0:572a79c1c040 42
Rhyme 0:572a79c1c040 43 DigitalOut backlight(PTA12) ;
Rhyme 0:572a79c1c040 44 ILI9341 TFT(SPI_8, 10000000,
Rhyme 0:572a79c1c040 45 PIN_MOSI, PIN_MISO, PIN_SCLK,
Rhyme 0:572a79c1c040 46 PIN_CS_TFT, PIN_RESET_TFT, PIN_DC_TFT, "Adafruit2.8") ;
Rhyme 0:572a79c1c040 47
Rhyme 0:572a79c1c040 48 /** Check if two pos_type values are same
Rhyme 0:572a79c1c040 49 * @param pos_type a
Rhyme 0:572a79c1c040 50 * @param pos_type b
Rhyme 0:572a79c1c040 51 * @returns if a and b are same position
Rhyme 0:572a79c1c040 52 */
Rhyme 0:572a79c1c040 53 bool isSame(pos_type a, pos_type b)
Rhyme 0:572a79c1c040 54 {
Rhyme 0:572a79c1c040 55 return((a.x == b.x)&&(a.y == b.y)) ;
Rhyme 0:572a79c1c040 56 }
Rhyme 0:572a79c1c040 57
Rhyme 0:572a79c1c040 58 /** Draw the maze defined in the "maze.h"
Rhyme 0:572a79c1c040 59 * @param pos_type *current actually the start point
Rhyme 0:572a79c1c040 60 * @param pos_type *goal the position of the goal
Rhyme 0:572a79c1c040 61 * @note those params are actually returned by this function
Rhyme 0:572a79c1c040 62 */
Rhyme 0:572a79c1c040 63 void drawTFTMaze(pos_type *current, pos_type *goal)
Rhyme 0:572a79c1c040 64 {
Rhyme 0:572a79c1c040 65 int x, y, i, j ;
Rhyme 0:572a79c1c040 66 TFT.BusEnable(true) ;
Rhyme 0:572a79c1c040 67
Rhyme 0:572a79c1c040 68 backlight = 0 ;
Rhyme 0:572a79c1c040 69 for (j = 0 ; j < MAZE_H ; j++ ) {
Rhyme 0:572a79c1c040 70 for (i = 0 ; i < MAZE_W ; i++ ) {
Rhyme 0:572a79c1c040 71 x = i * 10 ;
Rhyme 0:572a79c1c040 72 y = j * 10 ;
Rhyme 0:572a79c1c040 73 switch(maze[j][i]) {
Rhyme 0:572a79c1c040 74 case 0: // path
Rhyme 0:572a79c1c040 75 TFT.fillrect(x, y, x+9, y+9, Black) ;
Rhyme 0:572a79c1c040 76 break ;
Rhyme 0:572a79c1c040 77 case 1: // wall
Rhyme 0:572a79c1c040 78 TFT.fillrect(x, y, x+9, y+9, DarkGrey) ;
Rhyme 0:572a79c1c040 79 break ;
Rhyme 0:572a79c1c040 80 case 2: // Start point
Rhyme 0:572a79c1c040 81 TFT.fillcircle(x+5, y+4, 4, Green) ;
Rhyme 0:572a79c1c040 82 current->x = i ;
Rhyme 0:572a79c1c040 83 current->y = j ;
Rhyme 0:572a79c1c040 84 break ;
Rhyme 0:572a79c1c040 85 case 3: // Goal
Rhyme 0:572a79c1c040 86 TFT.fillcircle(x+5, y+4, 4, Red) ;
Rhyme 0:572a79c1c040 87 goal->x = i ;
Rhyme 0:572a79c1c040 88 goal->y = j ;
Rhyme 0:572a79c1c040 89 break ;
Rhyme 0:572a79c1c040 90 default: // should not be here
Rhyme 0:572a79c1c040 91 break ;
Rhyme 0:572a79c1c040 92 }
Rhyme 0:572a79c1c040 93 }
Rhyme 0:572a79c1c040 94 }
Rhyme 0:572a79c1c040 95 backlight = 1 ;
Rhyme 0:572a79c1c040 96 TFT.BusEnable(false) ;
Rhyme 0:572a79c1c040 97 }
Rhyme 0:572a79c1c040 98
Rhyme 0:572a79c1c040 99 /** Filter out too little move
Rhyme 0:572a79c1c040 100 * @param float in returned value from the acc
Rhyme 0:572a79c1c040 101 * @returns float result filtered value of in
Rhyme 0:572a79c1c040 102 */
Rhyme 0:572a79c1c040 103 float filterVal(float in)
Rhyme 0:572a79c1c040 104 {
Rhyme 0:572a79c1c040 105 float result = 0.0 ;
Rhyme 0:572a79c1c040 106 if ((-threshold > in)||(in > threshold)) {
Rhyme 0:572a79c1c040 107 result = in ;
Rhyme 0:572a79c1c040 108 }
Rhyme 0:572a79c1c040 109 return( result ) ;
Rhyme 0:572a79c1c040 110 }
Rhyme 0:572a79c1c040 111
Rhyme 0:572a79c1c040 112 /** Decide which direction to go
Rhyme 0:572a79c1c040 113 * @param float res[] acc value of x, y
Rhyme 0:572a79c1c040 114 * @returns int direction to move
Rhyme 0:572a79c1c040 115 */
Rhyme 0:572a79c1c040 116 int getDirection(float res[])
Rhyme 0:572a79c1c040 117 {
Rhyme 0:572a79c1c040 118 float dx, dy ;
Rhyme 0:572a79c1c040 119 int direction = DIR_STAY ;
Rhyme 0:572a79c1c040 120 dx = filterVal(res[0]) ;
Rhyme 0:572a79c1c040 121 dy = filterVal(res[1]) ;
Rhyme 0:572a79c1c040 122
Rhyme 0:572a79c1c040 123 if ((dx*dx) > (dy*dy)) { // holizontal move
Rhyme 0:572a79c1c040 124 if (dx > 0.0) {
Rhyme 0:572a79c1c040 125 direction = DIR_DOWN ;
Rhyme 0:572a79c1c040 126 } else if (dx < 0.0) {
Rhyme 0:572a79c1c040 127 direction = DIR_UP ;
Rhyme 0:572a79c1c040 128 }
Rhyme 0:572a79c1c040 129 } else { // vertical move
Rhyme 0:572a79c1c040 130 if (dy > 0.0) {
Rhyme 0:572a79c1c040 131 direction = DIR_RIGHT ;
Rhyme 0:572a79c1c040 132 } else if (dy < 0.0) {
Rhyme 0:572a79c1c040 133 direction = DIR_LEFT ;
Rhyme 0:572a79c1c040 134 }
Rhyme 0:572a79c1c040 135 }
Rhyme 0:572a79c1c040 136 return(direction) ;
Rhyme 0:572a79c1c040 137 }
Rhyme 0:572a79c1c040 138
Rhyme 0:572a79c1c040 139 /** Get next positon to move to
Rhyme 0:572a79c1c040 140 * @param pos_type current where we are now
Rhyme 0:572a79c1c040 141 * @param int direction which way we'd like to move
Rhyme 0:572a79c1c040 142 * @returns the candidate positon for the next move
Rhyme 0:572a79c1c040 143 */
Rhyme 0:572a79c1c040 144 pos_type getNext(pos_type current, int direction)
Rhyme 0:572a79c1c040 145 {
Rhyme 0:572a79c1c040 146 pos_type next = current ;
Rhyme 0:572a79c1c040 147 switch(direction) {
Rhyme 0:572a79c1c040 148 case DIR_STAY:
Rhyme 0:572a79c1c040 149 break ;
Rhyme 0:572a79c1c040 150 case DIR_UP:
Rhyme 0:572a79c1c040 151 if (next.y > 0) {
Rhyme 0:572a79c1c040 152 next.y-- ;
Rhyme 0:572a79c1c040 153 }
Rhyme 0:572a79c1c040 154 break ;
Rhyme 0:572a79c1c040 155 case DIR_DOWN:
Rhyme 0:572a79c1c040 156 if (next.y < (MAZE_H - 1)) {
Rhyme 0:572a79c1c040 157 next.y++ ;
Rhyme 0:572a79c1c040 158 }
Rhyme 0:572a79c1c040 159 break ;
Rhyme 0:572a79c1c040 160 case DIR_RIGHT:
Rhyme 0:572a79c1c040 161 if (next.x < (MAZE_W - 1)) {
Rhyme 0:572a79c1c040 162 next.x++ ;
Rhyme 0:572a79c1c040 163 }
Rhyme 0:572a79c1c040 164 break ;
Rhyme 0:572a79c1c040 165 case DIR_LEFT:
Rhyme 0:572a79c1c040 166 if (next.x > 0) {
Rhyme 0:572a79c1c040 167 next.x-- ;
Rhyme 0:572a79c1c040 168 }
Rhyme 0:572a79c1c040 169 break ;
Rhyme 0:572a79c1c040 170 default:
Rhyme 0:572a79c1c040 171 break ;
Rhyme 0:572a79c1c040 172 }
Rhyme 0:572a79c1c040 173 return( next ) ;
Rhyme 0:572a79c1c040 174 }
Rhyme 0:572a79c1c040 175
Rhyme 0:572a79c1c040 176 /** Notice of the goal
Rhyme 0:572a79c1c040 177 */
Rhyme 0:572a79c1c040 178 void showGoal(void)
Rhyme 0:572a79c1c040 179 {
Rhyme 0:572a79c1c040 180 int x0, y0, x1, y1 ;
Rhyme 0:572a79c1c040 181 x0 = ((MAZE_W/2)-4) * 10 ;
Rhyme 0:572a79c1c040 182 y0 = ((MAZE_H/2)-1) * 10 ;
Rhyme 0:572a79c1c040 183 x1 = ((MAZE_W/2)+4) * 10 ;
Rhyme 0:572a79c1c040 184 y1 = ((MAZE_H/2)+1) * 10 ;
Rhyme 0:572a79c1c040 185
Rhyme 0:572a79c1c040 186 TFT.BusEnable(true) ;
Rhyme 0:572a79c1c040 187 TFT.fillrect(x0, y0, x1, y1, Blue) ;
Rhyme 0:572a79c1c040 188 TFT.rect(x0,y0,x1,y1, Yellow) ;
Rhyme 0:572a79c1c040 189 TFT.foreground(Red) ;
Rhyme 0:572a79c1c040 190 TFT.background(Blue) ;
Rhyme 0:572a79c1c040 191 x0 = ((MAZE_W/2) - 2) * 10 ;
Rhyme 0:572a79c1c040 192 y0 = (MAZE_H/2) * 10 - 3 ;
Rhyme 0:572a79c1c040 193 TFT.locate(x0, y0) ;
Rhyme 0:572a79c1c040 194 TFT.printf("G O A L") ;
Rhyme 0:572a79c1c040 195 TFT.BusEnable(false) ;
Rhyme 0:572a79c1c040 196 }
Rhyme 0:572a79c1c040 197
Rhyme 0:572a79c1c040 198 /** Check if we can move to the next position
Rhyme 0:572a79c1c040 199 * @param pos_type next the position we'd like to move next
Rhyme 0:572a79c1c040 200 * @returns if the position is empty (movable)
Rhyme 0:572a79c1c040 201 */
Rhyme 0:572a79c1c040 202 bool checkMove(pos_type next)
Rhyme 0:572a79c1c040 203 {
Rhyme 0:572a79c1c040 204 bool result = false ;
Rhyme 0:572a79c1c040 205
Rhyme 0:572a79c1c040 206 switch(maze[next.y][next.x]) {
Rhyme 0:572a79c1c040 207 case POS_PATH:
Rhyme 0:572a79c1c040 208 case POS_GOAL:
Rhyme 0:572a79c1c040 209 result = true ;
Rhyme 0:572a79c1c040 210 break ;
Rhyme 0:572a79c1c040 211 case POS_START:
Rhyme 0:572a79c1c040 212 case POS_WALL:
Rhyme 0:572a79c1c040 213 default:
Rhyme 0:572a79c1c040 214 result = false ;
Rhyme 0:572a79c1c040 215 break ;
Rhyme 0:572a79c1c040 216 }
Rhyme 0:572a79c1c040 217 return( result ) ;
Rhyme 0:572a79c1c040 218 }
Rhyme 0:572a79c1c040 219
Rhyme 0:572a79c1c040 220 /** main a simple maze program
Rhyme 0:572a79c1c040 221 */
Rhyme 0:572a79c1c040 222 int main() {
Rhyme 0:572a79c1c040 223 float res[3] ;
Rhyme 0:572a79c1c040 224 pos_type current, next, goal ;
Rhyme 0:572a79c1c040 225 int direction = DIR_STAY ;
Rhyme 0:572a79c1c040 226 int x, y ;
Rhyme 0:572a79c1c040 227
Rhyme 0:572a79c1c040 228 drawTFTMaze(&current, &goal) ;
Rhyme 0:572a79c1c040 229
Rhyme 0:572a79c1c040 230 for (;;) {
Rhyme 0:572a79c1c040 231 acc.getAccAllAxis(res) ;
Rhyme 0:572a79c1c040 232 direction = getDirection(res) ;
Rhyme 0:572a79c1c040 233 next = getNext(current, direction) ;
Rhyme 0:572a79c1c040 234 if ((!isSame(current, next)) && checkMove(next)) {
Rhyme 0:572a79c1c040 235 TFT.BusEnable(true) ;
Rhyme 0:572a79c1c040 236 x = current.x * 10 ;
Rhyme 0:572a79c1c040 237 y = current.y * 10 ;
Rhyme 0:572a79c1c040 238 TFT.fillrect(x, y, x+9, y+9, Black) ;
Rhyme 0:572a79c1c040 239 x = next.x * 10 ;
Rhyme 0:572a79c1c040 240 y = next.y * 10 ;
Rhyme 0:572a79c1c040 241 TFT.fillcircle(x+5, y+5, 4, Green) ;
Rhyme 0:572a79c1c040 242 TFT.BusEnable(false) ;
Rhyme 0:572a79c1c040 243 current = next ;
Rhyme 0:572a79c1c040 244 if (isSame(next, goal)) {
Rhyme 0:572a79c1c040 245 break ; // goal in!
Rhyme 0:572a79c1c040 246 }
Rhyme 0:572a79c1c040 247 }
Rhyme 0:572a79c1c040 248 wait(0.1) ;
Rhyme 0:572a79c1c040 249 }
Rhyme 0:572a79c1c040 250 showGoal() ;
Rhyme 0:572a79c1c040 251 for (;;) {
Rhyme 0:572a79c1c040 252 // wait for ever for reset
Rhyme 0:572a79c1c040 253 }
Rhyme 0:572a79c1c040 254 }