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”というバナーが出てゲーム終了です。
main.cpp@0:f2f2c76b9816, 2014-12-07 (annotated)
- Committer:
- Rhyme
- Date:
- Sun Dec 07 10:17:42 2014 +0000
- Revision:
- 0:f2f2c76b9816
- Child:
- 1:6fab471dffd5
First time commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Rhyme | 0:f2f2c76b9816 | 1 | #include "mbed.h" |
Rhyme | 0:f2f2c76b9816 | 2 | #include "MMA8451Q.h" |
Rhyme | 0:f2f2c76b9816 | 3 | #include "vt100.h" |
Rhyme | 0:f2f2c76b9816 | 4 | #include "maze.h" |
Rhyme | 0:f2f2c76b9816 | 5 | |
Rhyme | 0:f2f2c76b9816 | 6 | #define DIR_STAY 0 |
Rhyme | 0:f2f2c76b9816 | 7 | #define DIR_UP 1 |
Rhyme | 0:f2f2c76b9816 | 8 | #define DIR_DOWN 2 |
Rhyme | 0:f2f2c76b9816 | 9 | #define DIR_RIGHT 3 |
Rhyme | 0:f2f2c76b9816 | 10 | #define DIR_LEFT 4 |
Rhyme | 0:f2f2c76b9816 | 11 | |
Rhyme | 0:f2f2c76b9816 | 12 | |
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 | 0:f2f2c76b9816 | 23 | |
Rhyme | 0:f2f2c76b9816 | 24 | void drawTextMaze(pos_type *current, pos_type *goal) |
Rhyme | 0:f2f2c76b9816 | 25 | { |
Rhyme | 0:f2f2c76b9816 | 26 | int x, y ; |
Rhyme | 0:f2f2c76b9816 | 27 | char c ; |
Rhyme | 0:f2f2c76b9816 | 28 | |
Rhyme | 0:f2f2c76b9816 | 29 | tty.setBG(7) ; // set background to white |
Rhyme | 0:f2f2c76b9816 | 30 | tty.setFG(0) ; // set foreground to black |
Rhyme | 0:f2f2c76b9816 | 31 | tty.cls() ; |
Rhyme | 0:f2f2c76b9816 | 32 | |
Rhyme | 0:f2f2c76b9816 | 33 | for (y = 0 ; y < MAZE_H ; y++ ) { |
Rhyme | 0:f2f2c76b9816 | 34 | for (x = 0 ; x < MAZE_W ; x++ ) { |
Rhyme | 0:f2f2c76b9816 | 35 | switch(maze[y][x]) { |
Rhyme | 0:f2f2c76b9816 | 36 | case 0: |
Rhyme | 0:f2f2c76b9816 | 37 | c = ' ' ; |
Rhyme | 0:f2f2c76b9816 | 38 | break ; // path |
Rhyme | 0:f2f2c76b9816 | 39 | case 1: |
Rhyme | 0:f2f2c76b9816 | 40 | c = ' ' ; |
Rhyme | 0:f2f2c76b9816 | 41 | tty.setBG(0) ; // set background to black |
Rhyme | 0:f2f2c76b9816 | 42 | tty.setFG(7) ; // set foreground to white |
Rhyme | 0:f2f2c76b9816 | 43 | break ; // wall |
Rhyme | 0:f2f2c76b9816 | 44 | case 2: c = 'S' ; |
Rhyme | 0:f2f2c76b9816 | 45 | current->x = x ; |
Rhyme | 0:f2f2c76b9816 | 46 | current->y = y ; |
Rhyme | 0:f2f2c76b9816 | 47 | break ; // start |
Rhyme | 0:f2f2c76b9816 | 48 | case 3: c = 'G' ; |
Rhyme | 0:f2f2c76b9816 | 49 | tty.green() ; |
Rhyme | 0:f2f2c76b9816 | 50 | goal->x = x ; |
Rhyme | 0:f2f2c76b9816 | 51 | goal->y = y ; |
Rhyme | 0:f2f2c76b9816 | 52 | break ; // goal |
Rhyme | 0:f2f2c76b9816 | 53 | default: c = '?' ; break ; // wth? |
Rhyme | 0:f2f2c76b9816 | 54 | } |
Rhyme | 0:f2f2c76b9816 | 55 | tty.locate(x+1, y+1) ; |
Rhyme | 0:f2f2c76b9816 | 56 | printf("%c", c) ; |
Rhyme | 0:f2f2c76b9816 | 57 | tty.setBG(7) ; // set background to white |
Rhyme | 0:f2f2c76b9816 | 58 | tty.setFG(0) ; // set foreground to black |
Rhyme | 0:f2f2c76b9816 | 59 | } |
Rhyme | 0:f2f2c76b9816 | 60 | printf("\n\r") ; |
Rhyme | 0:f2f2c76b9816 | 61 | } |
Rhyme | 0:f2f2c76b9816 | 62 | } |
Rhyme | 0:f2f2c76b9816 | 63 | |
Rhyme | 0:f2f2c76b9816 | 64 | float filterVal(float in) |
Rhyme | 0:f2f2c76b9816 | 65 | { |
Rhyme | 0:f2f2c76b9816 | 66 | float result = 0.0 ; |
Rhyme | 0:f2f2c76b9816 | 67 | if ((-0.2 < in)&&(in < 0.2)) { |
Rhyme | 0:f2f2c76b9816 | 68 | result = 0.0 ; |
Rhyme | 0:f2f2c76b9816 | 69 | } else { |
Rhyme | 0:f2f2c76b9816 | 70 | result = in ; |
Rhyme | 0:f2f2c76b9816 | 71 | } |
Rhyme | 0:f2f2c76b9816 | 72 | return( result ) ; |
Rhyme | 0:f2f2c76b9816 | 73 | } |
Rhyme | 0:f2f2c76b9816 | 74 | |
Rhyme | 0:f2f2c76b9816 | 75 | int getDirection(float res[]) |
Rhyme | 0:f2f2c76b9816 | 76 | { |
Rhyme | 0:f2f2c76b9816 | 77 | float dx, dy ; |
Rhyme | 0:f2f2c76b9816 | 78 | int direction = DIR_STAY ; |
Rhyme | 0:f2f2c76b9816 | 79 | dx = filterVal(res[0]) ; |
Rhyme | 0:f2f2c76b9816 | 80 | dy = filterVal(res[1]) ; |
Rhyme | 0:f2f2c76b9816 | 81 | |
Rhyme | 0:f2f2c76b9816 | 82 | if ((dx*dx) > (dy*dy)) { // holizontal move |
Rhyme | 0:f2f2c76b9816 | 83 | if (dx == 0.0) { |
Rhyme | 0:f2f2c76b9816 | 84 | // |
Rhyme | 0:f2f2c76b9816 | 85 | } else if (dx > 0.0) { |
Rhyme | 0:f2f2c76b9816 | 86 | direction = DIR_DOWN ; |
Rhyme | 0:f2f2c76b9816 | 87 | } else { |
Rhyme | 0:f2f2c76b9816 | 88 | direction = DIR_UP ; |
Rhyme | 0:f2f2c76b9816 | 89 | } |
Rhyme | 0:f2f2c76b9816 | 90 | } else { // vertical move |
Rhyme | 0:f2f2c76b9816 | 91 | if (dy == 0.0) { |
Rhyme | 0:f2f2c76b9816 | 92 | // |
Rhyme | 0:f2f2c76b9816 | 93 | } else if (dy > 0) { |
Rhyme | 0:f2f2c76b9816 | 94 | direction = DIR_RIGHT ; |
Rhyme | 0:f2f2c76b9816 | 95 | } else { |
Rhyme | 0:f2f2c76b9816 | 96 | direction = DIR_LEFT ; |
Rhyme | 0:f2f2c76b9816 | 97 | } |
Rhyme | 0:f2f2c76b9816 | 98 | } |
Rhyme | 0:f2f2c76b9816 | 99 | return(direction) ; |
Rhyme | 0:f2f2c76b9816 | 100 | } |
Rhyme | 0:f2f2c76b9816 | 101 | |
Rhyme | 0:f2f2c76b9816 | 102 | pos_type getNext(pos_type current, int direction) |
Rhyme | 0:f2f2c76b9816 | 103 | { |
Rhyme | 0:f2f2c76b9816 | 104 | pos_type next = current ; |
Rhyme | 0:f2f2c76b9816 | 105 | switch(direction) { |
Rhyme | 0:f2f2c76b9816 | 106 | case DIR_STAY: |
Rhyme | 0:f2f2c76b9816 | 107 | break ; |
Rhyme | 0:f2f2c76b9816 | 108 | case DIR_UP: |
Rhyme | 0:f2f2c76b9816 | 109 | if (next.y > 0) { |
Rhyme | 0:f2f2c76b9816 | 110 | next.y-- ; |
Rhyme | 0:f2f2c76b9816 | 111 | } |
Rhyme | 0:f2f2c76b9816 | 112 | break ; |
Rhyme | 0:f2f2c76b9816 | 113 | case DIR_DOWN: |
Rhyme | 0:f2f2c76b9816 | 114 | if (next.y < (MAZE_H - 1)) { |
Rhyme | 0:f2f2c76b9816 | 115 | next.y++ ; |
Rhyme | 0:f2f2c76b9816 | 116 | } |
Rhyme | 0:f2f2c76b9816 | 117 | break ; |
Rhyme | 0:f2f2c76b9816 | 118 | case DIR_RIGHT: |
Rhyme | 0:f2f2c76b9816 | 119 | if (next.x < (MAZE_W - 1)) { |
Rhyme | 0:f2f2c76b9816 | 120 | next.x++ ; |
Rhyme | 0:f2f2c76b9816 | 121 | } ; |
Rhyme | 0:f2f2c76b9816 | 122 | break ; |
Rhyme | 0:f2f2c76b9816 | 123 | case DIR_LEFT: |
Rhyme | 0:f2f2c76b9816 | 124 | if (next.x > 0) { |
Rhyme | 0:f2f2c76b9816 | 125 | next.x-- ; |
Rhyme | 0:f2f2c76b9816 | 126 | } |
Rhyme | 0:f2f2c76b9816 | 127 | break ; |
Rhyme | 0:f2f2c76b9816 | 128 | default: |
Rhyme | 0:f2f2c76b9816 | 129 | break ; |
Rhyme | 0:f2f2c76b9816 | 130 | } |
Rhyme | 0:f2f2c76b9816 | 131 | return( next ) ; |
Rhyme | 0:f2f2c76b9816 | 132 | } |
Rhyme | 0:f2f2c76b9816 | 133 | |
Rhyme | 0:f2f2c76b9816 | 134 | bool checkMove(pos_type next) |
Rhyme | 0:f2f2c76b9816 | 135 | { |
Rhyme | 0:f2f2c76b9816 | 136 | bool result = false ; |
Rhyme | 0:f2f2c76b9816 | 137 | |
Rhyme | 0:f2f2c76b9816 | 138 | switch(maze[next.y][next.x]) { |
Rhyme | 0:f2f2c76b9816 | 139 | case POS_PATH: |
Rhyme | 0:f2f2c76b9816 | 140 | case POS_GOAL: |
Rhyme | 0:f2f2c76b9816 | 141 | result = true ; |
Rhyme | 0:f2f2c76b9816 | 142 | break ; |
Rhyme | 0:f2f2c76b9816 | 143 | case POS_START: |
Rhyme | 0:f2f2c76b9816 | 144 | case POS_WALL: |
Rhyme | 0:f2f2c76b9816 | 145 | default: |
Rhyme | 0:f2f2c76b9816 | 146 | result = false ; |
Rhyme | 0:f2f2c76b9816 | 147 | break ; |
Rhyme | 0:f2f2c76b9816 | 148 | } |
Rhyme | 0:f2f2c76b9816 | 149 | return( result ) ; |
Rhyme | 0:f2f2c76b9816 | 150 | } |
Rhyme | 0:f2f2c76b9816 | 151 | |
Rhyme | 0:f2f2c76b9816 | 152 | int main() { |
Rhyme | 0:f2f2c76b9816 | 153 | float res[3] ; |
Rhyme | 0:f2f2c76b9816 | 154 | pos_type current, next, goal ; |
Rhyme | 0:f2f2c76b9816 | 155 | int direction = DIR_STAY ; |
Rhyme | 0:f2f2c76b9816 | 156 | |
Rhyme | 0:f2f2c76b9816 | 157 | tty.cls() ; |
Rhyme | 0:f2f2c76b9816 | 158 | drawTextMaze(¤t, &goal) ; |
Rhyme | 0:f2f2c76b9816 | 159 | tty.locate(current.x+1, current.y+1) ; |
Rhyme | 0:f2f2c76b9816 | 160 | tty.black() ; |
Rhyme | 0:f2f2c76b9816 | 161 | printf("@") ; |
Rhyme | 0:f2f2c76b9816 | 162 | printf("\r\n") ; |
Rhyme | 0:f2f2c76b9816 | 163 | |
Rhyme | 0:f2f2c76b9816 | 164 | for (;;) { |
Rhyme | 0:f2f2c76b9816 | 165 | acc.getAccAllAxis(res) ; |
Rhyme | 0:f2f2c76b9816 | 166 | direction = getDirection(res) ; |
Rhyme | 0:f2f2c76b9816 | 167 | next = getNext(current, direction) ; |
Rhyme | 0:f2f2c76b9816 | 168 | if (((next.x != current.x)||(next.y != current.y)) && checkMove(next)) { |
Rhyme | 0:f2f2c76b9816 | 169 | tty.locate(current.x+1, current.y+1) ; |
Rhyme | 0:f2f2c76b9816 | 170 | printf(" ") ; |
Rhyme | 0:f2f2c76b9816 | 171 | tty.locate(next.x+1, next.y+1) ; |
Rhyme | 0:f2f2c76b9816 | 172 | printf("@") ; |
Rhyme | 0:f2f2c76b9816 | 173 | tty.green() ; |
Rhyme | 0:f2f2c76b9816 | 174 | tty.locate(1, MAZE_H+1) ; |
Rhyme | 0:f2f2c76b9816 | 175 | printf("\r\n") ; |
Rhyme | 0:f2f2c76b9816 | 176 | tty.black() ; |
Rhyme | 0:f2f2c76b9816 | 177 | current = next ; |
Rhyme | 0:f2f2c76b9816 | 178 | if ((next.x == goal.x)&&(next.y == goal.y)) { |
Rhyme | 0:f2f2c76b9816 | 179 | break ; |
Rhyme | 0:f2f2c76b9816 | 180 | } |
Rhyme | 0:f2f2c76b9816 | 181 | } |
Rhyme | 0:f2f2c76b9816 | 182 | wait(0.1) ; |
Rhyme | 0:f2f2c76b9816 | 183 | } |
Rhyme | 0:f2f2c76b9816 | 184 | tty.blue() ; |
Rhyme | 0:f2f2c76b9816 | 185 | tty.frame((MAZE_W/2)-4,(MAZE_H/2)-1,(MAZE_W/2)+4,(MAZE_H/2)+1) ; |
Rhyme | 0:f2f2c76b9816 | 186 | tty.red() ; |
Rhyme | 0:f2f2c76b9816 | 187 | tty.locate((MAZE_W/2)-3, (MAZE_H/2)) ; |
Rhyme | 0:f2f2c76b9816 | 188 | printf("G O A L\n\r") ; |
Rhyme | 0:f2f2c76b9816 | 189 | for (;;) { |
Rhyme | 0:f2f2c76b9816 | 190 | } |
Rhyme | 0:f2f2c76b9816 | 191 | } |