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” を編集することで独自の迷路を作成していただけます。
After download and reset, the point "@" is located at the start point. Goal is the green "G."
ダウンロードしてリセットすると、画面のスタート位置に“@”が置かれます。 ゴールは緑色の“G”です。
Move the FRDM-KL25Z board to let the "@" go.
FRDM-KL25Z を動かして、“@”を移動させます。
When "@" arrives at the goal with the "goal" banner, game ends.
“@”がゴールにたどり着くと“goal”というバナーが出てゲーム終了です。
Revision 1:6fab471dffd5, committed 2014-12-08
- Comitter:
- Rhyme
- Date:
- Mon Dec 08 11:37:21 2014 +0000
- Parent:
- 0:f2f2c76b9816
- Commit message:
- First commit with some comments added
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r f2f2c76b9816 -r 6fab471dffd5 main.cpp --- a/main.cpp Sun Dec 07 10:17:42 2014 +0000 +++ b/main.cpp Mon Dec 08 11:37:21 2014 +0000 @@ -1,3 +1,5 @@ +/** maze_vt100 a simple maze using vt100 and MMA8451Q libraries + */ #include "mbed.h" #include "MMA8451Q.h" #include "vt100.h" @@ -9,8 +11,6 @@ #define DIR_RIGHT 3 #define DIR_LEFT 4 - - #define MMA8451_I2C_ADDRESS (0x1D<<1) typedef struct _pos { @@ -20,7 +20,23 @@ MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS) ; vt100 tty ; +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 drawTextMaze(pos_type *current, pos_type *goal) { int x, y ; @@ -33,27 +49,30 @@ for (y = 0 ; y < MAZE_H ; y++ ) { for (x = 0 ; x < MAZE_W ; x++ ) { switch(maze[y][x]) { - case 0: + case 0: // path c = ' ' ; - break ; // path - case 1: + break ; + case 1: // wall c = ' ' ; tty.setBG(0) ; // set background to black tty.setFG(7) ; // set foreground to white - break ; // wall - case 2: c = 'S' ; + break ; + case 2: // Start point + c = 'S' ; current->x = x ; current->y = y ; break ; // start - case 3: c = 'G' ; + case 3: // Goal + c = 'G' ; tty.green() ; goal->x = x ; goal->y = y ; - break ; // goal - default: c = '?' ; break ; // wth? + break ; + default: // should not be here + c = '?' ; + break ; // wth? } - tty.locate(x+1, y+1) ; - printf("%c", c) ; + tty.putChar(x+1, y+1, c) ; tty.setBG(7) ; // set background to white tty.setFG(0) ; // set foreground to black } @@ -61,17 +80,23 @@ } } +/** 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 ((-0.2 < in)&&(in < 0.2)) { - result = 0.0 ; - } else { + 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 ; @@ -80,25 +105,26 @@ dy = filterVal(res[1]) ; if ((dx*dx) > (dy*dy)) { // holizontal move - if (dx == 0.0) { - // - } else if (dx > 0.0) { + if (dx > 0.0) { direction = DIR_DOWN ; - } else { + } else if (dx < 0.0) { direction = DIR_UP ; } } else { // vertical move - if (dy == 0.0) { - // - } else if (dy > 0) { + if (dy > 0.0) { direction = DIR_RIGHT ; - } else { + } 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 ; @@ -118,7 +144,7 @@ case DIR_RIGHT: if (next.x < (MAZE_W - 1)) { next.x++ ; - } ; + } break ; case DIR_LEFT: if (next.x > 0) { @@ -131,6 +157,21 @@ return( next ) ; } +/** Notice of the goal + */ +void showGoal(void) +{ + tty.blue() ; + tty.frame((MAZE_W/2)-4,(MAZE_H/2)-1,(MAZE_W/2)+4,(MAZE_H/2)+1) ; + tty.red() ; + tty.locate((MAZE_W/2)-3, (MAZE_H/2)) ; + printf("G O A L\n\r") ; +} + +/** 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 ; @@ -148,7 +189,9 @@ } return( result ) ; } - + +/** main a simple maze program + */ int main() { float res[3] ; pos_type current, next, goal ; @@ -156,36 +199,29 @@ tty.cls() ; drawTextMaze(¤t, &goal) ; - tty.locate(current.x+1, current.y+1) ; tty.black() ; - printf("@") ; + tty.putChar(current.x+1, current.y+1, '@') ; printf("\r\n") ; for (;;) { acc.getAccAllAxis(res) ; direction = getDirection(res) ; next = getNext(current, direction) ; - if (((next.x != current.x)||(next.y != current.y)) && checkMove(next)) { - tty.locate(current.x+1, current.y+1) ; - printf(" ") ; - tty.locate(next.x+1, next.y+1) ; - printf("@") ; - tty.green() ; - tty.locate(1, MAZE_H+1) ; - printf("\r\n") ; - tty.black() ; - current = next ; - if ((next.x == goal.x)&&(next.y == goal.y)) { - break ; - } + if ((!isSame(current, next)) && checkMove(next)) { + tty.putChar(current.x+1, current.y+1, ' ') ; + tty.putChar(next.x+1, next.y+1, '@') ; + tty.green() ; + tty.putStr(1, MAZE_H+1, "\r\n") ; + tty.black() ; + current = next ; + if (isSame(next, goal)) { + break ; // goal in! + } } wait(0.1) ; } - tty.blue() ; - tty.frame((MAZE_W/2)-4,(MAZE_H/2)-1,(MAZE_W/2)+4,(MAZE_H/2)+1) ; - tty.red() ; - tty.locate((MAZE_W/2)-3, (MAZE_H/2)) ; - printf("G O A L\n\r") ; + showGoal() ; for (;;) { + // wait for ever for reset } }