parent
b00fc85548
commit
4314b68643
@ -0,0 +1,45 @@
|
||||
#ifndef MICROBIT_TEMPERATURE_SERVICE_H
|
||||
#define MICROBIT_TEMPERATURE_SERVICE_H
|
||||
|
||||
#include "MicroBit.h"
|
||||
|
||||
// UUIDs for our service and characteristics
|
||||
extern const uint8_t MicroBitTemperatureServiceUUID[];
|
||||
extern const uint8_t MicroBitTemperatureServiceDataUUID[];
|
||||
|
||||
|
||||
/**
|
||||
* Class definition for a MicroBit BLE Temperture Service.
|
||||
* Provides access to live temperature data via BLE.
|
||||
*/
|
||||
class MicroBitTemperatureService
|
||||
{
|
||||
public:
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Create a representation of the TempertureService
|
||||
* @param _ble The instance of a BLE device that we're running on.
|
||||
*/
|
||||
MicroBitTemperatureService(BLEDevice &_ble);
|
||||
|
||||
/**
|
||||
* Temperature update callback
|
||||
*/
|
||||
void temperatureUpdate(MicroBitEvent e);
|
||||
|
||||
private:
|
||||
|
||||
// Bluetooth stack we're running on.
|
||||
BLEDevice &ble;
|
||||
|
||||
// memory for our 8 bit temperature characteristic.
|
||||
int8_t temperatureDataCharacteristicBuffer;
|
||||
|
||||
// Handles to access each characteristic when they are held by Soft Device.
|
||||
GattAttribute::Handle_t temperatureDataCharacteristicHandle;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
@ -0,0 +1,54 @@
|
||||
/**
|
||||
* Class definition for the custom MicroBit Temperature Service.
|
||||
* Provides a BLE service to remotely read the state of the temperature, and configure its behaviour.
|
||||
*/
|
||||
|
||||
#include "MicroBit.h"
|
||||
#include "ble/UUID.h"
|
||||
|
||||
#include "MicroBitTemperatureService.h"
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Create a representation of the TemperatureService
|
||||
* @param _ble The instance of a BLE device that we're running on.
|
||||
*/
|
||||
MicroBitTemperatureService::MicroBitTemperatureService(BLEDevice &_ble) :
|
||||
ble(_ble)
|
||||
{
|
||||
// Create the data structures that represent each of our characteristics in Soft Device.
|
||||
GattCharacteristic temperatureDataCharacteristic(MicroBitTemperatureServiceDataUUID, (uint8_t *)temperatureDataCharacteristicBuffer, 0,
|
||||
sizeof(temperatureDataCharacteristicBuffer), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
|
||||
|
||||
// Initialise our characteristic values.
|
||||
temperatureDataCharacteristicBuffer = 0;
|
||||
|
||||
GattCharacteristic *characteristics[] = {&temperatureDataCharacteristic};
|
||||
GattService service(MicroBitTemperatureServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
|
||||
|
||||
ble.addService(service);
|
||||
|
||||
temperatureDataCharacteristicHandle = temperatureDataCharacteristic.getValueHandle();
|
||||
ble.updateCharacteristicValue(temperatureDataCharacteristicHandle, (const uint8_t *)&temperatureDataCharacteristicBuffer, sizeof(temperatureDataCharacteristicBuffer));
|
||||
|
||||
uBit.MessageBus.listen(MICROBIT_ID_COMPASS, MICROBIT_COMPASS_EVT_TEMPERATURE_UPDATE, this, &MicroBitTemperatureService::temperatureUpdate, MESSAGE_BUS_LISTENER_NONBLOCKING | MESSAGE_BUS_LISTENER_URGENT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Temperature update callback
|
||||
*/
|
||||
void MicroBitTemperatureService::temperatureUpdate(MicroBitEvent e)
|
||||
{
|
||||
temperatureDataCharacteristicBuffer = uBit.compass.getTemperature();
|
||||
ble.gattServer().notify(temperatureDataCharacteristicHandle,(uint8_t *)temperatureDataCharacteristicBuffer, sizeof(temperatureDataCharacteristicBuffer));
|
||||
}
|
||||
|
||||
const uint8_t MicroBitTemperatureServiceUUID[] = {
|
||||
0xe9,0x5d,0x61,0x00,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8
|
||||
};
|
||||
|
||||
const uint8_t MicroBitTemperatureServiceDataUUID[] = {
|
||||
0xe9,0x5d,0x8a,0x38,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8
|
||||
};
|
||||
|
||||
|
Loading…
Reference in new issue