This is THE 447 FINAL PROJECT this is the frame work put your code in the spot where the it suppose to go and. Make sure you import this into your complier and work on your section, once you done just commit the changes and fork to a new folder

Dependencies:   mbed-rtos mbed draw_test EALib SWSPI

Revision:
7:f7c3dc874db2
Parent:
6:0f60a4070f09
Child:
8:1dc719b54bbc
--- a/main.cpp	Sat Dec 05 10:24:47 2015 +0000
+++ b/main.cpp	Mon Dec 07 04:08:11 2015 +0000
@@ -5,14 +5,23 @@
 #include "mbed.h"
 #include "cmsis_os.h"
 #include "MMA7455.h"
+#include "SWSPI.h"
 #include "game_board.h" //duck soup
-
+// need to configure uart to 115200 for game shot placement  using uart;
+//SPI 2 is use// SPI0 is use
 using namespace std;
 game_board battle;
-RawSerial pc(USBTX, USBRX); // uart
-MMA7455 accSensor (P0_27, P0_28); //acceleronmeter i2c
-SPI LEDs(p5, NC, p7); // LEDs driver spi
-DigitalOut cs (p30);
+RawSerial pc(USBTX, USBRX); // uart //not conflit
+MMA7455 accSensor (P0_27, P0_28); //acceleronmeter i2c // no conflit
+SPI LEDs(p5, NC, p7); // LEDs driver spi used
+//SPISlave test(p18,p19,p20,p17);
+//SPI between board
+DigitalOut cs (p30); //no conflict 
+SWSPI Sender(p17,p18,p19); // p11,p12,p13
+DigitalOut b_cs (p20);
+//SPI SLAVE to receive;
+SPISlave Receiver(p11,p12,p13,p14);
+
 AnalogIn trimpot(p15);//might need to change the trimpot location if conflict with spi
 //SPI (PinName mosi, PinName miso, PinName sclk, PinName ssel=NC) //check mapping for pin
 //SPISlave (mosi, miso, sclk, ssel) //check mapping for pin
@@ -44,15 +53,17 @@
 int friendly [6][6]; // array to determine friendly location
 int winner; 
 int A_x_old = 0,A_y_old = 0,A_z_old = 0;
+int send_check = 0;
+int update_game_look = 0;
 //X_coor is x coordinate from 0 to 6
 //Y_coor is y coordinate form A to F
 //
 //
 //
 void LEDsoutput(uint8_t value) {
-    cs = 0;
+    cs.write(0);
     LEDs.write(value);
-    cs = 1;
+    cs.write(1);
 }
 // this thread is your SPI communication between 2 board
 void SPI_communication(void const *args) {
@@ -70,6 +81,48 @@
     //       0 = still no winner
     //       1 = i am lose
     //       so if you receive a 1 that mean you win
+    while (true) {
+        if (send_check == 1) {
+            int message = 0;
+            int Echo = 0;
+            message = (SPI_X_send << 4)|SPI_Y_send;
+            pc.printf ("send message %d\n\r",message);
+            b_cs.write(0);
+            Echo = Sender.write(0x12);
+            //osDelay (10);
+            b_cs.write(1);
+            pc.printf("get from echo 1 %d\n\r",Echo);
+            b_cs.write(0);
+            Echo = Sender.write (0x1FF);
+            b_cs.write(1);
+            pc.printf("get from echo 2 %d\n\r",Echo);
+            bool stat = (Echo &0x100)>>8;
+            int x_coor = (Echo & 0x0F0)>>4;
+            int y_coor = (Echo & 0x00F);
+            battle.place_hit_miss_enemy(x_coor,y_coor,stat);
+            update_game_look = 1;
+            send_check = 0;
+        }
+        if (Receiver.receive()) {
+            bool status = 0;
+            int R_message = Receiver.read();
+            pc.printf("receive message = %d\n\r",R_message);
+            if ((R_message & 0x200) !=  0) {
+                winner = 1;
+            }
+            else if (R_message != 0x1FF) {
+                int Y_coor = R_message & 0x00F;
+                int X_coor = (R_message & 0x0F0) >> 4;
+                status = battle.place_hit_miss_friendly (X_coor,Y_coor);
+                int E_message = (status << 8)|R_message;
+                Receiver.reply(E_message);
+                pc.printf("echo message = %d",E_message);
+                update_game_look = 1;
+            }
+        }
+        osDelay (30);
+        
+    }
 }
 
 // joystick update x_coor and y_coor here
@@ -100,18 +153,21 @@
             if (my_X_coor == 0) {
                 my_X_coor = 6;
             }
-            //my_LEDs = my_X_coor;
+        }
+        //my_LEDs = my_X_coor;
+        else if (Center.read() == 0) {
+            pc.printf ("got middle\n\r");
+            SPI_X_send = my_X_coor;
+            SPI_Y_send = my_Y_coor;
+            pc.printf ("spi x = %d\n\r",SPI_X_send);
+            pc.printf ("spi y = %d\n\r",SPI_Y_send);
+            send_check = 1;
         } else {
             my_X_coor = my_X_coor;
             my_Y_coor = my_Y_coor;
         }
