aaaa

Dependencies:   mbed

Fork of mbed-os-example-ble-EddystoneObserver by mbed-os-examples

Files at this revision

API Documentation at this revision

Comitter:
fbdp1202
Date:
Thu Jun 29 06:17:05 2017 +0000
Parent:
33:d83bd71e5d82
Child:
35:9a1347f1960c
Commit message:
Heart Rate scanner

Changed in this revision

mbed.lib Show annotated file Show diff for this revision Revisions of this file
mbed_app.json Show annotated file Show diff for this revision Revisions of this file
shields/TARGET_ST_BLUENRG.lib Show diff for this revision Revisions of this file
source/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/mbed.lib	Thu Jun 29 06:17:05 2017 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/screamer/code/mbed/#aff670d0d510
--- a/mbed_app.json	Mon Jun 12 04:49:37 2017 +0000
+++ b/mbed_app.json	Thu Jun 29 06:17:05 2017 +0000
@@ -10,4 +10,4 @@
             "target.extra_labels_add": ["ST_BLUENRG"]
         }
     }
-}
+}
\ No newline at end of file
--- a/shields/TARGET_ST_BLUENRG.lib	Mon Jun 12 04:49:37 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://github.com/ARMmbed/ble-x-nucleo-idb0xa1/#6670a4495aafe1601a105ec9f6606f70b4c3424c
--- a/source/main.cpp	Mon Jun 12 04:49:37 2017 +0000
+++ b/source/main.cpp	Thu Jun 29 06:17:05 2017 +0000
@@ -1,19 +1,3 @@
-/* mbed Microcontroller Library
- * Copyright (c) 2006-2013 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.
- */
-
 #include <events/mbed_events.h>
 #include "mbed.h"
 #include "ble/BLE.h"
@@ -21,167 +5,34 @@
 #include "ble/DiscoveredService.h" 
 #include "ble/services/HeartRateService.h" 
 
-
-//static const int URI_MAX_LENGTH = 18;             // Maximum size of service data in ADV packets
-
-static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE);
-static const char PEER_NAME[] = "HRM"; 
+DigitalOut led1(LED1, 1); 
+//I2C i2c(p27, p26); // sda, scl
+Serial uart(p13, p14);  // tx, rx
 static DiscoveredCharacteristic ledCharacteristic; 
 static bool triggerLedCharacteristic; 
-
-DigitalOut led1(LED1, 1);
-
-ReadOnlyGattCharacteristic<uint32_t> readFrom(uuid, valuePtr,
-    GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
-
-void periodicCallback(void)
-{
-    led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
-}
+static const char PEER_NAME[] = "HRM"; 
+//const int address = 0x3C<<1;
 
-//void decodeURI(const uint8_t* uriData, const size_t uriLen)
-//{
-//    const char *prefixes[] = {
-//        "http://www.",
-//        "https://www.",
-//        "http://",
-//        "https://",
-//        "urn:uuid:"
-//    };
-//    const size_t NUM_PREFIXES = sizeof(prefixes) / sizeof(char *);
-//    const char *suffixes[] = {
-//        ".com/",
-//        ".org/",
-//        ".edu/",
-//        ".net/",
-//        ".info/",
-//        ".biz/",
-//        ".gov/",
-//        ".com",
-//        ".org",
-//        ".edu",
-//        ".net",
-//        ".info",
-//        ".biz",
-//        ".gov"
-//    };
-//    const size_t NUM_SUFFIXES = sizeof(suffixes) / sizeof(char *);
-//
-//    size_t index = 0;
-//
-//    /* First byte is the URL Scheme. */
-//    if (uriData[index] < NUM_PREFIXES) {
-//        printf("%s", prefixes[uriData[index]]);
-//        index++;
-//    } else {
-//        printf("URL Scheme was not encoded!");
-//        return;
-//    }
-//
-//    /* From second byte onwards we can have a character or a suffix */
-//    while(index < uriLen) {
-//        if (uriData[index] < NUM_SUFFIXES) {
-//            printf("%s", suffixes[uriData[index]]);
-//        } else {
-//            printf("%c", uriData[index]);
-//        }
-//        index++;
-//    }
-//
-//    printf("\n\r");
-//}
+static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE); 
+char HeartRate_Data = 0;
 
-void serviceDiscoveryCallback(const DiscoveredService *service) { 
-    if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) { 
-        printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle()); 
-    } else { 
-        printf("S UUID-"); 
-        const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID(); 
-        for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) { 
-            printf("%02x", longUUIDBytes[i]); 
-        } 
-        printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle()); 
-    } 
+void periodicCallback(void) { 
+    led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ 
 } 
 
-void updateLedCharacteristic(void) { 
-    if (!BLE::Instance().gattClient().isServiceDiscoveryActive()) { 
-        ledCharacteristic.read(); 
-    } 
-} 
 
