football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Revision:
89:425369a595c4
Parent:
88:d3f0394da5aa
Child:
90:b4d53a870147
--- a/main.cpp	Tue Jun 07 08:43:21 2016 +0000
+++ b/main.cpp	Sat Jun 11 06:50:32 2016 +0000
@@ -2,12 +2,24 @@
 #include <list>
 #include "output.h"
 #include <DebounceIn.h>
+#ifdef BLE_ENABLE
+  #include "ble/BLE.h"
+#endif 
 
 using namespace std;
 
-Timer t;
+Timer tmr;
 Output out;
 
+#ifdef BLE_ENABLE
+/* Optional: Device Name, add for human read-ability */
+const static char     DEVICE_NAME[] = "PassiveCoach";
+
+/* You have up to 26 bytes of advertising data to use. */
+//const static uint8_t AdvData[] = {0x01,0x02,0x03,0x04,0x05};   /* Example of hex data */
+const static uint8_t AdvData[] = {"Keep the Space!"};         /* Example of character data */
+#endif // BLE_ENABLE
+
 ///////////////////////////////////  PINS   ///////////////////////////////////
 
 DebounceIn buttonTeam(BUT_TEAM);
@@ -125,6 +137,67 @@
     }
 }
 
+#ifdef BLE_ENABLE
+/* Optional: Restart advertising when peer disconnects */
+void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
+{
+    BLE::Instance().gap().startAdvertising();
+}
+/**
+ * This function is called when the ble initialization process has failed
+ */
+void onBleInitError(BLE &ble, ble_error_t error)
+{
+    /* Avoid compiler warnings */
+    (void) ble;
+    (void) error;
+    
+    /* Initialization error handling should go here */
+}    
+
+/**
+ * Callback triggered when the ble initialization process has finished
+ */
+void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
+{
+    BLE&        ble   = params->ble;
+    ble_error_t error = params->error;
+
+    if (error != BLE_ERROR_NONE) {
+        /* In case of error, forward the error handling to onBleInitError */
+        onBleInitError(ble, error);
+        return;
+    }
+
+    /* Ensure that it is the default instance of BLE */
+    if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
+        return;
+    }
+    
+    /* Set device name characteristic data */
+    ble.gap().setDeviceName((const uint8_t *) DEVICE_NAME);
+
+    /* Optional: add callback for disconnection */
+    ble.gap().onDisconnection(disconnectionCallback);
+
+    /* Sacrifice 3B of 31B to Advertising Flags */
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE );
+    ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+
+    /* Sacrifice 2B of 31B to AdvType overhead, rest goes to AdvData array you define */
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, AdvData, sizeof(AdvData));
+
+    /* Optional: Add name to device */
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
+
+    /* Set advertising interval. Longer interval == longer battery life */
+    ble.gap().setAdvertisingInterval(100); /* 100ms */
+
+    /* Start advertising */
+    ble.gap().startAdvertising();
+}
+#endif // BLE_ENABLE
+
 /////////////////////////////////// CLASSES ///////////////////////////////////
 
 struct Contact {
@@ -145,7 +218,7 @@
         team = _team;
         Contact c;
         c.rssi = rssi;
-        c.time_ms = (time!=NULL) ? time : t.read_ms();
+        c.time_ms = (time!=NULL) ? time : tmr.read_ms();
         contacts.push_front(c);
         
         // Cleanup
@@ -154,7 +227,7 @@
     }
     int16_t get_distance(int &cnt) {
         // Find max RSSI
-        uint32_t cur_time = t.read_ms();
+        uint32_t cur_time = tmr.read_ms();
         int16_t max_rssi=-128;
         cnt=0;
         for (list<Contact>::iterator it=contacts.begin(); it != contacts.end(); ++it) {
@@ -230,28 +303,47 @@
     char other_name[NAME_LEN+1] = {0};
     int8_t myTeam = 1, otherTeam;
     uint8_t level = 1, volume = 1;      // SPACE10, VOL_LOW
+    int bTeamNew, bTeamOld, bSpaceOld, bSpaceNew, bVMNew, bVLNew, bVMOld, bVLOld;
     
+    // Blink all the space LEDs at startup
+    spaceLEDs(0); wait(0.2); spaceLEDs(1); wait(0.2); spaceLEDs(2); wait(0.2);  spaceLEDs(3); wait(0.2);
+    // ...and go to a single LED
+    spaceLEDs(level);
+    
+    // Beep on startup
     ledBuzzer = volume ? 1 : 0;
     beep(NOTE_A5, 0.5, volume);
 
+    // Turn on the team LED
+    ledTeamA = myTeam & 1;
+    ledTeamB = ~myTeam & 1;
+
+    // Buttons initialization
     buttonSpace.mode(PullDown);
     buttonVolMore.mode(PullDown);
     buttonVolLess.mode(PullDown);
 #ifdef NORDIC
     buttonTeam.mode(PullDown);
-    int bTeamOld = 0;
+    bTeamOld = 0;
 #else
     buttonTeam.mode(PullUp);
-    int bTeamOld = 1;
+    bTeamOld = 1;
 #endif
-    int bTeamNew, bSpaceOld=0, bSpaceNew, bVMNew, bVLNew, bVMOld=0, bVLOld=0;
-
-    spaceLEDs(level);
+    bSpaceOld = 0;
+    bVMOld = 0;
+    bVLOld = 0;
     
+#ifdef BLE_ENABLE
+    // BLE initialization
+    BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
+    ble.init(bleInitComplete);  // Initialize BLE baselayer
+#endif // BLE_ENABLE
+    
+    // Pick node number
     char this_node = int(ain.read()*255+17)*int(ain.read()*255+11); // random node value
     out.printf("Node: %d\r\n", this_node);
 
-    // Init radio    
+    // Initialize the radio    
     radio.initialize(FREQUENCY, this_node, NETWORKID);
 #ifdef HIGH_POWER
     radio.setHighPower(true);
@@ -262,23 +354,18 @@
         radio.setFrequency(868000000);
     else if(FREQUENCY == RF69_915MHZ) 
         radio.setFrequency(915000000);
-        
 
+    // Pick node name
     generate_name(rand_name, sizeof(rand_name));
     out.printf("Name: %s\r\n", rand_name);
     out.sleep(2.0);
         
-    t.start();
-    ledTeamA = myTeam & 1;
-    ledTeamB = ~myTeam & 1;
-#ifdef NORDIC
-    buzzMed = 1;
-#endif
+    tmr.start();
     
     Players players;
     
-    uint32_t last_send = t.read_ms();
-    uint32_t last_shown = t.read_ms();
+    uint32_t last_send = tmr.read_ms();
+    uint32_t last_shown = tmr.read_ms();
     //uint32_t min_wait = SEND_RATE_MS - 0.5*SEND_RATE_MS*SEND_DESYNC;
     //uint32_t send_wait = min_wait;
 
@@ -329,7 +416,7 @@
         bVLOld = bVLNew;
 
         // Output
-        unsigned long current_time = t.read_ms();
+        unsigned long current_time = tmr.read_ms();
         
         if (current_time - last_shown > CYCLE_MS)
         {
@@ -346,6 +433,7 @@
             last_send = current_time;
             //send_wait = min_wait + (rand() % SEND_RATE_MS)*SEND_DESYNC;
       
+            //// Some debugging info I used to display
             //out.clear();
             //out.printf("Sent: %s\r\n", tx_buff);
             //uint8_t tempC = radio.readTemperature(-1); // -1 = user cal factor, adjust for correct ambient
@@ -368,7 +456,7 @@
                 if(sizeof(rx_buff)>8 && rx_buff[7]==',') {
                     otherTeam = rx_buff[8]-'0';
                 } else otherTeam = 1;
-                players.update(other_name, otherTeam, radio.RSSI, t.read_ms());
+                players.update(other_name, otherTeam, radio.RSSI, tmr.read_ms());
 
                 //uint8_t gain = (radio.readReg(REG_LNA) & 0b111000)>>3;   // LNA Current Gain
                 //out.clear();
@@ -384,5 +472,9 @@
                 out.sleep(2.0);
             }
         }
+        
+#ifdef BLE_ENABLE
+        ble.waitForEvent();
+#endif
     }
 }
\ No newline at end of file