test 4

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_EvothingsExample_GATT by Austin Blackstone

Files at this revision

API Documentation at this revision

Comitter:
peteratsl
Date:
Fri Nov 13 03:33:39 2015 +0000
Parent:
18:8454c63488ee
Commit message:
test 4

Changed in this revision

AltBeaconService.h 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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AltBeaconService.h	Fri Nov 13 03:33:39 2015 +0000
@@ -0,0 +1,86 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2006-2015 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef __BLE_ALTBEACON_SERVICE_H__
+#define __BLE_ALTBEACON_SERVICE_H__
+
+#include "BLEDevice.h"
+
+/**
+* @class AltBeaconService
+* @brief AltBeacon Service. This service sets up a device to broadcast advertising packets to mimic an AltBeacon<br>
+*/
+
+class AltBeaconService
+{
+public:
+    /**
+    * @param[ref] _ble
+    *               BLEDevice object for the underlying controller.
+    * @param[in] mfgID
+    *               The beacon device manufacturer's company identifier code. 
+    *               Usually this will coorespond to the companies BLE SIG assigned number.
+    * @param[in] beaconID
+    *               A 20-byte value uniquely identifying the beacon. 
+    *               The big endian representation of the beacon identifier. 
+    *               For interoperability purposes, the first 16+ bytes of the beacon 
+    *               identifier should be unique to the advertiser's organizational unit. 
+    *               Any remaining bytes of the beacon identifier may be subdivided as needed for the use case.
+    * @param[in] refRSSI
+    *               The RSSI of the beacon (as signed value from 0 to -127) as measured 1 meter from the device. Used for micro-location.
+    * @param[in] mfgReserved
+    *               Used for special manufacturer data. Defaults to 0x00 if not specified.
+    */
+    AltBeaconService(BLEDevice &_ble, uint16_t mfgID, uint8_t beaconID[20], int8_t refRSSI, uint8_t mfgReserved = 0x00):
+        ble(_ble)
+    {
+        data.mfgID = ((mfgID<<8) | (mfgID >>8));
+        if(refRSSI > 0){refRSSI = 0;} // refRSSI can only be 0 to -127, smash everything above 0 to zero
+        data.refRSSI = refRSSI;
+        data.beaconCode = 0xACBE;
+        data.mfgReserved = mfgReserved;
+
+        // copy across beacon ID
+        for(int x=0; x<sizeof(data.beaconID); x++) {
+            data.beaconID[x] = beaconID[x];
+        }
+
+        // Set up alt beacon
+        ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE );
+        // Generate the 0x1BFF part of the Alt Prefix
+        ble.accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, data.raw, sizeof(data.raw));
+
+        // Set advertising type
+        ble.setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);
+    }
+
+public:
+    union {
+        uint8_t raw[26]; // AltBeacon advertisment data
+        struct {
+            uint16_t mfgID;         // little endian representation of manufacturer ID
+            uint16_t beaconCode;    // Big Endian representation of 0xBEAC
+            uint8_t  beaconID[20];  // 20byte beacon ID, usually 16byte UUID w/ remainder used as necessary
+            int8_t   refRSSI;       // 1 byte signed data, 0 to -127
+            uint8_t  mfgReserved;   // reserved for use by manufacturer to implement special features
+        };
+    } data;
+
+private:
+    BLEDevice &ble;
+
+};
+
+#endif //__BLE_ALTBEACON_SERVICE_H__
--- a/main.cpp	Mon Nov 09 14:34:14 2015 +0000
+++ b/main.cpp	Fri Nov 13 03:33:39 2015 +0000
@@ -1,8 +1,30 @@
 #include "mbed.h"
 #include "BLEDevice.h"
+#include "AltBeaconService.h"
+#include "iBeaconService.h"
+
 
 BLEDevice ble;
 DigitalOut led(LED1);
+
+/*..........................................................................
+uint8_t beaconID[] =    {   0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4,
+                            0xA1, 0x2F, 0x17, 0xD1, 0xAD, 0x07, 0xA9, 0x61,
+                            0x11, 0x22, 0x33, 0x44
+                        };
+uint16_t manufacturerID = 0x5900; //Nordic SIG ID
+int8_t rssi = -122;
+..........................................................................*/
+
+
+uint8_t beaconID[] =    {   0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4,
+                            0xA1, 0x2F, 0x17, 0xD1, 0xAD, 0x07, 0xA9, 0x61
+                        };
+uint16_t majorNumber = 1122;
+uint16_t minorNumber = 3344;
+uint16_t txPower = 0xC8;
+
+
 uint16_t customServiceUUID  = 0xA000;
 uint16_t readCharUUID       = 0xA001;
 uint16_t writeCharUUID      = 0xA002;
