Merge branch 'ble-profile' of https://github.com/lancaster-university/microbit-dal into ble-profile
commit
7459213d10
@ -0,0 +1,54 @@
|
||||
#ifndef MICROBIT_BUTTON_SERVICE_H
|
||||
#define MICROBIT_BUTTON_SERVICE_H
|
||||
|
||||
#include "MicroBit.h"
|
||||
|
||||
// UUIDs for our service and characteristics
|
||||
extern const uint8_t MicroBitButtonServiceUUID[];
|
||||
extern const uint8_t MicroBitButtonAServiceDataUUID[];
|
||||
extern const uint8_t MicroBitButtonBServiceDataUUID[];
|
||||
|
||||
|
||||
/**
|
||||
* Class definition for a MicroBit BLE Button Service.
|
||||
* Provides access to live button data via BLE, and provides basic configuration options.
|
||||
*/
|
||||
class MicroBitButtonService
|
||||
{
|
||||
public:
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Create a representation of the ButtonService
|
||||
* @param _ble The instance of a BLE device that we're running on.
|
||||
*/
|
||||
MicroBitButtonService(BLEDevice &_ble);
|
||||
|
||||
|
||||
private:
|
||||
|
||||
/**
|
||||
* Button A update callback
|
||||
*/
|
||||
void buttonAUpdate(MicroBitEvent e);
|
||||
|
||||
/**
|
||||
* Button B update callback
|
||||
*/
|
||||
void buttonBUpdate(MicroBitEvent e);
|
||||
|
||||
// Bluetooth stack we're running on.
|
||||
BLEDevice &ble;
|
||||
|
||||
// memory for our 8 bit control characteristics.
|
||||
uint8_t buttonADataCharacteristicBuffer;
|
||||
uint8_t buttonBDataCharacteristicBuffer;
|
||||
|
||||
// Handles to access each characteristic when they are held by Soft Device.
|
||||
GattAttribute::Handle_t buttonADataCharacteristicHandle;
|
||||
GattAttribute::Handle_t buttonBDataCharacteristicHandle;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
@ -0,0 +1,111 @@
|
||||
#ifndef MICROBIT_IO_PIN_SERVICE_H
|
||||
#define MICROBIT_IO_PIN_SERVICE_H
|
||||
|
||||
#include "MicroBit.h"
|
||||
|
||||
#define MICROBIT_IO_PIN_SERVICE_PINCOUNT 20
|
||||
#define MICROBIT_IO_PIN_SERVICE_DATA_SIZE 10
|
||||
|
||||
// UUIDs for our service and characteristics
|
||||
extern const uint8_t MicroBitIOPinServiceUUID[];
|
||||
extern const uint8_t MicroBitIOPinServiceADConfigurationUUID[];
|
||||
extern const uint8_t MicroBitIOPinServiceIOConfigurationUUID[];
|
||||
extern const uint8_t MicroBitIOPinServiceDataUUID[];
|
||||
extern MicroBitPin * const MicroBitIOPins[];
|
||||
|
||||
/**
|
||||
* Name value pair definition, as used to read abd write pin values over BLE.
|
||||
*/
|
||||
struct IOData
|
||||
{
|
||||
uint8_t pin;
|
||||
uint8_t value;
|
||||
};
|
||||
|
||||
/**
|
||||
* Class definition for a MicroBit BLE IOPin Service.
|
||||
* Provides access to live ioPin data via BLE, and provides basic configuration options.
|
||||
*/
|
||||
class MicroBitIOPinService : public MicroBitComponent
|
||||
{
|
||||
public:
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Create a representation of the IOPinService
|
||||
* @param _ble The instance of a BLE device that we're running on.
|
||||
*/
|
||||
MicroBitIOPinService(BLEDevice &_ble);
|
||||
|
||||
/**
|
||||
* periodic callback from MicroBit scheduler.
|
||||
* Check if any of the pins we're watching need updating. Apply a BLE NOTIFY if so...
|
||||
*/
|
||||
virtual void idleTick();
|
||||
|
||||
private:
|
||||
|
||||
/**
|
||||
* Callback. Invoked when any of our attributes are written via BLE.
|
||||
*/
|
||||
void onDataWritten(const GattWriteCallbackParams *params);
|
||||
|
||||
/**
|
||||
* Callback. invoked when the BLE data characteristic is read.
|
||||
* reads all the pins marked as inputs, and updates the data stored in the BLE stack.
|
||||
*/
|
||||
void onDataRead(GattReadAuthCallbackParams *params);
|
||||
|
||||
/**
|
||||
* Determines if the given pin was configured as a digital pin by the BLE ADPinConfigurationCharacterisitic.
|
||||
*
|
||||
* @param pin the enumeration of the pin to test
|
||||
* @return 1 if this pin is configured as a digital value, 0 otherwise
|
||||
*/
|
||||
int isDigital(int i);
|
||||
|
||||
/**
|
||||
* Determines if the given pin was configured as an analog pin by the BLE ADPinConfigurationCharacterisitic.
|
||||
*
|
||||
* @param pin the enumeration of the pin to test
|
||||
* @return 1 if this pin is configured as a analog value, 0 otherwise
|
||||
*/
|
||||
int isAnalog(int i);
|
||||
|
||||
/**
|
||||
* Determines if the given pin was configured as an input by the BLE IOPinConfigurationCharacterisitic.
|
||||
*
|
||||
* @param pin the enumeration of the pin to test
|
||||
* @return 1 if this pin is configured as an input, 0 otherwise
|
||||
*/
|
||||
int isInput(int i);
|
||||
|
||||
/**
|
||||
* Determines if the given pin was configured as output by the BLE IOPinConfigurationCharacterisitic.
|
||||
*
|
||||
* @param pin the enumeration of the pin to test
|
||||
* @return 1 if this pin is configured as an output, 0 otherwise
|
||||
*/
|
||||
int isOutput(int i);
|
||||
|
||||
|
||||
// Bluetooth stack we're running on.
|
||||
BLEDevice &ble;
|
||||
|
||||
// memory for our 8 bit control characteristics.
|
||||
uint32_t ioPinServiceADCharacteristicBuffer;
|
||||
uint32_t ioPinServiceIOCharacteristicBuffer;
|
||||
IOData ioPinServiceDataCharacteristicBuffer[MICROBIT_IO_PIN_SERVICE_DATA_SIZE];
|
||||
|
||||
// Historic information about our pin data data.
|
||||
uint8_t ioPinServiceIOData[MICROBIT_IO_PIN_SERVICE_PINCOUNT];
|
||||
|
||||
// Handles to access each characteristic when they are held by Soft Device.
|
||||
GattAttribute::Handle_t ioPinServiceADCharacteristicHandle;
|
||||
GattAttribute::Handle_t ioPinServiceIOCharacteristicHandle;
|
||||
GattCharacteristic *ioPinServiceDataCharacteristic;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
@ -0,0 +1,63 @@
|
||||
#ifndef MICROBIT_MAGNETOMETER_SERVICE_H
|
||||
#define MICROBIT_MAGNETOMETER_SERVICE_H
|
||||
|
||||
#include "MicroBit.h"
|
||||
|
||||
// UUIDs for our service and characteristics
|
||||
extern const uint8_t MicroBitMagnetometerServiceUUID[];
|
||||
extern const uint8_t MicroBitMagnetometerServiceDataUUID[];
|
||||
extern const uint8_t MicroBitMagnetometerServiceBearingUUID[];
|
||||
extern const uint8_t MicroBitMagnetometerServicePeriodUUID[];
|
||||
|
||||
|
||||
/**
|
||||
* Class definition for a MicroBit BLE Magnetometer Service.
|
||||
* Provides access to live magnetometer data via BLE, and provides basic configuration options.
|
||||
*/
|
||||
class MicroBitMagnetometerService
|
||||
{
|
||||
public:
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Create a representation of the MagnetometerService
|
||||
* @param _ble The instance of a BLE device that we're running on.
|
||||
*/
|
||||
MicroBitMagnetometerService(BLEDevice &_ble);
|
||||
|
||||
|
||||
private:
|
||||
|
||||
/**
|
||||
* Callback. Invoked when any of our attributes are written via BLE.
|
||||
*/
|
||||
void onDataWritten(const GattWriteCallbackParams *params);
|
||||
|
||||
/**
|
||||
* Magnetometer update callback
|
||||
*/
|
||||
void magnetometerUpdate(MicroBitEvent e);
|
||||
|
||||
/**
|
||||
* Sample Period Change Needed callback.
|
||||
* Reconfiguring the magnetometer can to a REALLY long time (sometimes even seconds to complete)
|
||||
* So we do this in the background when necessary, through this event handler.
|
||||
*/
|
||||
void samplePeriodUpdateNeeded(MicroBitEvent e);
|
||||
|
||||
// Bluetooth stack we're running on.
|
||||
BLEDevice &ble;
|
||||
|
||||
// memory for our 8 bit control characteristics.
|
||||
int16_t magnetometerDataCharacteristicBuffer[3];
|
||||
uint16_t magnetometerBearingCharacteristicBuffer;
|
||||
uint16_t magnetometerPeriodCharacteristicBuffer;
|
||||
|
||||
// Handles to access each characteristic when they are held by Soft Device.
|
||||
GattAttribute::Handle_t magnetometerDataCharacteristicHandle;
|
||||
GattAttribute::Handle_t magnetometerBearingCharacteristicHandle;
|
||||
GattAttribute::Handle_t magnetometerPeriodCharacteristicHandle;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -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,102 @@
|
||||
#ifndef MICROBIT_THERMOMETER_H
|
||||
#define MICROBIT_THERMOMETER_H
|
||||
|
||||
#include "mbed.h"
|
||||
#include "MicroBitComponent.h"
|
||||
|
||||
#define MICROBIT_THERMOMETER_PERIOD 1000
|
||||
|
||||
|
||||
#define MAG3110_SAMPLE_RATES 11
|
||||
|
||||
/*
|
||||
* Temperature events
|
||||
*/
|
||||
#define MICROBIT_THERMOMETER_EVT_UPDATE 1
|
||||
|
||||
/**
|
||||
* Class definition for MicroBit Thermometer.
|
||||
*
|
||||
* Infers and stores the ambient temoperature based on the surface temperature
|
||||
* of the various chips on the micro:bit.
|
||||
*
|
||||
*/
|
||||
class MicroBitThermometer : public MicroBitComponent
|
||||
{
|
||||
unsigned long sampleTime;
|
||||
uint32_t samplePeriod;
|
||||
int16_t temperature;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Constructor.
|
||||
* Create new object that can sense temperature.
|
||||
* @param id the ID of the new MicroBitThermometer object.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* thermometer(MICROBIT_ID_THERMOMETER);
|
||||
* @endcode
|
||||
*
|
||||
* Possible Events:
|
||||
* @code
|
||||
* MICROBIT_THERMOMETER_EVT_CHANGED
|
||||
* @endcode
|
||||
*/
|
||||
MicroBitThermometer(uint16_t id);
|
||||
|
||||
/**
|
||||
* Set the sample rate at which the temperatureis read (in ms).
|
||||
* n.b. the temperature is alwasy read in the background, so wis only updated
|
||||
* when the processor is idle, or when the temperature is explicitly read.
|
||||
* The default sample period is 1 second.
|
||||
* @param period the requested time between samples, in milliseconds.
|
||||
*/
|
||||
void setPeriod(int period);
|
||||
|
||||
/**
|
||||
* Reads the currently configured sample rate of the thermometer.
|
||||
* @return The time between samples, in milliseconds.
|
||||
*/
|
||||
int getPeriod();
|
||||
|
||||
/**
|
||||
* Gets the current temperature of the microbit.
|
||||
* @return the current temperature, in degrees celsius.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* uBit.thermometer.getTemperature();
|
||||
* @endcode
|
||||
*/
|
||||
int getTemperature();
|
||||
|
||||
/**
|
||||
* Periodic callback from MicroBit idle thread.
|
||||
* Check if any data is ready for reading by checking the interrupt.
|
||||
*/
|
||||
virtual void idleTick();
|
||||
|
||||
/**
|
||||
* Indicates if we'd like some processor time to sense the temperature. 0 means we're not due to read the tmeperature yet.
|
||||
* @returns 1 if we'd like some processor time, 0 otherwise.
|
||||
*/
|
||||
virtual int isIdleCallbackNeeded();
|
||||
|
||||
private:
|
||||
|
||||
/**
|
||||
* Determines if we're due to take another temeoratur reading
|
||||
* @return 1 if we're due to take a temperature reading, 0 otherwise.
|
||||
*/
|
||||
int isSampleNeeded();
|
||||
|
||||
/**
|
||||
* Updates our recorded temeprature from the many sensors on the micro:bit!
|
||||
*/
|
||||
void updateTemperature();
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif
|
@ -0,0 +1,136 @@
|
||||
#include "MicroBit.h"
|
||||
#include "nrf_soc.h"
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Create new object that can sense temperature.
|
||||
* @param id the ID of the new MicroBitThermometer object.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* thermometer(MICROBIT_ID_THERMOMETER);
|
||||
* @endcode
|
||||
*
|
||||
* Possible Events:
|
||||
* @code
|
||||
* MICROBIT_THERMOMETER_EVT_CHANGED
|
||||
* @endcode
|
||||
*/
|
||||
MicroBitThermometer::MicroBitThermometer(uint16_t id)
|
||||
{
|
||||
this->id = id;
|
||||
this->samplePeriod = MICROBIT_THERMOMETER_PERIOD;
|
||||
this->sampleTime = 0;
|
||||
|
||||
uBit.addIdleComponent(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current temperature of the microbit.
|
||||
* @return the current temperature, in degrees celsius.
|
||||
*
|
||||
* Example:
|
||||