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:
6:0f60a4070f09
Parent:
4:b192d0d47309
Child:
7:f7c3dc874db2
--- a/main.cpp	Thu Dec 03 09:45:36 2015 +0000
+++ b/main.cpp	Sat Dec 05 10:24:47 2015 +0000
@@ -1,27 +1,59 @@
+#include <stdio.h>
+#include <stdlib.h>
 #include <iostream>
+#include <time.h>
 #include "mbed.h"
 #include "cmsis_os.h"
+#include "MMA7455.h"
 #include "game_board.h" //duck soup
+
 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);
+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
 
+//joystick
+DigitalIn Up(p32);
+DigitalIn Down(p38);
+DigitalIn Left(p39);
+DigitalIn Right(p37);
+DigitalIn Center(p31);
+//joystick done
+
+//RGB LED
+DigitalOut led1(p25); //red
+DigitalOut led2(p26);//green
+DigitalOut led3(p28);//blue
+//RGB LED
+int initlize = 0;
 //declare your input pin and output pin here:
 //DigitalOut led1(p25);
 //DigitalOut led2(p26);
 //DigitalOut led3(p28);
 //
 // declare you globle variable here:
-unsigned char my_X_coor = 0, my_Y_coor = 0;
-unsigned char my_LEDs = 0x1A;
+unsigned char my_X_coor = 1, my_Y_coor = 1;
+uint8_t my_LEDs = 0x1A;
+uint8_t SPI_X_send = 0x01,SPI_Y_send = 0x01;
 unsigned char other_X_coor = 0, other_Y_coor = 0;
 int friendly [6][6]; // array to determine friendly location
- 
-
+int winner; 
+int A_x_old = 0,A_y_old = 0,A_z_old = 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;
+    LEDs.write(value);
+    cs = 1;
+}
 // this thread is your SPI communication between 2 board
 void SPI_communication(void const *args) {
     //send my_X_coor and my_Y_coor if the center button is hit
@@ -45,6 +77,45 @@
     // joy stick to update global val of x_coor and y_coor;
     // convert y coor to hex offset
     // update the 8 LEDs here
+    while (true) {
+        //pc.printf ("in joystick");
+        if (Up.read() == 0) {
+            my_Y_coor = my_Y_coor + 1;
+            if (my_Y_coor == 7) {
+                my_Y_coor = 1;
+            }
+        } else if (Down.read() == 0) {
+            my_Y_coor = my_Y_coor - 1;
+            if (my_Y_coor == 0) {
+                my_Y_coor = 6;
+            }
+        } else if (Left.read() == 0) {
+            my_X_coor = my_X_coor + 1 ;
+            if (my_X_coor == 7) {
+                my_X_coor = 1;
+            }
+            //my_LEDs = my_X_coor;
+        } else if (Right.read() == 0) {
+            my_X_coor = my_X_coor - 1;
+            if (my_X_coor == 0) {
+                my_X_coor = 6;
+            }
+            //my_LEDs = my_X_coor;
+        } else {
+            my_X_coor = my_X_coor;
+            my_Y_coor = my_Y_coor;
+        }
+        my_LEDs = (my_X_coor << 4)|(0x0F-(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
+     
 }
 
 // this thread use to update the game board
@@ -91,12 +162,75 @@
 // code here to check for winer and turn on the RGB
 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    
+    }
+    else if (winner == 1)//spi message is the winner)
+    {
+        //blink the RGB LED
+        //
+    }
+    led1 = 1; 
+    led2 = 1; 
+    led3 = 1; 
+
 }
 
 //read the accele
-void checkreset (void const *args) {
+void checkreset (void const *args)
+{
+
     //read the acceleron meter and compare against the previous value to
     //determine if the board have been shake the reset the game board;
+    //the determinant is the x axis
+    while (true) {
+
+        int Ax,Ay,Az;
+        int x_old = 0, y_old = 0;
+        int x, y;
+        int i;
+        int seed = trimpot.read()*1000;
+        srand (seed);
+        if(! accSensor.read(Ax,Ay,Az)) {};
+        if (abs(Ax-A_x_old) > 20) {
+            pc.printf("new game \n\r");
+            battle.new_game_board();
+            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);
+                while (x == x_old) {
+                    x = (rand()%6 +1);
+                }
+                x_old = x;
+                y = (rand()%5 +1);
+                while (y == y_old) {
+                    y = (rand()%5 +1);
+                }
+                y_old = y;
+                battle.place_ship(x,y);
+            }
+            for (i = 1; i < 9; i++) {
+                string a;
+                a = battle.get1row(i);
+                pc.printf ("%s", a);
+            }
+            Ax = 0;
+            A_x_old = 0;
+        } else
+            A_x_old = Ax;
+        osDelay(500);
+    }
+    //reset is done and working do not touch
 }
 
 
@@ -108,10 +242,10 @@
 
 int main() {
     osThreadCreate(osThread(SPI_communication), NULL);
-    osThreadCreate(osThread(joy_stick_read), NULL);
+    osThreadCreate(osThread(joy_stick_read), NULL); //done
     osThreadCreate(osThread(game_view_update), NULL);
     osThreadCreate(osThread(winnercheck), NULL);
-    osThreadCreate(osThread(checkreset), NULL);
+    osThreadCreate(osThread(checkreset), NULL);//done
     // initialize all peripheral here
     //
     //
@@ -122,8 +256,59 @@
     
     //note game initialize and setup upon shake the board
     
+    if (initlize == 0)
+    {
+        int x_old = 0, y_old = 0;
+        int x, y;
+        int i;
+        int seed = trimpot.read()*1000;
+        srand (seed);
+        pc.baud(921600);
+        pc.printf("Battle Ship by Andy, Adam, Loc, Ivan\n\r");
+        while(!accSensor.setMode(MMA7455::ModeMeasurement)) {
+           pc.printf("Unable to set measurement mode.\n\r");
+        }
+        while(!accSensor.setRange(MMA7455::Range_8g)) {
+           pc.printf("Unable to set the range.\n\r");
+        }
+        while(!accSensor.calibrate()) {
+           pc.printf("Unable to calibrate\n\r");
+        }
+       battle.new_game_board();
+       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);
+            while (x == x_old) 
+            {
+                x = (rand()%6 +1);
+            }
+            x_old = x;
+            y = (rand()%5 +1);
+            while (y == y_old) 
+            {
+                y = (rand()%5 +1);
+            }
+            y_old = y;
+            battle.place_ship(x,y);
+        }
+        for (i = 1; i < 9; i++){
+            string a;
+            a = battle.get1row(i);
+            pc.printf ("%s", a);
+        }
+        initlize = 1;
+        
+    }
     //Main Thread
     while (true) {
+        //osDelay (30); 
+        //pc.printf ("in main");
         //check status and update status
     }
 }
\ No newline at end of file