BLE_Health_Thermometer for mbed HRM1017 with new library

Dependencies:   BLE_API TMP102 mbed nRF51822

Files at this revision

API Documentation at this revision

Comitter:
todotani
Date:
Fri Aug 22 11:27:32 2014 +0000
Child:
1:50056f53e2ac
Commit message:
BLE_Health_Thermometer for mbed HRM1017 with new library

Changed in this revision

BLE_API.lib Show annotated file Show diff for this revision Revisions of this file
TMP102.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
mbed.bld 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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BLE_API.lib	Fri Aug 22 11:27:32 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/#189ff241dae1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TMP102.lib	Fri Aug 22 11:27:32 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/chris/code/TMP102/#694792b93731
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Aug 22 11:27:32 2014 +0000
@@ -0,0 +1,148 @@
+#include "mbed.h"
+#include "TMP102.h"
+#include "BLEDevice.h"
+#include "ble_hts.h"
+
+#define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console;
+                               * it will have an impact on code-size and power consumption. */
+
+#if NEED_CONSOLE_OUTPUT
+Serial  pc(USBTX, USBRX);
+#define DEBUG(...) { pc.printf(__VA_ARGS__); }
+#else
+#define DEBUG(...) /* nothing */
+#endif /* #if NEED_CONSOLE_OUTPUT */
+
+const static char  DEVICE_NAME[] = "HRM1017_HTM";
+
+BLEDevice  ble;
+TMP102      healthThemometer(p22, p20, 0x90);  /* The TMP102 connected to our board */
+
+/* LEDs for indication: */
+DigitalOut  oneSecondLed(LED1);        /* LED1 is toggled every second. */
+DigitalOut  advertisingStateLed(LED2); /* LED2 is on when we are advertising, otherwise off. */
+
+
+/* Health Thermometer Service */ 
+uint8_t             thermTempPayload[5] = { 0, 0, 0, 0, 0 };
+
+GattCharacteristic  tempChar (GattCharacteristic::UUID_TEMPERATURE_MEASUREMENT_CHAR,
+                                thermTempPayload, 5, 5,
+                                GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE);
+/* Battery Level Service */
+uint8_t            batt = 100;     /* Battery level */
+uint8_t            read_batt = 0;  /* Variable to hold battery level reads */
+GattCharacteristic battLevel ( GattCharacteristic::UUID_BATTERY_LEVEL_CHAR, 
+                                 (uint8_t *)batt, 1, 1,
+                                 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
+GattCharacteristic *htmChars[] = {&tempChar, };
+GattCharacteristic *battChars[] = {&battLevel, };
+GattService        htmService(GattService::UUID_HEALTH_THERMOMETER_SERVICE, htmChars, 
+                                sizeof(htmChars) / sizeof(GattCharacteristic *));
+GattService        battService(GattService::UUID_BATTERY_SERVICE, battChars,
+                                sizeof(battChars) / sizeof(GattCharacteristic *));
+
+uint16_t             uuid16_list[] = {GattService::UUID_HEALTH_THERMOMETER_SERVICE,
+                                      GattService::UUID_BATTERY_SERVICE};
+
+uint32_t quick_ieee11073_from_float(float temperature);
+void updateServiceValues(void);
+
+static Gap::ConnectionParams_t connectionParams;
+
+void disconnectionCallback(Gap::Handle_t handle)
+{
+    advertisingStateLed = 1;
+    
+    DEBUG("Disconnected handle %u!\n\r", handle);
+    DEBUG("Restarting the advertising process\n\r");
+    ble.startAdvertising();
+}
+
+void onConnectionCallback(Gap::Handle_t handle)
+{
+    advertisingStateLed = 0;
+
+    DEBUG("connected. Got handle %u\r\n", handle);
+
+    connectionParams.slaveLatency = 1;
+    if (ble.updateConnectionParams(handle, &connectionParams) != BLE_ERROR_NONE) {
+        DEBUG("failed to update connection paramter\r\n");
+    }
+}
+
+/**************************************************************************/
+/*!
+    @brief  Program entry point
+*/
+/**************************************************************************/
+int main(void)
+{
+    
+    /* Setup blinky led */
+    oneSecondLed = 1;
+    
+    DEBUG("Initialising the nRF51822\n\r");
+    ble.init();
+    ble.onDisconnection(disconnectionCallback);
+    ble.onConnection(onConnectionCallback);
+
+    ble.getPreferredConnectionParams(&connectionParams);
+
+    /* setup advertising */
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t*)uuid16_list, sizeof(uuid16_list));
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_THERMOMETER);
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
+    ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+    ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
+    ble.startAdvertising();
+    advertisingStateLed = 1;
+
+    ble.addService(htmService);
+    ble.addService(battService);
+
+    for (;;)
+    {
+        /* Now that we're live, update the battery level & temperature characteristics */
+        updateServiceValues();
+        wait(1);
+        ble.waitForEvent();
+    }
+}
+
+/**************************************************************************/
+/*!
+    @brief  Ticker callback to switch advertisingStateLed state
+*/
+/**************************************************************************/
+void updateServiceValues(void)
+{
+      /* Toggle the one second LEDs */
+      oneSecondLed = !oneSecondLed;
+      
+      /* Decrement the battery level. */
+      batt <=50 ? batt=100 : batt--;
+      
+      /* Update the temperature. Note that we need to convert to an ieee11073 format float. */
+      float temperature = healthThemometer.read();
+      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));
+}
+
+/**
+ * @brief A very quick conversion between a float temperature and 11073-20601 FLOAT-Type.
+ * @param temperature The temperature as a float.
+ * @return The temperature in 11073-20601 FLOAT-Type format.
+ */
+uint32_t quick_ieee11073_from_float(float temperature)
+{
+    uint8_t  exponent = 0xFF; //exponent is -1
+    uint32_t mantissa = (uint32_t)(temperature*10);
+    
+    return ( ((uint32_t)exponent) << 24) | mantissa;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Fri Aug 22 11:27:32 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/9327015d4013
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nRF51822.lib	Fri Aug 22 11:27:32 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/teams/Nordic-Semiconductor/code/nRF51822/#f3badab88f02