hrm1017 with gy-801 temp

Dependencies:   BLE_API TMP102 mbed nRF51822

Fork of BLE_HTM_HRM1017 by Switch Science

Revision:
2:daf2344afc28
Parent:
0:5e4210d108ac
Child:
5:ab8889077be9
--- a/main.cpp	Fri Aug 22 11:32:14 2014 +0000
+++ b/main.cpp	Fri Sep 05 14:26:54 2014 +0000
@@ -14,6 +14,7 @@
 #endif /* #if NEED_CONSOLE_OUTPUT */
 
 const static char  DEVICE_NAME[] = "HRM1017_HTM";
+static volatile bool  triggerSensorPolling = false;
 
 BLEDevice  ble;
 TMP102      healthThemometer(p22, p20, 0x90);  /* The TMP102 connected to our board */
@@ -50,7 +51,7 @@
 
 static Gap::ConnectionParams_t connectionParams;
 
-void disconnectionCallback(Gap::Handle_t handle)
+void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)    // Mod
 {
     advertisingStateLed = 1;
     
@@ -59,7 +60,7 @@
     ble.startAdvertising();
 }
 
-void onConnectionCallback(Gap::Handle_t handle)
+void onConnectionCallback(Gap::Handle_t handle, const Gap::ConnectionParams_t *params)   //Mod
 {
     advertisingStateLed = 0;
 
@@ -71,6 +72,15 @@
     }
 }
 
+void periodicCallback(void)
+{
+    oneSecondLed = !oneSecondLed; /* Do blinky on LED1 while we're waiting for BLE events */
+
+    /* Note that the periodicCallback() executes in interrupt context, so it is safer to do
+     * heavy-weight sensor polling from the main thread. */
+    triggerSensorPolling = true;
+}
+
 /**************************************************************************/
 /*!
     @brief  Program entry point
@@ -81,9 +91,12 @@
     
     /* Setup blinky led */
     oneSecondLed = 1;
-    
-    DEBUG("Initialising the nRF51822\n\r");
+    Ticker ticker;
+    ticker.attach(periodicCallback, 1);
+       
+    DEBUG("Initialising the nRF51822\n");
     ble.init();
+    DEBUG("Init done\n");
     ble.onDisconnection(disconnectionCallback);
     ble.onConnection(onConnectionCallback);
 
@@ -98,16 +111,19 @@
     ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
     ble.startAdvertising();
     advertisingStateLed = 1;
+    DEBUG("Start Advertising\n");
 
     ble.addService(htmService);
     ble.addService(battService);
+    DEBUG("Add Service\n");
 
-    for (;;)
-    {
-        /* Now that we're live, update the battery level & temperature characteristics */
-        updateServiceValues();
-        wait(1);
-        ble.waitForEvent();
+    while (true) {
+        if (triggerSensorPolling) {
+            triggerSensorPolling = false;
+            updateServiceValues();
+        } else {
+            ble.waitForEvent();
+        }
     }
 }
 
@@ -118,9 +134,6 @@
 /**************************************************************************/
 void updateServiceValues(void)
 {
-      /* Toggle the one second LEDs */
-      oneSecondLed = !oneSecondLed;
-      
       /* Decrement the battery level. */
       batt <=50 ? batt=100 : batt--;
       
@@ -129,8 +142,8 @@
       DEBUG("temp:%f\n", temperature);
       uint32_t temp_ieee11073 = quick_ieee11073_from_float(temperature);
       memcpy(thermTempPayload+1, &temp_ieee11073, 4);
-      ble.updateCharacteristicValue(tempChar.getHandle(), thermTempPayload, sizeof(thermTempPayload));
-      ble.updateCharacteristicValue(battLevel.getHandle(), (uint8_t *)&batt, sizeof(batt));
+      ble.updateCharacteristicValue(tempChar.getValueAttribute().getHandle(), thermTempPayload, sizeof(thermTempPayload));  //Mod
+      ble.updateCharacteristicValue(battLevel.getValueAttribute().getHandle(), (uint8_t *)&batt, sizeof(batt));             //Mod
 }
 
 /**