fork
Dependencies: BLE_API mbed-dev-bin nRF51822
Revision 74:06ddfe5ccbea, committed 2022-11-04
- Comitter:
- leejinRMX
- Date:
- Fri Nov 04 18:30:19 2022 +0000
- Parent:
- 73:eb91bba49623
- Commit message:
- add blutooth service
Changed in this revision
--- a/inc/bluetooth/MicroBitBLEManager.h Wed Jul 13 14:32:54 2016 +0000 +++ b/inc/bluetooth/MicroBitBLEManager.h Fri Nov 04 18:30:19 2022 +0000 @@ -52,6 +52,7 @@ #include "MicroBitDFUService.h" #include "MicroBitEventService.h" #include "MicroBitLEDService.h" +#include "MicroBitValueService.h" #include "MicroBitAccelerometerService.h" #include "MicroBitMagnetometerService.h" #include "MicroBitButtonService.h"
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/inc/bluetooth/MicroBitValueService.h Fri Nov 04 18:30:19 2022 +0000 @@ -0,0 +1,81 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 British Broadcasting Corporation. +This software is provided by Lancaster University by arrangement with the BBC. + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. +*/ + +#ifndef MICROBIT_VALUE_SERVICE_H +#define MICROBIT_VALUE_SERVICE_H + +#include "ble/BLE.h" +#include "MicroBitConfig.h" +#include "EventModel.h" + +#define MICROBIT_VALUE_EVT_UPDATE 1 + +// UUIDs for our service and characteristics +extern const uint8_t MicroBitValueServiceUUID[]; +extern const uint8_t MicroBitValueServiceDataUUID[]; +extern const uint8_t MicroBitValueServiceBearingUUID[]; +extern const uint8_t MicroBitValueServicePeriodUUID[]; + + +/** + * Class definition for the MicroBit BLE Value Service. + * Provides access to live data via BLE, and provides basic configuration options. + */ +class MicroBitValueService +{ + public: + + /** + * Constructor. + * Create a representation of the MagnetometerService. + * @param _ble The instance of a BLE device that we're running on. + * @param _value An instance of MicroBitCompass to use as our Magnetometer source. + */ + MicroBitValueService(BLEDevice &_ble, uint16_t *_value); + + private: + + /** + * Callback. Invoked when any of our attributes are written via BLE. + */ + void onDataWritten(const GattWriteCallbackParams *params); + + /** + * Value update callback + */ + void valueUpdate(MicroBitEvent e); + + // Bluetooth stack we're running on. + BLEDevice &ble; + uint16_t *value; + + // memory for our 8 bit control characteristics. + uint16_t valueCharacteristicBuffer; + + // Handles to access each characteristic when they are held by Soft Device. + GattAttribute::Handle_t valueCharacteristicHandle; +}; + +#endif
--- a/inc/core/MicroBitComponent.h Wed Jul 13 14:32:54 2016 +0000 +++ b/inc/core/MicroBitComponent.h Fri Nov 04 18:30:19 2022 +0000 @@ -68,6 +68,8 @@ #define MICROBIT_ID_MULTIBUTTON_ATTACH 31 #define MICROBIT_ID_SERIAL 32 +#define MICROBIT_ID_VALUE 40 + #define MICROBIT_ID_MESSAGE_BUS_LISTENER 1021 // Message bus indication that a handler for a given ID has been registered. #define MICROBIT_ID_NOTIFY_ONE 1022 // Notfication channel, for general purpose synchronisation #define MICROBIT_ID_NOTIFY 1023 // Notfication channel, for general purpose synchronisation
--- a/inc/core/MicroBitConfig.h Wed Jul 13 14:32:54 2016 +0000 +++ b/inc/core/MicroBitConfig.h Fri Nov 04 18:30:19 2022 +0000 @@ -101,7 +101,7 @@ // For standard S110 builds, this should be word aligned and in the range 0x300 - 0x700. // Any unused memory will be automatically reclaimed as HEAP memory if both MICROBIT_HEAP_REUSE_SD and MICROBIT_HEAP_ALLOCATOR are enabled. #ifndef MICROBIT_SD_GATT_TABLE_SIZE -#define MICROBIT_SD_GATT_TABLE_SIZE 0x300 +#define MICROBIT_SD_GATT_TABLE_SIZE 0x500 #endif // @@ -187,7 +187,7 @@ // Open BLE links are not secure, but commonly used during the development of BLE services // Set '1' to disable all secuity #ifndef MICROBIT_BLE_OPEN -#define MICROBIT_BLE_OPEN 0 +#define MICROBIT_BLE_OPEN 1 #endif // Configure for open BLE operation if so configured @@ -350,7 +350,7 @@ // n.b. This also disables the user serial port 'uBit.serial'. // Set '1' to enable. #ifndef MICROBIT_DBG -#define MICROBIT_DBG 0 +#define MICROBIT_DBG 1 #endif // Enable this to receive diagnostic messages from the heap allocator via the USB serial interface.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/inc/drivers/MicroBitValue.h Fri Nov 04 18:30:19 2022 +0000 @@ -0,0 +1,165 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 British Broadcasting Corporation. +This software is provided by Lancaster University by arrangement with the BBC. + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. +*/ + +#ifndef MICROBIT_VALUE_H +#define MICROBIT_VALUE_H + +#include "mbed.h" +#include "MicroBitConfig.h" +#include "MicroBitComponent.h" +#include "MicroBitStorage.h" + +/** + * Value events + */ +#define MICROBIT_VALUE_EVT_DATA_UPDATE 1 + +struct ValueSample +{ + int v; + + ValueSample() + { + this->v = 0; + } + + ValueSample(int v) + { + this->v = v; + } + + bool operator==(const ValueSample& other) const + { + return v == other.v; + } + + bool operator!=(const ValueSample& other) const + { + return !(v == other.v); + } +}; + +/** + * Class definition for MicroBit Value. + * + * Represents an implementation of shared value. + * Also includes basic caching. + */ +class MicroBitValue : public MicroBitComponent +{ + + ValueSample sample; // The latest sample data recorded. + MicroBitStorage* storage; // An instance of MicroBitStorage used for persistence. + + public: + + /** + * Constructor. + * Create a software representation of an e-value. + * + * + * @param _storage an instance of MicroBitStorage, used to persist calibration data across resets. + * + * @param id the ID of the new MicroBitCompass object. Defaults to MICROBIT_ID_VALUE. + * + * @code + * + * MicroBitStorage storage; + * + * MicroBitValue uBitValue(storage); + * @endcode + */ + MicroBitValue(MicroBitStorage& _storage, uint16_t id = MICROBIT_ID_VALUE); + + /** + * Constructor. + * Create a software representation of an e-value. + * + * @param id the ID of the new MicroBitCompass object. Defaults to MICROBIT_ID_VALUE. + * + * @code + * MicroBitI2C i2c(I2C_SDA0, I2C_SCL0); + * + * MicroBitCompass compass(i2c); + * @endcode + */ + MicroBitValue(uint16_t id = MICROBIT_ID_VALUE); + + /** + * Reads the value of the V integer. + * + * @return The value V. + * + * @code + * value.getV(); + * @endcode + */ + int getV(); + + /** + * Set the value of the V integer. + * + * @param The value V. + * + * @code + * value.setV(v); + * @endcode + */ + void setV(int v); + + /** + * Updates the local sample, only if the compass indicates that + * data is stale. + * + * @note Can be used to trigger manual updates, if the device is running without a scheduler. + * Also called internally by getV() member functions. + */ + int updateSample(); + + /** + * Periodic callback from MicroBit idle thread. + * + * Calls updateSample(). + */ + virtual void idleTick(); + + + /** + * Destructor for MicroBitCompass, where we deregister this instance from the array of fiber components. + */ + ~MicroBitValue(); + + private: + + /** + * An initialisation member function used by the many constructors of MicroBitValue. + * + * @param id the unique identifier for this value instance. + * + */ + void init(uint16_t id); +}; + +#endif \ No newline at end of file
--- a/source/CMakeLists.txt Wed Jul 13 14:32:54 2016 +0000 +++ b/source/CMakeLists.txt Fri Nov 04 18:30:19 2022 +0000 @@ -52,6 +52,7 @@ "bluetooth/MicroBitMagnetometerService.cpp" "bluetooth/MicroBitTemperatureService.cpp" "bluetooth/MicroBitUARTService.cpp" + "bluetooth/MicroBitValueService.cpp" ) execute_process(WORKING_DIRECTORY "../../yotta_modules/${PROJECT_NAME}" COMMAND "git" "log" "--pretty=format:%h" "-n" "1" OUTPUT_VARIABLE git_hash)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/source/bluetooth/MicroBitValueService.cpp Fri Nov 04 18:30:19 2022 +0000 @@ -0,0 +1,122 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 British Broadcasting Corporation. +This software is provided by Lancaster University by arrangement with the BBC. + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. +*/ + +/** + * Class definition for the MicroBit BLE Magnetometer Service. + * Provides access to live magnetometer data via BLE, and provides basic configuration options. + */ +#include "MicroBitConfig.h" +#include "ble/UUID.h" + +#include "MicroBitValueService.h" + +/** + * Constructor. + * Create a representation of the MagnetometerService. + * @param _ble The instance of a BLE device that we're running on. + * @param _compass An instance of MicroBitCompass to use as our Magnetometer source. + */ +MicroBitValueService::MicroBitValueService(BLEDevice &_ble, uint16_t *_value) : + ble(_ble), value(_value) +{ +#if CONFIG_ENABLED(MICROBIT_DBG) + if(SERIAL_DEBUG) SERIAL_DEBUG->printf("MicroBitValueService::MicroBitValueService value = %i\r\n", *value); +#endif + // Create the data structures that represent each of our characteristics in Soft Device. + + GattCharacteristic valueCharacteristic(MicroBitValueServiceDataUUID, (uint8_t *)&valueCharacteristicBuffer, 0, + sizeof(valueCharacteristicBuffer), + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); + + // Initialise our characteristic values. + valueCharacteristicBuffer = *value; + + // Set default security requirements + valueCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL); + + GattCharacteristic *characteristics[] = {&valueCharacteristic}; + GattService service(MicroBitValueServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *)); + + ble.addService(service); + + valueCharacteristicHandle = valueCharacteristic.getValueHandle(); + + ble.gattServer().write(valueCharacteristicHandle, (const uint8_t *)&valueCharacteristicBuffer, sizeof(valueCharacteristicBuffer)); + + ble.gattServer().onDataWritten(this, &MicroBitValueService::onDataWritten); + + if (EventModel::defaultEventBus) + { + EventModel::defaultEventBus->listen(MICROBIT_ID_VALUE, MICROBIT_VALUE_EVT_UPDATE, this, &MicroBitValueService::valueUpdate, MESSAGE_BUS_LISTENER_IMMEDIATE); + } +} + +/** + * Callback. Invoked when any of our attributes are written via BLE. + */ +void MicroBitValueService::onDataWritten(const GattWriteCallbackParams *params) +{ + + if (params->handle == valueCharacteristicHandle && params->len >= sizeof(valueCharacteristicBuffer)) + { + valueCharacteristicBuffer = *((uint16_t *)params->data); + *value = valueCharacteristicBuffer; + //MicroBitEvent e(41, MICROBIT_VALUE_EVT_UPDATE); + } +} + +/** + * Magnetometer update callback + */ +void MicroBitValueService::valueUpdate(MicroBitEvent) +{ + +#if CONFIG_ENABLED(MICROBIT_DBG) + if(SERIAL_DEBUG) SERIAL_DEBUG->printf("MicroBitValueService::valueUpdate\r\n"); +#endif + + if (ble.getGapState().connected) + { + valueCharacteristicBuffer = *value; + ble.gattServer().write(valueCharacteristicHandle, (const uint8_t *)&valueCharacteristicBuffer, sizeof(valueCharacteristicBuffer)); + } + +} + +const uint8_t MicroBitValueServiceUUID[] = { + 0xab,0x05,0xf2,0xd8,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8 +}; + +const uint8_t MicroBitValueServiceDataUUID[] = { + 0xab,0x05,0xfb,0x11,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8 +}; + +const uint8_t MicroBitValueServicePeriodUUID[] = { + 0xab,0x05,0x38,0x6c,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8 +}; + +const uint8_t MicroBitValueServiceBearingUUID[] = { + 0xab,0x05,0x97,0x15,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8 +}; \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/source/drivers/MicroBitValue.cpp Fri Nov 04 18:30:19 2022 +0000 @@ -0,0 +1,188 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 British Broadcasting Corporation. +This software is provided by Lancaster University by arrangement with the BBC. + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. +*/ + +/** + * Class definition for MicroBit Value. + * + * Represents an implementation of the Freescale MAG3110 I2C Magnetmometer. + * Also includes basic caching. + */ +#include "MicroBitConfig.h" +#include "MicroBitValue.h" +#include "MicroBitFiber.h" +#include "ErrorNo.h" + +/** + * An initialisation member function used by the many constructors of MicroBitValue. + * + * @param id the unique identifier for this value instance. + * + */ +void MicroBitValue::init(uint16_t id) +{ + this->id = id; + + if(this->storage != NULL) + { + /* + KeyValuePair *calibrationData = storage->get("compassCal"); + + if(calibrationData != NULL) + { + CompassSample storedSample = CompassSample(); + + memcpy(&storedSample, calibrationData->value, sizeof(CompassSample)); + + setCalibration(storedSample); + + delete calibrationData; + } + */ + } + + // Indicate that we're up and running. + status |= MICROBIT_COMPONENT_RUNNING; +} + + + +/** + * Constructor. + * Create a software representation of an e-compass. + * + * @param _i2c an instance of i2c, which the compass is accessible from. + * + * @param _storage an instance of MicroBitStorage, used to persist calibration data across resets. + * + * @param address the default address for the compass register on the i2c bus. Defaults to MAG3110_DEFAULT_ADDR. + * + * @param id the ID of the new MicroBitCompass object. Defaults to MAG3110_DEFAULT_ADDR. + * + * @code + * MicroBitI2C i2c(I2C_SDA0, I2C_SCL0); + * + * MicroBitStorage storage; + * + * MicroBitCompass compass(i2c, storage); + * @endcode + */ +MicroBitValue::MicroBitValue(MicroBitStorage& _storage, uint16_t id) : + sample(), + storage(&_storage) +{ + init(id); +} + +/** + * Constructor. + * Create a software representation of an e-compass. + * + * @param _i2c an instance of i2c, which the compass is accessible from. + * + * @param address the default address for the compass register on the i2c bus. Defaults to MAG3110_DEFAULT_ADDR. + * + * @param id the ID of the new MicroBitCompass object. Defaults to MAG3110_DEFAULT_ADDR. + * + * @code + * MicroBitI2C i2c(I2C_SDA0, I2C_SCL0); + * + * MicroBitCompass compass(i2c); + * @endcode + */ +MicroBitValue::MicroBitValue(uint16_t id) : + sample(), + storage(NULL) +{ + init(id); +} + +/** + * Updates the local sample, only if the compass indicates that + * data is stale. + * + * @note Can be used to trigger manual updates, if the device is running without a scheduler. + * Also called internally by all get[X,Y,Z]() member functions. + */ +int MicroBitValue::updateSample() +{ + /** + * Adds the compass to idle, if it hasn't been added already. + * This is an optimisation so that the compass is only added on first 'use'. + */ + if(!(status & 0x01)) + { + fiber_add_idle_component(this); + status |= 0x01; + } + + return MICROBIT_OK; +} + +/** + * Periodic callback from MicroBit idle thread. + * + * Calls updateSample(). + */ +void MicroBitValue::idleTick() +{ + updateSample(); +} + +/** + * Attempts to set the sample rate of the compass to the specified value (in ms). + * + * @param period the requested time between samples, in milliseconds. + * + * @return MICROBIT_OK or MICROBIT_I2C_ERROR if the magnetometer could not be updated. + * + * @code + * // sample rate is now 20 ms. + * compass.setPeriod(20); + * @endcode + * + * @note The requested rate may not be possible on the hardware. In this case, the + * nearest lower rate is chosen. + */ +void MicroBitValue::setV(int v) +{ + this->sample.v = v; +} + +/** + * Reads the currently configured sample rate of the compass. + * + * @return The time between samples, in milliseconds. + */ +int MicroBitValue::getV() +{ + return (int)sample.v; +} +/** + * Destructor for MicroBitCompass, where we deregister this instance from the array of fiber components. + */ +MicroBitValue::~MicroBitValue() +{ + fiber_remove_idle_component(this); +}