Official Sheffield ARMBand micro:bit program

Committer:
MrBedfordVan
Date:
Mon Oct 17 12:41:20 2016 +0000
Revision:
0:b9164b348919
Official Sheffield ARMBand Micro:bit program

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MrBedfordVan 0:b9164b348919 1 /*
MrBedfordVan 0:b9164b348919 2 The MIT License (MIT)
MrBedfordVan 0:b9164b348919 3
MrBedfordVan 0:b9164b348919 4 Copyright (c) 2016 British Broadcasting Corporation.
MrBedfordVan 0:b9164b348919 5 This software is provided by Lancaster University by arrangement with the BBC.
MrBedfordVan 0:b9164b348919 6
MrBedfordVan 0:b9164b348919 7 Permission is hereby granted, free of charge, to any person obtaining a
MrBedfordVan 0:b9164b348919 8 copy of this software and associated documentation files (the "Software"),
MrBedfordVan 0:b9164b348919 9 to deal in the Software without restriction, including without limitation
MrBedfordVan 0:b9164b348919 10 the rights to use, copy, modify, merge, publish, distribute, sublicense,
MrBedfordVan 0:b9164b348919 11 and/or sell copies of the Software, and to permit persons to whom the
MrBedfordVan 0:b9164b348919 12 Software is furnished to do so, subject to the following conditions:
MrBedfordVan 0:b9164b348919 13
MrBedfordVan 0:b9164b348919 14 The above copyright notice and this permission notice shall be included in
MrBedfordVan 0:b9164b348919 15 all copies or substantial portions of the Software.
MrBedfordVan 0:b9164b348919 16
MrBedfordVan 0:b9164b348919 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
MrBedfordVan 0:b9164b348919 18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
MrBedfordVan 0:b9164b348919 19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
MrBedfordVan 0:b9164b348919 20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
MrBedfordVan 0:b9164b348919 21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
MrBedfordVan 0:b9164b348919 22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
MrBedfordVan 0:b9164b348919 23 DEALINGS IN THE SOFTWARE.
MrBedfordVan 0:b9164b348919 24 */
MrBedfordVan 0:b9164b348919 25
MrBedfordVan 0:b9164b348919 26 /**
MrBedfordVan 0:b9164b348919 27 * Class definition for the MicroBit BLE Magnetometer Service.
MrBedfordVan 0:b9164b348919 28 * Provides access to live magnetometer data via BLE, and provides basic configuration options.
MrBedfordVan 0:b9164b348919 29 */
MrBedfordVan 0:b9164b348919 30 #include "MicroBitConfig.h"
MrBedfordVan 0:b9164b348919 31 #include "ble/UUID.h"
MrBedfordVan 0:b9164b348919 32
MrBedfordVan 0:b9164b348919 33 #include "MicroBitMagnetometerService.h"
MrBedfordVan 0:b9164b348919 34
MrBedfordVan 0:b9164b348919 35 /**
MrBedfordVan 0:b9164b348919 36 * Constructor.
MrBedfordVan 0:b9164b348919 37 * Create a representation of the MagnetometerService.
MrBedfordVan 0:b9164b348919 38 * @param _ble The instance of a BLE device that we're running on.
MrBedfordVan 0:b9164b348919 39 * @param _compass An instance of MicroBitCompass to use as our Magnetometer source.
MrBedfordVan 0:b9164b348919 40 */
MrBedfordVan 0:b9164b348919 41 MicroBitMagnetometerService::MicroBitMagnetometerService(BLEDevice &_ble, MicroBitCompass &_compass) :
MrBedfordVan 0:b9164b348919 42 ble(_ble), compass(_compass)
MrBedfordVan 0:b9164b348919 43 {
MrBedfordVan 0:b9164b348919 44 // Create the data structures that represent each of our characteristics in Soft Device.
MrBedfordVan 0:b9164b348919 45 GattCharacteristic magnetometerDataCharacteristic(MicroBitMagnetometerServiceDataUUID, (uint8_t *)magnetometerDataCharacteristicBuffer, 0,
MrBedfordVan 0:b9164b348919 46 sizeof(magnetometerDataCharacteristicBuffer), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
MrBedfordVan 0:b9164b348919 47
MrBedfordVan 0:b9164b348919 48 GattCharacteristic magnetometerBearingCharacteristic(MicroBitMagnetometerServiceBearingUUID, (uint8_t *)&magnetometerBearingCharacteristicBuffer, 0,
MrBedfordVan 0:b9164b348919 49 sizeof(magnetometerBearingCharacteristicBuffer), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
MrBedfordVan 0:b9164b348919 50
MrBedfordVan 0:b9164b348919 51 GattCharacteristic magnetometerPeriodCharacteristic(MicroBitMagnetometerServicePeriodUUID, (uint8_t *)&magnetometerPeriodCharacteristicBuffer, 0,
MrBedfordVan 0:b9164b348919 52 sizeof(magnetometerPeriodCharacteristicBuffer),
MrBedfordVan 0:b9164b348919 53 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE);
MrBedfordVan 0:b9164b348919 54
MrBedfordVan 0:b9164b348919 55 // Initialise our characteristic values.
MrBedfordVan 0:b9164b348919 56 magnetometerDataCharacteristicBuffer[0] = 0;
MrBedfordVan 0:b9164b348919 57 magnetometerDataCharacteristicBuffer[1] = 0;
MrBedfordVan 0:b9164b348919 58 magnetometerDataCharacteristicBuffer[2] = 0;
MrBedfordVan 0:b9164b348919 59 magnetometerBearingCharacteristicBuffer = 0;
MrBedfordVan 0:b9164b348919 60 magnetometerPeriodCharacteristicBuffer = compass.getPeriod();
MrBedfordVan 0:b9164b348919 61
MrBedfordVan 0:b9164b348919 62 // Set default security requirements
MrBedfordVan 0:b9164b348919 63 magnetometerDataCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL);
MrBedfordVan 0:b9164b348919 64 magnetometerBearingCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL);
MrBedfordVan 0:b9164b348919 65 magnetometerPeriodCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL);
MrBedfordVan 0:b9164b348919 66
MrBedfordVan 0:b9164b348919 67 GattCharacteristic *characteristics[] = {&magnetometerDataCharacteristic, &magnetometerBearingCharacteristic, &magnetometerPeriodCharacteristic};
MrBedfordVan 0:b9164b348919 68 GattService service(MicroBitMagnetometerServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
MrBedfordVan 0:b9164b348919 69
MrBedfordVan 0:b9164b348919 70 ble.addService(service);
MrBedfordVan 0:b9164b348919 71
MrBedfordVan 0:b9164b348919 72 magnetometerDataCharacteristicHandle = magnetometerDataCharacteristic.getValueHandle();
MrBedfordVan 0:b9164b348919 73 magnetometerBearingCharacteristicHandle = magnetometerBearingCharacteristic.getValueHandle();
MrBedfordVan 0:b9164b348919 74 magnetometerPeriodCharacteristicHandle = magnetometerPeriodCharacteristic.getValueHandle();
MrBedfordVan 0:b9164b348919 75
MrBedfordVan 0:b9164b348919 76 ble.gattServer().notify(magnetometerDataCharacteristicHandle,(uint8_t *)magnetometerDataCharacteristicBuffer, sizeof(magnetometerDataCharacteristicBuffer));
MrBedfordVan 0:b9164b348919 77 ble.gattServer().notify(magnetometerBearingCharacteristicHandle,(uint8_t *)&magnetometerBearingCharacteristicBuffer, sizeof(magnetometerBearingCharacteristicBuffer));
MrBedfordVan 0:b9164b348919 78 ble.gattServer().write(magnetometerPeriodCharacteristicHandle, (const uint8_t *)&magnetometerPeriodCharacteristicBuffer, sizeof(magnetometerPeriodCharacteristicBuffer));
MrBedfordVan 0:b9164b348919 79
MrBedfordVan 0:b9164b348919 80 ble.onDataWritten(this, &MicroBitMagnetometerService::onDataWritten);
MrBedfordVan 0:b9164b348919 81 if (EventModel::defaultEventBus)
MrBedfordVan 0:b9164b348919 82 {
MrBedfordVan 0:b9164b348919 83 EventModel::defaultEventBus->listen(MICROBIT_ID_COMPASS, MICROBIT_COMPASS_EVT_DATA_UPDATE, this, &MicroBitMagnetometerService::magnetometerUpdate, MESSAGE_BUS_LISTENER_IMMEDIATE);
MrBedfordVan 0:b9164b348919 84 EventModel::defaultEventBus->listen(MICROBIT_ID_COMPASS, MICROBIT_COMPASS_EVT_CONFIG_NEEDED, this, &MicroBitMagnetometerService::samplePeriodUpdateNeeded);
MrBedfordVan 0:b9164b348919 85 }
MrBedfordVan 0:b9164b348919 86 }
MrBedfordVan 0:b9164b348919 87
MrBedfordVan 0:b9164b348919 88 /**
MrBedfordVan 0:b9164b348919 89 * Callback. Invoked when any of our attributes are written via BLE.
MrBedfordVan 0:b9164b348919 90 */
MrBedfordVan 0:b9164b348919 91 void MicroBitMagnetometerService::onDataWritten(const GattWriteCallbackParams *params)
MrBedfordVan 0:b9164b348919 92 {
MrBedfordVan 0:b9164b348919 93 if (params->handle == magnetometerPeriodCharacteristicHandle && params->len >= sizeof(magnetometerPeriodCharacteristicBuffer))
MrBedfordVan 0:b9164b348919 94 {
MrBedfordVan 0:b9164b348919 95 magnetometerPeriodCharacteristicBuffer = *((uint16_t *)params->data);
MrBedfordVan 0:b9164b348919 96 MicroBitEvent evt(MICROBIT_ID_COMPASS, MICROBIT_COMPASS_EVT_CONFIG_NEEDED);
MrBedfordVan 0:b9164b348919 97 }
MrBedfordVan 0:b9164b348919 98 }
MrBedfordVan 0:b9164b348919 99
MrBedfordVan 0:b9164b348919 100 /**
MrBedfordVan 0:b9164b348919 101 * Magnetometer update callback
MrBedfordVan 0:b9164b348919 102 */
MrBedfordVan 0:b9164b348919 103 void MicroBitMagnetometerService::magnetometerUpdate(MicroBitEvent)
MrBedfordVan 0:b9164b348919 104 {
MrBedfordVan 0:b9164b348919 105 if (ble.getGapState().connected)
MrBedfordVan 0:b9164b348919 106 {
MrBedfordVan 0:b9164b348919 107 magnetometerDataCharacteristicBuffer[0] = compass.getX();
MrBedfordVan 0:b9164b348919 108 magnetometerDataCharacteristicBuffer[1] = compass.getY();
MrBedfordVan 0:b9164b348919 109 magnetometerDataCharacteristicBuffer[2] = compass.getZ();
MrBedfordVan 0:b9164b348919 110 magnetometerPeriodCharacteristicBuffer = compass.getPeriod();
MrBedfordVan 0:b9164b348919 111
MrBedfordVan 0:b9164b348919 112 ble.gattServer().write(magnetometerPeriodCharacteristicHandle, (const uint8_t *)&magnetometerPeriodCharacteristicBuffer, sizeof(magnetometerPeriodCharacteristicBuffer));
MrBedfordVan 0:b9164b348919 113 ble.gattServer().notify(magnetometerDataCharacteristicHandle,(uint8_t *)magnetometerDataCharacteristicBuffer, sizeof(magnetometerDataCharacteristicBuffer));
MrBedfordVan 0:b9164b348919 114
MrBedfordVan 0:b9164b348919 115 if (compass.isCalibrated())
MrBedfordVan 0:b9164b348919 116 {
MrBedfordVan 0:b9164b348919 117 magnetometerBearingCharacteristicBuffer = (uint16_t) compass.heading();
MrBedfordVan 0:b9164b348919 118 ble.gattServer().notify(magnetometerBearingCharacteristicHandle,(uint8_t *)&magnetometerBearingCharacteristicBuffer, sizeof(magnetometerBearingCharacteristicBuffer));
MrBedfordVan 0:b9164b348919 119 }
MrBedfordVan 0:b9164b348919 120 }
MrBedfordVan 0:b9164b348919 121 }
MrBedfordVan 0:b9164b348919 122
MrBedfordVan 0:b9164b348919 123 /**
MrBedfordVan 0:b9164b348919 124 * Sample Period Change Needed callback.
MrBedfordVan 0:b9164b348919 125 * Reconfiguring the magnetometer can to a REALLY long time (sometimes even seconds to complete)
MrBedfordVan 0:b9164b348919 126 * So we do this in the background when necessary, through this event handler.
MrBedfordVan 0:b9164b348919 127 */
MrBedfordVan 0:b9164b348919 128 void MicroBitMagnetometerService::samplePeriodUpdateNeeded(MicroBitEvent)
MrBedfordVan 0:b9164b348919 129 {
MrBedfordVan 0:b9164b348919 130 // Reconfigure the compass. This might take a while...
MrBedfordVan 0:b9164b348919 131 compass.setPeriod(magnetometerPeriodCharacteristicBuffer);
MrBedfordVan 0:b9164b348919 132
MrBedfordVan 0:b9164b348919 133 // The compass will choose the nearest sample period to that we've specified.
MrBedfordVan 0:b9164b348919 134 // Read the ACTUAL sample period back.
MrBedfordVan 0:b9164b348919 135 magnetometerPeriodCharacteristicBuffer = compass.getPeriod();
MrBedfordVan 0:b9164b348919 136
MrBedfordVan 0:b9164b348919 137 // Ensure this is reflected in our BLE connection.
MrBedfordVan 0:b9164b348919 138 ble.gattServer().write(magnetometerPeriodCharacteristicHandle, (const uint8_t *)&magnetometerPeriodCharacteristicBuffer, sizeof(magnetometerPeriodCharacteristicBuffer));
MrBedfordVan 0:b9164b348919 139
MrBedfordVan 0:b9164b348919 140 }
MrBedfordVan 0:b9164b348919 141
MrBedfordVan 0:b9164b348919 142 const uint8_t MicroBitMagnetometerServiceUUID[] = {
MrBedfordVan 0:b9164b348919 143 0xe9,0x5d,0xf2,0xd8,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8
MrBedfordVan 0:b9164b348919 144 };
MrBedfordVan 0:b9164b348919 145
MrBedfordVan 0:b9164b348919 146 const uint8_t MicroBitMagnetometerServiceDataUUID[] = {
MrBedfordVan 0:b9164b348919 147 0xe9,0x5d,0xfb,0x11,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8
MrBedfordVan 0:b9164b348919 148 };
MrBedfordVan 0:b9164b348919 149
MrBedfordVan 0:b9164b348919 150 const uint8_t MicroBitMagnetometerServicePeriodUUID[] = {
MrBedfordVan 0:b9164b348919 151 0xe9,0x5d,0x38,0x6c,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8
MrBedfordVan 0:b9164b348919 152 };
MrBedfordVan 0:b9164b348919 153
MrBedfordVan 0:b9164b348919 154 const uint8_t MicroBitMagnetometerServiceBearingUUID[] = {
MrBedfordVan 0:b9164b348919 155 0xe9,0x5d,0x97,0x15,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8
MrBedfordVan 0:b9164b348919 156 };