Random-number ordering game controlled via Bluetooth.
Dependencies: 4DGL-uLCD-SE SDFileSystem mbed-rtos mbed wave_player
Fork of Random number ordering with Bluetooth by
main.cpp@0:a78403083f60, 2016-03-17 (annotated)
- Committer:
- antenehtes
- Date:
- Thu Mar 17 17:57:51 2016 +0000
- Revision:
- 0:a78403083f60
Random number ordering with Bluetooth capability
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
antenehtes | 0:a78403083f60 | 1 | #include <string> |
antenehtes | 0:a78403083f60 | 2 | #include "mbed.h" |
antenehtes | 0:a78403083f60 | 3 | #include "uLCD_4DGL.h" |
antenehtes | 0:a78403083f60 | 4 | #include "wave_player.h" |
antenehtes | 0:a78403083f60 | 5 | #include "rtos.h" |
antenehtes | 0:a78403083f60 | 6 | #include "SDFileSystem.h" |
antenehtes | 0:a78403083f60 | 7 | |
antenehtes | 0:a78403083f60 | 8 | DigitalOut led1(LED1); |
antenehtes | 0:a78403083f60 | 9 | DigitalOut led2(LED2); |
antenehtes | 0:a78403083f60 | 10 | uLCD_4DGL uLCD(p28,p27,p30); |
antenehtes | 0:a78403083f60 | 11 | SDFileSystem sd(p5, p6, p7, p8, "sd"); |
antenehtes | 0:a78403083f60 | 12 | //BusOut mbedleds(LED1,LED2,LED3,LED4); |
antenehtes | 0:a78403083f60 | 13 | //BusOut/In is faster than multiple DigitalOut/Ins |
antenehtes | 0:a78403083f60 | 14 | |
antenehtes | 0:a78403083f60 | 15 | Serial blue(p9,p10); |
antenehtes | 0:a78403083f60 | 16 | Serial pc(USBTX, USBRX); |
antenehtes | 0:a78403083f60 | 17 | |
antenehtes | 0:a78403083f60 | 18 | |
antenehtes | 0:a78403083f60 | 19 | AnalogOut DACout(p18); |
antenehtes | 0:a78403083f60 | 20 | wave_player waver(&DACout); |
antenehtes | 0:a78403083f60 | 21 | Ticker ticker; |
antenehtes | 0:a78403083f60 | 22 | Mutex lcd_mutex; |
antenehtes | 0:a78403083f60 | 23 | Mutex count_mutex; |
antenehtes | 0:a78403083f60 | 24 | int count = 0; |
antenehtes | 0:a78403083f60 | 25 | bool done = true; |
antenehtes | 0:a78403083f60 | 26 | |
antenehtes | 0:a78403083f60 | 27 | int xloc = 60; |
antenehtes | 0:a78403083f60 | 28 | int yloc = 60; |
antenehtes | 0:a78403083f60 | 29 | int rv[9][4] = {{1,1,41,41},{43,1,84,41},{86,1,127,41},{1,43,41,84},{43,43,84,84},{86,43,127,84},{1,86,41,127},{43,86,84,127},{86,86,127,127}}; |
antenehtes | 0:a78403083f60 | 30 | int spots[9] = {-1,-1,-1,-1,-1,-1,-1,-1,-1}; |
antenehtes | 0:a78403083f60 | 31 | int emptyloc = -1; |
antenehtes | 0:a78403083f60 | 32 | int temploc = -1; |
antenehtes | 0:a78403083f60 | 33 | |
antenehtes | 0:a78403083f60 | 34 | // Number print variables |
antenehtes | 0:a78403083f60 | 35 | int printx[9] = {2,8,14,2,8,14,2,8,14}; |
antenehtes | 0:a78403083f60 | 36 | int printy[9] = {2,2,2,7,7,7,13,13,13}; |
antenehtes | 0:a78403083f60 | 37 | |
antenehtes | 0:a78403083f60 | 38 | // Menu variables |
antenehtes | 0:a78403083f60 | 39 | int selection = 0; |
antenehtes | 0:a78403083f60 | 40 | int difficulty = 1; // Default difficulty is hard |
antenehtes | 0:a78403083f60 | 41 | int setselect = 1; |
antenehtes | 0:a78403083f60 | 42 | |
antenehtes | 0:a78403083f60 | 43 | |
antenehtes | 0:a78403083f60 | 44 | void increment() |
antenehtes | 0:a78403083f60 | 45 | { |
antenehtes | 0:a78403083f60 | 46 | count_mutex.lock(); |
antenehtes | 0:a78403083f60 | 47 | ++count; |
antenehtes | 0:a78403083f60 | 48 | count_mutex.unlock(); |
antenehtes | 0:a78403083f60 | 49 | } |
antenehtes | 0:a78403083f60 | 50 | |
antenehtes | 0:a78403083f60 | 51 | |
antenehtes | 0:a78403083f60 | 52 | void thread4(void const *args) |
antenehtes | 0:a78403083f60 | 53 | { |
antenehtes | 0:a78403083f60 | 54 | while(true) { // thread loop |
antenehtes | 0:a78403083f60 | 55 | FILE *wave_file; |
antenehtes | 0:a78403083f60 | 56 | string sfx = "/sd/club1.wav"; |
antenehtes | 0:a78403083f60 | 57 | wave_file=fopen(sfx.c_str(),"r"); |
antenehtes | 0:a78403083f60 | 58 | waver.play(wave_file); |
antenehtes | 0:a78403083f60 | 59 | fclose(wave_file); |
antenehtes | 0:a78403083f60 | 60 | } |
antenehtes | 0:a78403083f60 | 61 | } |
antenehtes | 0:a78403083f60 | 62 | |
antenehtes | 0:a78403083f60 | 63 | int getInput() |
antenehtes | 0:a78403083f60 | 64 | { |
antenehtes | 0:a78403083f60 | 65 | |
antenehtes | 0:a78403083f60 | 66 | int direction = -1; |
antenehtes | 0:a78403083f60 | 67 | |
antenehtes | 0:a78403083f60 | 68 | // pc.printf("getInput()\n"); |
antenehtes | 0:a78403083f60 | 69 | pc.printf("blue.getc() = %d\n\n\n",blue.getc()); |
antenehtes | 0:a78403083f60 | 70 | while(direction < 0) { |
antenehtes | 0:a78403083f60 | 71 | char bnum=0; |
antenehtes | 0:a78403083f60 | 72 | char bhit=0; |
antenehtes | 0:a78403083f60 | 73 | if (blue.getc()=='!') { |
antenehtes | 0:a78403083f60 | 74 | pc.printf("first if\n"); |
antenehtes | 0:a78403083f60 | 75 | if (blue.getc()=='B') { //button data packet |
antenehtes | 0:a78403083f60 | 76 | bnum = blue.getc(); //button number |
antenehtes | 0:a78403083f60 | 77 | bhit = blue.getc(); //1=hit, 0=release |
antenehtes | 0:a78403083f60 | 78 | if (blue.getc()==char(~('!' + 'B' + bnum + bhit))) { //checksum OK? |
antenehtes | 0:a78403083f60 | 79 | pc.printf("before switch\n"); |
antenehtes | 0:a78403083f60 | 80 | pc.printf("bnum %d\n\n",bnum); |
antenehtes | 0:a78403083f60 | 81 | switch (bnum) { |
antenehtes | 0:a78403083f60 | 82 | case '1': //number button 1 |
antenehtes | 0:a78403083f60 | 83 | if (bhit=='1') { |
antenehtes | 0:a78403083f60 | 84 | direction = 5; |
antenehtes | 0:a78403083f60 | 85 | pc.printf("fire"); |
antenehtes | 0:a78403083f60 | 86 | } |
antenehtes | 0:a78403083f60 | 87 | break; |
antenehtes | 0:a78403083f60 | 88 | case '2': //number button 2 |
antenehtes | 0:a78403083f60 | 89 | if (bhit=='1') { |
antenehtes | 0:a78403083f60 | 90 | //add hit code here |
antenehtes | 0:a78403083f60 | 91 | } |
antenehtes | 0:a78403083f60 | 92 | break; |
antenehtes | 0:a78403083f60 | 93 | case '3': //number button 3 |
antenehtes | 0:a78403083f60 | 94 | if (bhit=='1') { |
antenehtes | 0:a78403083f60 | 95 | //add hit code here |
antenehtes | 0:a78403083f60 | 96 | } |
antenehtes | 0:a78403083f60 | 97 | break; |
antenehtes | 0:a78403083f60 | 98 | case '4': //number button 4 |
antenehtes | 0:a78403083f60 | 99 | if (bhit=='1') { |
antenehtes | 0:a78403083f60 | 100 | //add hit code here |
antenehtes | 0:a78403083f60 | 101 | } |
antenehtes | 0:a78403083f60 | 102 | break; |
antenehtes | 0:a78403083f60 | 103 | case '5': //button 5 up arrow |
antenehtes | 0:a78403083f60 | 104 | if (bhit=='1') { |
antenehtes | 0:a78403083f60 | 105 | direction = 1; |
antenehtes | 0:a78403083f60 | 106 | pc.printf("up"); |
antenehtes | 0:a78403083f60 | 107 | } |
antenehtes | 0:a78403083f60 | 108 | break; |
antenehtes | 0:a78403083f60 | 109 | case '6': //button 6 down arrow |
antenehtes | 0:a78403083f60 | 110 | if (bhit=='1') { |
antenehtes | 0:a78403083f60 | 111 | direction = 3; |
antenehtes | 0:a78403083f60 | 112 | pc.printf("down"); |
antenehtes | 0:a78403083f60 | 113 | } |
antenehtes | 0:a78403083f60 | 114 | break; |
antenehtes | 0:a78403083f60 | 115 | case '7': //button 7 left arrow |
antenehtes | 0:a78403083f60 | 116 | if (bhit=='1') { |
antenehtes | 0:a78403083f60 | 117 | direction = 2;; |
antenehtes | 0:a78403083f60 | 118 | pc.printf("left"); |
antenehtes | 0:a78403083f60 | 119 | } |
antenehtes | 0:a78403083f60 | 120 | break; |
antenehtes | 0:a78403083f60 | 121 | case '8': //button 8 right arrow |
antenehtes | 0:a78403083f60 | 122 | if (bhit=='1') { |
antenehtes | 0:a78403083f60 | 123 | direction = 0; |
antenehtes | 0:a78403083f60 | 124 | pc.printf("right"); |
antenehtes | 0:a78403083f60 | 125 | } |
antenehtes | 0:a78403083f60 | 126 | break; |
antenehtes | 0:a78403083f60 | 127 | default: |
antenehtes | 0:a78403083f60 | 128 | break; |
antenehtes | 0:a78403083f60 | 129 | } |
antenehtes | 0:a78403083f60 | 130 | } |
antenehtes | 0:a78403083f60 | 131 | } |
antenehtes | 0:a78403083f60 | 132 | } |
antenehtes | 0:a78403083f60 | 133 | |
antenehtes | 0:a78403083f60 | 134 | wait(0.01); |
antenehtes | 0:a78403083f60 | 135 | } |
antenehtes | 0:a78403083f60 | 136 | |
antenehtes | 0:a78403083f60 | 137 | return direction; |
antenehtes | 0:a78403083f60 | 138 | } |
antenehtes | 0:a78403083f60 | 139 | |
antenehtes | 0:a78403083f60 | 140 | |
antenehtes | 0:a78403083f60 | 141 | void drawRectangles() |
antenehtes | 0:a78403083f60 | 142 | { |
antenehtes | 0:a78403083f60 | 143 | for(int i = 0; i < 9; i++) { |
antenehtes | 0:a78403083f60 | 144 | if(i != emptyloc) |
antenehtes | 0:a78403083f60 | 145 | uLCD.filled_rectangle(rv[i][0],rv[i][1],rv[i][2],rv[i][3],BLUE); |
antenehtes | 0:a78403083f60 | 146 | else |
antenehtes | 0:a78403083f60 | 147 | uLCD.filled_rectangle(rv[i][0],rv[i][1],rv[i][2],rv[i][3],BLACK); |
antenehtes | 0:a78403083f60 | 148 | } |
antenehtes | 0:a78403083f60 | 149 | } |
antenehtes | 0:a78403083f60 | 150 | |
antenehtes | 0:a78403083f60 | 151 | // Registers joystick flick as a directino change |
antenehtes | 0:a78403083f60 | 152 | void updateLocations(int direction) |
antenehtes | 0:a78403083f60 | 153 | { |
antenehtes | 0:a78403083f60 | 154 | temploc = emptyloc; |
antenehtes | 0:a78403083f60 | 155 | |
antenehtes | 0:a78403083f60 | 156 | if(direction == 0) { |
antenehtes | 0:a78403083f60 | 157 | if(emptyloc !=2 && emptyloc != 5 && emptyloc != 8) |
antenehtes | 0:a78403083f60 | 158 | emptyloc = emptyloc + 1; //new empty location |
antenehtes | 0:a78403083f60 | 159 | } else if(direction == 3) { |
antenehtes | 0:a78403083f60 | 160 | if(emptyloc != 6 && emptyloc != 7 && emptyloc !=8) |
antenehtes | 0:a78403083f60 | 161 | emptyloc = emptyloc + 3; |
antenehtes | 0:a78403083f60 | 162 | } else if(direction == 2) { |
antenehtes | 0:a78403083f60 | 163 | if(emptyloc != 0 && emptyloc != 3 && emptyloc != 6) |
antenehtes | 0:a78403083f60 | 164 | emptyloc = emptyloc - 1; |
antenehtes | 0:a78403083f60 | 165 | } else if(direction == 1) { |
antenehtes | 0:a78403083f60 | 166 | if(emptyloc != 0 && emptyloc != 1 && emptyloc != 2) |
antenehtes | 0:a78403083f60 | 167 | emptyloc = emptyloc - 3; |
antenehtes | 0:a78403083f60 | 168 | } |
antenehtes | 0:a78403083f60 | 169 | } |
antenehtes | 0:a78403083f60 | 170 | |
antenehtes | 0:a78403083f60 | 171 | // Updates spots array and reprints the numbers in the rectangles |
antenehtes | 0:a78403083f60 | 172 | void updateNumbers() |
antenehtes | 0:a78403083f60 | 173 | { |
antenehtes | 0:a78403083f60 | 174 | //Flip empty box with box next to its number |
antenehtes | 0:a78403083f60 | 175 | spots[temploc] = spots[emptyloc]; |
antenehtes | 0:a78403083f60 | 176 | spots[emptyloc] = 0; |
antenehtes | 0:a78403083f60 | 177 | |
antenehtes | 0:a78403083f60 | 178 | for(int i=0; i<9; i++) { |
antenehtes | 0:a78403083f60 | 179 | uLCD.locate(printx[i],printy[i]); |
antenehtes | 0:a78403083f60 | 180 | if(spots[i] != 0) |
antenehtes | 0:a78403083f60 | 181 | uLCD.printf("%d",spots[i]); |
antenehtes | 0:a78403083f60 | 182 | } |
antenehtes | 0:a78403083f60 | 183 | } |
antenehtes | 0:a78403083f60 | 184 | |
antenehtes | 0:a78403083f60 | 185 | // Check if boxes are ordered correctly |
antenehtes | 0:a78403083f60 | 186 | int checkWinCondition() |
antenehtes | 0:a78403083f60 | 187 | { |
antenehtes | 0:a78403083f60 | 188 | int winner = 1; |
antenehtes | 0:a78403083f60 | 189 | |
antenehtes | 0:a78403083f60 | 190 | for(int i=0; i<9; i++) { |
antenehtes | 0:a78403083f60 | 191 | if(spots[i] != i) |
antenehtes | 0:a78403083f60 | 192 | winner = 0; |
antenehtes | 0:a78403083f60 | 193 | } |
antenehtes | 0:a78403083f60 | 194 | |
antenehtes | 0:a78403083f60 | 195 | return winner; |
antenehtes | 0:a78403083f60 | 196 | } |
antenehtes | 0:a78403083f60 | 197 | |
antenehtes | 0:a78403083f60 | 198 | // For help when randomly generating the board |
antenehtes | 0:a78403083f60 | 199 | int already(int dist,int lookfor) |
antenehtes | 0:a78403083f60 | 200 | { |
antenehtes | 0:a78403083f60 | 201 | int found = 0; |
antenehtes | 0:a78403083f60 | 202 | |
antenehtes | 0:a78403083f60 | 203 | for(int i=0; i<dist; i++) { |
antenehtes | 0:a78403083f60 | 204 | if(spots[i] == lookfor) |
antenehtes | 0:a78403083f60 | 205 | found = 1; |
antenehtes | 0:a78403083f60 | 206 | } |
antenehtes | 0:a78403083f60 | 207 | |
antenehtes | 0:a78403083f60 | 208 | return found; |
antenehtes | 0:a78403083f60 | 209 | } |
antenehtes | 0:a78403083f60 | 210 | |
antenehtes | 0:a78403083f60 | 211 | // Generates a gameboard based off of the difficulty settings |
antenehtes | 0:a78403083f60 | 212 | void generateBoard() |
antenehtes | 0:a78403083f60 | 213 | { |
antenehtes | 0:a78403083f60 | 214 | |
antenehtes | 0:a78403083f60 | 215 | srand(time(NULL)); |
antenehtes | 0:a78403083f60 | 216 | int ran = rand() % 9; |
antenehtes | 0:a78403083f60 | 217 | |
antenehtes | 0:a78403083f60 | 218 | if(difficulty == 0) { |
antenehtes | 0:a78403083f60 | 219 | int easyspots[9] = {1,2,0,3,4,5,6,7,8}; |
antenehtes | 0:a78403083f60 | 220 | |
antenehtes | 0:a78403083f60 | 221 | for(int i=0; i<9; i++) { |
antenehtes | 0:a78403083f60 | 222 | spots[i] = easyspots[i]; |
antenehtes | 0:a78403083f60 | 223 | } |
antenehtes | 0:a78403083f60 | 224 | |
antenehtes | 0:a78403083f60 | 225 | emptyloc = 2; |
antenehtes | 0:a78403083f60 | 226 | temploc = 2; |
antenehtes | 0:a78403083f60 | 227 | } else { |
antenehtes | 0:a78403083f60 | 228 | for(int i=0; i<9; i++) { |
antenehtes | 0:a78403083f60 | 229 | // If already in the spots array |
antenehtes | 0:a78403083f60 | 230 | while(already(i,ran) != 0) |
antenehtes | 0:a78403083f60 | 231 | ran = rand() % 9; |
antenehtes | 0:a78403083f60 | 232 | |
antenehtes | 0:a78403083f60 | 233 | if(ran == 0) { |
antenehtes | 0:a78403083f60 | 234 | emptyloc = i; |
antenehtes | 0:a78403083f60 | 235 | temploc = i; |
antenehtes | 0:a78403083f60 | 236 | } |
antenehtes | 0:a78403083f60 | 237 | |
antenehtes | 0:a78403083f60 | 238 | spots[i] = ran; |
antenehtes | 0:a78403083f60 | 239 | } |
antenehtes | 0:a78403083f60 | 240 | } |
antenehtes | 0:a78403083f60 | 241 | |
antenehtes | 0:a78403083f60 | 242 | uLCD.locate(4,7); |
antenehtes | 0:a78403083f60 | 243 | uLCD.printf("Shuffling"); |
antenehtes | 0:a78403083f60 | 244 | wait(0.2); |
antenehtes | 0:a78403083f60 | 245 | uLCD.locate(4,7); |
antenehtes | 0:a78403083f60 | 246 | uLCD.printf("Shuffling.."); |
antenehtes | 0:a78403083f60 | 247 | wait(0.2); |
antenehtes | 0:a78403083f60 | 248 | uLCD.locate(4,7); |
antenehtes | 0:a78403083f60 | 249 | uLCD.printf("Shuffling...."); |
antenehtes | 0:a78403083f60 | 250 | wait(0.2); |
antenehtes | 0:a78403083f60 | 251 | uLCD.cls(); |
antenehtes | 0:a78403083f60 | 252 | wait(0.2); |
antenehtes | 0:a78403083f60 | 253 | } |
antenehtes | 0:a78403083f60 | 254 | |
antenehtes | 0:a78403083f60 | 255 | // Game loop |
antenehtes | 0:a78403083f60 | 256 | void game() |
antenehtes | 0:a78403083f60 | 257 | { |
antenehtes | 0:a78403083f60 | 258 | drawRectangles(); |
antenehtes | 0:a78403083f60 | 259 | updateNumbers(); |
antenehtes | 0:a78403083f60 | 260 | |
antenehtes | 0:a78403083f60 | 261 | int win = 0; |
antenehtes | 0:a78403083f60 | 262 | |
antenehtes | 0:a78403083f60 | 263 | while(1) { |
antenehtes | 0:a78403083f60 | 264 | int direction = getInput(); |
antenehtes | 0:a78403083f60 | 265 | // If center button is clicked, keep checking |
antenehtes | 0:a78403083f60 | 266 | if(direction == 5) |
antenehtes | 0:a78403083f60 | 267 | break; |
antenehtes | 0:a78403083f60 | 268 | |
antenehtes | 0:a78403083f60 | 269 | updateLocations(direction); |
antenehtes | 0:a78403083f60 | 270 | drawRectangles(); |
antenehtes | 0:a78403083f60 | 271 | updateNumbers(); |
antenehtes | 0:a78403083f60 | 272 | win = checkWinCondition(); |
antenehtes | 0:a78403083f60 | 273 | |
antenehtes | 0:a78403083f60 | 274 | if(win == 1) |
antenehtes | 0:a78403083f60 | 275 | break; |
antenehtes | 0:a78403083f60 | 276 | |
antenehtes | 0:a78403083f60 | 277 | wait(0.3); |
antenehtes | 0:a78403083f60 | 278 | } |
antenehtes | 0:a78403083f60 | 279 | uLCD.cls(); |
antenehtes | 0:a78403083f60 | 280 | wait(0.2); |
antenehtes | 0:a78403083f60 | 281 | //uLCD.textbackground_color(BLACK); |
antenehtes | 0:a78403083f60 | 282 | // uLCD.color(RED); |
antenehtes | 0:a78403083f60 | 283 | // uLCD.locate(5,7); |
antenehtes | 0:a78403083f60 | 284 | // uLCD.printf("Game Over!"); |
antenehtes | 0:a78403083f60 | 285 | |
antenehtes | 0:a78403083f60 | 286 | |
antenehtes | 0:a78403083f60 | 287 | if(win == 1) { |
antenehtes | 0:a78403083f60 | 288 | //uLCD.locate(6,10); |
antenehtes | 0:a78403083f60 | 289 | // uLCD.printf("You Won!"); |
antenehtes | 0:a78403083f60 | 290 | uLCD.media_init(); |
antenehtes | 0:a78403083f60 | 291 | uLCD.set_sector_address(0x001D, 0x5C06); |
antenehtes | 0:a78403083f60 | 292 | uLCD.display_image(0,0); |
antenehtes | 0:a78403083f60 | 293 | } else if(win == 0){ |
antenehtes | 0:a78403083f60 | 294 | uLCD.media_init(); |
antenehtes | 0:a78403083f60 | 295 | uLCD.set_sector_address(0x001D, 0x4C01); |
antenehtes | 0:a78403083f60 | 296 | uLCD.display_image(0,0); |
antenehtes | 0:a78403083f60 | 297 | } |
antenehtes | 0:a78403083f60 | 298 | |
antenehtes | 0:a78403083f60 | 299 | int direction = getInput(); |
antenehtes | 0:a78403083f60 | 300 | while(direction != 5) |
antenehtes | 0:a78403083f60 | 301 | getInput(); |
antenehtes | 0:a78403083f60 | 302 | |
antenehtes | 0:a78403083f60 | 303 | uLCD.cls(); |
antenehtes | 0:a78403083f60 | 304 | wait(0.2); |
antenehtes | 0:a78403083f60 | 305 | } |
antenehtes | 0:a78403083f60 | 306 | |
antenehtes | 0:a78403083f60 | 307 | // Settings menu loop |
antenehtes | 0:a78403083f60 | 308 | void settings() |
antenehtes | 0:a78403083f60 | 309 | { |
antenehtes | 0:a78403083f60 | 310 | uLCD.locate(5,4); |
antenehtes | 0:a78403083f60 | 311 | uLCD.printf("Easy Mode"); |
antenehtes | 0:a78403083f60 | 312 | uLCD.locate(5,11); |
antenehtes | 0:a78403083f60 | 313 | uLCD.printf("Hard Mode"); |
antenehtes | 0:a78403083f60 | 314 | |
antenehtes | 0:a78403083f60 | 315 | if(setselect == 0) |
antenehtes | 0:a78403083f60 | 316 | uLCD.line(22, 42, 106, 42, WHITE); |
antenehtes | 0:a78403083f60 | 317 | else |
antenehtes | 0:a78403083f60 | 318 | uLCD.line(22, 98, 106, 98, WHITE); |
antenehtes | 0:a78403083f60 | 319 | |
antenehtes | 0:a78403083f60 | 320 | int direction = getInput(); |
antenehtes | 0:a78403083f60 | 321 | |
antenehtes | 0:a78403083f60 | 322 | while(direction !=5 ) { |
antenehtes | 0:a78403083f60 | 323 | if(direction == 1 && setselect == 1) { |
antenehtes | 0:a78403083f60 | 324 | uLCD.line(22, 98, 106, 98, BLACK); |
antenehtes | 0:a78403083f60 | 325 | uLCD.line(22, 42, 106, 42, WHITE); |
antenehtes | 0:a78403083f60 | 326 | setselect = 0; |
antenehtes | 0:a78403083f60 | 327 | } else if(direction == 3 && setselect == 0) { |
antenehtes | 0:a78403083f60 | 328 | uLCD.line(22, 42, 106, 42, BLACK); |
antenehtes | 0:a78403083f60 | 329 | uLCD.line(22, 98, 106, 98, WHITE); |
antenehtes | 0:a78403083f60 | 330 | setselect = 1; |
antenehtes | 0:a78403083f60 | 331 | } |
antenehtes | 0:a78403083f60 | 332 | |
antenehtes | 0:a78403083f60 | 333 | direction = getInput(); |
antenehtes | 0:a78403083f60 | 334 | } |
antenehtes | 0:a78403083f60 | 335 | |
antenehtes | 0:a78403083f60 | 336 | difficulty = setselect; |
antenehtes | 0:a78403083f60 | 337 | uLCD.cls(); |
antenehtes | 0:a78403083f60 | 338 | wait(0.2); |
antenehtes | 0:a78403083f60 | 339 | } |
antenehtes | 0:a78403083f60 | 340 | |
antenehtes | 0:a78403083f60 | 341 | // Help screen loop |
antenehtes | 0:a78403083f60 | 342 | void help() |
antenehtes | 0:a78403083f60 | 343 | { |
antenehtes | 0:a78403083f60 | 344 | uLCD.locate(1,1); |
antenehtes | 0:a78403083f60 | 345 | uLCD.printf("Change difficulty in settings"); |
antenehtes | 0:a78403083f60 | 346 | uLCD.locate(1,4); |
antenehtes | 0:a78403083f60 | 347 | uLCD.printf("Flick stick to\n move blocks"); |
antenehtes | 0:a78403083f60 | 348 | uLCD.locate(1,7); |
antenehtes | 0:a78403083f60 | 349 | uLCD.printf("Empty block in\n top left"); |
antenehtes | 0:a78403083f60 | 350 | uLCD.locate(1,10); |
antenehtes | 0:a78403083f60 | 351 | uLCD.printf("Click to reset\n back to menu"); |
antenehtes | 0:a78403083f60 | 352 | uLCD.locate(1,14); |
antenehtes | 0:a78403083f60 | 353 | uLCD.printf("Click to return\n to menu now"); |
antenehtes | 0:a78403083f60 | 354 | |
antenehtes | 0:a78403083f60 | 355 | int direction = getInput(); |
antenehtes | 0:a78403083f60 | 356 | |
antenehtes | 0:a78403083f60 | 357 | while(direction != 5) { |
antenehtes | 0:a78403083f60 | 358 | direction = getInput(); |
antenehtes | 0:a78403083f60 | 359 | } |
antenehtes | 0:a78403083f60 | 360 | |
antenehtes | 0:a78403083f60 | 361 | uLCD.cls(); |
antenehtes | 0:a78403083f60 | 362 | wait(0.2); |
antenehtes | 0:a78403083f60 | 363 | } |
antenehtes | 0:a78403083f60 | 364 | |
antenehtes | 0:a78403083f60 | 365 | |
antenehtes | 0:a78403083f60 | 366 | int main() |
antenehtes | 0:a78403083f60 | 367 | { |
antenehtes | 0:a78403083f60 | 368 | uLCD.cls(); |
antenehtes | 0:a78403083f60 | 369 | uLCD.baudrate(3000000); |
antenehtes | 0:a78403083f60 | 370 | |
antenehtes | 0:a78403083f60 | 371 | uLCD.textbackground_color(BLACK); |
antenehtes | 0:a78403083f60 | 372 | uLCD.color(RED); |
antenehtes | 0:a78403083f60 | 373 | |
antenehtes | 0:a78403083f60 | 374 | uLCD.locate(4,4); |
antenehtes | 0:a78403083f60 | 375 | uLCD.printf("Start Game"); |
antenehtes | 0:a78403083f60 | 376 | uLCD.locate(5,7); |
antenehtes | 0:a78403083f60 | 377 | uLCD.printf("Settings"); |
antenehtes | 0:a78403083f60 | 378 | uLCD.locate(7,11); |
antenehtes | 0:a78403083f60 | 379 | uLCD.printf("Help"); |
antenehtes | 0:a78403083f60 | 380 | uLCD.line(22, 42, 106, 42, WHITE); |
antenehtes | 0:a78403083f60 | 381 | Thread thread4task(thread4); |
antenehtes | 0:a78403083f60 | 382 | ticker.attach(&increment, 1.0); |
antenehtes | 0:a78403083f60 | 383 | while(1) { |
antenehtes | 0:a78403083f60 | 384 | |
antenehtes | 0:a78403083f60 | 385 | int direction = getInput(); |
antenehtes | 0:a78403083f60 | 386 | |
antenehtes | 0:a78403083f60 | 387 | while(direction == 0 || direction == 2) |
antenehtes | 0:a78403083f60 | 388 | direction = getInput(); |
antenehtes | 0:a78403083f60 | 389 | |
antenehtes | 0:a78403083f60 | 390 | // If clicked in |
antenehtes | 0:a78403083f60 | 391 | if(direction == 5) { |
antenehtes | 0:a78403083f60 | 392 | if(selection == 0) { |
antenehtes | 0:a78403083f60 | 393 | uLCD.cls(); |
antenehtes | 0:a78403083f60 | 394 | wait(0.2); |
antenehtes | 0:a78403083f60 | 395 | //uLCD.textbackground_color(BLUE); |
antenehtes | 0:a78403083f60 | 396 | generateBoard(); |
antenehtes | 0:a78403083f60 | 397 | game(); |
antenehtes | 0:a78403083f60 | 398 | } else if(selection == 1) { |
antenehtes | 0:a78403083f60 | 399 | uLCD.cls(); |
antenehtes | 0:a78403083f60 | 400 | wait(0.2); |
antenehtes | 0:a78403083f60 | 401 | settings(); |
antenehtes | 0:a78403083f60 | 402 | selection = 0; |
antenehtes | 0:a78403083f60 | 403 | } else if(selection == 2) { |
antenehtes | 0:a78403083f60 | 404 | uLCD.cls(); |
antenehtes | 0:a78403083f60 | 405 | wait(0.2); |
antenehtes | 0:a78403083f60 | 406 | help(); |
antenehtes | 0:a78403083f60 | 407 | selection = 0; |
antenehtes | 0:a78403083f60 | 408 | } |
antenehtes | 0:a78403083f60 | 409 | |
antenehtes | 0:a78403083f60 | 410 | uLCD.locate(4,4); |
antenehtes | 0:a78403083f60 | 411 | uLCD.printf("Start Game"); |
antenehtes | 0:a78403083f60 | 412 | uLCD.locate(5,7); |
antenehtes | 0:a78403083f60 | 413 | uLCD.printf("Settings"); |
antenehtes | 0:a78403083f60 | 414 | uLCD.locate(7,11); |
antenehtes | 0:a78403083f60 | 415 | uLCD.printf("Help"); |
antenehtes | 0:a78403083f60 | 416 | |
antenehtes | 0:a78403083f60 | 417 | uLCD.line(22, 42, 106, 42, WHITE); |
antenehtes | 0:a78403083f60 | 418 | } else if(direction == 1 && (selection == 1 || selection == 2)) { |
antenehtes | 0:a78403083f60 | 419 | selection = selection-1; |
antenehtes | 0:a78403083f60 | 420 | if(selection == 0) { |
antenehtes | 0:a78403083f60 | 421 | uLCD.line(22, 42, 106, 42, WHITE); |
antenehtes | 0:a78403083f60 | 422 | uLCD.line(22, 68, 106, 68, BLACK); |
antenehtes | 0:a78403083f60 | 423 | uLCD.line(22, 98, 106, 98, BLACK); |
antenehtes | 0:a78403083f60 | 424 | } else { |
antenehtes | 0:a78403083f60 | 425 | uLCD.line(22, 42, 106, 42, BLACK); |
antenehtes | 0:a78403083f60 | 426 | uLCD.line(22, 68, 106, 68, WHITE); |
antenehtes | 0:a78403083f60 | 427 | uLCD.line(22, 98, 106, 98, BLACK); |
antenehtes | 0:a78403083f60 | 428 | } |
antenehtes | 0:a78403083f60 | 429 | } else if(direction == 3 && (selection == 0 || selection == 1)) { |
antenehtes | 0:a78403083f60 | 430 | selection = selection+1; |
antenehtes | 0:a78403083f60 | 431 | if(selection == 1) { |
antenehtes | 0:a78403083f60 | 432 | uLCD.line(22, 42, 106, 42, BLACK); |
antenehtes | 0:a78403083f60 | 433 | uLCD.line(22, 68, 106, 68, WHITE); |
antenehtes | 0:a78403083f60 | 434 | uLCD.line(22, 98, 106, 98, BLACK); |
antenehtes | 0:a78403083f60 | 435 | } else { |
antenehtes | 0:a78403083f60 | 436 | uLCD.line(22, 42, 106, 42, BLACK); |
antenehtes | 0:a78403083f60 | 437 | uLCD.line(22, 68, 106, 68, BLACK); |
antenehtes | 0:a78403083f60 | 438 | uLCD.line(22, 98, 106, 98, WHITE); |
antenehtes | 0:a78403083f60 | 439 | } |
antenehtes | 0:a78403083f60 | 440 | } |
antenehtes | 0:a78403083f60 | 441 | |
antenehtes | 0:a78403083f60 | 442 | //Wait to essentialially debounce (want to flick switch) |
antenehtes | 0:a78403083f60 | 443 | wait(0.3); |
antenehtes | 0:a78403083f60 | 444 | |
antenehtes | 0:a78403083f60 | 445 | } |
antenehtes | 0:a78403083f60 | 446 | } |