ST / Mbed OS Node_BLE_Sensors_Device

Dependencies:   X_NUCLEO_IKS01A2

Fork of Node_BLE_Switch_Device by ST

Bluetooth Low Energy enabled device with environmental and inertial features, i.e. "Pressure", "Humidity", "Temperature", "Accelerometer", "Gyroscope", and "Magnetometer", plus "Switch" feature, compatible with BlueST Protocol.

Revision:
6:01d49589410e
Parent:
5:445024130101
Child:
7:2b5ed57b088c
diff -r 445024130101 -r 01d49589410e source/CustomService.h
--- a/source/CustomService.h	Fri May 18 18:41:27 2018 +0000
+++ b/source/CustomService.h	Tue May 22 16:35:05 2018 +0000
@@ -41,22 +41,50 @@
 #ifndef __BLE_CUSTOM_SERVICE_H__
 #define __BLE_CUSTOM_SERVICE_H__
 
+
+/* Includes ------------------------------------------------------------------*/
+
 #include "ble_utils.h"
 
-#define TIMESTAMP_LENGTH    (sizeof(uint16_t))
-#define STATE_LENGTH        (sizeof(uint8_t))
-#define COMMAND_LENGTH      (sizeof(uint8_t))
-#define PRESSURE_LENGTH     (sizeof(uint32_t))
-#define HUMIDITY_LENGTH     (sizeof(uint16_t))
-#define TEMPERATURE_LENGTH  (sizeof(uint16_t))
+
+/* Definitions ---------------------------------------------------------------*/
+
+#define TIMESTAMP_LENGTH        (sizeof(uint16_t))
+#define STATE_LENGTH            (sizeof(uint8_t))
+#define COMMAND_LENGTH          (sizeof(uint8_t))
+#define PRESSURE_LENGTH         (sizeof(uint32_t))
+#define HUMIDITY_LENGTH         (sizeof(uint16_t))
+#define TEMPERATURE_LENGTH      (sizeof(uint16_t))
+#define ACCELEROMETER_LENGTH    (sizeof(uint16_t))
+#define GYROSCOPE_LENGTH        (sizeof(uint16_t))
+#define MAGNETOMETER_LENGTH     (sizeof(uint16_t))
+
+#define STATE_DATA_LENGTH       (TIMESTAMP_LENGTH + STATE_LENGTH)
+#define COMMAND_DATA_LENGTH     (COMMAND_LENGTH)
+#define ENV_SENSORS_DATA_LENGTH (TIMESTAMP_LENGTH + PRESSURE_LENGTH + HUMIDITY_LENGTH + TEMPERATURE_LENGTH)
+#define INE_SENSORS_DATA_LENGTH (TIMESTAMP_LENGTH + (3 * ACCELEROMETER_LENGTH) + (3 * GYROSCOPE_LENGTH) + (3 * MAGNETOMETER_LENGTH))
+
 
-#define STATE_DATA_LENGTH   (TIMESTAMP_LENGTH + STATE_LENGTH)
-#define COMMAND_DATA_LENGTH (COMMAND_LENGTH)
-#define SENSORS_DATA_LENGTH (TIMESTAMP_LENGTH + PRESSURE_LENGTH + HUMIDITY_LENGTH + TEMPERATURE_LENGTH)
+/* Types ---------------------------------------------------------------------*/
+
+/* Axes. */
+typedef struct
+{
+    int32_t x;
+    int32_t y;
+    int32_t z;
+} axes_t;
 
