This is a ball on the Mbed.
Dependencies: 4DGL-uLCD-SE IMUfilter ITG3200 Music mbed-rtos mbed
main.cpp@0:680348a938f8, 2014-03-14 (annotated)
- Committer:
- Strikewolf
- Date:
- Fri Mar 14 17:03:16 2014 +0000
- Revision:
- 0:680348a938f8
Initial commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Strikewolf | 0:680348a938f8 | 1 | #include <math.h> |
Strikewolf | 0:680348a938f8 | 2 | #include <time.h> |
Strikewolf | 0:680348a938f8 | 3 | |
Strikewolf | 0:680348a938f8 | 4 | #include "mbed.h" |
Strikewolf | 0:680348a938f8 | 5 | #include "uLCD_4DGL.h" |
Strikewolf | 0:680348a938f8 | 6 | #include "rtos.h" |
Strikewolf | 0:680348a938f8 | 7 | #include "IMU_RPY.h" |
Strikewolf | 0:680348a938f8 | 8 | |
Strikewolf | 0:680348a938f8 | 9 | // game configuration macros |
Strikewolf | 0:680348a938f8 | 10 | #define NUMBER_OF_ROUNDS 3 |
Strikewolf | 0:680348a938f8 | 11 | #define CURSOR_SPEED 0.5 |
Strikewolf | 0:680348a938f8 | 12 | #define CURSOR_COLOR 0xFFFF00 |
Strikewolf | 0:680348a938f8 | 13 | #define SPACE_COLOR BLACK |
Strikewolf | 0:680348a938f8 | 14 | #define WALL_COLOR RED |
Strikewolf | 0:680348a938f8 | 15 | #define START_COLOR GREEN |
Strikewolf | 0:680348a938f8 | 16 | #define END_COLOR BLUE |
Strikewolf | 0:680348a938f8 | 17 | |
Strikewolf | 0:680348a938f8 | 18 | // game mechanism macros |
Strikewolf | 0:680348a938f8 | 19 | #define MAZE_DIMENSION 25 |
Strikewolf | 0:680348a938f8 | 20 | #define SCALAR (125 / MAZE_DIMENSION) |
Strikewolf | 0:680348a938f8 | 21 | #define START_BLOCK 3 |
Strikewolf | 0:680348a938f8 | 22 | #define END_BLOCK 2 |
Strikewolf | 0:680348a938f8 | 23 | #define WALL_BLOCK 1 |
Strikewolf | 0:680348a938f8 | 24 | #define SPACE_BLOCK 0 |
Strikewolf | 0:680348a938f8 | 25 | |
Strikewolf | 0:680348a938f8 | 26 | // prototypes in alphabetical order |
Strikewolf | 0:680348a938f8 | 27 | bool checkVictory(); |
Strikewolf | 0:680348a938f8 | 28 | void displaySplashScreen(); |
Strikewolf | 0:680348a938f8 | 29 | void displayMaze(); |
Strikewolf | 0:680348a938f8 | 30 | void displayVictory(); |
Strikewolf | 0:680348a938f8 | 31 | void displayEndGame(); |
Strikewolf | 0:680348a938f8 | 32 | void generateMaze(); |
Strikewolf | 0:680348a938f8 | 33 | void depthFirstSearch(int r, int c); |
Strikewolf | 0:680348a938f8 | 34 | void updateVelocity(); |
Strikewolf | 0:680348a938f8 | 35 | void updateBall(); |
Strikewolf | 0:680348a938f8 | 36 | void xthread(void const *args); |
Strikewolf | 0:680348a938f8 | 37 | void ythread(void const *args); |
Strikewolf | 0:680348a938f8 | 38 | |
Strikewolf | 0:680348a938f8 | 39 | // display variables |
Strikewolf | 0:680348a938f8 | 40 | uLCD_4DGL uLCD(p9, p10, p11); |
Strikewolf | 0:680348a938f8 | 41 | |
Strikewolf | 0:680348a938f8 | 42 | // cursor variables |
Strikewolf | 0:680348a938f8 | 43 | Mutex x_mutex, y_mutex; |
Strikewolf | 0:680348a938f8 | 44 | char cursor_x_pos, cursor_y_pos; |
Strikewolf | 0:680348a938f8 | 45 | char old_cursor_x_pos, old_cursor_y_pos; |
Strikewolf | 0:680348a938f8 | 46 | float ballxvel, ballyvel; |
Strikewolf | 0:680348a938f8 | 47 | |
Strikewolf | 0:680348a938f8 | 48 | // start and end variables |
Strikewolf | 0:680348a938f8 | 49 | char start_x, start_y; |
Strikewolf | 0:680348a938f8 | 50 | char end_x, end_y; |
Strikewolf | 0:680348a938f8 | 51 | |
Strikewolf | 0:680348a938f8 | 52 | // maze data structure |
Strikewolf | 0:680348a938f8 | 53 | char maze[MAZE_DIMENSION][MAZE_DIMENSION]; |
Strikewolf | 0:680348a938f8 | 54 | |
Strikewolf | 0:680348a938f8 | 55 | // level counter |
Strikewolf | 0:680348a938f8 | 56 | char level = 0; |
Strikewolf | 0:680348a938f8 | 57 | |
Strikewolf | 0:680348a938f8 | 58 | int main() |
Strikewolf | 0:680348a938f8 | 59 | { |
Strikewolf | 0:680348a938f8 | 60 | pc.printf("Super Mbed Ball!!!!!\n\r"); |
Strikewolf | 0:680348a938f8 | 61 | |
Strikewolf | 0:680348a938f8 | 62 | // set up gyroscope/accelerometer (rpy = roll, pitch, yaw) |
Strikewolf | 0:680348a938f8 | 63 | rpy_init(); |
Strikewolf | 0:680348a938f8 | 64 | |
Strikewolf | 0:680348a938f8 | 65 | //Overclock uLCD |
Strikewolf | 0:680348a938f8 | 66 | uLCD.baudrate(3000000); |
Strikewolf | 0:680348a938f8 | 67 | |
Strikewolf | 0:680348a938f8 | 68 | //Title Screen |
Strikewolf | 0:680348a938f8 | 69 | displaySplashScreen(); |
Strikewolf | 0:680348a938f8 | 70 | |
Strikewolf | 0:680348a938f8 | 71 | //Start physics engine threads |
Strikewolf | 0:680348a938f8 | 72 | Thread t1(xthread); |
Strikewolf | 0:680348a938f8 | 73 | Thread t2(ythread); |
Strikewolf | 0:680348a938f8 | 74 | |
Strikewolf | 0:680348a938f8 | 75 | // each iteration runs a complete game until user wins. |
Strikewolf | 0:680348a938f8 | 76 | // wait for user to complete max allowed levels before |
Strikewolf | 0:680348a938f8 | 77 | // terminating |
Strikewolf | 0:680348a938f8 | 78 | while(level < NUMBER_OF_ROUNDS) { |
Strikewolf | 0:680348a938f8 | 79 | |
Strikewolf | 0:680348a938f8 | 80 | //Initial velocity |
Strikewolf | 0:680348a938f8 | 81 | ballxvel = 0; |
Strikewolf | 0:680348a938f8 | 82 | ballyvel = 0; |
Strikewolf | 0:680348a938f8 | 83 | |
Strikewolf | 0:680348a938f8 | 84 | //create Maze |
Strikewolf | 0:680348a938f8 | 85 | generateMaze(); |
Strikewolf | 0:680348a938f8 | 86 | displayMaze(); |
Strikewolf | 0:680348a938f8 | 87 | |
Strikewolf | 0:680348a938f8 | 88 | //Game Execution Loop |
Strikewolf | 0:680348a938f8 | 89 | while (checkVictory() == false) { |
Strikewolf | 0:680348a938f8 | 90 | updateVelocity(); |
Strikewolf | 0:680348a938f8 | 91 | updateBall(); |
Strikewolf | 0:680348a938f8 | 92 | } |
Strikewolf | 0:680348a938f8 | 93 | |
Strikewolf | 0:680348a938f8 | 94 | // victory splash screen |
Strikewolf | 0:680348a938f8 | 95 | displayVictory(); |
Strikewolf | 0:680348a938f8 | 96 | |
Strikewolf | 0:680348a938f8 | 97 | // increment level counter |
Strikewolf | 0:680348a938f8 | 98 | level++; |
Strikewolf | 0:680348a938f8 | 99 | } |
Strikewolf | 0:680348a938f8 | 100 | |
Strikewolf | 0:680348a938f8 | 101 | // display last end game screen before exiting program |
Strikewolf | 0:680348a938f8 | 102 | displayEndGame(); |
Strikewolf | 0:680348a938f8 | 103 | } |
Strikewolf | 0:680348a938f8 | 104 | |
Strikewolf | 0:680348a938f8 | 105 | // checks if cursor has moved to the end block |
Strikewolf | 0:680348a938f8 | 106 | bool checkVictory() |
Strikewolf | 0:680348a938f8 | 107 | { |
Strikewolf | 0:680348a938f8 | 108 | // check if cursor made it to end row and column |
Strikewolf | 0:680348a938f8 | 109 | return(maze[cursor_x_pos][cursor_y_pos] == END_BLOCK); |
Strikewolf | 0:680348a938f8 | 110 | } |
Strikewolf | 0:680348a938f8 | 111 | |
Strikewolf | 0:680348a938f8 | 112 | // depth first search, called by generateMaze() |
Strikewolf | 0:680348a938f8 | 113 | void depthFirstSearch(int r, int c) |
Strikewolf | 0:680348a938f8 | 114 | { |
Strikewolf | 0:680348a938f8 | 115 | // 4 random direction |
Strikewolf | 0:680348a938f8 | 116 | int randDirs1[] = {1, 2, 3, 4}; |
Strikewolf | 0:680348a938f8 | 117 | int randDirs2[] = {4, 2, 3, 1}; |
Strikewolf | 0:680348a938f8 | 118 | int *randDirs = NULL; |
Strikewolf | 0:680348a938f8 | 119 | |
Strikewolf | 0:680348a938f8 | 120 | if (rand() % 2) |
Strikewolf | 0:680348a938f8 | 121 | randDirs = randDirs1; |
Strikewolf | 0:680348a938f8 | 122 | else |
Strikewolf | 0:680348a938f8 | 123 | randDirs = randDirs2; |
Strikewolf | 0:680348a938f8 | 124 | |
Strikewolf | 0:680348a938f8 | 125 | // Examine each direction |
Strikewolf | 0:680348a938f8 | 126 | for (int i = 0; i < 4; i++) { |
Strikewolf | 0:680348a938f8 | 127 | switch (randDirs[i]) { |
Strikewolf | 0:680348a938f8 | 128 | case 1: // Up |
Strikewolf | 0:680348a938f8 | 129 | // Whether 2 cells up is out or not |
Strikewolf | 0:680348a938f8 | 130 | if (r - 2 <= 0) |
Strikewolf | 0:680348a938f8 | 131 | continue; |
Strikewolf | 0:680348a938f8 | 132 | if (maze[r - 2][c] != 0) { |
Strikewolf | 0:680348a938f8 | 133 | maze[r - 2][c] = SPACE_BLOCK; |
Strikewolf | 0:680348a938f8 | 134 | maze[r - 1][c] = SPACE_BLOCK; |
Strikewolf | 0:680348a938f8 | 135 | depthFirstSearch(r - 2, c); |
Strikewolf | 0:680348a938f8 | 136 | } |
Strikewolf | 0:680348a938f8 | 137 | break; |
Strikewolf | 0:680348a938f8 | 138 | case 2: // Right |
Strikewolf | 0:680348a938f8 | 139 | // Whether 2 cells to the right is out or not |
Strikewolf | 0:680348a938f8 | 140 | if (c + 2 >= MAZE_DIMENSION - 1) |
Strikewolf | 0:680348a938f8 | 141 | continue; |
Strikewolf | 0:680348a938f8 | 142 | if (maze[r][c + 2] != 0) { |
Strikewolf | 0:680348a938f8 | 143 | maze[r][c + 2] = SPACE_BLOCK; |
Strikewolf | 0:680348a938f8 | 144 | maze[r][c + 1] = SPACE_BLOCK; |
Strikewolf | 0:680348a938f8 | 145 | depthFirstSearch(r, c + 2); |
Strikewolf | 0:680348a938f8 | 146 | } |
Strikewolf | 0:680348a938f8 | 147 | break; |
Strikewolf | 0:680348a938f8 | 148 | case 3: // Down |
Strikewolf | 0:680348a938f8 | 149 | // Whether 2 cells down is out or not |
Strikewolf | 0:680348a938f8 | 150 | if (r + 2 >= MAZE_DIMENSION - 1) |
Strikewolf | 0:680348a938f8 | 151 | continue; |
Strikewolf | 0:680348a938f8 | 152 | if (maze[r + 2][c] != 0) { |
Strikewolf | 0:680348a938f8 | 153 | maze[r + 2][c] = SPACE_BLOCK; |
Strikewolf | 0:680348a938f8 | 154 | maze[r + 1][c] = SPACE_BLOCK; |
Strikewolf | 0:680348a938f8 | 155 | depthFirstSearch(r + 2, c); |
Strikewolf | 0:680348a938f8 | 156 | } |
Strikewolf | 0:680348a938f8 | 157 | break; |
Strikewolf | 0:680348a938f8 | 158 | case 4: // Left |
Strikewolf | 0:680348a938f8 | 159 | // Whether 2 cells to the left is out or not |
Strikewolf | 0:680348a938f8 | 160 | if (c - 2 <= 0) |
Strikewolf | 0:680348a938f8 | 161 | continue; |
Strikewolf | 0:680348a938f8 | 162 | if (maze[r][c - 2] != 0) { |
Strikewolf | 0:680348a938f8 | 163 | maze[r][c - 2] = SPACE_BLOCK; |
Strikewolf | 0:680348a938f8 | 164 | maze[r][c - 1] = SPACE_BLOCK; |
Strikewolf | 0:680348a938f8 | 165 | depthFirstSearch(r, c - 2); |
Strikewolf | 0:680348a938f8 | 166 | } |
Strikewolf | 0:680348a938f8 | 167 | break; |
Strikewolf | 0:680348a938f8 | 168 | } |
Strikewolf | 0:680348a938f8 | 169 | } |
Strikewolf | 0:680348a938f8 | 170 | } |
Strikewolf | 0:680348a938f8 | 171 | |
Strikewolf | 0:680348a938f8 | 172 | //Take whats in the mazearr and write it |
Strikewolf | 0:680348a938f8 | 173 | void displayMaze() |
Strikewolf | 0:680348a938f8 | 174 | { |
Strikewolf | 0:680348a938f8 | 175 | |
Strikewolf | 0:680348a938f8 | 176 | // Clear previous maze |
Strikewolf | 0:680348a938f8 | 177 | uLCD.filled_rectangle(0, 0, 127, 127, SPACE_COLOR); |
Strikewolf | 0:680348a938f8 | 178 | |
Strikewolf | 0:680348a938f8 | 179 | // display start location |
Strikewolf | 0:680348a938f8 | 180 | uLCD.filled_rectangle(start_x * SCALAR, start_y * SCALAR, |
Strikewolf | 0:680348a938f8 | 181 | (start_x + 1) * SCALAR - 1, (start_y + 1) * SCALAR - 1, START_COLOR); |
Strikewolf | 0:680348a938f8 | 182 | |
Strikewolf | 0:680348a938f8 | 183 | // display end location |
Strikewolf | 0:680348a938f8 | 184 | uLCD.filled_rectangle(end_x * SCALAR, end_y * SCALAR, |
Strikewolf | 0:680348a938f8 | 185 | (end_x + 1) * SCALAR - 1, (end_y + 1) * SCALAR - 1, END_COLOR); |
Strikewolf | 0:680348a938f8 | 186 | |
Strikewolf | 0:680348a938f8 | 187 | // display walls of maze |
Strikewolf | 0:680348a938f8 | 188 | for (int i = 0; i < MAZE_DIMENSION; i++) { |
Strikewolf | 0:680348a938f8 | 189 | for (int j = 0; j < MAZE_DIMENSION; j++) { |
Strikewolf | 0:680348a938f8 | 190 | if (maze[i][j] == WALL_BLOCK) |
Strikewolf | 0:680348a938f8 | 191 | uLCD.filled_rectangle(i * SCALAR, j * SCALAR, (i + 1) * SCALAR - 1, (j + 1) * SCALAR - 1, WALL_COLOR); |
Strikewolf | 0:680348a938f8 | 192 | } |
Strikewolf | 0:680348a938f8 | 193 | } |
Strikewolf | 0:680348a938f8 | 194 | } |
Strikewolf | 0:680348a938f8 | 195 | |
Strikewolf | 0:680348a938f8 | 196 | //Game title screen |
Strikewolf | 0:680348a938f8 | 197 | void displaySplashScreen() |
Strikewolf | 0:680348a938f8 | 198 | { |
Strikewolf | 0:680348a938f8 | 199 | uLCD.text_width(2); |
Strikewolf | 0:680348a938f8 | 200 | uLCD.text_height(2); |
Strikewolf | 0:680348a938f8 | 201 | uLCD.locate(0, 0); |
Strikewolf | 0:680348a938f8 | 202 | uLCD.printf("SuperMbed"); |
Strikewolf | 0:680348a938f8 | 203 | uLCD.text_width(2); |
Strikewolf | 0:680348a938f8 | 204 | uLCD.text_height(3); |
Strikewolf | 0:680348a938f8 | 205 | uLCD.locate(2, 1); |
Strikewolf | 0:680348a938f8 | 206 | uLCD.printf("Ball!"); |
Strikewolf | 0:680348a938f8 | 207 | wait(3); |
Strikewolf | 0:680348a938f8 | 208 | } |
Strikewolf | 0:680348a938f8 | 209 | |
Strikewolf | 0:680348a938f8 | 210 | //Victory screen - 5 sec delay and then next level |
Strikewolf | 0:680348a938f8 | 211 | void displayVictory() |
Strikewolf | 0:680348a938f8 | 212 | { |
Strikewolf | 0:680348a938f8 | 213 | uLCD.text_width(2); |
Strikewolf | 0:680348a938f8 | 214 | uLCD.text_height(2); |
Strikewolf | 0:680348a938f8 | 215 | uLCD.locate(1, 3); |
Strikewolf | 0:680348a938f8 | 216 | uLCD.printf("VICTORY!"); |
Strikewolf | 0:680348a938f8 | 217 | wait(5); |
Strikewolf | 0:680348a938f8 | 218 | } |
Strikewolf | 0:680348a938f8 | 219 | |
Strikewolf | 0:680348a938f8 | 220 | // End game screen |
Strikewolf | 0:680348a938f8 | 221 | void displayEndGame() { |
Strikewolf | 0:680348a938f8 | 222 | // wipe screen |
Strikewolf | 0:680348a938f8 | 223 | uLCD.filled_rectangle(0, 0, 127, 127, BLACK); |
Strikewolf | 0:680348a938f8 | 224 | |
Strikewolf | 0:680348a938f8 | 225 | // write game over |
Strikewolf | 0:680348a938f8 | 226 | uLCD.text_width(2); |
Strikewolf | 0:680348a938f8 | 227 | uLCD.text_height(2); |
Strikewolf | 0:680348a938f8 | 228 | uLCD.locate(1, 3); |
Strikewolf | 0:680348a938f8 | 229 | uLCD.printf("GAME OVER"); |
Strikewolf | 0:680348a938f8 | 230 | } |
Strikewolf | 0:680348a938f8 | 231 | |
Strikewolf | 0:680348a938f8 | 232 | // randomely generates a maze using depth first search |
Strikewolf | 0:680348a938f8 | 233 | void generateMaze() |
Strikewolf | 0:680348a938f8 | 234 | { |
Strikewolf | 0:680348a938f8 | 235 | // Initialize all spaces to walls |
Strikewolf | 0:680348a938f8 | 236 | for (int i = 0; i < MAZE_DIMENSION; i++) |
Strikewolf | 0:680348a938f8 | 237 | for (int j = 0; j < MAZE_DIMENSION; j++) |
Strikewolf | 0:680348a938f8 | 238 | maze[i][j] = WALL_BLOCK; |
Strikewolf | 0:680348a938f8 | 239 | |
Strikewolf | 0:680348a938f8 | 240 | // unused z-value of gyroscope will be random seed |
Strikewolf | 0:680348a938f8 | 241 | srand(imuFilter.getYaw()); |
Strikewolf | 0:680348a938f8 | 242 | |
Strikewolf | 0:680348a938f8 | 243 | // calculate starting row and column of maze DFS |
Strikewolf | 0:680348a938f8 | 244 | // starting row and column must be an odd number |
Strikewolf | 0:680348a938f8 | 245 | int seed = 0; |
Strikewolf | 0:680348a938f8 | 246 | while(seed % 2 == 0) |
Strikewolf | 0:680348a938f8 | 247 | seed = rand() % (MAZE_DIMENSION -1) + 1; |
Strikewolf | 0:680348a938f8 | 248 | |
Strikewolf | 0:680348a938f8 | 249 | pc.printf("seed: %d\r\n", seed); |
Strikewolf | 0:680348a938f8 | 250 | |
Strikewolf | 0:680348a938f8 | 251 | // Starting cell |
Strikewolf | 0:680348a938f8 | 252 | maze[seed][seed] = SPACE_BLOCK; |
Strikewolf | 0:680348a938f8 | 253 | |
Strikewolf | 0:680348a938f8 | 254 | // Allocate the maze with recursive method |
Strikewolf | 0:680348a938f8 | 255 | depthFirstSearch(seed, seed); |
Strikewolf | 0:680348a938f8 | 256 | |
Strikewolf | 0:680348a938f8 | 257 | // find start and end positions |
Strikewolf | 0:680348a938f8 | 258 | start_x = start_y = 0xF; |
Strikewolf | 0:680348a938f8 | 259 | end_x = end_y = 0; |
Strikewolf | 0:680348a938f8 | 260 | for (int i = 0; i < MAZE_DIMENSION; i++) { |
Strikewolf | 0:680348a938f8 | 261 | for (int j = 0; j < MAZE_DIMENSION; j++) { |
Strikewolf | 0:680348a938f8 | 262 | |
Strikewolf | 0:680348a938f8 | 263 | if (maze[i][j] == SPACE_BLOCK) { |
Strikewolf | 0:680348a938f8 | 264 | // start space |
Strikewolf | 0:680348a938f8 | 265 | if((i*MAZE_DIMENSION + j) < (start_x*MAZE_DIMENSION + start_y)) { |
Strikewolf | 0:680348a938f8 | 266 | start_x = i; |
Strikewolf | 0:680348a938f8 | 267 | start_y = j; |
Strikewolf | 0:680348a938f8 | 268 | } |
Strikewolf | 0:680348a938f8 | 269 | |
Strikewolf | 0:680348a938f8 | 270 | // end space |
Strikewolf | 0:680348a938f8 | 271 | if((i*MAZE_DIMENSION + j) > (end_x*MAZE_DIMENSION + end_y)) { |
Strikewolf | 0:680348a938f8 | 272 | end_x = i; |
Strikewolf | 0:680348a938f8 | 273 | end_y = j; |
Strikewolf | 0:680348a938f8 | 274 | } |
Strikewolf | 0:680348a938f8 | 275 | } |
Strikewolf | 0:680348a938f8 | 276 | } |
Strikewolf | 0:680348a938f8 | 277 | } |
Strikewolf | 0:680348a938f8 | 278 | |
Strikewolf | 0:680348a938f8 | 279 | // reset cursor starting position |
Strikewolf | 0:680348a938f8 | 280 | cursor_x_pos = old_cursor_x_pos = start_x; |
Strikewolf | 0:680348a938f8 | 281 | cursor_y_pos = old_cursor_y_pos = start_y; |
Strikewolf | 0:680348a938f8 | 282 | |
Strikewolf | 0:680348a938f8 | 283 | // mark spots in maze data structure |
Strikewolf | 0:680348a938f8 | 284 | maze[start_x][start_y] = START_BLOCK; |
Strikewolf | 0:680348a938f8 | 285 | maze[end_x][end_y] = END_BLOCK; |
Strikewolf | 0:680348a938f8 | 286 | |
Strikewolf | 0:680348a938f8 | 287 | } |
Strikewolf | 0:680348a938f8 | 288 | |
Strikewolf | 0:680348a938f8 | 289 | //Move the ball around and draw to the screen |
Strikewolf | 0:680348a938f8 | 290 | void updateBall() |
Strikewolf | 0:680348a938f8 | 291 | { |
Strikewolf | 0:680348a938f8 | 292 | x_mutex.lock(); |
Strikewolf | 0:680348a938f8 | 293 | y_mutex.lock(); |
Strikewolf | 0:680348a938f8 | 294 | |
Strikewolf | 0:680348a938f8 | 295 | // redraw ball only if the position has changed |
Strikewolf | 0:680348a938f8 | 296 | if (cursor_x_pos != old_cursor_x_pos || cursor_y_pos != old_cursor_y_pos) { |
Strikewolf | 0:680348a938f8 | 297 | |
Strikewolf | 0:680348a938f8 | 298 | //Wipe the old ball |
Strikewolf | 0:680348a938f8 | 299 | |
Strikewolf | 0:680348a938f8 | 300 | uLCD.filled_rectangle(old_cursor_x_pos * SCALAR, old_cursor_y_pos * SCALAR, |
Strikewolf | 0:680348a938f8 | 301 | (old_cursor_x_pos + 1) * SCALAR - 1, (old_cursor_y_pos + 1) * SCALAR - 1, SPACE_COLOR); |
Strikewolf | 0:680348a938f8 | 302 | |
Strikewolf | 0:680348a938f8 | 303 | //Out with the old in with the new! |
Strikewolf | 0:680348a938f8 | 304 | uLCD.filled_rectangle(cursor_x_pos * SCALAR, cursor_y_pos * SCALAR, |
Strikewolf | 0:680348a938f8 | 305 | (cursor_x_pos + 1) * SCALAR - 1, (cursor_y_pos + 1) * SCALAR - 1, CURSOR_COLOR); |
Strikewolf | 0:680348a938f8 | 306 | |
Strikewolf | 0:680348a938f8 | 307 | // store new position |
Strikewolf | 0:680348a938f8 | 308 | old_cursor_x_pos = cursor_x_pos; |
Strikewolf | 0:680348a938f8 | 309 | old_cursor_y_pos = cursor_y_pos; |
Strikewolf | 0:680348a938f8 | 310 | } |
Strikewolf | 0:680348a938f8 | 311 | |
Strikewolf | 0:680348a938f8 | 312 | x_mutex.unlock(); |
Strikewolf | 0:680348a938f8 | 313 | y_mutex.unlock(); |
Strikewolf | 0:680348a938f8 | 314 | } |
Strikewolf | 0:680348a938f8 | 315 | |
Strikewolf | 0:680348a938f8 | 316 | |
Strikewolf | 0:680348a938f8 | 317 | //This will be where the gyro values are used to accelerate/decelerate the ball |
Strikewolf | 0:680348a938f8 | 318 | void updateVelocity() |
Strikewolf | 0:680348a938f8 | 319 | { |
Strikewolf | 0:680348a938f8 | 320 | x_mutex.lock(); |
Strikewolf | 0:680348a938f8 | 321 | y_mutex.lock(); |
Strikewolf | 0:680348a938f8 | 322 | |
Strikewolf | 0:680348a938f8 | 323 | // sample gyroscope/accelerometer through filter |
Strikewolf | 0:680348a938f8 | 324 | ballxvel = toDegrees(imuFilter.getPitch()) / -10; |
Strikewolf | 0:680348a938f8 | 325 | ballyvel = toDegrees(imuFilter.getRoll()) / 10; |
Strikewolf | 0:680348a938f8 | 326 | |
Strikewolf | 0:680348a938f8 | 327 | // bound velocities to max speed for x |
Strikewolf | 0:680348a938f8 | 328 | if (ballxvel > 1) |
Strikewolf | 0:680348a938f8 | 329 | ballxvel = CURSOR_SPEED; |
Strikewolf | 0:680348a938f8 | 330 | else if (ballxvel < -1) |
Strikewolf | 0:680348a938f8 | 331 | ballxvel = -CURSOR_SPEED; |
Strikewolf | 0:680348a938f8 | 332 | |
Strikewolf | 0:680348a938f8 | 333 | // bound velocities to max speed for y |
Strikewolf | 0:680348a938f8 | 334 | if (ballyvel > 1) |
Strikewolf | 0:680348a938f8 | 335 | ballyvel = CURSOR_SPEED; |
Strikewolf | 0:680348a938f8 | 336 | else if (ballyvel < -1) |
Strikewolf | 0:680348a938f8 | 337 | ballyvel = -CURSOR_SPEED; |
Strikewolf | 0:680348a938f8 | 338 | |
Strikewolf | 0:680348a938f8 | 339 | // round to 2 decimal places |
Strikewolf | 0:680348a938f8 | 340 | ballxvel = floorf(ballxvel * 100.0) / 100.0; |
Strikewolf | 0:680348a938f8 | 341 | ballyvel = floorf(ballyvel * 100.0) / 100.0; |
Strikewolf | 0:680348a938f8 | 342 | |
Strikewolf | 0:680348a938f8 | 343 | x_mutex.unlock(); |
Strikewolf | 0:680348a938f8 | 344 | y_mutex.unlock(); |
Strikewolf | 0:680348a938f8 | 345 | } |
Strikewolf | 0:680348a938f8 | 346 | |
Strikewolf | 0:680348a938f8 | 347 | |
Strikewolf | 0:680348a938f8 | 348 | //xthread and ythread act as the physics engine, simulating velocity and wall detection |
Strikewolf | 0:680348a938f8 | 349 | void xthread(const void* args) |
Strikewolf | 0:680348a938f8 | 350 | { |
Strikewolf | 0:680348a938f8 | 351 | while (1) { |
Strikewolf | 0:680348a938f8 | 352 | x_mutex.lock(); |
Strikewolf | 0:680348a938f8 | 353 | y_mutex.lock(); |
Strikewolf | 0:680348a938f8 | 354 | if (ballxvel > 0) { |
Strikewolf | 0:680348a938f8 | 355 | if (maze[cursor_x_pos + 1][cursor_y_pos] != WALL_BLOCK) |
Strikewolf | 0:680348a938f8 | 356 | cursor_x_pos++; |
Strikewolf | 0:680348a938f8 | 357 | } else if (ballxvel < 0) { |
Strikewolf | 0:680348a938f8 | 358 | if (maze[cursor_x_pos - 1][cursor_y_pos] != WALL_BLOCK) |
Strikewolf | 0:680348a938f8 | 359 | cursor_x_pos--; |
Strikewolf | 0:680348a938f8 | 360 | } |
Strikewolf | 0:680348a938f8 | 361 | x_mutex.unlock(); |
Strikewolf | 0:680348a938f8 | 362 | y_mutex.unlock(); |
Strikewolf | 0:680348a938f8 | 363 | Thread::wait(100 - 98 * abs(ballxvel)); |
Strikewolf | 0:680348a938f8 | 364 | } |
Strikewolf | 0:680348a938f8 | 365 | } |
Strikewolf | 0:680348a938f8 | 366 | |
Strikewolf | 0:680348a938f8 | 367 | void ythread(const void* args) |
Strikewolf | 0:680348a938f8 | 368 | { |
Strikewolf | 0:680348a938f8 | 369 | while (1) { |
Strikewolf | 0:680348a938f8 | 370 | x_mutex.lock(); |
Strikewolf | 0:680348a938f8 | 371 | y_mutex.lock(); |
Strikewolf | 0:680348a938f8 | 372 | if (ballyvel > 0) { |
Strikewolf | 0:680348a938f8 | 373 | if (maze[cursor_x_pos][cursor_y_pos + 1] != WALL_BLOCK) |
Strikewolf | 0:680348a938f8 | 374 | cursor_y_pos++; |
Strikewolf | 0:680348a938f8 | 375 | } else if (ballyvel < 0) { |
Strikewolf | 0:680348a938f8 | 376 | if (maze[cursor_x_pos][cursor_y_pos - 1] != WALL_BLOCK) |
Strikewolf | 0:680348a938f8 | 377 | cursor_y_pos--; |
Strikewolf | 0:680348a938f8 | 378 | } |
Strikewolf | 0:680348a938f8 | 379 | x_mutex.unlock(); |
Strikewolf | 0:680348a938f8 | 380 | y_mutex.unlock(); |
Strikewolf | 0:680348a938f8 | 381 | Thread::wait(100 - 98 * abs(ballyvel)); |
Strikewolf | 0:680348a938f8 | 382 | } |
Strikewolf | 0:680348a938f8 | 383 | } |
Strikewolf | 0:680348a938f8 | 384 |