Laser Tag - University of Oxford CWM 2015 / Mbed 2 deprecated Armour

Dependencies:   communication mbed

Files at this revision

API Documentation at this revision

Comitter:
Huqi
Date:
Thu Jun 11 15:28:37 2015 +0000
Child:
1:6db021f1bba2
Commit message:
initial build

Changed in this revision

communication.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/communication.lib	Thu Jun 11 15:28:37 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/teams/Oxford-cwm-laser-tag/code/communication/#7c0f460e2717
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Jun 11 15:28:37 2015 +0000
@@ -0,0 +1,246 @@
+#include "mbed.h"
+#include "comm.h"
+
+DigitalOut myled(LED1);
+Serial RX(NC,PTE1);      //Set up serial for IR sensor
+DigitalOut LEDHigh(PTE30);       //Need to define a port for this
+DigitalOut LEDLow(PTE29);        //Need to define a port for this
+Serial gun(PTE22,PTE23);          //Need to define a port for this
+Serial pc(USBTX, USBRX);        //Pc serial for debugging
+DigitalOut gunEnableFlag(PTE21);        //Flag for gunEnable
+
+void checkHit();
+void checkScoreboard();
+void gameStart();
+void gameEnd();
+void gamePause();
+void gameContinue();
+void powerDown();
+void sendGunInfo(char code);
+
+char* rxBuffer = new char[4];
+char* txBuffer = new char[4];
+
+int readSize = 0;
+int writeSize = 0;
+
+char gameOn;            //Initialize boolean for game state
+
+int main()
+{
+
+
+
+    RX.baud(1000);          //Set baud rate for the receiver
+
+
+    LEDLow = 0;
+    LEDHigh = 1;
+
+
+
+
+    gameOn = 1;
+
+    commInit(1);             //Initialize communications
+
+
+
+
+    while(gameOn) {
+        //Game loop
+        checkHit();
+        //checkScoreboard();
+    }
+
+    while(!gameOn) {
+        //Loop to run when game is off
+        checkScoreboard();
+    }
+
+}
+
+void checkHit()
+{
+    char identifier = 0;
+    pc.printf("Looking\n\r");
+
+    //Logic to see if we have been hit
+    if(RX.readable()) {
+        while(identifier != 'a' && identifier != 'z' && identifier != 'B') {
+            identifier = RX.getc();
+            pc.putc(identifier);
+        }
+    }
+
+    if(identifier != 0) {
+        //Store data
+
+        if(identifier == 'a') {
+            txBuffer[0]++;
+        }
+        if(identifier == 'z') {
+            txBuffer[1]++;
+        }
+        if(identifier == 'B') {
+            txBuffer[2]++;
+        }
+
+
+        powerDown();
+
+
+    }
+
+    //If not hit, continue as normal
+
+
+}
+
+void checkScoreboard()
+{
+
+// comm check, needs to be done regularly
+    if(commRead(rxBuffer)) {
+
+        //Get gun shots and load it in
+        sendGunInfo('z');
+        wait(1);
+        if(gun.readable())
+        {
+            char shots;
+            shots = gun.getc();
+            txBuffer[3] = shots;
+            
+            while(gun.readable())
+            {
+                char trash;
+                trash = gun.getc();
+            }
+        }
+        commSetAck(txBuffer);
+        switch(rxBuffer[0]) 
+        {
+            case 0: //code to be executed on start signal e.g. game = true
+
+                gameStart();
+
+                break;
+            case 1: // code to be executed on a get hits request, probably nothing
+
+
+                break;
+            case 2: // code to be executed on end signal e.g. game = false
+
+                gameEnd();
+
+                break;
+            case 3: // code to be executed on pause signal e.g. pause = true
+
+                gamePause();
+
+                break;
+            case 4: // code to be executed on resume signal e.g. pause = false
+
+                gameContinue();
+
+                break;
+
+        }
+    }
+
+}
+
+void powerDown()
+{
+
+    sendGunInfo('a');
+
+    
+    //Blinking LEDs
+    //Switch LEDs off
+    for(int i = 0; i < 5; i++)
+    {
+        LEDHigh = 0;
+        wait(0.1);
+        LEDHigh = 1;
+        wait(0.1);
+    }
+    LEDHigh = 0;
+    
+    //Time delay for the armour to be down
+    wait(4);
+
+
+
+    //Switch LEDs on
+    LEDHigh = 1;
+
+    //Enable gun
+
+    sendGunInfo('a');
+ 
+    
+
+    while(RX.readable()) 
+    {
+        //Loop to wipe buffer of the receiver
+        //Need to ignore any shots received during the power down phase
+        char trash;
+        trash = RX.getc();
+    }
+
+
+}
+
+void gameStart()
+{
+    //Enable gun
+    sendGunInfo('a');
+
+    //Switch LEDs on
+    LEDHigh = 1;
+    //Enable game loop
+    gameOn = 1;
+    
+    for (int i = 0; i < 4; i++) {
+        txBuffer[i] = 0;
+    }
+    
+    
+
+}
+
+
+void gameEnd()
+{
+    gameOn = 0;
+    sendGunInfo('a');
+
+    LEDHigh = 0;
+}
+
+void gamePause()
+{
+    gameOn = 0;
+    sendGunInfo('a');
+
+}
+
+void gameContinue()
+{
+    gameOn = 1;
+    sendGunInfo('a');
+
+}
+
+void sendGunInfo(char code)
+
+{
+    gunEnableFlag = 1;
+    gun.putc(code);
+    gunEnableFlag = 0;
+}
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Jun 11 15:28:37 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/cbbeb26dbd92
\ No newline at end of file