aa

Dependencies:   BLE_API Peripheral_1 mbed nRF51822

Fork of Peripheral_1 by -deleted-

Files at this revision

API Documentation at this revision

Comitter:
ssenwkdw
Date:
Mon Jan 18 01:42:57 2016 +0000
Parent:
3:3f4aa38b0fb7
Commit message:
ADD demo

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Oct 01 17:33:32 2015 +0000
+++ b/main.cpp	Mon Jan 18 01:42:57 2016 +0000
@@ -13,7 +13,9 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
+#include <cstdlib>
+#include <ctime>
+#include <math.h>
 #include "mbed.h"
 #include "ble/BLE.h"
 #include "ble/DiscoveredCharacteristic.h"
@@ -28,34 +30,23 @@
 DigitalOut pin15(P0_15);
 DigitalOut pin29(P0_29);
 
-static const unsigned NUM_ROBOTS = 8;
-static const unsigned ADDR_LEN = 6;
+static const unsigned NUM_ROBOTS = 4;
 
 uint16_t customServiceUUID  = 0xA000;
 uint16_t readwriteCharUUID  = 0xA001;
-static const uint16_t secure_code[] = {0xABCD};
-
-uint8_t info[NUM_ROBOTS]= {0};
 
-ReadWriteArrayGattCharacteristic<uint8_t, NUM_ROBOTS> readwriteChar(readwriteCharUUID, info, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE);
+uint8_t info[NUM_ROBOTS]= {0,0,0,1};
 
-//GattCharacteristic  readwriteChar (readwriteCharUUID, info, NUM_ROBOTS, NUM_ROBOTS, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE);
+ReadWriteArrayGattCharacteristic<uint8_t, NUM_ROBOTS> readwriteChar(readwriteCharUUID, info);
 
 GattCharacteristic *characteristics[] = {&readwriteChar};
 
 GattService        customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
 
-bool triggerLedCharacteristic = false;
 DiscoveredCharacteristic characteristics_discovered;
 
-int8_t temp;
-uint8_t pt_addr = 0;
-uint8_t list_addr[NUM_ROBOTS][ADDR_LEN+NUM_ROBOTS]= {0,};
-int8_t list_rssi[NUM_ROBOTS];
-uint8_t initial_list_addr[NUM_ROBOTS][ADDR_LEN + 1]= {0,};
-// 1st Device, 2nd device has '10(2)' information
+uint8_t rgb=1;
 uint8_t info_t[NUM_ROBOTS]= {0,};
