Pull request for i.a. sensor buffer template

Dependencies:   BLE_API MPU6050 mbed nRF51822

Files at this revision

API Documentation at this revision

Comitter:
pietermaljaars
Date:
Wed Sep 19 19:49:39 2018 +0000
Parent:
4:aea4ff8e52ef
Child:
6:ed0dc0647c01
Commit message:
Add temperature

Changed in this revision

TemperatureService.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/TemperatureService.h	Wed Sep 19 19:49:39 2018 +0000
@@ -0,0 +1,64 @@
+
+#ifndef __TEMPERATURE_SERVICE_H__
+#define __TEMPERATURE_SERVICE_H__
+
+#include "ble/BLE.h"
+
+#define UUID_TEMPERATURE_CELSIUS_CHAR 0x2A1F
+#define UUID_TEMPERATURE_SERVICE "5D34E2F0-3DD2-07AC-38F1-BBAD120EF852"
+      
+
+/**
+* @class TemperatureService
+* @brief BLE Temperature Service. This service displays the temperature, represented as an 16bit number.
+*/
+class TemperatureService {
+public:
+    /**
+     * @param[in] _ble
+     *               BLE object for the underlying controller.
+     * @param[in] temperature
+     *               16bit temperature in 0.1 degrees Celsius
+     */
+    TemperatureService(BLE &_ble, int16_t temperature = 200) :
+        ble(_ble),
+        temperature(temperature),
+        temperatureCharacteristic(UUID_TEMPERATURE_CELSIUS_CHAR, &temperature, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) {
+
+        GattCharacteristic *charTable[] = {&temperatureCharacteristic};
+        GattService         TemperatureService(UUID(UUID_TEMPERATURE_SERVICE), charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
+
+        ble.addService(TemperatureService);
+    }
+
+    /**
+     * @brief Update the temperature with a new value. Valid values lie between -2732 and above.
+     *
+     * @param newTemp
+     *              Update to temperature.
+     */
+    void updateTemperature(int16_t newTemp) {
+        temperature = newTemp;
+        uint16_t temp = (uint16_t)temperature;
+        ble.gattServer().write(temperatureCharacteristic.getValueHandle(), (const uint8_t*)&temp, sizeof(temperature));
+    }
+
+protected:
+    /**
+     * A reference to the underlying BLE instance that this object is attached to.
+     * The services and characteristics will be registered in this BLE instance.
+     */
+    BLE &ble;
+
+    /**
+     * The current temperature represented as 0.1 degrees Celsius.
+     */
+    int16_t    temperature;
+    /**
+     * A ReadOnlyGattCharacteristic that allows access to the peer device to the
+     * temperature value through BLE.
+     */
+    ReadOnlyGattCharacteristic<int16_t> temperatureCharacteristic;
+};
+
+#endif /* #ifndef __TEMPERATURE_SERVICE_H__*/
\ No newline at end of file
--- a/main.cpp	Wed Sep 19 18:24:52 2018 +0000
+++ b/main.cpp	Wed Sep 19 19:49:39 2018 +0000
@@ -9,6 +9,7 @@
 #include "UARTService.h"
 #include "BatteryService.h"
 #include "DeviceInformationService.h"
+#include "TemperatureService.h"
 
 
 #define LOG(...)    { pc.printf(__VA_ARGS__); }
@@ -167,6 +168,7 @@
     //uartService.retargetStdout();
     BatteryService battery(ble);
     DeviceInformationService deviceInfo(ble, MANUFACTURER, MODELNUMBER, SERIALNUMBER, HARDWAREREVISION, FIRMWAREREVISION, SOFTWAREREVISION);
+    TemperatureService tempService(ble);
 
     ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
     ble.gap().startAdvertising();
@@ -187,7 +189,13 @@
             startMeasure = false;
             
             float temp = mpu.getTemp();
-            LOG("Temp = %f\n", temp);
+            LOG("Temp = %.01f\n", temp);
+            int16_t tempRaw = mpu.getTempRaw();
+            tempRaw *= 10;
+            tempRaw += 5210;
+            tempRaw /= 340;
+            tempRaw += 350;
+            tempService.updateTemperature(tempRaw);
         }
     }
 }