Updated Space Invaders on the mbed. Improved upon Michael Son's "Mbed Space Invaders" at https://os.mbed.com/users/michaeljson/notebook/mbed-space-invaders/.

Dependencies:   mbed wave_player mbed-rtos 4DGL-uLCD-SE SparkfunAnalogJoystick SDFileSystem LSM9DS1_Library_cal_updated

Fork of Two-PlayerSpaceInvaders by William Minix

test

Revision:
7:9a826617be92
Parent:
6:c44055f94cc3
Child:
8:27c4345be3db
diff -r c44055f94cc3 -r 9a826617be92 main.cpp
--- a/main.cpp	Fri Apr 16 17:07:46 2021 +0000
+++ b/main.cpp	Fri Apr 16 17:27:30 2021 +0000
@@ -111,6 +111,7 @@
 volatile int numWins = 0;
 volatile bool two_player_win = false;
 volatile bool two_player_lose = false;
+Mutex mbed;
 
 // Initialize global player object
 player_t player;
@@ -458,9 +459,12 @@
         while(numPlayers == 1) Thread::yield(); // with one player, thread is unneeded and should yield.
         while(!first_player_ready || !second_player_ready) { // while either player isn't ready, send a start code, and become ready
         //if (!first_player_ready || !second_player_ready) {
+            mbed.lock();
             secondMbed.putc('S');
+            mbed.unlock();
             first_player_ready = true;
-            pc.printf("first player ready");
+            //pc.printf("first player ready");
+            Thread::wait(1000);
         //}            
         //    if (secondMbed.readable()) { // read in a start code to know that the second player is ready (if the second player sends a start code)
         //        if (secondMbed.getc() == 'S') {
@@ -471,32 +475,40 @@
         //    Thread::wait(1000); // run once a second
         }
         if (two_player_win) { // if this player wins, notify the other mbed/player that they lost.
+            mbed.lock();
             secondMbed.putc('W');
+            mbed.unlock();
         }
         //if (secondMbed.readable()) {
         //    if (secondMbed.getc() == 'W') {
         //        two_player_lose = true;
         //    }
         //}
-        Thread::wait(500); // check twice a second for a win
+        Thread::wait(2000); // check twice a second for a win
     }
 }
 
 void mbedReceive(void const *args) {
+    char rx;
     while(1) {
-        while(numPlayers == 1 || secondMbed.readable()) Thread::yield(); // with one player, thread is unneeded and should yield.
-        if (!second_player_ready) { // read in a start code to know that the second player is ready (if the second player sends a start code)
-                if (secondMbed.getc() == 'S') {
-                    second_player_ready = true;
-                    pc.printf("second play ready");
-                }
+        mbed.lock();
+        while(numPlayers == 1 || !secondMbed.readable()) {
+             mbed.unlock();
+             //pc.printf("yielding");
+             Thread::yield(); // with one player, thread is unneeded and should yield.
+        }
+        rx = secondMbed.getc();
+        mbed.unlock();
+        if (rx == 'S') {
+            second_player_ready = true;
+            //pc.printf("second play ready");
         }
         //    Thread::wait(1000); // run once a second
         //}
-        if (secondMbed.getc() == 'W') {
+        if (rx == 'W') {
             two_player_lose = true;
         }
-        Thread::wait(500); // check twice a second for a win
+        Thread::wait(2000); // check twice a second for a win
     }
 }