Dance Dance Revolution program for main mbed
Dependencies: 4DGL-uLCD-SE SDFileSystem mbed-rtos mbed wave_player
main.cpp@0:8a02ac09eae0, 2016-04-29 (annotated)
- Committer:
- wliu88
- Date:
- Fri Apr 29 02:04:07 2016 +0000
- Revision:
- 0:8a02ac09eae0
new
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wliu88 | 0:8a02ac09eae0 | 1 | |
wliu88 | 0:8a02ac09eae0 | 2 | //FULL GAME (MAIN MBED) |
wliu88 | 0:8a02ac09eae0 | 3 | |
wliu88 | 0:8a02ac09eae0 | 4 | #include "mbed.h" |
wliu88 | 0:8a02ac09eae0 | 5 | #include "uLCD_4DGL.h" |
wliu88 | 0:8a02ac09eae0 | 6 | #include "rtos.h" |
wliu88 | 0:8a02ac09eae0 | 7 | #include "SDFileSystem.h" |
wliu88 | 0:8a02ac09eae0 | 8 | #include <string> |
wliu88 | 0:8a02ac09eae0 | 9 | #include <vector> |
wliu88 | 0:8a02ac09eae0 | 10 | #include "joystick.h" |
wliu88 | 0:8a02ac09eae0 | 11 | #include "background.h" |
wliu88 | 0:8a02ac09eae0 | 12 | #include "arrows.h" |
wliu88 | 0:8a02ac09eae0 | 13 | #include "releaser.h" |
wliu88 | 0:8a02ac09eae0 | 14 | #include "songs.h" |
wliu88 | 0:8a02ac09eae0 | 15 | |
wliu88 | 0:8a02ac09eae0 | 16 | |
wliu88 | 0:8a02ac09eae0 | 17 | Mutex mutex; |
wliu88 | 0:8a02ac09eae0 | 18 | Mutex m; |
wliu88 | 0:8a02ac09eae0 | 19 | Mutex arm; |
wliu88 | 0:8a02ac09eae0 | 20 | uLCD_4DGL uLCD2(p9,p10,p16); // serial tx, serial rx, reset pin; |
wliu88 | 0:8a02ac09eae0 | 21 | uLCD_4DGL uLCD(p28,p27,p29); |
wliu88 | 0:8a02ac09eae0 | 22 | SDFileSystem sd(p5, p6, p7, p8, "sd"); |
wliu88 | 0:8a02ac09eae0 | 23 | //InterruptIn pb1(p22); |
wliu88 | 0:8a02ac09eae0 | 24 | //InterruptIn pb2(p21); |
wliu88 | 0:8a02ac09eae0 | 25 | volatile bool up1 = false; |
wliu88 | 0:8a02ac09eae0 | 26 | volatile bool down1 = false; |
wliu88 | 0:8a02ac09eae0 | 27 | volatile bool left1 = false; |
wliu88 | 0:8a02ac09eae0 | 28 | volatile bool right1 = false; |
wliu88 | 0:8a02ac09eae0 | 29 | volatile bool fire1 = false; |
wliu88 | 0:8a02ac09eae0 | 30 | volatile bool up2 = false; |
wliu88 | 0:8a02ac09eae0 | 31 | volatile bool down2 = false; |
wliu88 | 0:8a02ac09eae0 | 32 | volatile bool left2 = false; |
wliu88 | 0:8a02ac09eae0 | 33 | volatile bool right2 = false; |
wliu88 | 0:8a02ac09eae0 | 34 | volatile bool fire2 = false; |
wliu88 | 0:8a02ac09eae0 | 35 | volatile int song1 = 0; |
wliu88 | 0:8a02ac09eae0 | 36 | volatile int song2 = 0; |
wliu88 | 0:8a02ac09eae0 | 37 | Nav_Switch js1( p12, p14, p13, p15, p11, &up1, &down1, &left1, &right1, &fire1,&song1); //Nav_Switch(PinName up,PinName down,PinName left,PinName right,PinName fire); |
wliu88 | 0:8a02ac09eae0 | 38 | Nav_Switch js2( p23, p25, p24, p21, p22, &up2, &down2, &left2, &right2, &fire2,&song2); //Nav_Switch(PinName up,PinName down,PinName left,PinName right,PinName fire); |
wliu88 | 0:8a02ac09eae0 | 39 | // variables used for controlling the process |
wliu88 | 0:8a02ac09eae0 | 40 | volatile bool pushed1 = false; |
wliu88 | 0:8a02ac09eae0 | 41 | volatile bool pushed2 = false; |
wliu88 | 0:8a02ac09eae0 | 42 | volatile bool selected1 = false; |
wliu88 | 0:8a02ac09eae0 | 43 | volatile bool selected2 = false; |
wliu88 | 0:8a02ac09eae0 | 44 | volatile bool process1 = false; |
wliu88 | 0:8a02ac09eae0 | 45 | volatile bool process2 = false; |
wliu88 | 0:8a02ac09eae0 | 46 | volatile bool finished1 = false; |
wliu88 | 0:8a02ac09eae0 | 47 | volatile bool finished2 = false; |
wliu88 | 0:8a02ac09eae0 | 48 | ArrowReleaser ar = ArrowReleaser(); |
wliu88 | 0:8a02ac09eae0 | 49 | ArrowReleaser ar2 = ArrowReleaser(); |
wliu88 | 0:8a02ac09eae0 | 50 | double nextdelay = 1.0; |
wliu88 | 0:8a02ac09eae0 | 51 | int indx = 0; |
wliu88 | 0:8a02ac09eae0 | 52 | int chk; |
wliu88 | 0:8a02ac09eae0 | 53 | int chk2; |
wliu88 | 0:8a02ac09eae0 | 54 | volatile int counter = 0; |
wliu88 | 0:8a02ac09eae0 | 55 | volatile int counter2 = 0; |
wliu88 | 0:8a02ac09eae0 | 56 | volatile bool empty = false; |
wliu88 | 0:8a02ac09eae0 | 57 | volatile bool scoreScreen = false; |
wliu88 | 0:8a02ac09eae0 | 58 | DigitalOut songSelected(p19); |
wliu88 | 0:8a02ac09eae0 | 59 | DigitalOut specificSong(p20); |
wliu88 | 0:8a02ac09eae0 | 60 | |
wliu88 | 0:8a02ac09eae0 | 61 | |
wliu88 | 0:8a02ac09eae0 | 62 | |
wliu88 | 0:8a02ac09eae0 | 63 | |
wliu88 | 0:8a02ac09eae0 | 64 | vector<string> filenames; |
wliu88 | 0:8a02ac09eae0 | 65 | |
wliu88 | 0:8a02ac09eae0 | 66 | // helper function |
wliu88 | 0:8a02ac09eae0 | 67 | // read files names in dir and put them in the variable "filenames" |
wliu88 | 0:8a02ac09eae0 | 68 | void read_file_names(char *dir) |
wliu88 | 0:8a02ac09eae0 | 69 | { |
wliu88 | 0:8a02ac09eae0 | 70 | DIR *dp; |
wliu88 | 0:8a02ac09eae0 | 71 | struct dirent *dirp; |
wliu88 | 0:8a02ac09eae0 | 72 | dp = opendir(dir); |
wliu88 | 0:8a02ac09eae0 | 73 | //read all directory and file names in current directory into filename vector |
wliu88 | 0:8a02ac09eae0 | 74 | while((dirp = readdir(dp)) != NULL) { |
wliu88 | 0:8a02ac09eae0 | 75 | string filename = string(dirp->d_name); |
wliu88 | 0:8a02ac09eae0 | 76 | if ((filename[0] >= 97 && filename[0] <=122) || (filename[0] >= 65 && filename[0] <=90)) { |
wliu88 | 0:8a02ac09eae0 | 77 | filenames.push_back(filename); |
wliu88 | 0:8a02ac09eae0 | 78 | } |
wliu88 | 0:8a02ac09eae0 | 79 | } |
wliu88 | 0:8a02ac09eae0 | 80 | closedir(dp); |
wliu88 | 0:8a02ac09eae0 | 81 | } |
wliu88 | 0:8a02ac09eae0 | 82 | |
wliu88 | 0:8a02ac09eae0 | 83 | void uLCD_thread(void const *args) { |
wliu88 | 0:8a02ac09eae0 | 84 | |
wliu88 | 0:8a02ac09eae0 | 85 | // 1. title screen |
wliu88 | 0:8a02ac09eae0 | 86 | mutex.lock(); |
wliu88 | 0:8a02ac09eae0 | 87 | //uLCD.cls(); |
wliu88 | 0:8a02ac09eae0 | 88 | //uLCD.reset(); |
wliu88 | 0:8a02ac09eae0 | 89 | uLCD.textbackground_color(BLACK); |
wliu88 | 0:8a02ac09eae0 | 90 | uLCD.color(RED); |
wliu88 | 0:8a02ac09eae0 | 91 | uLCD.locate(4,0); |
wliu88 | 0:8a02ac09eae0 | 92 | uLCD.printf("Dance Dance"); |
wliu88 | 0:8a02ac09eae0 | 93 | uLCD.locate(4,1); |
wliu88 | 0:8a02ac09eae0 | 94 | uLCD.printf("Revolution!"); |
wliu88 | 0:8a02ac09eae0 | 95 | uLCD.locate(6,15); |
wliu88 | 0:8a02ac09eae0 | 96 | uLCD.printf("Player 1"); |
wliu88 | 0:8a02ac09eae0 | 97 | mutex.unlock(); |
wliu88 | 0:8a02ac09eae0 | 98 | while(!pushed1) { |
wliu88 | 0:8a02ac09eae0 | 99 | mutex.lock(); |
wliu88 | 0:8a02ac09eae0 | 100 | uLCD.locate(4,7); |
wliu88 | 0:8a02ac09eae0 | 101 | uLCD.printf("Press CENTER"); |
wliu88 | 0:8a02ac09eae0 | 102 | mutex.unlock(); |
wliu88 | 0:8a02ac09eae0 | 103 | Thread::wait(1000); |
wliu88 | 0:8a02ac09eae0 | 104 | mutex.lock(); |
wliu88 | 0:8a02ac09eae0 | 105 | uLCD.locate(4,7); |
wliu88 | 0:8a02ac09eae0 | 106 | uLCD.printf(" "); |
wliu88 | 0:8a02ac09eae0 | 107 | mutex.unlock(); |
wliu88 | 0:8a02ac09eae0 | 108 | pushed1 = fire1; |
wliu88 | 0:8a02ac09eae0 | 109 | } |
wliu88 | 0:8a02ac09eae0 | 110 | |
wliu88 | 0:8a02ac09eae0 | 111 | // 2. ready screen |
wliu88 | 0:8a02ac09eae0 | 112 | mutex.lock(); |
wliu88 | 0:8a02ac09eae0 | 113 | uLCD.cls(); |
wliu88 | 0:8a02ac09eae0 | 114 | uLCD.locate(0,7); |
wliu88 | 0:8a02ac09eae0 | 115 | uLCD.printf("Player 1 is ready!"); |
wliu88 | 0:8a02ac09eae0 | 116 | mutex.unlock(); |
wliu88 | 0:8a02ac09eae0 | 117 | Thread::wait(1000); |
wliu88 | 0:8a02ac09eae0 | 118 | while(!pushed2) { |
wliu88 | 0:8a02ac09eae0 | 119 | mutex.lock(); |
wliu88 | 0:8a02ac09eae0 | 120 | uLCD.locate(0,9); |
wliu88 | 0:8a02ac09eae0 | 121 | uLCD.printf("Wait player 2..."); |
wliu88 | 0:8a02ac09eae0 | 122 | mutex.unlock(); |
wliu88 | 0:8a02ac09eae0 | 123 | } |
wliu88 | 0:8a02ac09eae0 | 124 | |
wliu88 | 0:8a02ac09eae0 | 125 | // 3. song selection screen |
wliu88 | 0:8a02ac09eae0 | 126 | while(!process1) { |
wliu88 | 0:8a02ac09eae0 | 127 | Thread::yield(); |
wliu88 | 0:8a02ac09eae0 | 128 | } |
wliu88 | 0:8a02ac09eae0 | 129 | mutex.lock(); |
wliu88 | 0:8a02ac09eae0 | 130 | uLCD.cls(); |
wliu88 | 0:8a02ac09eae0 | 131 | uLCD.printf("Select a song"); |
wliu88 | 0:8a02ac09eae0 | 132 | mutex.unlock(); |
wliu88 | 0:8a02ac09eae0 | 133 | fire1 = false; |
wliu88 | 0:8a02ac09eae0 | 134 | while(!selected1 || !selected2) { |
wliu88 | 0:8a02ac09eae0 | 135 | for (int i = 0; i < filenames.size(); i++) { |
wliu88 | 0:8a02ac09eae0 | 136 | mutex.lock(); |
wliu88 | 0:8a02ac09eae0 | 137 | if (i == song1) { |
wliu88 | 0:8a02ac09eae0 | 138 | uLCD.color(RED); |
wliu88 | 0:8a02ac09eae0 | 139 | } else { |
wliu88 | 0:8a02ac09eae0 | 140 | uLCD.color(GREEN); |
wliu88 | 0:8a02ac09eae0 | 141 | } |
wliu88 | 0:8a02ac09eae0 | 142 | uLCD.locate(0,i+2); |
wliu88 | 0:8a02ac09eae0 | 143 | uLCD.printf("%s\n\r", filenames[i].c_str()); |
wliu88 | 0:8a02ac09eae0 | 144 | uLCD.locate(16,i+2); |
wliu88 | 0:8a02ac09eae0 | 145 | uLCD.printf(" "); |
wliu88 | 0:8a02ac09eae0 | 146 | if (i == song2) { |
wliu88 | 0:8a02ac09eae0 | 147 | uLCD.color(BLUE); |
wliu88 | 0:8a02ac09eae0 | 148 | uLCD.locate(16,i+2); |
wliu88 | 0:8a02ac09eae0 | 149 | uLCD.printf("<-"); |
wliu88 | 0:8a02ac09eae0 | 150 | } |
wliu88 | 0:8a02ac09eae0 | 151 | mutex.unlock(); |
wliu88 | 0:8a02ac09eae0 | 152 | } |
wliu88 | 0:8a02ac09eae0 | 153 | if (selected1) { |
wliu88 | 0:8a02ac09eae0 | 154 | mutex.lock(); |
wliu88 | 0:8a02ac09eae0 | 155 | uLCD.locate(0,8); |
wliu88 | 0:8a02ac09eae0 | 156 | uLCD.color(RED); |
wliu88 | 0:8a02ac09eae0 | 157 | uLCD.printf("Ready!"); |
wliu88 | 0:8a02ac09eae0 | 158 | mutex.unlock(); |
wliu88 | 0:8a02ac09eae0 | 159 | } |
wliu88 | 0:8a02ac09eae0 | 160 | selected1 = fire1; |
wliu88 | 0:8a02ac09eae0 | 161 | } |
wliu88 | 0:8a02ac09eae0 | 162 | |
wliu88 | 0:8a02ac09eae0 | 163 | |
wliu88 | 0:8a02ac09eae0 | 164 | // 5. count down |
wliu88 | 0:8a02ac09eae0 | 165 | mutex.lock(); |
wliu88 | 0:8a02ac09eae0 | 166 | uLCD.cls(); |
wliu88 | 0:8a02ac09eae0 | 167 | uLCD.color(RED); |
wliu88 | 0:8a02ac09eae0 | 168 | uLCD.printf("%s\n\r", filenames[song1].c_str()); |
wliu88 | 0:8a02ac09eae0 | 169 | uLCD.locate(0,1); |
wliu88 | 0:8a02ac09eae0 | 170 | uLCD.printf("is picked"); |
wliu88 | 0:8a02ac09eae0 | 171 | mutex.unlock(); |
wliu88 | 0:8a02ac09eae0 | 172 | while(!process2) { |
wliu88 | 0:8a02ac09eae0 | 173 | Thread::yield(); |
wliu88 | 0:8a02ac09eae0 | 174 | } |
wliu88 | 0:8a02ac09eae0 | 175 | mutex.lock(); |
wliu88 | 0:8a02ac09eae0 | 176 | uLCD.cls(); |
wliu88 | 0:8a02ac09eae0 | 177 | uLCD.text_width(4); //4X size text |
wliu88 | 0:8a02ac09eae0 | 178 | uLCD.text_height(4); |
wliu88 | 0:8a02ac09eae0 | 179 | mutex.unlock(); |
wliu88 | 0:8a02ac09eae0 | 180 | uLCD.color(RED); |
wliu88 | 0:8a02ac09eae0 | 181 | for (int i=5; i>=0; --i) { |
wliu88 | 0:8a02ac09eae0 | 182 | mutex.lock(); |
wliu88 | 0:8a02ac09eae0 | 183 | uLCD.locate(1,2); |
wliu88 | 0:8a02ac09eae0 | 184 | uLCD.printf("%2D",i); |
wliu88 | 0:8a02ac09eae0 | 185 | mutex.unlock(); |
wliu88 | 0:8a02ac09eae0 | 186 | Thread::wait(1000); |
wliu88 | 0:8a02ac09eae0 | 187 | } |
wliu88 | 0:8a02ac09eae0 | 188 | finished1 = true; |
wliu88 | 0:8a02ac09eae0 | 189 | } |
wliu88 | 0:8a02ac09eae0 | 190 | |
wliu88 | 0:8a02ac09eae0 | 191 | void uLCD2_thread(void const *args) { |
wliu88 | 0:8a02ac09eae0 | 192 | |
wliu88 | 0:8a02ac09eae0 | 193 | // 1. title screen |
wliu88 | 0:8a02ac09eae0 | 194 | mutex.lock(); |
wliu88 | 0:8a02ac09eae0 | 195 | //uuLCD2.cls(); |
wliu88 | 0:8a02ac09eae0 | 196 | //uuLCD2.reset(); |
wliu88 | 0:8a02ac09eae0 | 197 | uLCD2.textbackground_color(BLACK); |
wliu88 | 0:8a02ac09eae0 | 198 | uLCD2.color(BLUE); |
wliu88 | 0:8a02ac09eae0 | 199 | uLCD2.locate(4,0); |
wliu88 | 0:8a02ac09eae0 | 200 | uLCD2.printf("Dance Dance"); |
wliu88 | 0:8a02ac09eae0 | 201 | uLCD2.locate(4,1); |
wliu88 | 0:8a02ac09eae0 | 202 | uLCD2.printf("Revolution!"); |
wliu88 | 0:8a02ac09eae0 | 203 | uLCD2.locate(6,15); |
wliu88 | 0:8a02ac09eae0 | 204 | uLCD2.printf("Player 2"); |
wliu88 | 0:8a02ac09eae0 | 205 | mutex.unlock(); |
wliu88 | 0:8a02ac09eae0 | 206 | while(!pushed2) { |
wliu88 | 0:8a02ac09eae0 | 207 | mutex.lock(); |
wliu88 | 0:8a02ac09eae0 | 208 | uLCD2.locate(4,7); |
wliu88 | 0:8a02ac09eae0 | 209 | uLCD2.printf("Press CENTER"); |
wliu88 | 0:8a02ac09eae0 | 210 | mutex.unlock(); |
wliu88 | 0:8a02ac09eae0 | 211 | wait(1); |
wliu88 | 0:8a02ac09eae0 | 212 | mutex.lock(); |
wliu88 | 0:8a02ac09eae0 | 213 | uLCD2.locate(4,7); |
wliu88 | 0:8a02ac09eae0 | 214 | uLCD2.printf(" "); |
wliu88 | 0:8a02ac09eae0 | 215 | mutex.unlock(); |
wliu88 | 0:8a02ac09eae0 | 216 | pushed2 = fire2; |
wliu88 | 0:8a02ac09eae0 | 217 | } |
wliu88 | 0:8a02ac09eae0 | 218 | |
wliu88 | 0:8a02ac09eae0 | 219 | // 2. ready screen |
wliu88 | 0:8a02ac09eae0 | 220 | mutex.lock(); |
wliu88 | 0:8a02ac09eae0 | 221 | uLCD2.cls(); |
wliu88 | 0:8a02ac09eae0 | 222 | uLCD2.locate(0,7); |
wliu88 | 0:8a02ac09eae0 | 223 | uLCD2.printf("Player 2 is ready!"); |
wliu88 | 0:8a02ac09eae0 | 224 | mutex.unlock(); |
wliu88 | 0:8a02ac09eae0 | 225 | Thread::wait(1000); |
wliu88 | 0:8a02ac09eae0 | 226 | while(!pushed1){ |
wliu88 | 0:8a02ac09eae0 | 227 | mutex.lock(); |
wliu88 | 0:8a02ac09eae0 | 228 | uLCD2.locate(0,9); |
wliu88 | 0:8a02ac09eae0 | 229 | uLCD2.printf("Wait player 1..."); |
wliu88 | 0:8a02ac09eae0 | 230 | mutex.unlock(); |
wliu88 | 0:8a02ac09eae0 | 231 | } |
wliu88 | 0:8a02ac09eae0 | 232 | |
wliu88 | 0:8a02ac09eae0 | 233 | // 3. song selection screen |
wliu88 | 0:8a02ac09eae0 | 234 | Thread::wait(1000); |
wliu88 | 0:8a02ac09eae0 | 235 | process1 = true; |
wliu88 | 0:8a02ac09eae0 | 236 | mutex.lock(); |
wliu88 | 0:8a02ac09eae0 | 237 | uLCD2.cls(); |
wliu88 | 0:8a02ac09eae0 | 238 | uLCD2.printf("Select a song"); |
wliu88 | 0:8a02ac09eae0 | 239 | mutex.unlock(); |
wliu88 | 0:8a02ac09eae0 | 240 | fire2 = false; |
wliu88 | 0:8a02ac09eae0 | 241 | while(!selected2 || !selected1) { |
wliu88 | 0:8a02ac09eae0 | 242 | for (int i = 0; i < filenames.size(); i++) { |
wliu88 | 0:8a02ac09eae0 | 243 | mutex.lock(); |
wliu88 | 0:8a02ac09eae0 | 244 | if (i == song2) { |
wliu88 | 0:8a02ac09eae0 | 245 | uLCD2.color(BLUE); |
wliu88 | 0:8a02ac09eae0 | 246 | } else { |
wliu88 | 0:8a02ac09eae0 | 247 | uLCD2.color(GREEN); |
wliu88 | 0:8a02ac09eae0 | 248 | } |
wliu88 | 0:8a02ac09eae0 | 249 | uLCD2.locate(0,i+2); |
wliu88 | 0:8a02ac09eae0 | 250 | uLCD2.printf("%s\n\r", filenames[i].c_str()); |
wliu88 | 0:8a02ac09eae0 | 251 | uLCD2.locate(16,i+2); |
wliu88 | 0:8a02ac09eae0 | 252 | uLCD2.printf(" "); |
wliu88 | 0:8a02ac09eae0 | 253 | // add a marker to notify player 2 about player 1's selection |
wliu88 | 0:8a02ac09eae0 | 254 | if (i == song1) { |
wliu88 | 0:8a02ac09eae0 | 255 | uLCD2.color(RED); |
wliu88 | 0:8a02ac09eae0 | 256 | uLCD2.locate(16,i+2); |
wliu88 | 0:8a02ac09eae0 | 257 | uLCD2.printf("<-"); |
wliu88 | 0:8a02ac09eae0 | 258 | } |
wliu88 | 0:8a02ac09eae0 | 259 | mutex.unlock(); |
wliu88 | 0:8a02ac09eae0 | 260 | } |
wliu88 | 0:8a02ac09eae0 | 261 | if (selected2) { |
wliu88 | 0:8a02ac09eae0 | 262 | mutex.lock(); |
wliu88 | 0:8a02ac09eae0 | 263 | uLCD2.locate(0,8); |
wliu88 | 0:8a02ac09eae0 | 264 | uLCD2.color(BLUE); |
wliu88 | 0:8a02ac09eae0 | 265 | uLCD2.printf("Ready!"); |
wliu88 | 0:8a02ac09eae0 | 266 | mutex.unlock(); |
wliu88 | 0:8a02ac09eae0 | 267 | } |
wliu88 | 0:8a02ac09eae0 | 268 | selected2 = fire2; |
wliu88 | 0:8a02ac09eae0 | 269 | } |
wliu88 | 0:8a02ac09eae0 | 270 | |
wliu88 | 0:8a02ac09eae0 | 271 | // 5. count down |
wliu88 | 0:8a02ac09eae0 | 272 | mutex.lock(); |
wliu88 | 0:8a02ac09eae0 | 273 | uLCD2.cls(); |
wliu88 | 0:8a02ac09eae0 | 274 | uLCD2.color(BLUE); |
wliu88 | 0:8a02ac09eae0 | 275 | uLCD2.printf("%s\n\r", filenames[song1].c_str()); |
wliu88 | 0:8a02ac09eae0 | 276 | uLCD2.locate(0,1); |
wliu88 | 0:8a02ac09eae0 | 277 | uLCD2.printf("is picked"); |
wliu88 | 0:8a02ac09eae0 | 278 | mutex.unlock(); |
wliu88 | 0:8a02ac09eae0 | 279 | Thread::wait(3000); |
wliu88 | 0:8a02ac09eae0 | 280 | process2 = true; |
wliu88 | 0:8a02ac09eae0 | 281 | mutex.lock(); |
wliu88 | 0:8a02ac09eae0 | 282 | uLCD2.cls(); |
wliu88 | 0:8a02ac09eae0 | 283 | uLCD2.text_width(4); //4X size text |
wliu88 | 0:8a02ac09eae0 | 284 | uLCD2.text_height(4); |
wliu88 | 0:8a02ac09eae0 | 285 | mutex.unlock(); |
wliu88 | 0:8a02ac09eae0 | 286 | uLCD2.color(BLUE); |
wliu88 | 0:8a02ac09eae0 | 287 | for (int i=5; i>=0; --i) { |
wliu88 | 0:8a02ac09eae0 | 288 | mutex.lock(); |
wliu88 | 0:8a02ac09eae0 | 289 | uLCD2.locate(1,2); |
wliu88 | 0:8a02ac09eae0 | 290 | uLCD2.printf("%2D",i); |
wliu88 | 0:8a02ac09eae0 | 291 | mutex.unlock(); |
wliu88 | 0:8a02ac09eae0 | 292 | Thread::wait(1000); |
wliu88 | 0:8a02ac09eae0 | 293 | } |
wliu88 | 0:8a02ac09eae0 | 294 | finished2 = true; |
wliu88 | 0:8a02ac09eae0 | 295 | } |
wliu88 | 0:8a02ac09eae0 | 296 | |
wliu88 | 0:8a02ac09eae0 | 297 | //GAME |
wliu88 | 0:8a02ac09eae0 | 298 | |
wliu88 | 0:8a02ac09eae0 | 299 | void arrowreleasing_thread1(void const *args) { |
wliu88 | 0:8a02ac09eae0 | 300 | Thread::wait(100); |
wliu88 | 0:8a02ac09eae0 | 301 | if(!song1){ |
wliu88 | 0:8a02ac09eae0 | 302 | while(1){ |
wliu88 | 0:8a02ac09eae0 | 303 | chk = 0; chk2 =0; |
wliu88 | 0:8a02ac09eae0 | 304 | while(!chk && !chk2){ |
wliu88 | 0:8a02ac09eae0 | 305 | arm.lock(); |
wliu88 | 0:8a02ac09eae0 | 306 | m.lock(); |
wliu88 | 0:8a02ac09eae0 | 307 | chk = ar.ReleaseArrow(DemoArray2,indx,2); |
wliu88 | 0:8a02ac09eae0 | 308 | m.unlock(); |
wliu88 | 0:8a02ac09eae0 | 309 | arm.unlock(); |
wliu88 | 0:8a02ac09eae0 | 310 | Thread::wait(100); |
wliu88 | 0:8a02ac09eae0 | 311 | } |
wliu88 | 0:8a02ac09eae0 | 312 | indx++; |
wliu88 | 0:8a02ac09eae0 | 313 | if(indx == 53){ |
wliu88 | 0:8a02ac09eae0 | 314 | arm.lock(); |
wliu88 | 0:8a02ac09eae0 | 315 | ar.setTermStatus(1); |
wliu88 | 0:8a02ac09eae0 | 316 | arm.unlock(); |
wliu88 | 0:8a02ac09eae0 | 317 | break; |
wliu88 | 0:8a02ac09eae0 | 318 | } |
wliu88 | 0:8a02ac09eae0 | 319 | nextdelay = DemoArray2[indx-1].getDelay(); |
wliu88 | 0:8a02ac09eae0 | 320 | Thread::wait(nextdelay*1000); |
wliu88 | 0:8a02ac09eae0 | 321 | } |
wliu88 | 0:8a02ac09eae0 | 322 | } |
wliu88 | 0:8a02ac09eae0 | 323 | else{ |
wliu88 | 0:8a02ac09eae0 | 324 | while(1){ |
wliu88 | 0:8a02ac09eae0 | 325 | chk = 0; chk2 =0; |
wliu88 | 0:8a02ac09eae0 | 326 | while(!chk && !chk2){ |
wliu88 | 0:8a02ac09eae0 | 327 | arm.lock(); |
wliu88 | 0:8a02ac09eae0 | 328 | m.lock(); |
wliu88 | 0:8a02ac09eae0 | 329 | chk = ar.ReleaseArrow(DemoArray,indx,2); |
wliu88 | 0:8a02ac09eae0 | 330 | m.unlock(); |
wliu88 | 0:8a02ac09eae0 | 331 | arm.unlock(); |
wliu88 | 0:8a02ac09eae0 | 332 | Thread::wait(100); |
wliu88 | 0:8a02ac09eae0 | 333 | } |
wliu88 | 0:8a02ac09eae0 | 334 | indx++; |
wliu88 | 0:8a02ac09eae0 | 335 | if(indx == 53){ |
wliu88 | 0:8a02ac09eae0 | 336 | arm.lock(); |
wliu88 | 0:8a02ac09eae0 | 337 | ar.setTermStatus(1); |
wliu88 | 0:8a02ac09eae0 | 338 | arm.unlock(); |
wliu88 | 0:8a02ac09eae0 | 339 | break; |
wliu88 | 0:8a02ac09eae0 | 340 | } |
wliu88 | 0:8a02ac09eae0 | 341 | nextdelay = DemoArray[indx-1].getDelay(); |
wliu88 | 0:8a02ac09eae0 | 342 | Thread::wait(nextdelay*1000); |
wliu88 | 0:8a02ac09eae0 | 343 | } |
wliu88 | 0:8a02ac09eae0 | 344 | } |
wliu88 | 0:8a02ac09eae0 | 345 | } |
wliu88 | 0:8a02ac09eae0 | 346 | |
wliu88 | 0:8a02ac09eae0 | 347 | void arrowdrawing_thread1(void const *args){ |
wliu88 | 0:8a02ac09eae0 | 348 | int term; |
wliu88 | 0:8a02ac09eae0 | 349 | while(!scoreScreen){ |
wliu88 | 0:8a02ac09eae0 | 350 | for(int i=0; i< 5; i++){ |
wliu88 | 0:8a02ac09eae0 | 351 | arm.lock(); |
wliu88 | 0:8a02ac09eae0 | 352 | if(ar.ActiveArrows[i].getActive()){ |
wliu88 | 0:8a02ac09eae0 | 353 | m.lock(); |
wliu88 | 0:8a02ac09eae0 | 354 | term = ar.ActiveArrows[i].update(2); |
wliu88 | 0:8a02ac09eae0 | 355 | m.unlock(); |
wliu88 | 0:8a02ac09eae0 | 356 | if(term){ |
wliu88 | 0:8a02ac09eae0 | 357 | ar.ActiveArrows[i].setActive(0); |
wliu88 | 0:8a02ac09eae0 | 358 | m.lock(); |
wliu88 | 0:8a02ac09eae0 | 359 | setup_goal_arrows(); |
wliu88 | 0:8a02ac09eae0 | 360 | uLCD.locate(0,0); |
wliu88 | 0:8a02ac09eae0 | 361 | uLCD.printf("%d",counter); |
wliu88 | 0:8a02ac09eae0 | 362 | setup_goal_arrows2(); |
wliu88 | 0:8a02ac09eae0 | 363 | uLCD2.locate(0,0); |
wliu88 | 0:8a02ac09eae0 | 364 | uLCD2.printf("%d",counter2); |
wliu88 | 0:8a02ac09eae0 | 365 | m.unlock(); |
wliu88 | 0:8a02ac09eae0 | 366 | term = 0; |
wliu88 | 0:8a02ac09eae0 | 367 | } |
wliu88 | 0:8a02ac09eae0 | 368 | } |
wliu88 | 0:8a02ac09eae0 | 369 | arm.unlock(); |
wliu88 | 0:8a02ac09eae0 | 370 | } |
wliu88 | 0:8a02ac09eae0 | 371 | /*arm.lock(); |
wliu88 | 0:8a02ac09eae0 | 372 | if(empty && ar.getTermStatus()){ |
wliu88 | 0:8a02ac09eae0 | 373 | arm.unlock(); |
wliu88 | 0:8a02ac09eae0 | 374 | //Song has ended and all arrows have ended |
wliu88 | 0:8a02ac09eae0 | 375 | scoreScreen = true; |
wliu88 | 0:8a02ac09eae0 | 376 | break; |
wliu88 | 0:8a02ac09eae0 | 377 | }*/ |
wliu88 | 0:8a02ac09eae0 | 378 | } |
wliu88 | 0:8a02ac09eae0 | 379 | |
wliu88 | 0:8a02ac09eae0 | 380 | } |
wliu88 | 0:8a02ac09eae0 | 381 | |
wliu88 | 0:8a02ac09eae0 | 382 | |
wliu88 | 0:8a02ac09eae0 | 383 | void joystick2_thread(void const *args) { |
wliu88 | 0:8a02ac09eae0 | 384 | while(!scoreScreen){ |
wliu88 | 0:8a02ac09eae0 | 385 | if(left2){ |
wliu88 | 0:8a02ac09eae0 | 386 | left2 = false; |
wliu88 | 0:8a02ac09eae0 | 387 | for(int i=0; i < 5; i++){ |
wliu88 | 0:8a02ac09eae0 | 388 | arm.lock(); |
wliu88 | 0:8a02ac09eae0 | 389 | if(ar.ActiveArrows[i].getActive() && ar.ActiveArrows[i].getType() == 0 && abs(ar.ActiveArrows[i].curry - refarrow_left_y) <= 10){ |
wliu88 | 0:8a02ac09eae0 | 390 | arm.unlock(); |
wliu88 | 0:8a02ac09eae0 | 391 | counter2++; |
wliu88 | 0:8a02ac09eae0 | 392 | m.lock(); |
wliu88 | 0:8a02ac09eae0 | 393 | uLCD2.locate(0,0); |
wliu88 | 0:8a02ac09eae0 | 394 | uLCD2.printf("%d",counter2); |
wliu88 | 0:8a02ac09eae0 | 395 | m.unlock(); |
wliu88 | 0:8a02ac09eae0 | 396 | break; |
wliu88 | 0:8a02ac09eae0 | 397 | } |
wliu88 | 0:8a02ac09eae0 | 398 | arm.unlock(); |
wliu88 | 0:8a02ac09eae0 | 399 | } |
wliu88 | 0:8a02ac09eae0 | 400 | m.lock(); |
wliu88 | 0:8a02ac09eae0 | 401 | uLCD2.circle(refarrow_left_x,refarrow_left_y,10,0x00FF00); |
wliu88 | 0:8a02ac09eae0 | 402 | m.unlock(); |
wliu88 | 0:8a02ac09eae0 | 403 | wait(0.04); |
wliu88 | 0:8a02ac09eae0 | 404 | m.lock(); |
wliu88 | 0:8a02ac09eae0 | 405 | uLCD2.circle(refarrow_left_x,refarrow_left_y,10,0x000000); |
wliu88 | 0:8a02ac09eae0 | 406 | uLCD2.locate(0,0); |
wliu88 | 0:8a02ac09eae0 | 407 | uLCD2.printf("%d",counter2); |
wliu88 | 0:8a02ac09eae0 | 408 | m.unlock(); |
wliu88 | 0:8a02ac09eae0 | 409 | } |
wliu88 | 0:8a02ac09eae0 | 410 | if(right2){ |
wliu88 | 0:8a02ac09eae0 | 411 | right2 = false; |
wliu88 | 0:8a02ac09eae0 | 412 | for(int i=0; i < 5; i++){ |
wliu88 | 0:8a02ac09eae0 | 413 | arm.lock(); |
wliu88 | 0:8a02ac09eae0 | 414 | if(ar.ActiveArrows[i].getActive() && ar.ActiveArrows[i].getType() == 3 && abs(ar.ActiveArrows[i].curry - refarrow_right_y) <= 10){ |
wliu88 | 0:8a02ac09eae0 | 415 | arm.unlock(); |
wliu88 | 0:8a02ac09eae0 | 416 | counter2++; |
wliu88 | 0:8a02ac09eae0 | 417 | m.lock(); |
wliu88 | 0:8a02ac09eae0 | 418 | uLCD2.locate(0,0); |
wliu88 | 0:8a02ac09eae0 | 419 | uLCD2.printf("%d",counter2); |
wliu88 | 0:8a02ac09eae0 | 420 | m.unlock(); |
wliu88 | 0:8a02ac09eae0 | 421 | break; |
wliu88 | 0:8a02ac09eae0 | 422 | } |
wliu88 | 0:8a02ac09eae0 | 423 | arm.unlock(); |
wliu88 | 0:8a02ac09eae0 | 424 | } |
wliu88 | 0:8a02ac09eae0 | 425 | m.lock(); |
wliu88 | 0:8a02ac09eae0 | 426 | uLCD2.circle(refarrow_right_x,refarrow_right_y,10,0x00FF00); |
wliu88 | 0:8a02ac09eae0 | 427 | m.unlock(); |
wliu88 | 0:8a02ac09eae0 | 428 | wait(0.04); |
wliu88 | 0:8a02ac09eae0 | 429 | m.lock(); |
wliu88 | 0:8a02ac09eae0 | 430 | uLCD2.circle(refarrow_right_x,refarrow_right_y,10,0x000000); |
wliu88 | 0:8a02ac09eae0 | 431 | uLCD2.locate(0,0); |
wliu88 | 0:8a02ac09eae0 | 432 | uLCD2.printf("%d",counter2); |
wliu88 | 0:8a02ac09eae0 | 433 | m.unlock(); |
wliu88 | 0:8a02ac09eae0 | 434 | } |
wliu88 | 0:8a02ac09eae0 | 435 | if(up2){ |
wliu88 | 0:8a02ac09eae0 | 436 | up2 = false; |
wliu88 | 0:8a02ac09eae0 | 437 | for(int i=0; i < 5; i++){ |
wliu88 | 0:8a02ac09eae0 | 438 | arm.lock(); |
wliu88 | 0:8a02ac09eae0 | 439 | if(ar.ActiveArrows[i].getActive() && ar.ActiveArrows[i].getType() == 1 && abs(ar.ActiveArrows[i].curry - refarrow_up_y) <= 10){ |
wliu88 | 0:8a02ac09eae0 | 440 | arm.unlock(); |
wliu88 | 0:8a02ac09eae0 | 441 | counter2++; |
wliu88 | 0:8a02ac09eae0 | 442 | m.lock(); |
wliu88 | 0:8a02ac09eae0 | 443 | uLCD2.locate(0,0); |
wliu88 | 0:8a02ac09eae0 | 444 | uLCD2.printf("%d",counter2); |
wliu88 | 0:8a02ac09eae0 | 445 | m.unlock(); |
wliu88 | 0:8a02ac09eae0 | 446 | break; |
wliu88 | 0:8a02ac09eae0 | 447 | } |
wliu88 | 0:8a02ac09eae0 | 448 | arm.unlock(); |
wliu88 | 0:8a02ac09eae0 | 449 | } |
wliu88 | 0:8a02ac09eae0 | 450 | m.lock(); |
wliu88 | 0:8a02ac09eae0 | 451 | uLCD2.circle(refarrow_up_x,refarrow_up_y,10,0x00FF00); |
wliu88 | 0:8a02ac09eae0 | 452 | m.unlock(); |
wliu88 | 0:8a02ac09eae0 | 453 | wait(0.04); |
wliu88 | 0:8a02ac09eae0 | 454 | m.lock(); |
wliu88 | 0:8a02ac09eae0 | 455 | uLCD2.circle(refarrow_up_x,refarrow_up_y,10,0x000000); |
wliu88 | 0:8a02ac09eae0 | 456 | uLCD2.locate(0,0); |
wliu88 | 0:8a02ac09eae0 | 457 | uLCD2.printf("%d",counter2); |
wliu88 | 0:8a02ac09eae0 | 458 | m.unlock(); |
wliu88 | 0:8a02ac09eae0 | 459 | } |
wliu88 | 0:8a02ac09eae0 | 460 | if(down2){ |
wliu88 | 0:8a02ac09eae0 | 461 | down2 = false; |
wliu88 | 0:8a02ac09eae0 | 462 | for(int i=0; i < 5; i++){ |
wliu88 | 0:8a02ac09eae0 | 463 | arm.lock(); |
wliu88 | 0:8a02ac09eae0 | 464 | if(ar.ActiveArrows[i].getActive() && ar.ActiveArrows[i].getType() == 2 && abs(ar.ActiveArrows[i].curry - refarrow_down_y) <= 10){ |
wliu88 | 0:8a02ac09eae0 | 465 | arm.unlock(); |
wliu88 | 0:8a02ac09eae0 | 466 | counter2++; |
wliu88 | 0:8a02ac09eae0 | 467 | m.lock(); |
wliu88 | 0:8a02ac09eae0 | 468 | uLCD2.locate(0,0); |
wliu88 | 0:8a02ac09eae0 | 469 | uLCD2.printf("%d",counter2); |
wliu88 | 0:8a02ac09eae0 | 470 | m.unlock(); |
wliu88 | 0:8a02ac09eae0 | 471 | break; |
wliu88 | 0:8a02ac09eae0 | 472 | } |
wliu88 | 0:8a02ac09eae0 | 473 | arm.unlock(); |
wliu88 | 0:8a02ac09eae0 | 474 | } |
wliu88 | 0:8a02ac09eae0 | 475 | m.lock(); |
wliu88 | 0:8a02ac09eae0 | 476 | uLCD2.circle(refarrow_down_x,refarrow_down_y,10,0x00FF00); |
wliu88 | 0:8a02ac09eae0 | 477 | m.unlock(); |
wliu88 | 0:8a02ac09eae0 | 478 | wait(0.04); |
wliu88 | 0:8a02ac09eae0 | 479 | m.lock(); |
wliu88 | 0:8a02ac09eae0 | 480 | uLCD2.circle(refarrow_down_x,refarrow_down_y,10,0x000000); |
wliu88 | 0:8a02ac09eae0 | 481 | uLCD2.locate(0,0); |
wliu88 | 0:8a02ac09eae0 | 482 | uLCD2.printf("%d",counter2); |
wliu88 | 0:8a02ac09eae0 | 483 | m.unlock(); |
wliu88 | 0:8a02ac09eae0 | 484 | } |
wliu88 | 0:8a02ac09eae0 | 485 | Thread::wait(10); |
wliu88 | 0:8a02ac09eae0 | 486 | |
wliu88 | 0:8a02ac09eae0 | 487 | } |
wliu88 | 0:8a02ac09eae0 | 488 | } |
wliu88 | 0:8a02ac09eae0 | 489 | |
wliu88 | 0:8a02ac09eae0 | 490 | |
wliu88 | 0:8a02ac09eae0 | 491 | int main() |
wliu88 | 0:8a02ac09eae0 | 492 | { |
wliu88 | 0:8a02ac09eae0 | 493 | |
wliu88 | 0:8a02ac09eae0 | 494 | uLCD.baudrate(3000000); |
wliu88 | 0:8a02ac09eae0 | 495 | uLCD2.baudrate(3000000); |
wliu88 | 0:8a02ac09eae0 | 496 | read_file_names("/sd/wavfiles"); |
wliu88 | 0:8a02ac09eae0 | 497 | Thread thread1(uLCD_thread); |
wliu88 | 0:8a02ac09eae0 | 498 | Thread thread2(uLCD2_thread); |
wliu88 | 0:8a02ac09eae0 | 499 | while(true) |
wliu88 | 0:8a02ac09eae0 | 500 | { |
wliu88 | 0:8a02ac09eae0 | 501 | if(finished1 && finished2){ |
wliu88 | 0:8a02ac09eae0 | 502 | thread1.terminate(); |
wliu88 | 0:8a02ac09eae0 | 503 | thread2.terminate(); |
wliu88 | 0:8a02ac09eae0 | 504 | uLCD.cls(); |
wliu88 | 0:8a02ac09eae0 | 505 | uLCD2.cls(); |
wliu88 | 0:8a02ac09eae0 | 506 | break; |
wliu88 | 0:8a02ac09eae0 | 507 | } |
wliu88 | 0:8a02ac09eae0 | 508 | } |
wliu88 | 0:8a02ac09eae0 | 509 | |
wliu88 | 0:8a02ac09eae0 | 510 | songSelected = 1; |
wliu88 | 0:8a02ac09eae0 | 511 | specificSong = song1; |
wliu88 | 0:8a02ac09eae0 | 512 | |
wliu88 | 0:8a02ac09eae0 | 513 | //GAME |
wliu88 | 0:8a02ac09eae0 | 514 | uLCD.baudrate(8000); |
wliu88 | 0:8a02ac09eae0 | 515 | uLCD2.baudrate(8000); |
wliu88 | 0:8a02ac09eae0 | 516 | uLCD.color(GREEN); |
wliu88 | 0:8a02ac09eae0 | 517 | uLCD2.color(GREEN); |
wliu88 | 0:8a02ac09eae0 | 518 | uLCD.locate(0,0); |
wliu88 | 0:8a02ac09eae0 | 519 | uLCD.printf("%d",counter); |
wliu88 | 0:8a02ac09eae0 | 520 | setup_lining(); |
wliu88 | 0:8a02ac09eae0 | 521 | setup_goal_arrows(); |
wliu88 | 0:8a02ac09eae0 | 522 | uLCD2.locate(0,0); |
wliu88 | 0:8a02ac09eae0 | 523 | uLCD2.printf("%d",counter); |
wliu88 | 0:8a02ac09eae0 | 524 | setup_lining2(); |
wliu88 | 0:8a02ac09eae0 | 525 | setup_goal_arrows2(); |
wliu88 | 0:8a02ac09eae0 | 526 | wait(1); |
wliu88 | 0:8a02ac09eae0 | 527 | Thread t1(arrowreleasing_thread1); |
wliu88 | 0:8a02ac09eae0 | 528 | t1.set_priority(osPriorityRealtime); |
wliu88 | 0:8a02ac09eae0 | 529 | Thread t2(arrowdrawing_thread1); |
wliu88 | 0:8a02ac09eae0 | 530 | Thread t3(joystick2_thread); |
wliu88 | 0:8a02ac09eae0 | 531 | t3.set_priority(osPriorityHigh); |
wliu88 | 0:8a02ac09eae0 | 532 | while(1){ |
wliu88 | 0:8a02ac09eae0 | 533 | |
wliu88 | 0:8a02ac09eae0 | 534 | if(left1){ |
wliu88 | 0:8a02ac09eae0 | 535 | left1 = false; |
wliu88 | 0:8a02ac09eae0 | 536 | for(int i=0; i < 5; i++){ |
wliu88 | 0:8a02ac09eae0 | 537 | arm.lock(); |
wliu88 | 0:8a02ac09eae0 | 538 | if(ar.ActiveArrows[i].getActive() && ar.ActiveArrows[i].getType() == 0 && abs(ar.ActiveArrows[i].curry - refarrow_left_y) <= 10){ |
wliu88 | 0:8a02ac09eae0 | 539 | arm.unlock(); |
wliu88 | 0:8a02ac09eae0 | 540 | counter++; |
wliu88 | 0:8a02ac09eae0 | 541 | m.lock(); |
wliu88 | 0:8a02ac09eae0 | 542 | uLCD.locate(0,0); |
wliu88 | 0:8a02ac09eae0 | 543 | uLCD.printf("%d",counter); |
wliu88 | 0:8a02ac09eae0 | 544 | m.unlock(); |
wliu88 | 0:8a02ac09eae0 | 545 | break; |
wliu88 | 0:8a02ac09eae0 | 546 | } |
wliu88 | 0:8a02ac09eae0 | 547 | arm.unlock(); |
wliu88 | 0:8a02ac09eae0 | 548 | } |
wliu88 | 0:8a02ac09eae0 | 549 | m.lock(); |
wliu88 | 0:8a02ac09eae0 | 550 | uLCD.circle(refarrow_left_x,refarrow_left_y,10,0x00FF00); |
wliu88 | 0:8a02ac09eae0 | 551 | m.unlock(); |
wliu88 | 0:8a02ac09eae0 | 552 | wait(0.04); |
wliu88 | 0:8a02ac09eae0 | 553 | m.lock(); |
wliu88 | 0:8a02ac09eae0 | 554 | uLCD.circle(refarrow_left_x,refarrow_left_y,10,0x000000);uLCD.locate(0,0); |
wliu88 | 0:8a02ac09eae0 | 555 | uLCD.locate(0,0); |
wliu88 | 0:8a02ac09eae0 | 556 | uLCD.printf("%d",counter); |
wliu88 | 0:8a02ac09eae0 | 557 | m.unlock(); |
wliu88 | 0:8a02ac09eae0 | 558 | } |
wliu88 | 0:8a02ac09eae0 | 559 | if(right1){ |
wliu88 | 0:8a02ac09eae0 | 560 | right1 = false; |
wliu88 | 0:8a02ac09eae0 | 561 | for(int i=0; i < 5; i++){ |
wliu88 | 0:8a02ac09eae0 | 562 | arm.lock(); |
wliu88 | 0:8a02ac09eae0 | 563 | if(ar.ActiveArrows[i].getActive() && ar.ActiveArrows[i].getType() == 3 && abs(ar.ActiveArrows[i].curry - refarrow_right_y) <= 10){ |
wliu88 | 0:8a02ac09eae0 | 564 | arm.unlock(); |
wliu88 | 0:8a02ac09eae0 | 565 | counter++; |
wliu88 | 0:8a02ac09eae0 | 566 | m.lock(); |
wliu88 | 0:8a02ac09eae0 | 567 | uLCD.locate(0,0); |
wliu88 | 0:8a02ac09eae0 | 568 | uLCD.printf("%d",counter); |
wliu88 | 0:8a02ac09eae0 | 569 | m.unlock(); |
wliu88 | 0:8a02ac09eae0 | 570 | break; |
wliu88 | 0:8a02ac09eae0 | 571 | } |
wliu88 | 0:8a02ac09eae0 | 572 | arm.unlock(); |
wliu88 | 0:8a02ac09eae0 | 573 | } |
wliu88 | 0:8a02ac09eae0 | 574 | m.lock(); |
wliu88 | 0:8a02ac09eae0 | 575 | uLCD.circle(refarrow_right_x,refarrow_right_y,10,0x00FF00); |
wliu88 | 0:8a02ac09eae0 | 576 | m.unlock(); |
wliu88 | 0:8a02ac09eae0 | 577 | wait(0.04); |
wliu88 | 0:8a02ac09eae0 | 578 | m.lock(); |
wliu88 | 0:8a02ac09eae0 | 579 | uLCD.circle(refarrow_right_x,refarrow_right_y,10,0x000000); |
wliu88 | 0:8a02ac09eae0 | 580 | uLCD.locate(0,0); |
wliu88 | 0:8a02ac09eae0 | 581 | uLCD.printf("%d",counter); |
wliu88 | 0:8a02ac09eae0 | 582 | m.unlock(); |
wliu88 | 0:8a02ac09eae0 | 583 | } |
wliu88 | 0:8a02ac09eae0 | 584 | if(up1){ |
wliu88 | 0:8a02ac09eae0 | 585 | up1 = false; |
wliu88 | 0:8a02ac09eae0 | 586 | for(int i=0; i < 5; i++){ |
wliu88 | 0:8a02ac09eae0 | 587 | arm.lock(); |
wliu88 | 0:8a02ac09eae0 | 588 | if(ar.ActiveArrows[i].getActive() && ar.ActiveArrows[i].getType() == 1 && abs(ar.ActiveArrows[i].curry - refarrow_up_y) <= 10){ |
wliu88 | 0:8a02ac09eae0 | 589 | arm.unlock(); |
wliu88 | 0:8a02ac09eae0 | 590 | counter++; |
wliu88 | 0:8a02ac09eae0 | 591 | m.lock(); |
wliu88 | 0:8a02ac09eae0 | 592 | uLCD.locate(0,0); |
wliu88 | 0:8a02ac09eae0 | 593 | uLCD.printf("%d",counter); |
wliu88 | 0:8a02ac09eae0 | 594 | m.unlock(); |
wliu88 | 0:8a02ac09eae0 | 595 | break; |
wliu88 | 0:8a02ac09eae0 | 596 | } |
wliu88 | 0:8a02ac09eae0 | 597 | arm.unlock(); |
wliu88 | 0:8a02ac09eae0 | 598 | } |
wliu88 | 0:8a02ac09eae0 | 599 | m.lock(); |
wliu88 | 0:8a02ac09eae0 | 600 | uLCD.circle(refarrow_up_x,refarrow_up_y,10,0x00FF00); |
wliu88 | 0:8a02ac09eae0 | 601 | m.unlock(); |
wliu88 | 0:8a02ac09eae0 | 602 | wait(0.04); |
wliu88 | 0:8a02ac09eae0 | 603 | m.lock(); |
wliu88 | 0:8a02ac09eae0 | 604 | uLCD.circle(refarrow_up_x,refarrow_up_y,10,0x000000); |
wliu88 | 0:8a02ac09eae0 | 605 | uLCD.locate(0,0); |
wliu88 | 0:8a02ac09eae0 | 606 | uLCD.printf("%d",counter); |
wliu88 | 0:8a02ac09eae0 | 607 | m.unlock(); |
wliu88 | 0:8a02ac09eae0 | 608 | } |
wliu88 | 0:8a02ac09eae0 | 609 | if(down1){ |
wliu88 | 0:8a02ac09eae0 | 610 | down1 = false; |
wliu88 | 0:8a02ac09eae0 | 611 | for(int i=0; i < 5; i++){ |
wliu88 | 0:8a02ac09eae0 | 612 | arm.lock(); |
wliu88 | 0:8a02ac09eae0 | 613 | if(ar.ActiveArrows[i].getActive() && ar.ActiveArrows[i].getType() == 2 && abs(ar.ActiveArrows[i].curry - refarrow_down_y) <= 10){ |
wliu88 | 0:8a02ac09eae0 | 614 | arm.unlock(); |
wliu88 | 0:8a02ac09eae0 | 615 | counter++; |
wliu88 | 0:8a02ac09eae0 | 616 | m.lock(); |
wliu88 | 0:8a02ac09eae0 | 617 | uLCD.locate(0,0); |
wliu88 | 0:8a02ac09eae0 | 618 | uLCD.printf("%d",counter); |
wliu88 | 0:8a02ac09eae0 | 619 | m.unlock(); |
wliu88 | 0:8a02ac09eae0 | 620 | break; |
wliu88 | 0:8a02ac09eae0 | 621 | } |
wliu88 | 0:8a02ac09eae0 | 622 | arm.unlock(); |
wliu88 | 0:8a02ac09eae0 | 623 | } |
wliu88 | 0:8a02ac09eae0 | 624 | m.lock(); |
wliu88 | 0:8a02ac09eae0 | 625 | uLCD.circle(refarrow_down_x,refarrow_down_y,10,0x00FF00); |
wliu88 | 0:8a02ac09eae0 | 626 | m.unlock(); |
wliu88 | 0:8a02ac09eae0 | 627 | wait(0.04); |
wliu88 | 0:8a02ac09eae0 | 628 | m.lock(); |
wliu88 | 0:8a02ac09eae0 | 629 | uLCD.circle(refarrow_down_x,refarrow_down_y,10,0x000000); |
wliu88 | 0:8a02ac09eae0 | 630 | uLCD.locate(0,0); |
wliu88 | 0:8a02ac09eae0 | 631 | uLCD.printf("%d",counter); |
wliu88 | 0:8a02ac09eae0 | 632 | m.unlock(); |
wliu88 | 0:8a02ac09eae0 | 633 | } |
wliu88 | 0:8a02ac09eae0 | 634 | if(fire1){ |
wliu88 | 0:8a02ac09eae0 | 635 | //nothing |
wliu88 | 0:8a02ac09eae0 | 636 | } |
wliu88 | 0:8a02ac09eae0 | 637 | arm.lock(); |
wliu88 | 0:8a02ac09eae0 | 638 | if(ar.getTermStatus()){ |
wliu88 | 0:8a02ac09eae0 | 639 | empty = true; |
wliu88 | 0:8a02ac09eae0 | 640 | for(int i=0; i < 5; i++){ |
wliu88 | 0:8a02ac09eae0 | 641 | if(ar.ActiveArrows[i].getActive()) { |
wliu88 | 0:8a02ac09eae0 | 642 | empty = false; |
wliu88 | 0:8a02ac09eae0 | 643 | break; |
wliu88 | 0:8a02ac09eae0 | 644 | } |
wliu88 | 0:8a02ac09eae0 | 645 | } |
wliu88 | 0:8a02ac09eae0 | 646 | if(empty) { |
wliu88 | 0:8a02ac09eae0 | 647 | scoreScreen = true; |
wliu88 | 0:8a02ac09eae0 | 648 | uLCD.cls(); |
wliu88 | 0:8a02ac09eae0 | 649 | uLCD2.cls(); |
wliu88 | 0:8a02ac09eae0 | 650 | break; |
wliu88 | 0:8a02ac09eae0 | 651 | } |
wliu88 | 0:8a02ac09eae0 | 652 | } |
wliu88 | 0:8a02ac09eae0 | 653 | arm.unlock(); |
wliu88 | 0:8a02ac09eae0 | 654 | wait(0.01); |
wliu88 | 0:8a02ac09eae0 | 655 | } |
wliu88 | 0:8a02ac09eae0 | 656 | |
wliu88 | 0:8a02ac09eae0 | 657 | //End of game |
wliu88 | 0:8a02ac09eae0 | 658 | uLCD.cls(); |
wliu88 | 0:8a02ac09eae0 | 659 | uLCD2.cls(); |
wliu88 | 0:8a02ac09eae0 | 660 | if(counter > counter2){ |
wliu88 | 0:8a02ac09eae0 | 661 | end_screen(); |
wliu88 | 0:8a02ac09eae0 | 662 | uLCD.locate(6,6); |
wliu88 | 0:8a02ac09eae0 | 663 | uLCD.color(GREEN); |
wliu88 | 0:8a02ac09eae0 | 664 | uLCD.printf("YOU WIN!\n PLAYER 1 WINS!"); |
wliu88 | 0:8a02ac09eae0 | 665 | uLCD2.locate(5,6); |
wliu88 | 0:8a02ac09eae0 | 666 | uLCD2.color(RED); |
wliu88 | 0:8a02ac09eae0 | 667 | uLCD2.printf("YOU LOSE!\n PLAYER 1 WINS!"); |
wliu88 | 0:8a02ac09eae0 | 668 | end_screen(); |
wliu88 | 0:8a02ac09eae0 | 669 | } |
wliu88 | 0:8a02ac09eae0 | 670 | else if (counter2 > counter) { |
wliu88 | 0:8a02ac09eae0 | 671 | end_screen(); |
wliu88 | 0:8a02ac09eae0 | 672 | uLCD.locate(5,6); |
wliu88 | 0:8a02ac09eae0 | 673 | uLCD.color(RED); |
wliu88 | 0:8a02ac09eae0 | 674 | uLCD.printf("YOU LOSE!\n PLAYER 2 WINS!"); |
wliu88 | 0:8a02ac09eae0 | 675 | uLCD2.locate(6,6); |
wliu88 | 0:8a02ac09eae0 | 676 | uLCD.color(GREEN); |
wliu88 | 0:8a02ac09eae0 | 677 | uLCD2.printf("YOU WIN!\n PLAYER 2 WINS!"); |
wliu88 | 0:8a02ac09eae0 | 678 | end_screen(); |
wliu88 | 0:8a02ac09eae0 | 679 | } |
wliu88 | 0:8a02ac09eae0 | 680 | else { |
wliu88 | 0:8a02ac09eae0 | 681 | end_screen(); |
wliu88 | 0:8a02ac09eae0 | 682 | uLCD.locate(7,7); |
wliu88 | 0:8a02ac09eae0 | 683 | uLCD.printf("DRAW"); |
wliu88 | 0:8a02ac09eae0 | 684 | uLCD2.locate(7,7); |
wliu88 | 0:8a02ac09eae0 | 685 | uLCD2.printf("DRAW"); |
wliu88 | 0:8a02ac09eae0 | 686 | end_screen(); |
wliu88 | 0:8a02ac09eae0 | 687 | } |
wliu88 | 0:8a02ac09eae0 | 688 | wait(5); |
wliu88 | 0:8a02ac09eae0 | 689 | uLCD.cls(); |
wliu88 | 0:8a02ac09eae0 | 690 | uLCD2.cls(); |
wliu88 | 0:8a02ac09eae0 | 691 | end_screen(); |
wliu88 | 0:8a02ac09eae0 | 692 | uLCD.locate(6,6); |
wliu88 | 0:8a02ac09eae0 | 693 | uLCD.color(GREEN); |
wliu88 | 0:8a02ac09eae0 | 694 | uLCD.printf("Thanks\n for playing!"); |
wliu88 | 0:8a02ac09eae0 | 695 | uLCD2.locate(6,6); |
wliu88 | 0:8a02ac09eae0 | 696 | uLCD2.color(GREEN); |
wliu88 | 0:8a02ac09eae0 | 697 | uLCD2.printf("Thanks\n for playing!"); |
wliu88 | 0:8a02ac09eae0 | 698 | end_screen(); |
wliu88 | 0:8a02ac09eae0 | 699 | } |