-uint8_t point;
 
 void RGB_Show(uint8_t r, uint8_t g, uint8_t b)
 {
@@ -90,252 +81,81 @@
 void periodicCallback(void)
 {
     led=!led;
-    point = 0;
-    for(int i=0; i<NUM_ROBOTS; i++) {
-        point+=info[i];
-    }
-    if(point==0) {
-        RGB_Show(0x00,0x00,0xff);
-    } else if(point==1) {
-        RGB_Show(0xff,0xff,0xff);
-    } else if(point==2) {
-        RGB_Show(0x00,0xff,0x00);
-    } else if(point==3) {
-        RGB_Show(0x00,0xff,0xff);
-    } else if(point==4) {
-        RGB_Show(0xff,0x00,0x00);
-    } else if(point==5) {
-        RGB_Show(0xff,0x00,0xff);
-    } else if(point==6) {
-        RGB_Show(0xff,0xff,0x00);
-    } else if(point==7) {
-        RGB_Show(0xff,0xff,0xff);
-    } else if(point==8) {
-        RGB_Show(0x00,0x00,0xff);
-    }
-}
-
-void initialSetup(const Gap::AdvertisementCallbackParams_t *params)
-{
-    if ( params->advertisingData[1] == 0xAF ) {
-        for(int i = 0; i < NUM_ROBOTS; i++) {
-            if(initial_list_addr[i][5] == params->peerAddr[5]
-                    && initial_list_addr[i][4] == params->peerAddr[4]
-                    && initial_list_addr[i][3] == params->peerAddr[3]
-                    && initial_list_addr[i][2] == params->peerAddr[2]
-                    && initial_list_addr[i][1] == params->peerAddr[1]
-                    && initial_list_addr[i][0] == params->peerAddr[0]) {
-                break;
-            }
-            if(initial_list_addr[i][5] == 0  && initial_list_addr[i][4] == 0
-                    && initial_list_addr[i][3] == 0  && initial_list_addr[i][2] == 0
-                    && initial_list_addr[i][1] == 0  && initial_list_addr[i][0] == 0) {
-                initial_list_addr[i][5] = params->peerAddr[5];
-                initial_list_addr[i][4] = params->peerAddr[4];
-                initial_list_addr[i][3] = params->peerAddr[3];
-                initial_list_addr[i][2] = params->peerAddr[2];
-                initial_list_addr[i][1] = params->peerAddr[1];
-                initial_list_addr[i][0] = params->peerAddr[0];
-
-                initial_list_addr[i][6] = 1;
-                break;
-            }
-        }
-        printf("initialsetup\r\n");
-        printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u, %0x %0x\r\n",
-               params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],
-               params->rssi, params->isScanResponse, params->type, params->advertisingData[1], params->advertisingData[0]);
-    }
 }
 
 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params)
 {
-    if ( params->rssi > -50 && params->advertisingData[1] == 0xAF ) {
-        printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u, %0x %0x %0x\r\n",
-               params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],
-               params->rssi, params->isScanResponse, params->type, params->advertisingData[3], params->advertisingData[2], params->advertisingData[1]);
-
-        ble.gap().connect(params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL);
-    }
-    else{
-        printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u, %0x %0x %0x\r\n",
-               params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],
-               params->rssi, params->isScanResponse, params->type, params->advertisingData[3], params->advertisingData[2], params->advertisingData[1]);
-    }
-    /*
-    int i;
-
-    for(i = 0; i < NUM_ROBOTS; i++) {
-        if(list_addr[i][5] == params->peerAddr[5] && list_addr[i][4] == params->peerAddr[4] && list_addr[i][3] == params->peerAddr[3]
-                && list_addr[i][2] == params->peerAddr[2] && list_addr[i][1] == params->peerAddr[1] && list_addr[i][0] == params->peerAddr[0]) {
-            list_rssi[i] = params->rssi;
-            break;
-        }
-        if(list_addr[i][5] == 0 && list_addr[i][4] == 0 && list_addr[i][3] == 0 && list_addr[i][2] == 0 && list_addr[i][1] == 0 && list_addr[i][0] == 0) {
-            list_addr[i][5] = params->peerAddr[5];
-            list_addr[i][4] = params->peerAddr[4];
-            list_addr[i][3] = params->peerAddr[3];
-            list_addr[i][2] = params->peerAddr[2];
-            list_addr[i][1] = params->peerAddr[1];
-            list_addr[i][0] = params->peerAddr[0];
-
-            list_addr[i][6] = params->advertisingData[0];
-
-            list_rssi[i] = params->rssi;
-
-            if ( i==NUM_ROBOTS-1 ) {
-                for(unsigned j=0; j<NUM_ROBOTS; j++) {
-                    for(unsigned k=j ; k<NUM_ROBOTS ; k++) {
-                        if(list_rssi[j] < list_rssi[k]) {
-                            temp = list_rssi[j];
-                            list_rssi[j] = list_rssi[k];
-                            list_rssi[k] = temp;
-                            for(unsigned l = 0; l < 6 ; l++) {
-                                temp = list_addr[j][l];
-                                list_addr[j][l] = list_addr[k][l];
-                                list_addr[k][l] = temp;
-                            }
-                        }
-                    }
-                }
-                break;
-            }
-        }
-
-        if( i == NUM_ROBOTS-1 ) {
-            if( i == 2 ) {
-                ble.gap().stopScan();
-                ble.gap().connect(list_addr[0], Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL);
-            }
-        }
-    }
-    */
-}
-
-void serviceDiscoveryCallback(const DiscoveredService *service)
-{
-}
-
-void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP)
-{
-    if (characteristicP->getUUID() == 0xA001) { /* !ALERT! Alter this filter to suit your device. */
-        characteristics_discovered        = *characteristicP;
-        printf("characteristic discovered\r\t\n");
-        characteristics_discovered.read();
-    }
+    
 }
 
 void connectionCallback(const Gap::ConnectionCallbackParams_t *params)
 {
-    printf("connected\r\n");
+    printf("adv peerAddr[%02x %02x %02x %02x %02x %02x]\r\n",
+           params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0]);
     ble.gap().stopAdvertising();