-const UUID::LongUUIDBytes_t CUSTOM_SERVICE_UUID                = {0x00,0x00,0x00,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b};
-const UUID::LongUUIDBytes_t CUSTOM_SWITCH_CHARACTERISTIC_UUID  = {0x20,0x00,0x00,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b};
-const UUID::LongUUIDBytes_t CUSTOM_SENSORS_CHARACTERISTIC_UUID = {0x00,0x1c,0x00,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b};
+
+/* Constants -----------------------------------------------------------------*/
+
+const UUID::LongUUIDBytes_t CUSTOM_SERVICE_UUID                    = {0x00,0x00,0x00,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b};
+const UUID::LongUUIDBytes_t CUSTOM_SWITCH_CHARACTERISTIC_UUID      = {0x20,0x00,0x00,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b};
+const UUID::LongUUIDBytes_t CUSTOM_ENV_SENSORS_CHARACTERISTIC_UUID = {0x00,0x1c,0x00,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b};
+const UUID::LongUUIDBytes_t CUSTOM_INE_SENSORS_CHARACTERISTIC_UUID = {0x00,0xe0,0x00,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b};
+
+
+/* Classes -------------------------------------------------------------------*/
 
 class CustomService {
 public:
@@ -72,14 +100,17 @@
         ble(_ble),
         state_command(CUSTOM_SWITCH_CHARACTERISTIC_UUID, packed_state_command, STATE_DATA_LENGTH, STATE_DATA_LENGTH,
             /*GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | */GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
-        sensors(CUSTOM_SENSORS_CHARACTERISTIC_UUID, packed_sensors, SENSORS_DATA_LENGTH, SENSORS_DATA_LENGTH,
+        env_sensors(CUSTOM_ENV_SENSORS_CHARACTERISTIC_UUID, packed_env_sensors, ENV_SENSORS_DATA_LENGTH, ENV_SENSORS_DATA_LENGTH,
+            GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
+        ine_sensors(CUSTOM_INE_SENSORS_CHARACTERISTIC_UUID, packed_ine_sensors, INE_SENSORS_DATA_LENGTH, INE_SENSORS_DATA_LENGTH,
             GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)
     {
-        GattCharacteristic *char_table[] = {&state_command, &sensors};
-        GattService switch_service(CUSTOM_SERVICE_UUID, char_table, sizeof(char_table) / sizeof(GattCharacteristic *));
-        ble.addService(switch_service);
+        GattCharacteristic *char_table[] = {&state_command, &env_sensors, &ine_sensors};
+        GattService iot_service(CUSTOM_SERVICE_UUID, char_table, sizeof(char_table) / sizeof(GattCharacteristic *));
+        ble.addService(iot_service);
         memset (packed_state_command, 0, STATE_DATA_LENGTH);
-        memset (packed_sensors, 0, SENSORS_DATA_LENGTH);
+        memset (packed_env_sensors, 0, ENV_SENSORS_DATA_LENGTH);
+        memset (packed_ine_sensors, 0, INE_SENSORS_DATA_LENGTH);
     }
 
     void send_state(uint16_t time_stamp, uint8_t current_state) {
@@ -89,17 +120,48 @@
         ble.gattServer().write(get_state_command_handle(), (uint8_t *) &packed_state_command, STATE_DATA_LENGTH, 0);
     }
 
-    void send_sensors(uint16_t time_stamp, uint32_t pressure, uint16_t humidity, uint16_t temperature) {
-        memset (packed_sensors, 0, SENSORS_DATA_LENGTH);
+    void send_env_sensors(
+        uint16_t time_stamp,
+        float pressure, float humidity, float temperature
+    ) {
+        memset (packed_env_sensors, 0, ENV_SENSORS_DATA_LENGTH);
         int p = 0;
-        STORE_LE_16(&packed_sensors[p], time_stamp);
+        STORE_LE_16(&packed_env_sensors[p], time_stamp);
         p += TIMESTAMP_LENGTH;
-        STORE_LE_32(&packed_sensors[p], pressure);
+        STORE_LE_32(&packed_env_sensors[p], (uint32_t) (pressure * 10));
         p += PRESSURE_LENGTH;
-        STORE_LE_16(&packed_sensors[p], humidity);
+        STORE_LE_16(&packed_env_sensors[p], (uint16_t) (humidity * 10));
         p += HUMIDITY_LENGTH;
-        STORE_LE_16(&packed_sensors[p], temperature);
-        ble.gattServer().write(get_sensors_handle(), (uint8_t *) &packed_sensors, SENSORS_DATA_LENGTH, 0);
+        STORE_LE_16(&packed_env_sensors[p], (uint16_t) (temperature * 10));
+        ble.gattServer().write(get_env_sensors_handle(), (uint8_t *) &packed_env_sensors, ENV_SENSORS_DATA_LENGTH, 0);
+    }
+
+    void send_ine_sensors(
+        uint16_t time_stamp,
+        axes_t *accelerometer, axes_t *gyroscope, axes_t *magnetometer
+    ) {
+        memset (packed_ine_sensors, 0, INE_SENSORS_DATA_LENGTH);
+        int p = 0;
+        STORE_LE_16(&packed_ine_sensors[p], time_stamp);
+        p += TIMESTAMP_LENGTH;
+        STORE_LE_16(&packed_ine_sensors[p], (int16_t) accelerometer->x);
+        p += ACCELEROMETER_LENGTH;
+        STORE_LE_16(&packed_ine_sensors[p], (int16_t) accelerometer->y);
+        p += ACCELEROMETER_LENGTH;
+        STORE_LE_16(&packed_ine_sensors[p], (int16_t) accelerometer->z);
+        p += ACCELEROMETER_LENGTH;
+        STORE_LE_16(&packed_ine_sensors[p], (int16_t) gyroscope->x);
+        p += GYROSCOPE_LENGTH;
+        STORE_LE_16(&packed_ine_sensors[p], (int16_t) gyroscope->y);
+        p += GYROSCOPE_LENGTH;
+        STORE_LE_16(&packed_ine_sensors[p], (int16_t) gyroscope->z);
+        p += GYROSCOPE_LENGTH;
+        STORE_LE_16(&packed_ine_sensors[p], (int16_t) magnetometer->x);
+        p += MAGNETOMETER_LENGTH;
+        STORE_LE_16(&packed_ine_sensors[p], (int16_t) magnetometer->y);
+        p += MAGNETOMETER_LENGTH;
+        STORE_LE_16(&packed_ine_sensors[p], (int16_t) magnetometer->z);
+        ble.gattServer().write(get_ine_sensors_handle(), (uint8_t *) &packed_ine_sensors, INE_SENSORS_DATA_LENGTH, 0);
     }
 
     GattAttribute::Handle_t get_state_command_handle() const
@@ -107,17 +169,24 @@
         return state_command.getValueAttribute().getHandle();
     }
 
-    GattAttribute::Handle_t get_sensors_handle() const
+    GattAttribute::Handle_t get_env_sensors_handle() const
     {
-        return sensors.getValueAttribute().getHandle();
+        return env_sensors.getValueAttribute().getHandle();
+    }
+
+    GattAttribute::Handle_t get_ine_sensors_handle() const
+    {
+        return ine_sensors.getValueAttribute().getHandle();
     }
 
 private:
     BLEDevice &ble;
     GattCharacteristic state_command;
-    GattCharacteristic sensors;
+    GattCharacteristic env_sensors;
+    GattCharacteristic ine_sensors;
     uint8_t packed_state_command[STATE_DATA_LENGTH];
-    uint8_t packed_sensors[SENSORS_DATA_LENGTH];
+    uint8_t packed_env_sensors[ENV_SENSORS_DATA_LENGTH];
+    uint8_t packed_ine_sensors[INE_SENSORS_DATA_LENGTH];
 };
 
 #endif /* #ifndef __BLE_CUSTOM_SERVICE_H__ */