Laser tag guns code https://os.mbed.com/users/ddakev/notebook/laser-tag-system/

Dependencies:   mbed 4DGL-uLCD-SE PinDetect SoftI2C

Committer:
ddakev
Date:
Mon Apr 22 17:40:30 2019 +0000
Revision:
1:7d957da791a5
Parent:
0:4c644bb83761
Bug fixes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ddakev 0:4c644bb83761 1 #include "mbed.h"
ddakev 0:4c644bb83761 2 #include "uLCD_4DGL.h"
ddakev 0:4c644bb83761 3 #include "MMA8452.h"
ddakev 0:4c644bb83761 4 #include "PinDetect.h"
ddakev 0:4c644bb83761 5 #include "sfx.h"
ddakev 0:4c644bb83761 6
ddakev 0:4c644bb83761 7
ddakev 0:4c644bb83761 8 uLCD_4DGL uLCD(p9,p10,p11); // LCD (serial tx, serial rx, reset pin;)
ddakev 0:4c644bb83761 9 Serial pc(USBTX,USBRX);
ddakev 0:4c644bb83761 10 PinDetect trigger(p12, PullUp);
ddakev 0:4c644bb83761 11 Ticker acc_reader;
ddakev 0:4c644bb83761 12 Ticker reloader;
ddakev 0:4c644bb83761 13 Ticker reload_animation;
ddakev 0:4c644bb83761 14 Ticker countdown_ticker;
ddakev 0:4c644bb83761 15 Ticker timeRemaining_ticker;
ddakev 0:4c644bb83761 16 Ticker red_ticker, green_ticker;
ddakev 0:4c644bb83761 17 Timeout red_timeout, green_timeout;
ddakev 0:4c644bb83761 18 Timeout message_timeout;
ddakev 0:4c644bb83761 19 Timeout vibrator_timeout;
ddakev 0:4c644bb83761 20 MMA8452 acc(p6, p7, 100000);
ddakev 0:4c644bb83761 21 Serial xbee(p13, p14);
ddakev 0:4c644bb83761 22 DigitalOut rst1(p15);
ddakev 0:4c644bb83761 23 Serial IR(p28, p27);
ddakev 0:4c644bb83761 24 PwmOut IRLED(p21);
ddakev 0:4c644bb83761 25 AnalogOut speaker(p18);
ddakev 0:4c644bb83761 26 PwmOut vibrator(p24);
ddakev 0:4c644bb83761 27 DigitalOut greenLED(p22);
ddakev 0:4c644bb83761 28 DigitalOut redLED(p23);
ddakev 0:4c644bb83761 29
ddakev 0:4c644bb83761 30 Ticker sound_ticker;
ddakev 0:4c644bb83761 31 unsigned char* sound_data;
ddakev 0:4c644bb83761 32 unsigned int num_elements = 0;
ddakev 0:4c644bb83761 33 int sound_i = 0;
ddakev 0:4c644bb83761 34
ddakev 0:4c644bb83761 35 int xbeeId = -1;
ddakev 0:4c644bb83761 36 char playerName[11] = "N/A";
ddakev 0:4c644bb83761 37 bool waitingForId = false;
ddakev 0:4c644bb83761 38 int timeRemaining = 0;
ddakev 0:4c644bb83761 39 int countdown = 0;
ddakev 0:4c644bb83761 40 bool dead = false;
ddakev 0:4c644bb83761 41
ddakev 0:4c644bb83761 42 const unsigned int MAX_BULLETS = 8;
ddakev 0:4c644bb83761 43 volatile unsigned int bullets;
ddakev 0:4c644bb83761 44 volatile bool reloading = false;
ddakev 0:4c644bb83761 45
ddakev 0:4c644bb83761 46 enum GameState {NOT_STARTED, STARTING, RUNNING, ENDED};
ddakev 0:4c644bb83761 47
ddakev 0:4c644bb83761 48 GameState gameState = NOT_STARTED;
ddakev 0:4c644bb83761 49 volatile unsigned int score = 0;
ddakev 0:4c644bb83761 50
ddakev 0:4c644bb83761 51 void switchState(GameState newGameState);
ddakev 0:4c644bb83761 52 void shot_detected(int shooterId);
ddakev 0:4c644bb83761 53
ddakev 0:4c644bb83761 54
ddakev 0:4c644bb83761 55 void flash_red() {
ddakev 0:4c644bb83761 56 redLED = !redLED;
ddakev 0:4c644bb83761 57 }
ddakev 0:4c644bb83761 58
ddakev 0:4c644bb83761 59 void flash_green() {
ddakev 0:4c644bb83761 60 greenLED = !greenLED;
ddakev 0:4c644bb83761 61 }
ddakev 0:4c644bb83761 62
ddakev 0:4c644bb83761 63 void stop_red() {
ddakev 0:4c644bb83761 64 redLED = 0;
ddakev 0:4c644bb83761 65 red_ticker.detach();
ddakev 0:4c644bb83761 66 }
ddakev 0:4c644bb83761 67
ddakev 0:4c644bb83761 68 void stop_green() {
ddakev 0:4c644bb83761 69 greenLED = 0;
ddakev 0:4c644bb83761 70 green_ticker.detach();
ddakev 0:4c644bb83761 71 }
ddakev 0:4c644bb83761 72
ddakev 0:4c644bb83761 73 void play_sound() {
ddakev 0:4c644bb83761 74 speaker = sound_data[sound_i] / 256.0f;
ddakev 0:4c644bb83761 75 sound_i++;
ddakev 0:4c644bb83761 76 if (sound_i>= num_elements) {
ddakev 0:4c644bb83761 77 sound_i = 0;
ddakev 0:4c644bb83761 78 //speaker = 0.0f;
ddakev 0:4c644bb83761 79 sound_ticker.detach();
ddakev 0:4c644bb83761 80 }
ddakev 0:4c644bb83761 81 }
ddakev 0:4c644bb83761 82
ddakev 0:4c644bb83761 83 void clear_header() {
ddakev 0:4c644bb83761 84 uLCD.filled_rectangle(0, 0, 128, 10, 0x000000);
ddakev 0:4c644bb83761 85 }
ddakev 0:4c644bb83761 86
ddakev 0:4c644bb83761 87 void clear_middle() {
ddakev 0:4c644bb83761 88 uLCD.filled_rectangle(0, 30, 128, 95, 0x000000);
ddakev 0:4c644bb83761 89 }
ddakev 0:4c644bb83761 90
ddakev 0:4c644bb83761 91 void clear_footer() {
ddakev 0:4c644bb83761 92 uLCD.filled_rectangle(0, 95, 128, 128, 0x000000);
ddakev 0:4c644bb83761 93 }
ddakev 0:4c644bb83761 94
ddakev 0:4c644bb83761 95 void redraw_header() {
ddakev 0:4c644bb83761 96 clear_header();
ddakev 0:4c644bb83761 97 uLCD.color(GREEN);
ddakev 0:4c644bb83761 98 uLCD.text_width(1);
ddakev 0:4c644bb83761 99 uLCD.text_height(1);
ddakev 0:4c644bb83761 100 uLCD.locate(0, 0);
ddakev 0:4c644bb83761 101 uLCD.printf("%s", playerName);
ddakev 0:4c644bb83761 102 uLCD.locate(13, 0);
ddakev 0:4c644bb83761 103 int minutes = timeRemaining / 60;
ddakev 0:4c644bb83761 104 int seconds = timeRemaining % 60;
ddakev 0:4c644bb83761 105 uLCD.printf("%02d:%02d", minutes, seconds);
ddakev 0:4c644bb83761 106 }
ddakev 0:4c644bb83761 107
ddakev 0:4c644bb83761 108 void redraw_score() {
ddakev 0:4c644bb83761 109 clear_middle();
ddakev 0:4c644bb83761 110 uLCD.color(GREEN);
ddakev 0:4c644bb83761 111 uLCD.text_width(4);
ddakev 0:4c644bb83761 112 uLCD.text_height(4);
ddakev 0:4c644bb83761 113 uLCD.locate(1, 1);
ddakev 0:4c644bb83761 114 uLCD.printf("%03d", score);
ddakev 0:4c644bb83761 115 }
ddakev 0:4c644bb83761 116
ddakev 0:4c644bb83761 117 void redraw_middle_message(char* msg) {
ddakev 0:4c644bb83761 118 clear_middle();
ddakev 0:4c644bb83761 119 uLCD.color(GREEN);
ddakev 0:4c644bb83761 120 uLCD.text_width(1);
ddakev 0:4c644bb83761 121 uLCD.text_height(1);
ddakev 0:4c644bb83761 122 uLCD.locate(0, 4);
ddakev 0:4c644bb83761 123 uLCD.printf("%s", msg);
ddakev 0:4c644bb83761 124 }
ddakev 0:4c644bb83761 125
ddakev 0:4c644bb83761 126 void redraw_footer_message(char* msg) {
ddakev 0:4c644bb83761 127 clear_footer();
ddakev 0:4c644bb83761 128 uLCD.color(GREEN);
ddakev 0:4c644bb83761 129 uLCD.text_width(2);
ddakev 0:4c644bb83761 130 uLCD.text_height(2);
ddakev 0:4c644bb83761 131 uLCD.locate(0, 6);
ddakev 0:4c644bb83761 132 uLCD.printf("%s", msg);
ddakev 0:4c644bb83761 133 }
ddakev 0:4c644bb83761 134
ddakev 0:4c644bb83761 135 void redraw_bullets() {
ddakev 0:4c644bb83761 136 clear_footer();
ddakev 0:4c644bb83761 137 for(int i=0; i<bullets; i++) {
ddakev 0:4c644bb83761 138 int offset = 6 * (i+1) + 9 * i;
ddakev 0:4c644bb83761 139 uLCD.filled_rectangle(offset + 1, 95, offset + 8, 113, 0xFF0000);
ddakev 0:4c644bb83761 140 uLCD.filled_rectangle(offset + 1, 113, offset + 8, 118, 0xFFDF00);
ddakev 0:4c644bb83761 141 uLCD.filled_rectangle(offset, 118, offset + 9, 120, 0xFFDF00);
ddakev 0:4c644bb83761 142 }
ddakev 0:4c644bb83761 143 }
ddakev 0:4c644bb83761 144
ddakev 0:4c644bb83761 145 void draw_reload() {
ddakev 0:4c644bb83761 146 if(gameState != RUNNING) {
ddakev 0:4c644bb83761 147 reload_animation.detach();
ddakev 0:4c644bb83761 148 return;
ddakev 0:4c644bb83761 149 }
ddakev 0:4c644bb83761 150 uLCD.text_width(3);
ddakev 0:4c644bb83761 151 uLCD.text_height(3);
ddakev 0:4c644bb83761 152 uLCD.locate(0, 4);
ddakev 0:4c644bb83761 153 uLCD.color(RED);
ddakev 0:4c644bb83761 154 uLCD.printf("RELOAD");
ddakev 0:4c644bb83761 155 wait(0.25);
ddakev 0:4c644bb83761 156 uLCD.locate(0, 4);
ddakev 0:4c644bb83761 157 uLCD.color(WHITE);
ddakev 0:4c644bb83761 158 uLCD.printf("RELOAD");
ddakev 0:4c644bb83761 159 }
ddakev 0:4c644bb83761 160
ddakev 0:4c644bb83761 161 void redraw_countdown() {
ddakev 0:4c644bb83761 162 clear_middle();
ddakev 0:4c644bb83761 163 clear_footer();
ddakev 0:4c644bb83761 164 uLCD.text_width(1);
ddakev 0:4c644bb83761 165 uLCD.text_height(1);
ddakev 0:4c644bb83761 166 uLCD.locate(7, 6);
ddakev 0:4c644bb83761 167 uLCD.text_width(4);
ddakev 0:4c644bb83761 168 uLCD.text_height(4);
ddakev 0:4c644bb83761 169 uLCD.printf("%d", countdown);
ddakev 0:4c644bb83761 170 }
ddakev 0:4c644bb83761 171
ddakev 0:4c644bb83761 172 void start_countdown() {
ddakev 0:4c644bb83761 173 countdown --;
ddakev 0:4c644bb83761 174 if(countdown == 0) {
ddakev 0:4c644bb83761 175 // switch to running
ddakev 0:4c644bb83761 176 countdown_ticker.detach();
ddakev 0:4c644bb83761 177 switchState(RUNNING);
ddakev 0:4c644bb83761 178 return;
ddakev 0:4c644bb83761 179 }
ddakev 0:4c644bb83761 180 redraw_countdown();
ddakev 0:4c644bb83761 181 }
ddakev 0:4c644bb83761 182
ddakev 0:4c644bb83761 183 void start_tr_countdown() {
ddakev 0:4c644bb83761 184 timeRemaining --;
ddakev 0:4c644bb83761 185 redraw_header();
ddakev 0:4c644bb83761 186 if(timeRemaining == 0) {
ddakev 0:4c644bb83761 187 timeRemaining_ticker.detach();
ddakev 0:4c644bb83761 188 switchState(ENDED);
ddakev 0:4c644bb83761 189 return;
ddakev 0:4c644bb83761 190 }
ddakev 0:4c644bb83761 191 }
ddakev 0:4c644bb83761 192
ddakev 0:4c644bb83761 193 void turnOffVibrator() {
ddakev 0:4c644bb83761 194 vibrator = 0.0;
ddakev 0:4c644bb83761 195 }
ddakev 0:4c644bb83761 196
ddakev 0:4c644bb83761 197 void fire() {
ddakev 0:4c644bb83761 198 if(gameState == NOT_STARTED && xbeeId == -1) {
ddakev 0:4c644bb83761 199 waitingForId = true;
ddakev 0:4c644bb83761 200 xbee.printf("%s", "nrj\n");
ddakev 0:4c644bb83761 201 }
ddakev 0:4c644bb83761 202 else if(gameState == RUNNING && !dead) {
ddakev 0:4c644bb83761 203 if(bullets > 0) {
ddakev 1:7d957da791a5 204 IR.putc(xbeeId + '0'); // change to xbeeId
ddakev 0:4c644bb83761 205 bullets--;
ddakev 0:4c644bb83761 206 redraw_bullets();
ddakev 0:4c644bb83761 207 sound_i = 0;
ddakev 0:4c644bb83761 208 sound_data = Sounds::SHOT;
ddakev 0:4c644bb83761 209 num_elements = SHOT_NUM_ELEMENTS;
ddakev 0:4c644bb83761 210 sound_ticker.attach(&play_sound, 1.0 / SAMPLE_RATE);
ddakev 0:4c644bb83761 211 vibrator = 0.7;
ddakev 0:4c644bb83761 212 vibrator_timeout.attach(&turnOffVibrator, 0.5);
ddakev 0:4c644bb83761 213 if(bullets == 0) {
ddakev 0:4c644bb83761 214 draw_reload();
ddakev 0:4c644bb83761 215 reload_animation.attach(&draw_reload, 0.5);
ddakev 0:4c644bb83761 216 }
ddakev 0:4c644bb83761 217 xbee.printf("gh%d\n", xbeeId);
ddakev 0:4c644bb83761 218 }
ddakev 0:4c644bb83761 219 else {
ddakev 0:4c644bb83761 220 sound_i = 0;
ddakev 0:4c644bb83761 221 sound_data = Sounds::OUT_OF_BULLETS;
ddakev 0:4c644bb83761 222 num_elements = OUT_OF_BULLETS_NUM_ELEMENTS;
ddakev 0:4c644bb83761 223 sound_ticker.attach(&play_sound, 1.0 / SAMPLE_RATE);
ddakev 0:4c644bb83761 224 }
ddakev 0:4c644bb83761 225 }
ddakev 0:4c644bb83761 226 }
ddakev 0:4c644bb83761 227
ddakev 0:4c644bb83761 228 void reload() {
ddakev 0:4c644bb83761 229 if(bullets < MAX_BULLETS) {
ddakev 0:4c644bb83761 230 bullets ++;
ddakev 0:4c644bb83761 231 if(bullets == 1) {
ddakev 0:4c644bb83761 232 reload_animation.detach();
ddakev 0:4c644bb83761 233 }
ddakev 0:4c644bb83761 234 redraw_bullets();
ddakev 0:4c644bb83761 235 sound_i = 0;
ddakev 0:4c644bb83761 236 sound_data = Sounds::RELOADING;
ddakev 0:4c644bb83761 237 num_elements = RELOADING_NUM_ELEMENTS;
ddakev 0:4c644bb83761 238 sound_ticker.attach(&play_sound, 1.0 / SAMPLE_RATE);
ddakev 0:4c644bb83761 239 }
ddakev 0:4c644bb83761 240 else {
ddakev 0:4c644bb83761 241 reloading = false;
ddakev 0:4c644bb83761 242 reloader.detach();
ddakev 0:4c644bb83761 243 sound_i = 0;
ddakev 0:4c644bb83761 244 sound_data = Sounds::RELOAD_DONE;
ddakev 0:4c644bb83761 245 num_elements = RELOAD_DONE_NUM_ELEMENTS;
ddakev 0:4c644bb83761 246 sound_ticker.attach(&play_sound, 1.0 / SAMPLE_RATE);
ddakev 0:4c644bb83761 247 }
ddakev 0:4c644bb83761 248 }
ddakev 0:4c644bb83761 249
ddakev 0:4c644bb83761 250 void read_acc() {
ddakev 0:4c644bb83761 251 double ax, ay, az;
ddakev 0:4c644bb83761 252 acc.readXYZGravity(&ax, &ay, &az);
ddakev 0:4c644bb83761 253 double acc = ax*ax + ay*ay + az*az;
ddakev 0:4c644bb83761 254 double percent_y = ay * ay / acc;
ddakev 0:4c644bb83761 255 if(percent_y > 0.56 && ay < 0 && reloading == false && bullets < MAX_BULLETS) {
ddakev 0:4c644bb83761 256 pc.printf("RELOADING\r\n");
ddakev 0:4c644bb83761 257 reloading = true;
ddakev 0:4c644bb83761 258 reloader.attach(&reload, 0.5);
ddakev 0:4c644bb83761 259 }
ddakev 0:4c644bb83761 260 else if(!(percent_y > 0.56 && ay < 0) && reloading == true) {
ddakev 0:4c644bb83761 261 reloading = false;
ddakev 0:4c644bb83761 262 reloader.detach();
ddakev 0:4c644bb83761 263 sound_i = 0;
ddakev 0:4c644bb83761 264 sound_data = Sounds::RELOAD_DONE;
ddakev 0:4c644bb83761 265 num_elements = RELOAD_DONE_NUM_ELEMENTS;
ddakev 0:4c644bb83761 266 sound_ticker.attach(&play_sound, 1.0 / SAMPLE_RATE);
ddakev 0:4c644bb83761 267 }
ddakev 0:4c644bb83761 268 }
ddakev 0:4c644bb83761 269
ddakev 0:4c644bb83761 270 void readMessage(char* buffer) {
ddakev 0:4c644bb83761 271 int len = 0;
ddakev 0:4c644bb83761 272 while(1) {
ddakev 0:4c644bb83761 273 if(xbee.readable()) {
ddakev 0:4c644bb83761 274 char c = xbee.getc();
ddakev 0:4c644bb83761 275 if(c == '\n') {
ddakev 0:4c644bb83761 276 buffer[len++] = '\0';
ddakev 0:4c644bb83761 277 return;
ddakev 0:4c644bb83761 278 }
ddakev 0:4c644bb83761 279 buffer[len++] = c;
ddakev 0:4c644bb83761 280 }
ddakev 0:4c644bb83761 281 if(IR.readable()) {
ddakev 0:4c644bb83761 282 char c = IR.getc();
ddakev 0:4c644bb83761 283 pc.putc(c);
ddakev 0:4c644bb83761 284 if(c >= '0' && c <= '9') {
ddakev 0:4c644bb83761 285 shot_detected(c - '0');
ddakev 0:4c644bb83761 286 }
ddakev 0:4c644bb83761 287 }
ddakev 0:4c644bb83761 288 }
ddakev 0:4c644bb83761 289 }
ddakev 0:4c644bb83761 290
ddakev 0:4c644bb83761 291 void shot_detected(int shooterId) {
ddakev 0:4c644bb83761 292 if(!dead && shooterId != xbeeId) {
ddakev 0:4c644bb83761 293 xbee.printf("gk%ds%d\n", shooterId, xbeeId);
ddakev 0:4c644bb83761 294 }
ddakev 0:4c644bb83761 295 }
ddakev 0:4c644bb83761 296
ddakev 0:4c644bb83761 297 void respawn() {
ddakev 0:4c644bb83761 298 dead = false;
ddakev 0:4c644bb83761 299 redraw_score();
ddakev 0:4c644bb83761 300 }
ddakev 0:4c644bb83761 301
ddakev 0:4c644bb83761 302 void switchState(GameState newGameState) {
ddakev 0:4c644bb83761 303 switch(newGameState) {
ddakev 0:4c644bb83761 304 case NOT_STARTED:
ddakev 0:4c644bb83761 305 gameState = NOT_STARTED;
ddakev 0:4c644bb83761 306 redraw_header();
ddakev 0:4c644bb83761 307 redraw_middle_message("Press the trigger to register device");
ddakev 0:4c644bb83761 308 redraw_footer_message("Game Not Started");
ddakev 0:4c644bb83761 309 break;
ddakev 0:4c644bb83761 310 case STARTING:
ddakev 0:4c644bb83761 311 gameState = STARTING;
ddakev 0:4c644bb83761 312 countdown = 3;
ddakev 0:4c644bb83761 313 redraw_countdown();
ddakev 0:4c644bb83761 314 countdown_ticker.attach(&start_countdown, 1.0);
ddakev 0:4c644bb83761 315 break;
ddakev 0:4c644bb83761 316 case RUNNING:
ddakev 0:4c644bb83761 317 gameState = RUNNING;
ddakev 0:4c644bb83761 318 bullets = MAX_BULLETS;
ddakev 0:4c644bb83761 319 score = 0;
ddakev 0:4c644bb83761 320 redraw_score();
ddakev 0:4c644bb83761 321 redraw_bullets();
ddakev 0:4c644bb83761 322 timeRemaining_ticker.attach(&start_tr_countdown, 1.0);
ddakev 0:4c644bb83761 323 greenLED = 1;
ddakev 0:4c644bb83761 324 redLED = 0;
ddakev 0:4c644bb83761 325 green_ticker.attach(&flash_green, 0.1);
ddakev 0:4c644bb83761 326 red_ticker.attach(&flash_red, 0.1);
ddakev 0:4c644bb83761 327 green_timeout.attach(&stop_green, 1.0);
ddakev 0:4c644bb83761 328 red_timeout.attach(&stop_red, 1.0);
ddakev 0:4c644bb83761 329 vibrator = 1;
ddakev 0:4c644bb83761 330 vibrator_timeout.attach(&turnOffVibrator, 1.0);
ddakev 0:4c644bb83761 331 break;
ddakev 0:4c644bb83761 332 case ENDED:
ddakev 0:4c644bb83761 333 gameState = ENDED;
ddakev 0:4c644bb83761 334 clear_footer();
ddakev 1:7d957da791a5 335 message_timeout.detach();
ddakev 0:4c644bb83761 336 redraw_middle_message("Game Ended");
ddakev 0:4c644bb83761 337 greenLED = 1;
ddakev 0:4c644bb83761 338 redLED = 0;
ddakev 0:4c644bb83761 339 green_ticker.attach(&flash_green, 0.1);
ddakev 0:4c644bb83761 340 red_ticker.attach(&flash_red, 0.1);
ddakev 0:4c644bb83761 341 green_timeout.attach(&stop_green, 1.0);
ddakev 0:4c644bb83761 342 red_timeout.attach(&stop_red, 1.0);
ddakev 0:4c644bb83761 343 vibrator = 1;
ddakev 0:4c644bb83761 344 vibrator_timeout.attach(&turnOffVibrator, 1.0);
ddakev 0:4c644bb83761 345 break;
ddakev 0:4c644bb83761 346 default: break;
ddakev 0:4c644bb83761 347 }
ddakev 0:4c644bb83761 348 }
ddakev 0:4c644bb83761 349
ddakev 0:4c644bb83761 350 int main() {
ddakev 0:4c644bb83761 351 uLCD.baudrate(3000000);
ddakev 0:4c644bb83761 352 pc.baud(9600);
ddakev 0:4c644bb83761 353 xbee.baud(9600);
ddakev 0:4c644bb83761 354 trigger.attach_deasserted(&fire);
ddakev 0:4c644bb83761 355 trigger.setSampleFrequency();
ddakev 0:4c644bb83761 356 acc_reader.attach(&read_acc, 0.05);
ddakev 0:4c644bb83761 357 rst1 = 0; //Set reset pin to 0
ddakev 0:4c644bb83761 358 wait_ms(1);//Wait at least one millisecond
ddakev 0:4c644bb83761 359 rst1 = 1;//Set reset pin to 1
ddakev 0:4c644bb83761 360 wait_ms(1);//Wait another millisecond
ddakev 0:4c644bb83761 361 IRLED.period(1.0/38000.0);
ddakev 0:4c644bb83761 362 IRLED = 0.5;
ddakev 0:4c644bb83761 363 IR.baud(2400);
ddakev 0:4c644bb83761 364
ddakev 0:4c644bb83761 365 redraw_header();
ddakev 0:4c644bb83761 366
ddakev 0:4c644bb83761 367 switchState(NOT_STARTED);
ddakev 0:4c644bb83761 368
ddakev 0:4c644bb83761 369 while(1) {
ddakev 0:4c644bb83761 370 char msg[255];
ddakev 0:4c644bb83761 371 readMessage(msg);
ddakev 0:4c644bb83761 372 printf("%s\r\n", msg);
ddakev 0:4c644bb83761 373 if(msg[0] == 'n') {
ddakev 0:4c644bb83761 374 // action in not started
ddakev 0:4c644bb83761 375 if(gameState != NOT_STARTED) continue;
ddakev 0:4c644bb83761 376 if(msg[1] == 'i') {
ddakev 0:4c644bb83761 377 // receiving id
ddakev 0:4c644bb83761 378 if(waitingForId) {
ddakev 0:4c644bb83761 379 xbeeId = msg[2] - '0';
ddakev 0:4c644bb83761 380 pc.printf("ID: %d\n", xbeeId);
ddakev 0:4c644bb83761 381 waitingForId = false;
ddakev 0:4c644bb83761 382 }
ddakev 0:4c644bb83761 383 }
ddakev 0:4c644bb83761 384 else if(msg[1] == 'n') {
ddakev 0:4c644bb83761 385 // receiving name
ddakev 0:4c644bb83761 386 if(msg[2] - '0' == xbeeId) {
ddakev 0:4c644bb83761 387 // changing name of current gun
ddakev 0:4c644bb83761 388 char name[11];
ddakev 0:4c644bb83761 389 int index = 0;
ddakev 0:4c644bb83761 390 int msgIndex = 3;
ddakev 0:4c644bb83761 391 while(msg[msgIndex] != '\0') {
ddakev 0:4c644bb83761 392 if(index < 10) {
ddakev 0:4c644bb83761 393 name[index++] = msg[msgIndex];
ddakev 0:4c644bb83761 394 }
ddakev 0:4c644bb83761 395 msgIndex++;
ddakev 0:4c644bb83761 396 }
ddakev 0:4c644bb83761 397 name[index] = '\0';
ddakev 0:4c644bb83761 398 strcpy(playerName, name);
ddakev 0:4c644bb83761 399 pc.printf("Name: %s\n", name);
ddakev 0:4c644bb83761 400 redraw_header();
ddakev 0:4c644bb83761 401 }
ddakev 0:4c644bb83761 402 }
ddakev 0:4c644bb83761 403 }
ddakev 0:4c644bb83761 404 if(msg[0] == 'g') {
ddakev 0:4c644bb83761 405 // game action
ddakev 0:4c644bb83761 406 if(msg[1] == 's') {
ddakev 0:4c644bb83761 407 // game start initiated
ddakev 0:4c644bb83761 408 if(xbeeId != -1) {
ddakev 0:4c644bb83761 409 int duration = 0;
ddakev 0:4c644bb83761 410 int index = 2;
ddakev 0:4c644bb83761 411 while(msg[index] != '\0') {
ddakev 0:4c644bb83761 412 duration = duration * 10 + (msg[index++] - '0');
ddakev 0:4c644bb83761 413 }
ddakev 0:4c644bb83761 414 timeRemaining = duration;
ddakev 0:4c644bb83761 415 redraw_header();
ddakev 0:4c644bb83761 416 switchState(STARTING);
ddakev 0:4c644bb83761 417 }
ddakev 0:4c644bb83761 418 }
ddakev 0:4c644bb83761 419 if(msg[1] == 'e') {
ddakev 0:4c644bb83761 420 // game ended
ddakev 0:4c644bb83761 421 if(xbeeId != -1) {
ddakev 0:4c644bb83761 422 switchState(ENDED);
ddakev 0:4c644bb83761 423 }
ddakev 0:4c644bb83761 424 }
ddakev 0:4c644bb83761 425 if(msg[1] == 'i') {
ddakev 0:4c644bb83761 426 // kill info from server
ddakev 0:4c644bb83761 427 if(xbeeId != -1) {
ddakev 0:4c644bb83761 428 int shooterId = msg[2] - '0';
ddakev 0:4c644bb83761 429 int index = 3;
ddakev 0:4c644bb83761 430 char shooterName[11];
ddakev 0:4c644bb83761 431 int nameIndex = 0;
ddakev 0:4c644bb83761 432 while(msg[index] < '0' || msg[index] > '9') {
ddakev 0:4c644bb83761 433 if(nameIndex < 10) {
ddakev 0:4c644bb83761 434 shooterName[nameIndex++] = msg[index];
ddakev 0:4c644bb83761 435 }
ddakev 0:4c644bb83761 436 index++;
ddakev 0:4c644bb83761 437 }
ddakev 0:4c644bb83761 438 shooterName[nameIndex] = '\0';
ddakev 0:4c644bb83761 439 int shooteeId = msg[index++] - '0';
ddakev 0:4c644bb83761 440 char shooteeName[11];
ddakev 0:4c644bb83761 441 nameIndex = 0;
ddakev 0:4c644bb83761 442 while(msg[index] != '\0') {
ddakev 0:4c644bb83761 443 if(nameIndex < 10) {
ddakev 0:4c644bb83761 444 shooteeName[nameIndex++] = msg[index];
ddakev 0:4c644bb83761 445 }
ddakev 0:4c644bb83761 446 index++;
ddakev 0:4c644bb83761 447 }
ddakev 1:7d957da791a5 448 shooteeName[nameIndex] = '\0';
ddakev 0:4c644bb83761 449 if(shooterId == xbeeId) {
ddakev 0:4c644bb83761 450 char msg[80];
ddakev 0:4c644bb83761 451 strcpy(msg, "You shot ");
ddakev 0:4c644bb83761 452 strcat(msg, shooteeName);
ddakev 0:4c644bb83761 453 strcat(msg, "!");
ddakev 0:4c644bb83761 454 redraw_middle_message(msg);
ddakev 0:4c644bb83761 455 score += 200;
ddakev 0:4c644bb83761 456 message_timeout.attach(&redraw_score, 3.0);
ddakev 0:4c644bb83761 457 green_ticker.attach(&flash_green, 0.05);
ddakev 0:4c644bb83761 458 green_timeout.attach(&stop_green, 3.0);
ddakev 0:4c644bb83761 459 vibrator = 1;
ddakev 0:4c644bb83761 460 vibrator_timeout.attach(&turnOffVibrator, 1.0);
ddakev 0:4c644bb83761 461 }
ddakev 0:4c644bb83761 462 else if(shooteeId == xbeeId) {
ddakev 0:4c644bb83761 463 char msg[80];
ddakev 0:4c644bb83761 464 strcpy(msg, shooterName);
ddakev 0:4c644bb83761 465 strcat(msg, " shot you!");
ddakev 0:4c644bb83761 466 redraw_middle_message(msg);
ddakev 0:4c644bb83761 467 dead = true;
ddakev 0:4c644bb83761 468 if(score < 100) score = 0;
ddakev 0:4c644bb83761 469 else score -= 100;
ddakev 0:4c644bb83761 470 message_timeout.attach(&respawn, 5.0);
ddakev 0:4c644bb83761 471 red_ticker.attach(&flash_red, 0.05);
ddakev 0:4c644bb83761 472 red_timeout.attach(&stop_red, 5.0);
ddakev 0:4c644bb83761 473 vibrator = 1;
ddakev 0:4c644bb83761 474 vibrator_timeout.attach(&turnOffVibrator, 5.0);
ddakev 0:4c644bb83761 475 }
ddakev 0:4c644bb83761 476 }
ddakev 0:4c644bb83761 477 }
ddakev 0:4c644bb83761 478 if(msg[1] == 'r') {
ddakev 0:4c644bb83761 479 xbeeId = -1;
ddakev 0:4c644bb83761 480 strcpy(playerName, "N/A");
ddakev 0:4c644bb83761 481 waitingForId = false;
ddakev 0:4c644bb83761 482 dead = false;
ddakev 0:4c644bb83761 483 switchState(NOT_STARTED);
ddakev 0:4c644bb83761 484 score = 0;
ddakev 0:4c644bb83761 485 }
ddakev 0:4c644bb83761 486 }
ddakev 0:4c644bb83761 487 }
ddakev 0:4c644bb83761 488 }