Inspired by Mr. Jeff G.'s Mini Project LCD Maze, I hacked my version for FRDM-KL25Z. This one utilizes the MMA8451Q on the FRDM and my vt100 lib. By editing "maze.h" you can create your own maze.

Dependencies:   MMA8451Q mbed vt100

Another sample project to show how to use my vt100 lib along with MMA8451Q lib.

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

先にパブリッシュした vt100 ライブラリと、FRDM-KL25Zに搭載された加速度センサを使用した簡単な迷路プログラムです。

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

/media/uploads/Rhyme/maze_start.jpg

After download and reset, the point "@" is located at the start point. Goal is the green "G."

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

/media/uploads/Rhyme/maze_walking.jpg

Move the FRDM-KL25Z board to let the "@" go.

FRDM-KL25Z を動かして、“@”を移動させます。

/media/uploads/Rhyme/maze_goal.jpg

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

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

Committer:
Rhyme
Date:
Mon Dec 08 11:37:21 2014 +0000
Revision:
1:6fab471dffd5
Parent:
0:f2f2c76b9816
First commit with some comments added

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Rhyme 1:6fab471dffd5 1 /** maze_vt100 a simple maze using vt100 and MMA8451Q libraries
Rhyme 1:6fab471dffd5 2 */
Rhyme 0:f2f2c76b9816 3 #include "mbed.h"
Rhyme 0:f2f2c76b9816 4 #include "MMA8451Q.h"
Rhyme 0:f2f2c76b9816 5 #include "vt100.h"
Rhyme 0:f2f2c76b9816 6 #include "maze.h"
Rhyme 0:f2f2c76b9816 7
Rhyme 0:f2f2c76b9816 8 #define DIR_STAY 0
Rhyme 0:f2f2c76b9816 9 #define DIR_UP 1
Rhyme 0:f2f2c76b9816 10 #define DIR_DOWN 2
Rhyme 0:f2f2c76b9816 11 #define DIR_RIGHT 3
Rhyme 0:f2f2c76b9816 12 #define DIR_LEFT 4
Rhyme 0:f2f2c76b9816 13
Rhyme 0:f2f2c76b9816 14 #define MMA8451_I2C_ADDRESS (0x1D<<1)
Rhyme 0:f2f2c76b9816 15
Rhyme 0:f2f2c76b9816 16 typedef struct _pos {
Rhyme 0:f2f2c76b9816 17 int x ;
Rhyme 0:f2f2c76b9816 18 int y ;
Rhyme 0:f2f2c76b9816 19 } pos_type ;
Rhyme 0:f2f2c76b9816 20
Rhyme 0:f2f2c76b9816 21 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS) ;
Rhyme 0:f2f2c76b9816 22 vt100 tty ;
Rhyme 1:6fab471dffd5 23 float threshold = 0.2 ;
Rhyme 0:f2f2c76b9816 24
Rhyme 1:6fab471dffd5 25 /** Check if two pos_type values are same
Rhyme 1:6fab471dffd5 26 * @param pos_type a
Rhyme 1:6fab471dffd5 27 * @param pos_type b
Rhyme 1:6fab471dffd5 28 * @returns if a and b are same position
Rhyme 1:6fab471dffd5 29 */
Rhyme 1:6fab471dffd5 30 bool isSame(pos_type a, pos_type b)
Rhyme 1:6fab471dffd5 31 {
Rhyme 1:6fab471dffd5 32 return((a.x == b.x)&&(a.y == b.y)) ;
Rhyme 1:6fab471dffd5 33 }
Rhyme 1:6fab471dffd5 34
Rhyme 1:6fab471dffd5 35 /** Draw the maze defined in the "maze.h"
Rhyme 1:6fab471dffd5 36 * @param pos_type *current actually the start point
Rhyme 1:6fab471dffd5 37 * @param pos_type *goal the position of the goal
Rhyme 1:6fab471dffd5 38 * @note those params are actually returned by this function
Rhyme 1:6fab471dffd5 39 */
Rhyme 0:f2f2c76b9816 40 void drawTextMaze(pos_type *current, pos_type *goal)
Rhyme 0:f2f2c76b9816 41 {
Rhyme 0:f2f2c76b9816 42 int x, y ;
Rhyme 0:f2f2c76b9816 43 char c ;
Rhyme 0:f2f2c76b9816 44
Rhyme 0:f2f2c76b9816 45 tty.setBG(7) ; // set background to white
Rhyme 0:f2f2c76b9816 46 tty.setFG(0) ; // set foreground to black
Rhyme 0:f2f2c76b9816 47 tty.cls() ;
Rhyme 0:f2f2c76b9816 48
Rhyme 0:f2f2c76b9816 49 for (y = 0 ; y < MAZE_H ; y++ ) {
Rhyme 0:f2f2c76b9816 50 for (x = 0 ; x < MAZE_W ; x++ ) {
Rhyme 0:f2f2c76b9816 51 switch(maze[y][x]) {
Rhyme 1:6fab471dffd5 52 case 0: // path
Rhyme 0:f2f2c76b9816 53 c = ' ' ;
Rhyme 1:6fab471dffd5 54 break ;
Rhyme 1:6fab471dffd5 55 case 1: // wall
Rhyme 0:f2f2c76b9816 56 c = ' ' ;
Rhyme 0:f2f2c76b9816 57 tty.setBG(0) ; // set background to black
Rhyme 0:f2f2c76b9816 58 tty.setFG(7) ; // set foreground to white
Rhyme 1:6fab471dffd5 59 break ;
Rhyme 1:6fab471dffd5 60 case 2: // Start point
Rhyme 1:6fab471dffd5 61 c = 'S' ;
Rhyme 0:f2f2c76b9816 62 current->x = x ;
Rhyme 0:f2f2c76b9816 63 current->y = y ;
Rhyme 0:f2f2c76b9816 64 break ; // start
Rhyme 1:6fab471dffd5 65 case 3: // Goal
Rhyme 1:6fab471dffd5 66 c = 'G' ;
Rhyme 0:f2f2c76b9816 67 tty.green() ;
Rhyme 0:f2f2c76b9816 68 goal->x = x ;
Rhyme 0:f2f2c76b9816 69 goal->y = y ;
Rhyme 1:6fab471dffd5 70 break ;
Rhyme 1:6fab471dffd5 71 default: // should not be here
Rhyme 1:6fab471dffd5 72 c = '?' ;
Rhyme 1:6fab471dffd5 73 break ; // wth?
Rhyme 0:f2f2c76b9816 74 }
Rhyme 1:6fab471dffd5 75 tty.putChar(x+1, y+1, c) ;
Rhyme 0:f2f2c76b9816 76 tty.setBG(7) ; // set background to white
Rhyme 0:f2f2c76b9816 77 tty.setFG(0) ; // set foreground to black
Rhyme 0:f2f2c76b9816 78 }
Rhyme 0:f2f2c76b9816 79 printf("\n\r") ;
Rhyme 0:f2f2c76b9816 80 }
Rhyme 0:f2f2c76b9816 81 }
Rhyme 0:f2f2c76b9816 82
Rhyme 1:6fab471dffd5 83 /** Filter out too little move
Rhyme 1:6fab471dffd5 84 * @param float in returned value from the acc
Rhyme 1:6fab471dffd5 85 * @returns float result filtered value of in
Rhyme 1:6fab471dffd5 86 */
Rhyme 0:f2f2c76b9816 87 float filterVal(float in)
Rhyme 0:f2f2c76b9816 88 {
Rhyme 0:f2f2c76b9816 89 float result = 0.0 ;
Rhyme 1:6fab471dffd5 90 if ((-threshold > in)||(in > threshold)) {
Rhyme 0:f2f2c76b9816 91 result = in ;
Rhyme 0:f2f2c76b9816 92 }
Rhyme 0:f2f2c76b9816 93 return( result ) ;
Rhyme 0:f2f2c76b9816 94 }
Rhyme 0:f2f2c76b9816 95
Rhyme 1:6fab471dffd5 96 /** Decide which direction to go
Rhyme 1:6fab471dffd5 97 * @param float res[] acc value of x, y
Rhyme 1:6fab471dffd5 98 * @returns int direction to move
Rhyme 1:6fab471dffd5 99 */
Rhyme 0:f2f2c76b9816 100 int getDirection(float res[])
Rhyme 0:f2f2c76b9816 101 {
Rhyme 0:f2f2c76b9816 102 float dx, dy ;
Rhyme 0:f2f2c76b9816 103 int direction = DIR_STAY ;
Rhyme 0:f2f2c76b9816 104 dx = filterVal(res[0]) ;
Rhyme 0:f2f2c76b9816 105 dy = filterVal(res[1]) ;
Rhyme 0:f2f2c76b9816 106
Rhyme 0:f2f2c76b9816 107 if ((dx*dx) > (dy*dy)) { // holizontal move
Rhyme 1:6fab471dffd5 108 if (dx > 0.0) {
Rhyme 0:f2f2c76b9816 109 direction = DIR_DOWN ;
Rhyme 1:6fab471dffd5 110 } else if (dx < 0.0) {
Rhyme 0:f2f2c76b9816 111 direction = DIR_UP ;
Rhyme 0:f2f2c76b9816 112 }
Rhyme 0:f2f2c76b9816 113 } else { // vertical move
Rhyme 1:6fab471dffd5 114 if (dy > 0.0) {
Rhyme 0:f2f2c76b9816 115 direction = DIR_RIGHT ;
Rhyme 1:6fab471dffd5 116 } else if (dy < 0.0) {
Rhyme 0:f2f2c76b9816 117 direction = DIR_LEFT ;
Rhyme 0:f2f2c76b9816 118 }
Rhyme 0:f2f2c76b9816 119 }
Rhyme 0:f2f2c76b9816 120 return(direction) ;
Rhyme 0:f2f2c76b9816 121 }
Rhyme 0:f2f2c76b9816 122
Rhyme 1:6fab471dffd5 123 /** Get next positon to move to
Rhyme 1:6fab471dffd5 124 * @param pos_type current where we are now
Rhyme 1:6fab471dffd5 125 * @param int direction which way we'd like to move
Rhyme 1:6fab471dffd5 126 * @returns the candidate positon for the next move
Rhyme 1:6fab471dffd5 127 */
Rhyme 0:f2f2c76b9816 128 pos_type getNext(pos_type current, int direction)
Rhyme 0:f2f2c76b9816 129 {
Rhyme 0:f2f2c76b9816 130 pos_type next = current ;
Rhyme 0:f2f2c76b9816 131 switch(direction) {
Rhyme 0:f2f2c76b9816 132 case DIR_STAY:
Rhyme 0:f2f2c76b9816 133 break ;
Rhyme 0:f2f2c76b9816 134 case DIR_UP:
Rhyme 0:f2f2c76b9816 135 if (next.y > 0) {
Rhyme 0:f2f2c76b9816 136 next.y-- ;
Rhyme 0:f2f2c76b9816 137 }
Rhyme 0:f2f2c76b9816 138 break ;
Rhyme 0:f2f2c76b9816 139 case DIR_DOWN:
Rhyme 0:f2f2c76b9816 140 if (next.y < (MAZE_H - 1)) {
Rhyme 0:f2f2c76b9816 141 next.y++ ;
Rhyme 0:f2f2c76b9816 142 }
Rhyme 0:f2f2c76b9816 143 break ;
Rhyme 0:f2f2c76b9816 144 case DIR_RIGHT:
Rhyme 0:f2f2c76b9816 145 if (next.x < (MAZE_W - 1)) {
Rhyme 0:f2f2c76b9816 146 next.x++ ;
Rhyme 1:6fab471dffd5 147 }
Rhyme 0:f2f2c76b9816 148 break ;
Rhyme 0:f2f2c76b9816 149 case DIR_LEFT:
Rhyme 0:f2f2c76b9816 150 if (next.x > 0) {
Rhyme 0:f2f2c76b9816 151 next.x-- ;
Rhyme 0:f2f2c76b9816 152 }
Rhyme 0:f2f2c76b9816 153 break ;
Rhyme 0:f2f2c76b9816 154 default:
Rhyme 0:f2f2c76b9816 155 break ;
Rhyme 0:f2f2c76b9816 156 }
Rhyme 0:f2f2c76b9816 157 return( next ) ;
Rhyme 0:f2f2c76b9816 158 }
Rhyme 0:f2f2c76b9816 159
Rhyme 1:6fab471dffd5 160 /** Notice of the goal
Rhyme 1:6fab471dffd5 161 */
Rhyme 1:6fab471dffd5 162 void showGoal(void)
Rhyme 1:6fab471dffd5 163 {
Rhyme 1:6fab471dffd5 164 tty.blue() ;
Rhyme 1:6fab471dffd5 165 tty.frame((MAZE_W/2)-4,(MAZE_H/2)-1,(MAZE_W/2)+4,(MAZE_H/2)+1) ;
Rhyme 1:6fab471dffd5 166 tty.red() ;
Rhyme 1:6fab471dffd5 167 tty.locate((MAZE_W/2)-3, (MAZE_H/2)) ;
Rhyme 1:6fab471dffd5 168 printf("G O A L\n\r") ;
Rhyme 1:6fab471dffd5 169 }
Rhyme 1:6fab471dffd5 170
Rhyme 1:6fab471dffd5 171 /** Check if we can move to the next position
Rhyme 1:6fab471dffd5 172 * @param pos_type next the position we'd like to move next
Rhyme 1:6fab471dffd5 173 * @returns if the position is empty (movable)
Rhyme 1:6fab471dffd5 174 */
Rhyme 0:f2f2c76b9816 175 bool checkMove(pos_type next)
Rhyme 0:f2f2c76b9816 176 {
Rhyme 0:f2f2c76b9816 177 bool result = false ;
Rhyme 0:f2f2c76b9816 178
Rhyme 0:f2f2c76b9816 179 switch(maze[next.y][next.x]) {
Rhyme 0:f2f2c76b9816 180 case POS_PATH:
Rhyme 0:f2f2c76b9816 181 case POS_GOAL:
Rhyme 0:f2f2c76b9816 182 result = true ;
Rhyme 0:f2f2c76b9816 183 break ;
Rhyme 0:f2f2c76b9816 184 case POS_START:
Rhyme 0:f2f2c76b9816 185 case POS_WALL:
Rhyme 0:f2f2c76b9816 186 default:
Rhyme 0:f2f2c76b9816 187 result = false ;
Rhyme 0:f2f2c76b9816 188 break ;
Rhyme 0:f2f2c76b9816 189 }
Rhyme 0:f2f2c76b9816 190 return( result ) ;
Rhyme 0:f2f2c76b9816 191 }
Rhyme 1:6fab471dffd5 192
Rhyme 1:6fab471dffd5 193 /** main a simple maze program
Rhyme 1:6fab471dffd5 194 */
Rhyme 0:f2f2c76b9816 195 int main() {
Rhyme 0:f2f2c76b9816 196 float res[3] ;
Rhyme 0:f2f2c76b9816 197 pos_type current, next, goal ;
Rhyme 0:f2f2c76b9816 198 int direction = DIR_STAY ;
Rhyme 0:f2f2c76b9816 199
Rhyme 0:f2f2c76b9816 200 tty.cls() ;
Rhyme 0:f2f2c76b9816 201 drawTextMaze(&current, &goal) ;
Rhyme 0:f2f2c76b9816 202 tty.black() ;
Rhyme 1:6fab471dffd5 203 tty.putChar(current.x+1, current.y+1, '@') ;
Rhyme 0:f2f2c76b9816 204 printf("\r\n") ;
Rhyme 0:f2f2c76b9816 205
Rhyme 0:f2f2c76b9816 206 for (;;) {
Rhyme 0:f2f2c76b9816 207 acc.getAccAllAxis(res) ;
Rhyme 0:f2f2c76b9816 208 direction = getDirection(res) ;
Rhyme 0:f2f2c76b9816 209 next = getNext(current, direction) ;
Rhyme 1:6fab471dffd5 210 if ((!isSame(current, next)) && checkMove(next)) {
Rhyme 1:6fab471dffd5 211 tty.putChar(current.x+1, current.y+1, ' ') ;
Rhyme 1:6fab471dffd5 212 tty.putChar(next.x+1, next.y+1, '@') ;
Rhyme 1:6fab471dffd5 213 tty.green() ;
Rhyme 1:6fab471dffd5 214 tty.putStr(1, MAZE_H+1, "\r\n") ;
Rhyme 1:6fab471dffd5 215 tty.black() ;
Rhyme 1:6fab471dffd5 216 current = next ;
Rhyme 1:6fab471dffd5 217 if (isSame(next, goal)) {
Rhyme 1:6fab471dffd5 218 break ; // goal in!
Rhyme 1:6fab471dffd5 219 }
Rhyme 0:f2f2c76b9816 220 }
Rhyme 0:f2f2c76b9816 221 wait(0.1) ;
Rhyme 0:f2f2c76b9816 222 }
Rhyme 1:6fab471dffd5 223 showGoal() ;
Rhyme 0:f2f2c76b9816 224 for (;;) {
Rhyme 1:6fab471dffd5 225 // wait for ever for reset
Rhyme 0:f2f2c76b9816 226 }
Rhyme 0:f2f2c76b9816 227 }