Demo to get the die temperature value of 51822

Dependencies:   BLE_API TMP_nrf51 mbed nRF51822

use the observer project to view the temperature value: https://developer.mbed.org/users/sunsmile2015/code/BLE_Observer/

Files at this revision

API Documentation at this revision

Comitter:
sunsmile2015
Date:
Fri Jul 31 09:11:43 2015 +0000
Parent:
3:3eda308b78e6
Commit message:
use update adv payload API to change temperature value

Changed in this revision

BLE_API.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
nRF51822.lib Show annotated file Show diff for this revision Revisions of this file
--- a/BLE_API.lib	Mon Jul 20 08:58:21 2015 +0000
+++ b/BLE_API.lib	Fri Jul 31 09:11:43 2015 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/#9c2edf20ea56
+https://developer.mbed.org/users/sunsmile2015/code/BLE_API/#ce45c506a779
--- a/main.cpp	Mon Jul 20 08:58:21 2015 +0000
+++ b/main.cpp	Fri Jul 31 09:11:43 2015 +0000
@@ -17,51 +17,48 @@
 
 #include "mbed.h"
 #include "ble/BLE.h"
-#include "ble/DiscoveredCharacteristic.h"
-#include "ble/DiscoveredService.h"
 #include "TMP_nrf51/TMP_nrf51.h"
 
-#define COMP_ID_TEST 0xFEFE
+#define APP_SPECIFIC_ID_TEST 0xFEFE
 
 #pragma pack(1)
-typedef struct manufacturerData {
-    uint16_t companyId;
-    /* User defined manufacture data */
-    TMP_nrf51::tmpSensorValue_t tmpSensorValue;
-} manufacturerData_t;
+struct ApplicationData_t {
+    uint16_t applicationSpecificId;             /* An ID used to identify temperature value
+                                                   in the manufacture specific AD data field */
+    TMP_nrf51::tmpSensorValue_t tmpSensorValue; /* User defined application data */
+};
 #pragma pack()
 
 BLE ble;
 TMP_nrf51 tempSensor;
 DigitalOut alivenessLED(LED1, 1);
-static bool triggerTempValueRead = true;
+static bool triggerTempValueUpdate = false;
 
 void periodicCallback(void)
 {
     /* Do blinky on LED1 while we're waiting for BLE events */
     alivenessLED = !alivenessLED;
-    triggerTempValueRead = true;
+    triggerTempValueUpdate = true;
+}
+
+void accumulateApplicationData(ApplicationData_t &appData)
+{
+    appData.applicationSpecificId = APP_SPECIFIC_ID_TEST;
+    /* Read a new temperature value */
+    appData.tmpSensorValue = tempSensor.get();
 }
 
 void temperatureValueAdvertising(void)
 {
-    manufacturerData_t manuData;
-    manuData.companyId = COMP_ID_TEST;
+    ApplicationData_t appData;
     
-    /* Read a new temperature value */
-    manuData.tmpSensorValue = tempSensor.get();
-    printf("Temp is %f\r\n", (float)manuData.tmpSensorValue);
-    
-    /* Stop advertising and clear the payload if in advertising state */
-    if((ble.gap().getState()).advertising == 1) {
-        ble.gap().stopAdvertising();
-        ble.gap().clearAdvertisingPayload();
-    }
+    accumulateApplicationData(appData);
+    //printf("Temp is %f\r\n", (float)appData.tmpSensorValue);
     
     /* Setup advertising payload */
     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); /* Set flag */
     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_THERMOMETER); /* Set appearance */
-    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&manuData, sizeof(manufacturerData_t)); /* Set data */
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&appData, sizeof(ApplicationData_t)); /* Set data */
     /* Setup advertising parameters */
     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);
     ble.gap().setAdvertisingInterval(500);
@@ -69,18 +66,35 @@
     ble.gap().startAdvertising();
 }
 
+void updateSensorValueInAdvPayload(void)
+{
+    ApplicationData_t appData;
+    
+    accumulateApplicationData(appData);
+    
+    /* Stop advertising first */
+    ble.gap().stopAdvertising();
+    /* Only update temperature value field */
+    ble.gap().updateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&appData, sizeof(ApplicationData_t));
+    /* Start advertising again */
+    ble.gap().startAdvertising();
+}
+
 int main(void)
 {
     Ticker ticker;
-    /* Refresh temperature value every 2 seconds */
+    /* Enable trigger every 2 seconds */
     ticker.attach(periodicCallback, 2);
 
     ble.init();
-
+    /* Start temperature advertising */
+    temperatureValueAdvertising();
+    
     while (true) {
-        if (triggerTempValueRead) {
-            temperatureValueAdvertising();
-            triggerTempValueRead = false;
+        if (triggerTempValueUpdate) {
+            /* Update temperature value */
+            updateSensorValueInAdvPayload();
+            triggerTempValueUpdate = false;
         }
         ble.waitForEvent();
     }
--- a/nRF51822.lib	Mon Jul 20 08:58:21 2015 +0000
+++ b/nRF51822.lib	Fri Jul 31 09:11:43 2015 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/teams/Nordic-Semiconductor/code/nRF51822/#6c82f06746bb
+http://mbed.org/teams/Nordic-Semiconductor/code/nRF51822/#c7adea3c1e37