Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: X_NUCLEO_IKS01A2
source/ESS.h@0:9c0e0ac79e75, 2020-04-29 (annotated)
- Committer:
- jim_lsj
- Date:
- Wed Apr 29 10:42:44 2020 +0000
- Revision:
- 0:9c0e0ac79e75
- Child:
- 1:b12ac7b02a21
12_1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jim_lsj | 0:9c0e0ac79e75 | 1 | /* ARM University Program Microcontroller Library |
jim_lsj | 0:9c0e0ac79e75 | 2 | * |
jim_lsj | 0:9c0e0ac79e75 | 3 | * Environmental Sensing Service |
jim_lsj | 0:9c0e0ac79e75 | 4 | * |
jim_lsj | 0:9c0e0ac79e75 | 5 | * This software is distributed under an "AS IS" BASIS, |
jim_lsj | 0:9c0e0ac79e75 | 6 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
jim_lsj | 0:9c0e0ac79e75 | 7 | */ |
jim_lsj | 0:9c0e0ac79e75 | 8 | |
jim_lsj | 0:9c0e0ac79e75 | 9 | #ifndef __BLE_ENVIRONMENTAL_SENSING_SERVICE_H__ |
jim_lsj | 0:9c0e0ac79e75 | 10 | #define __BLE_ENVIRONMENTAL_SENSING_SERVICE_H__ |
jim_lsj | 0:9c0e0ac79e75 | 11 | |
jim_lsj | 0:9c0e0ac79e75 | 12 | #include "mbed.h" |
jim_lsj | 0:9c0e0ac79e75 | 13 | #include "blecommon.h" |
jim_lsj | 0:9c0e0ac79e75 | 14 | #include "Gap.h" |
jim_lsj | 0:9c0e0ac79e75 | 15 | #include "GattServer.h" |
jim_lsj | 0:9c0e0ac79e75 | 16 | #include "BLEDeviceInstanceBase.h" |
jim_lsj | 0:9c0e0ac79e75 | 17 | |
jim_lsj | 0:9c0e0ac79e75 | 18 | //#include "BLEDevice.h" |
jim_lsj | 0:9c0e0ac79e75 | 19 | |
jim_lsj | 0:9c0e0ac79e75 | 20 | /* Environmental Sensing Service */ |
jim_lsj | 0:9c0e0ac79e75 | 21 | /* Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.environmental_sensing.xml */ |
jim_lsj | 0:9c0e0ac79e75 | 22 | /* Humidity characteristic: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.humidity.xml */ |
jim_lsj | 0:9c0e0ac79e75 | 23 | /* Temperature: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature.xml */ |
jim_lsj | 0:9c0e0ac79e75 | 24 | |
jim_lsj | 0:9c0e0ac79e75 | 25 | class EnvironmentalSensingService { |
jim_lsj | 0:9c0e0ac79e75 | 26 | |
jim_lsj | 0:9c0e0ac79e75 | 27 | public: |
jim_lsj | 0:9c0e0ac79e75 | 28 | /** |
jim_lsj | 0:9c0e0ac79e75 | 29 | * Constructor. |
jim_lsj | 0:9c0e0ac79e75 | 30 | * |
jim_lsj | 0:9c0e0ac79e75 | 31 | * param[in] _ble |
jim_lsj | 0:9c0e0ac79e75 | 32 | * Reference to the underlying BLEDevice. |
jim_lsj | 0:9c0e0ac79e75 | 33 | * param[in] humidity percentage (16-bit unsigned, 2 decimals). |
jim_lsj | 0:9c0e0ac79e75 | 34 | * initial value for the humidity value. |
jim_lsj | 0:9c0e0ac79e75 | 35 | * param[in] temperature in degrees Celsius (16-bit signed, 2 decimals). |
jim_lsj | 0:9c0e0ac79e75 | 36 | * initial value for the temperature |
jim_lsj | 0:9c0e0ac79e75 | 37 | */ |
jim_lsj | 0:9c0e0ac79e75 | 38 | EnvironmentalSensingService(BLEDevice &_ble, uint16_t humidity, int16_t temperature) : |
jim_lsj | 0:9c0e0ac79e75 | 39 | ble(_ble), |
jim_lsj | 0:9c0e0ac79e75 | 40 | |
jim_lsj | 0:9c0e0ac79e75 | 41 | humiditychar(GattCharacteristic::UUID_HUMIDITY_CHAR, (uint8_t *)&humidity, |
jim_lsj | 0:9c0e0ac79e75 | 42 | sizeof(uint16_t), sizeof(uint16_t), |
jim_lsj | 0:9c0e0ac79e75 | 43 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
jim_lsj | 0:9c0e0ac79e75 | 44 | |
jim_lsj | 0:9c0e0ac79e75 | 45 | temperaturechar(GattCharacteristic::UUID_TEMPERATURE_CHAR, (uint8_t *)&temperature, |
jim_lsj | 0:9c0e0ac79e75 | 46 | sizeof(int16_t), sizeof(int16_t), |
jim_lsj | 0:9c0e0ac79e75 | 47 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) |
jim_lsj | 0:9c0e0ac79e75 | 48 | |
jim_lsj | 0:9c0e0ac79e75 | 49 | |
jim_lsj | 0:9c0e0ac79e75 | 50 | { // Setup Service |
jim_lsj | 0:9c0e0ac79e75 | 51 | |
jim_lsj | 0:9c0e0ac79e75 | 52 | GattCharacteristic *charTable[] = {&humiditychar, &temperaturechar, }; // 2 services. humidity and temp |
jim_lsj | 0:9c0e0ac79e75 | 53 | |
jim_lsj | 0:9c0e0ac79e75 | 54 | GattService EnvironmentalService(0x181A, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
jim_lsj | 0:9c0e0ac79e75 | 55 | |
jim_lsj | 0:9c0e0ac79e75 | 56 | ble.addService(EnvironmentalService); |
jim_lsj | 0:9c0e0ac79e75 | 57 | } |
jim_lsj | 0:9c0e0ac79e75 | 58 | |
jim_lsj | 0:9c0e0ac79e75 | 59 | |
jim_lsj | 0:9c0e0ac79e75 | 60 | /* Set a new 16-bit value for the humidity measurement. */ |
jim_lsj | 0:9c0e0ac79e75 | 61 | void updateHumidity(uint16_t humidity) { |
jim_lsj | 0:9c0e0ac79e75 | 62 | ble.updateCharacteristicValue(humiditychar.getValueAttribute().getHandle(), (uint8_t *)&humidity, sizeof(uint16_t)); |
jim_lsj | 0:9c0e0ac79e75 | 63 | } |
jim_lsj | 0:9c0e0ac79e75 | 64 | |
jim_lsj | 0:9c0e0ac79e75 | 65 | /* Set a new 16-bit value for the temperature measurement. */ |
jim_lsj | 0:9c0e0ac79e75 | 66 | void updateTemperature(int16_t temperature) { |
jim_lsj | 0:9c0e0ac79e75 | 67 | ble.updateCharacteristicValue(temperaturechar.getValueAttribute().getHandle(), (uint8_t *)&temperature, sizeof(int16_t)); |
jim_lsj | 0:9c0e0ac79e75 | 68 | } |
jim_lsj | 0:9c0e0ac79e75 | 69 | |
jim_lsj | 0:9c0e0ac79e75 | 70 | |
jim_lsj | 0:9c0e0ac79e75 | 71 | |
jim_lsj | 0:9c0e0ac79e75 | 72 | private: |
jim_lsj | 0:9c0e0ac79e75 | 73 | BLEDevice &ble; |
jim_lsj | 0:9c0e0ac79e75 | 74 | GattCharacteristic humiditychar; |
jim_lsj | 0:9c0e0ac79e75 | 75 | GattCharacteristic temperaturechar; |
jim_lsj | 0:9c0e0ac79e75 | 76 | |
jim_lsj | 0:9c0e0ac79e75 | 77 | }; |
jim_lsj | 0:9c0e0ac79e75 | 78 | |
jim_lsj | 0:9c0e0ac79e75 | 79 | #endif /* #ifndef __BLE_ENVIRONMENTAL_SENSING_SERVICE_H__*/ |
jim_lsj | 0:9c0e0ac79e75 | 80 | |
jim_lsj | 0:9c0e0ac79e75 | 81 | |
jim_lsj | 0:9c0e0ac79e75 | 82 | // *******************************ARM University Program Copyright © ARM Ltd 2015*************************************// |