@@ -11,23 +33,32 @@
 
 
 
-uint8_t b1_id               = 0x01;
-uint8_t b2_id               = 0x02;
-uint8_t b3_id               = 0x03;
-uint8_t b4_id               = 0x04;
-uint8_t b5_id               = 0x05;
-uint8_t b6_id               = 0x06;
-uint8_t b7_id               = 0x07;
+
+const uint8_t b1_id   = 0x01;
+const uint8_t b2_id   = 0x02;
+const uint8_t b3_id   = 0x03;
+const uint8_t b4_id   = 0x04;
+const uint8_t b5_id   = 0x05;
+const uint8_t b6_id   = 0x06;
+const uint8_t b7_id   = 0x07;
+const uint8_t b8_id   = 0x08;
 
-PinName flood_p              = P0_4;
+const uint8_t bA1_id  = 0x31;
+const uint8_t bA2_id  = 0x32;
+const uint8_t bA3_id  = 0x33;
+const uint8_t bA4_id  = 0x34;
+const uint8_t bA5_id  = 0x35;
+const uint8_t bA6_id  = 0x36;
+const uint8_t bA7_id  = 0x37;
 
-PinName b1_p             = P0_28;
-PinName b2_p             = P0_29;
-PinName b3_p             = P0_15;
-PinName b4_p             = P0_10; //CTX
-PinName b5_p             = P0_9;  //TXD
-PinName b6_p             = P0_11; //RXD
-PinName b7_p             = P0_8;  //RTS
+PinName flood_p     = P0_4;
+PinName b1_p        = P0_28;
+PinName b2_p        = P0_29;
+PinName b3_p        = P0_15;
+PinName b4_p        = P0_10; //CTX
+PinName b5_p        = P0_9;  //TXD
+PinName b6_p        = P0_11; //RXD
+PinName b7_p        = P0_8;  //RTS
 
 
 /*
@@ -41,31 +72,98 @@
 const static char     DEVICE_NAME[]        = "PETE2"; // change this
 static const uint16_t uuid16_list[]        = {0xFF02}; //Custom UUID, FFFF is reserved for development
 
-// Set Up custom Characteristics
+
+//setup custom characteristics
+
+//read
 static uint8_t readValue[10] = {0};
 ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(readValue)> readChar(readCharUUID, readValue);
-
+//write
 static uint8_t writeValue[10] = {0};
 WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(writeValue)> writeChar(writeCharUUID, writeValue);
 
+
 // Set up custom service
-GattCharacteristic *characteristics[] = {&readChar, &writeChar};
-GattService        customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
+GattCharacteristic  *characteristics[] = {&readChar, &writeChar};
+GattService         customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
 
 //setup led
 DigitalOut led1(P0_19);
 
-
+//setup digital outs
 DigitalOut b1(b1_p);
 DigitalOut b2(b2_p);
 DigitalOut b3(b3_p);
 DigitalOut b4(b4_p);
 DigitalOut b5(b5_p);
-DigitalOut b6(b6_p);
+//DigitalOut b6(b6_p);
 DigitalOut b7(b7_p);
 
+DigitalIn enable(flood_p);
 
-DigitalIn enable(flood_p);
+
+void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason);
+void writeCharCallback(const GattCharacteristicWriteCBParams *params);
+void testAll(void);
+void blinky(void);
+int flip(char value);
+
+
+//main loop............................................................................................................
+int main(void)
+{
+    /* initialize stuff */
+    led1 = 1;
+    
+    b1 = 1;//b1_p = P0_28;
+    b2 = 1;//b2_p = P0_29;
+    b3 = 1;//b3_p = P0_9;
+    b4 = 1;//b4_p = P0_10; //CTX
+    b5 = 1;//b5_p = P0_9;  //TXD
+    //b6 = 1;//b6_p = P0_11; //RXD
+    b7 = 1;//b7_p = P0_8;  //RTS
+              
+    
+
+    
+    ble.init();
+    
+    //setup beacon service
+    //AltBeaconService altbeacon(ble, manufacturerID, beaconID, rssi);
+    iBeaconService ibeacon(ble, beaconID, majorNumber, minorNumber, txPower);
+    
+    
+    ble.onDisconnection(disconnectionCallback);
+    ble.onDataWritten(writeCharCallback);
+
+
+    /* setup advertising */
+    
+    // BLE only, no classic BT
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
+    // advertising type
+    ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+    // add name
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
+    // UUID's broadcast in advertising packet
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
+    
+    
+    // set interval
+    ble.setAdvertisingInterval(100); // 100ms. 
+
+    // add our custom service
+    ble.addService(customService);
+
+    // start advertising
+    ble.startAdvertising(); 
+    
+    // infinite loop waiting for BLE interrupt events
+    while (true) {
+        ble.waitForEvent(); //Save power
+    }
+}
+//.......................end main............................................................................................
 
 /*
  *  Restart advertising when phone app disconnects
@@ -83,99 +181,76 @@
     // check to see what characteristic was written, by handle
     if(params->charHandle == writeChar.getValueHandle()) 
     {
-        // toggle LED if...
-        if(params->data[0] == b1_id)
-        {
-            b1 = !b1;
-            //led1 = 0;
-        }
-        else if(params->data[0] == b2_id)
-        {
-            b2 = !b2;
-            //led1 = 0;
-        }
-        else if(params->data[0] == b3_id)
-        {
-            b3 = !b3;
-            //led1 = 0;
-        }
-        if(params->data[0] == b4_id)
-        {
-            b4 = !b4;
-        }
-        else if(params->data[0] == b5_id)
-        {
-            b5 = !b5;
-            //led1 = 0;
-        }
-        else if(params->data[0] == b6_id)
-        {
-            b6 = !b6;
-            //led1 = 0;
-        }
-        else if(params->data[0] == b7_id)
-        {
-            b7 = !b7;
-            led1 = 0;
-            wait(3);
-            led1 = 1;
-        }
-        // print the data if more than 1 byte is written
-        else 
-        {
-            
-            printf("\n\r Data received: length = %d, data = 0x",params->len); 
-            for(int x=0; x < params->len; x++) {
-                printf("%x",params->data[x]);
-            }
-            
-        }
+        //testAll();
+        flip(params->data[0]);
+
         // update the readChar with the value of writeChar
         ble.updateCharacteristicValue(readChar.getValueHandle(),params->data,params->len);
     }
 }
 
-/*
- *  main loop
-*/ 
-int
-main(void)
+void testAll(void)
 {
-    /* initialize stuff */
-    printf("\n\r********* Starting Main Loop *********\n\r");
-    led1 = 1;
-    
-    b1 = 1;//b1_p = P0_28;
-    b2 = 1;//b2_p = P0_29;
-    b3 = 1;//b3_p = P0_9;
-    b4 = 1;//b4_p = P0_10; //CTX
-    b5 = 1;//b5_p = P0_9;  //TXD
-    b6 = 1;//b6_p = P0_11; //RXD
-    b7 = 1;//b7_p = P0_8;  //RTS
-              
-    
-
-    
-    ble.init();
-    ble.onDisconnection(disconnectionCallback);
-    
-    ble.onDataWritten(writeCharCallback);
-
-    /* setup advertising */
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); // BLE only, no classic BT
-    ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); // advertising type
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); // add name
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); // UUID's broadcast in advertising packet
-    ble.setAdvertisingInterval(100); // 100ms. 
-
-    // add our custom service
-    ble.addService(customService);
-
-    // start advertising
-    ble.startAdvertising(); 
-    
-    // infinite loop waiting for BLE interrupt events
-    while (true) {
-        ble.waitForEvent(); //Save power
+    for(int i=0; i<4; i++)    
+    {
+        led1 = !led1;
+        b1 = !b1;
+        b2 = !b2;
+        b3 = !b3;
+        b4 = !b4;
+        b5 = !b5;
+        //b6 = !b6;
+        b7 = !b7;
+        wait(1);
+    }
+}
+void blinky(void)
+{
+    for(int i=0; i<20; i++)    
+    {
+        led1 = !led1;
+        wait(.3);
     }
 }
+int flip(char value)
+{
+    int ret = 0;
+    
+    switch(value)
+    {
+        case b1_id  :
+        case bA1_id :       led1 = !led1;
+                            b1 = !b1;
+                            break;
+                            
+        case b2_id  :
+        case bA2_id :       led1 = !led1;
+                            b2 = !b2;
+                            break;
+        
+        case b3_id  :
+        case bA3_id :       led1 = !led1;
+                            b3 = !b3;
+                            break;
+        
+        case b4_id  :
+        case bA4_id :       led1 = !led1;
+                            b4 = !b4;
+                            break;
+        
+        case b5_id  :
+        case bA5_id :       led1 = !led1;
+                            b5 = !b5;
+                            break;
+        
+        case b7_id  :
+        case bA7_id :       led1 = !led1;
+                            b7 = !b7;
+                            break;
+        
+        default :           blinky();
+                            ret = 1;
+                            break;
+    }
+    return ret;
+}
\ No newline at end of file