-void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) { 
-    printf("  C UUID-%x valueAttr[%u] props[%x]\r\n", characteristicP->getUUID().getShortUUID(), characteristicP->getValueHandle(), (uint8_t)characteristicP->getProperties().broadcast()); 
-    if (characteristicP->getUUID().getShortUUID() == 0xa001) { /* !ALERT! Alter this filter to suit your device. */ 
-        ledCharacteristic        = *characteristicP; 
-        triggerLedCharacteristic = true; 
-    } 
-} 
-
- void triggerToggledWrite(const GattReadCallbackParams *response) { 
-    printf("triggerToggledWrite\n");
-     if (response->handle == ledCharacteristic.getValueHandle()) { 
-         printf("triggerToggledWrite: handle %u, offset %u, len %u\r\n", response->handle, response->offset, response->len); 
-         for (unsigned index = 0; index < response->len; index++) { 
-             printf("%c[%02x]", response->data[index], response->data[index]); 
-         } 
-         printf("\r\n"); 
- 
-
-         uint8_t toggledValue = response->data[0] ^ 0x1; 
-         ledCharacteristic.write(1, &toggledValue); 
-     } 
- } 
- 
-
- void triggerRead(const GattWriteCallbackParams *response) { 
-    printf("triggerRead\n");
-     if (response->handle == ledCharacteristic.getValueHandle()) { 
-         ledCharacteristic.read(); 
-     } 
- } 
- 
-/*
- * This function is called every time we scan an advertisement.
- */
-void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params)
-{
-//    printf( 
-//        "adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\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 
-//    ); 
-//    struct AdvertisingData_t {
-//        uint8_t                        length; /* doesn't include itself */
-//        GapAdvertisingData::DataType_t dataType;
-//        uint8_t                        data[1];
-//    } AdvDataPacket;
-//
-//    struct ApplicationData_t {
-//        uint8_t applicationSpecificId[2];
-//        uint8_t frameType;
-//        uint8_t advPowerLevels;
-//        uint8_t uriData[URI_MAX_LENGTH];
-//    } AppDataPacket;
-//
-//    const uint8_t BEACON_UUID[sizeof(UUID::ShortUUIDBytes_t)] = {0xAA, 0xFE};
-//    const uint8_t FRAME_TYPE_URL                              = 0x10;
-//    const uint8_t APPLICATION_DATA_OFFSET                     = sizeof(ApplicationData_t) + sizeof(AdvDataPacket.dataType) - sizeof(AppDataPacket.uriData);
-//
-//    AdvertisingData_t *pAdvData;
-//    size_t index = 0;
-//    while(index < params->advertisingDataLen) {
-//        pAdvData = (AdvertisingData_t *)&params->advertisingData[index];
-//        if (pAdvData->dataType == GapAdvertisingData::SERVICE_DATA) {
-//            ApplicationData_t *pAppData = (ApplicationData_t *) pAdvData->data;
-//            if (!memcmp(pAppData->applicationSpecificId, BEACON_UUID, sizeof(BEACON_UUID)) && (pAppData->frameType == FRAME_TYPE_URL)) {
-//                decodeURI(pAppData->uriData, pAdvData->length - APPLICATION_DATA_OFFSET);
-//                break;
-//            }
-//        }
-//        index += (pAdvData->length + 1);
-//    }
-
+void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) { 
+    // parse the advertising payload, looking for data type COMPLETE_LOCAL_NAME 
+    // The advertising payload is a collection of key/value records where 
+    // byte 0: length of the record excluding this byte 
+    // byte 1: The key, it is the type of the data 
+    // byte [2..N] The value. N is equal to byte0 - 1 
+    printf("Hello:)\r\n");
+    printf(
+        "adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\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 
+    );
     for (uint8_t i = 0; i < params->advertisingDataLen; ++i) { 
 
         const uint8_t record_length = params->advertisingData[i]; 
@@ -205,8 +56,49 @@
             } 
         } 
         i += record_length; 
-    }    
-}
+    } 
+} 
+
+
+void serviceDiscoveryCallback(const DiscoveredService *service) { 
+    if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) { 
+        printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle()); 
+    } else { 
+        printf("S UUID-"); 
+        const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID(); 
+        for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) { 
+            printf("%02x", longUUIDBytes[i]); 
+        } 
+        printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle());
+    } 
+} 
+
+
+void updateLedCharacteristic(void) { 
+    if (!BLE::Instance().gattClient().isServiceDiscoveryActive()) { 
+        ledCharacteristic.read(); 
+    } 
+} 
+
+void triggerToggledWrite(const GattReadCallbackParams *response) { 
+    printf("triggerToggledWrite\r\n");
+    printf("triggerToggledWrite: handle %u, offset %u, len %u\r\n", response->handle, response->offset, response->len); 
+    HeartRate_Data = response->data[1];
+    printf("HeartRate_Data = %d\r\n", HeartRate_Data);
+    uart.putc(HeartRate_Data);
+    ledCharacteristic.read(); 
+} 
+
+
+void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) {
+    printf("  C UUID-%x valueAttr[%u] props[%x]\r\n", characteristicP->getUUID().getShortUUID(), characteristicP->getValueHandle(), (uint8_t)characteristicP->getProperties().broadcast()); 
+    if (characteristicP->getUUID().getShortUUID() == 0x2a37) { /* !ALERT! Alter this filter to suit your device. */ 
+        characteristicP->read(0);
+        ledCharacteristic        = *characteristicP; 
+        triggerLedCharacteristic = true; 
+    } 
+} 
+
 
 void discoveryTerminationCallback(Gap::Handle_t connectionHandle) { 
     printf("terminated SD for handle %u\r\n", connectionHandle); 
@@ -216,63 +108,97 @@
     } 
 } 
 
