![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
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
Diff: main.cpp
- Revision:
- 8:1dc719b54bbc
- Parent:
- 7:f7c3dc874db2
- Child:
- 9:e6f3edc7f775
--- a/main.cpp Mon Dec 07 04:08:11 2015 +0000 +++ b/main.cpp Mon Dec 07 09:34:29 2015 +0000 @@ -12,15 +12,16 @@ using namespace std; game_board battle; RawSerial pc(USBTX, USBRX); // uart //not conflit +RawSerial xbee(P4_22, P4_23); // 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); +//SWSPI Sender(p17,p18,p19); // p11,p12,p13 +//DigitalOut b_cs (p20); //SPI SLAVE to receive; -SPISlave Receiver(p11,p12,p13,p14); +//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 @@ -81,47 +82,98 @@ // 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; + //SPI implementation could not get it to work due to run out out spi + //port for design purpose software spi does not compatible + //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); + + //} + //uart xbee impletation it is/ + while(true) + { + char R_message [5] = {0,0,0,0,0}; + int got_mess = 0;//temp + int i = 0; + + if(send_check == 1) + { + xbee.printf ("S00%d%dE",SPI_X_send,SPI_Y_send); 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; - } + + while(xbee.readable()) + { + char temp; + temp = xbee.getc(); + R_message[i] = temp; + i = i+1; + got_mess = 1; + osDelay(10); } - osDelay (30); - + if (R_message[0] == 'S') + { + int x_coor,y_coor; + int hit_miss; + x_coor = (int)R_message[3] - 48; + y_coor = (int)R_message[4] - 48; + hit_miss = battle.place_hit_miss_friendly(y_coor,x_coor); + xbee.printf ("E0%d%d%dE",hit_miss,x_coor,y_coor); + //pc.printf ("x=%d y=%d\n\r",x_coor,y_coor); + update_game_look = 1; + } + else if (R_message[0] == 'E') + { + int x_coor,y_coor; + int hit_miss; + hit_miss = (int)R_message[2] - 48; + x_coor = (int)R_message[3] - 48; + y_coor = (int)R_message[4] - 48; + battle.place_hit_miss_enemy(y_coor,x_coor,(bool)hit_miss); + update_game_look = 1; + } + else if (R_message[0] == 'W') + { + winner = 1; + } + //osDelay(30); } } @@ -216,7 +268,7 @@ if (update_game_look == 1) { update_game_look = 0; int i; - //pc.printf ("\f\n\r"); + pc.printf ("\f\n\r"); for (i = 1; i < 9; i++) { string a; a = battle.get1row(i); @@ -240,11 +292,12 @@ led1 = 1; led2 = 1; led3 = 1; + xbee.printf("W0000E"); //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) { + while (winner == 1) { //pc.printf ("%d \n\r",rand()%2); led1 = (rand()%2); led2 = (rand()%2); @@ -252,9 +305,11 @@ osDelay(30); } } - led1 = 1; - led2 = 1; - led3 = 1; + else { + led1 = 1; + led2 = 1; + led3 = 1; + } } } @@ -273,10 +328,11 @@ 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) { + winner = 0; pc.printf ("\fNew Game: Battle Ship Θ\n\r"); battle.new_game_board(); // for (i = 1; i < 9; i++) { @@ -344,12 +400,9 @@ 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); + xbee.baud(115200); + 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"); @@ -383,7 +436,7 @@ y_old = y; battle.place_ship(x,y); } - //pc.printf("\fBattle Ship by Andy, Adam, Loc, Ivan\n\r"); + pc.printf("\fBattle Ship by Andy, Adam, Loc, Ivan\n\r"); for (i = 1; i < 9; i++){ string a; a = battle.get1row(i);