-    ble.gap().stopScan();
-    if (params->role == Gap::CENTRAL) {
-        printf("CENTRAL\r\n");
-        ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, 0xA000, 0xA001);
-    } else {
-        printf("PERIPHERAL\r\n");
-    }
-}
-
-void triggerToggledWrite(const GattReadCallbackParams *response)
-{
-    if (response->handle == characteristics_discovered.getValueHandle()) {
-        for(int i=0; i < NUM_ROBOTS; i++) {
-            info_t[i] = info[i] || response->data[i];
-            info[i] = info_t[i];
-        }
-        characteristics_discovered.write(NUM_ROBOTS, info);
-    }
-}
-
-void onDataWriteCallback(const GattWriteCallbackParams *params)
-{
-    characteristics_discovered.read();
 }
 
 void onDataWrittenCallback(const GattWriteCallbackParams *params)
 {
-    for(int i=0; i<NUM_ROBOTS; i++) {
-        info[i] = params->data[i];
-    }
-    printf("data is written well\r\n");
-    
     ble.gap().disconnect(params->connHandle,Gap::REMOTE_USER_TERMINATED_CONNECTION);
 }
 
 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
 {
-    printf("disconnected\r\n");
-    
-    for(int i=0; i<NUM_ROBOTS; i++) {
-        printf("       %u", info[i]);
-    }
-    printf("\r\n");
-    
-    ble.gap().startAdvertising();
-    ble.gap().startScan(advertisementCallback);
-}
-
-
-int main(void)
-{
-    Ticker ticker;
-    ticker.attach(periodicCallback, 0.5);
-
-    int num = 0;
-
-    ble.init();
-    ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED);
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
-    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::RABOT_REDBEAR_BLE_NANO, (uint8_t *)secure_code, sizeof(secure_code));
-    ble.setAdvertisingType(GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED);
-    ble.gap().setAdvertisingInterval(32);
-    ble.gap().startAdvertising();
-    
-    ble.gap().setScanParams(1014 , 884);
-    ble.gap().startScan(initialSetup);
-    
-    wait(5);
-    
-    ble.gap().stopAdvertising();
-    ble.gap().stopScan();
-    
-    ble.gap().clearAdvertisingPayload();
-        
-    for(int i = 0; i< NUM_ROBOTS; i++) {
-        num += initial_list_addr[i][6];
-    }
-    info[num] = 1;
-
-    printf("initial setting is done\r\n");
-
-    printf("initial info : ");
+    printf("Current info : ");
     for(int i=0; i<NUM_ROBOTS; i++) {
         printf("%u \t",info[i]);
     }
     printf("\r\n");
     
+    wait(10);
+    
+    if(rgb==7) {
+        rgb = 1;
+    }else {
+        rgb++;
+    }
+    
+    info[0] = rgb%2;
+    info[1] = (rgb>>1)%2;
+    info[2] = (rgb>>2)%2;
+    info[3] = 1;
+    
+    ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::RABOT_REDBEAR_BLE_NANO, (uint8_t *)info, sizeof(info));
+    ble.gap().setAdvertisingInterval(59);
+    ble.gap().startAdvertising();
+}
 
-    //advertising for real experiment
-    printf("communication for connection is started\r\n");
-
+int main(void)
+{   
+    Ticker ticker;
+    ticker.attach(periodicCallback, 1);
+    
+    info[0] = rgb%2;
+    info[1] = (rgb>>1)%2;
+    info[2] = (rgb>>2)%2;
+    
+    printf("Source node \r\n");
+    printf("Current info : ");
+    for(int i=0; i<NUM_ROBOTS; i++) {
+        printf("%u \t",info[i]);
+    }
+    printf("\r\n");
+    
+    ble.init();
     ble.gap().onConnection(connectionCallback);
     ble.gap().onDisconnection(disconnectionCallback);
-
-    ble.gattClient().onDataRead(triggerToggledWrite);
-    ble.gattClient().onDataWrite(onDataWriteCallback);
     ble.gattServer().onDataWritten(onDataWrittenCallback);
     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
-    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::RABOT_REDBEAR_BLE_NANO, (uint8_t *)secure_code, sizeof(secure_code));
-    ble.gap().setAdvertisingInterval(29);
-
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::RABOT_REDBEAR_BLE_NANO, (uint8_t *)info, sizeof(info));
+    ble.gap().setAdvertisingInterval(59);
+    ble.gap().startAdvertising();
+    
     ble.addService(customService);
-    ble.gap().startAdvertising();
-    ble.gap().setScanParams(190 /* scan interval */, 181 /* scan window */);
-    ble.gap().startScan(advertisementCallback);
-
+ 
     while (true) {
         ble.waitForEvent();
+        RGB_Show(info[0],info[1],info[2]);
     }
-}
\ No newline at end of file
+}