+
 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) { 
-    printf("connectionCallback\n");
-    printf("params->role: %01x\n", params->role);
-    if (params->role == Gap::CENTRAL) { 
-        printf("ROLE: CENTRAL\n");
-        BLE &ble = BLE::Instance(); 
-        ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback); 
-        ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, GattService::UUID_HEART_RATE_SERVICE, 0xa001); 
-    } 
-} 
+    printf("connectionCallback\r\n");
+     if (params->role == Gap::CENTRAL) {
+         printf("Gap::CENTRAL\r\n"); 
+         BLE &ble = BLE::Instance(); 
+         ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback);
+         ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback/*, 0xa000, 0xa001*/); 
+     }
+}  
+
+ void triggerRead(const GattWriteCallbackParams *response) { 
+    printf("triggerRead\r\n");
+     if (response->handle == ledCharacteristic.getValueHandle()) { 
+         ledCharacteristic.read(); 
+     } 
+ } 
+ 
 
-void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *) { 
-    printf("disconnected\r\n"); 
-    /* Start scanning and try to connect again */ 
-    BLE::Instance().gap().startScan(advertisementCallback); 
-} 
+ void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *) { 
+     printf("disconnected\r\n"); 
+     /* Start scanning and try to connect again */ 
+     BLE::Instance().gap().startScan(advertisementCallback); 
+ } 
+ 
+
+ void onBleInitError(BLE &ble, ble_error_t error) 
+ { 
+    /* Initialization error handling should go here */ 
+ } 
+ 
 
-void onBleInitError(BLE &ble, ble_error_t error)
-{
-   /* Initialization error handling should go here */
-}
+ void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) 
+ { 
+     BLE&        ble   = params->ble; 
+     ble_error_t error = params->error; 
+ 
+    printf("Init_1\r\n");
 
-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; 
+     } 
+ 
+    printf("Init_2\r\n");
 
-    if (error != BLE_ERROR_NONE) {
-        onBleInitError(ble, error);
-        return;
-    }
+     /* Ensure that it is the default instance of BLE */ 
+     if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { 
+         return; 
+     } 
+ 
+
+     ble.gap().onDisconnection(disconnectionCallback); 
+     ble.gap().onConnection(connectionCallback); 
+ 
 
-    if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
-        return;
-    }
+     ble.gattClient().onDataRead(triggerToggledWrite); 
+     ble.gattClient().onDataWrite(triggerRead); 
+ 
 
-    ble.gap().onDisconnection(disconnectionCallback); 
-    ble.gap().onConnection(connectionCallback); 
-
-    ble.gap().setScanParams(1800 /* scan interval */, 1500 /* scan window */);
-    ble.gap().startScan(advertisementCallback);
-}
+     // scan interval: 400ms and scan window: 400ms. 
+     // Every 400ms the device will scan for 400ms 
+     // This means that the device will scan continuously. 
+     ble.gap().setScanParams(400, 400); 
+     ble.gap().startScan(advertisementCallback); 
+ } 
+ 
 
-void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
-    BLE &ble = BLE::Instance();
-    eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
-}
-
-int main()
-{
-    eventQueue.call_every(500, periodicCallback);
+ void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { 
+     BLE &ble = BLE::Instance(); 
+     eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); 
+ } 
+ 
 
-    BLE &ble = BLE::Instance();
-    ble.onEventsToProcess(scheduleBleEventsProcessing);
-    ble.init(bleInitComplete);
+ int main() 
+{
+    uart.baud(115200);
+    printf("main_1\r\n");
+    triggerLedCharacteristic = false;
+    eventQueue.call_every(1000, periodicCallback); 
+ 
+    printf("main_2\r\n");
+    BLE &ble = BLE::Instance(); 
+    ble.onEventsToProcess(scheduleBleEventsProcessing); 
+    ble.init(bleInitComplete); 
+    printf("main_3\r\n");
 
-    eventQueue.dispatch_forever();
+    eventQueue.dispatch_forever(); 
 
-    return 0;
-}
+    printf("main_4\r\n"); 
+
+    return 0; 
+} 
\ No newline at end of file