-        my_LEDs = (my_X_coor << 4)|(0x0F-(my_Y_coor-1));
+        my_LEDs = (my_X_coor << 4)|(0x0A+(my_Y_coor-1));
         LEDsoutput (my_LEDs);
-        SPI_X_send = my_X_coor;
-        SPI_Y_send = my_Y_coor;
-        if (Center.read() == 0) {
-            pc.printf ("got middle\n\r");
-        }
         osDelay (170);
     }
     //joystick is done and over with DO NOT touch
@@ -155,29 +211,52 @@
     // Turn 2 = We sink an enemy battle ship at 3E
     // Turn 3 = miss at 4D
     // Turn 4 = miss at 2C
-    // Tunr 5 = We sink an enemy battle ship at 4B 
+    // Tunr 5 = We sink an enemy battle ship at 4B
+    while (true) {
+        if (update_game_look == 1) {
+            update_game_look = 0;
+            int i;
+            //pc.printf ("\f\n\r");
+            for (i = 1; i < 9; i++) {
+                string a;
+                a = battle.get1row(i);
+                pc.printf ("%s", a);
+            }
+        }
+        osDelay (30);
+    }
            
 }
 
 // code here to check for winer and turn on the RGB
-void winnercheck (void const *args) {
+void winnercheck (void const *args)
+{
     //check for winner by destroy all enemy ship;
-    bool gamelost;
-    gamelost = battle.looser();
-    if (gamelost == 1)
-    {
-        //this board is loser
-        //send a message to the winner    
+    while (true) {
+        bool gamelost;
+        gamelost = battle.looser();
+        //winner = 1;
+        if (gamelost == 1) {
+            led1 = 1;
+            led2 = 1;
+            led3 = 1;
+            //this board is loser
+            //send a message of 0x200 to the winner
+        } else if (winner == 1) { //spi message is the winner)
+            pc.printf("Spectacular Battle commander\n\r");
+            while (true) {
+                //pc.printf ("%d \n\r",rand()%2);
+                led1 = (rand()%2);
+                led2 = (rand()%2);
+                led3 = (rand()%2);
+                osDelay(30);
+            }
+        }
+        led1 = 1;
+        led2 = 1;
+        led3 = 1;
+
     }
-    else if (winner == 1)//spi message is the winner)
-    {
-        //blink the RGB LED
-        //
-    }
-    led1 = 1; 
-    led2 = 1; 
-    led3 = 1; 
-
 }
 
 //read the accele
@@ -194,17 +273,17 @@
         int x, y;
         int i;
         int seed = trimpot.read()*1000;
+        winner = 0;
         srand (seed);
         if(! accSensor.read(Ax,Ay,Az)) {};
         if (abs(Ax-A_x_old) > 20) {
-            pc.printf("new game \n\r");
+            pc.printf ("\fNew Game: Battle Ship Θ\n\r");
             battle.new_game_board();
-            for (i = 1; i < 9; i++) {
-                string a;
-                a = battle.get1row(i);
-                pc.printf ("%s", a);
-
-            }
+        //    for (i = 1; i < 9; i++) {
+        //        string a;
+        //        a = battle.get1row(i);
+        //        pc.printf ("%s", a);
+        //    }
             //randomize ship position 6 ship total
             for (i = 0; i < 7; i++) {
                 x = (rand()%6 +1);
@@ -213,12 +292,13 @@
                 }
                 x_old = x;
                 y = (rand()%5 +1);
-                while (y == y_old) {
+                while (y == y_old) { 
                     y = (rand()%5 +1);
                 }
                 y_old = y;
                 battle.place_ship(x,y);
             }
+            //pc.printf ("\f");
             for (i = 1; i < 9; i++) {
                 string a;
                 a = battle.get1row(i);
@@ -262,7 +342,13 @@
         int x, y;
         int i;
         int seed = trimpot.read()*1000;
+        winner = 0;
         srand (seed);
+        Sender.format (10,3);
+        Receiver.format (10,3);
+        Sender.frequency (1000000);
+        Receiver.frequency (1000000);
+        b_cs.write(1);
         pc.baud(921600);
         pc.printf("Battle Ship by Andy, Adam, Loc, Ivan\n\r");
         while(!accSensor.setMode(MMA7455::ModeMeasurement)) {
@@ -297,6 +383,7 @@
             y_old = y;
             battle.place_ship(x,y);
         }
+        //pc.printf("\fBattle Ship by Andy, Adam, Loc, Ivan\n\r");
         for (i = 1; i < 9; i++){
             string a;
             a = battle.get1row(i);
@@ -307,8 +394,8 @@
     }
     //Main Thread
     while (true) {
-        //osDelay (30); 
+        osDelay (30); 
         //pc.printf ("in main");
         //check status and update status
     }
-}
\ No newline at end of file
+}