From 07852b0cd66ba0b54abdbabb9b1eda902884c115 Mon Sep 17 00:00:00 2001 From: Joe Finney Date: Sat, 16 Jan 2016 17:07:20 +0000 Subject: [PATCH] microbit: Added support for BLE transmission power control Added new method to MicroBitBLEManager to allow the radio transmission power to be controlled. Provides a linear power level scale in the range 0..7. --- inc/MicroBitBLEManager.h | 9 +++++++++ source/ble-services/MicroBitBLEManager.cpp | 23 ++++++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/inc/MicroBitBLEManager.h b/inc/MicroBitBLEManager.h index c4169e7..baab621 100644 --- a/inc/MicroBitBLEManager.h +++ b/inc/MicroBitBLEManager.h @@ -74,6 +74,15 @@ class MicroBitBLEManager */ void init(ManagedString deviceName, ManagedString serialNumber); + /** + * Change the output power level of the transmitter to the given value. + * + * @param power a value in the range 0..7, where 0 is the lowest power and 7 is the highest. + * @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER if the value is out of range. + * + */ + int setTransmitPower(int power); + /** * Enter pairing mode. This is mode is called to initiate pairing, and to enable FOTA programming * of the micro:bit in cases where BLE is disabled during normal operation. diff --git a/source/ble-services/MicroBitBLEManager.cpp b/source/ble-services/MicroBitBLEManager.cpp index 9596cd8..8356994 100644 --- a/source/ble-services/MicroBitBLEManager.cpp +++ b/source/ble-services/MicroBitBLEManager.cpp @@ -25,6 +25,7 @@ #define MICROBIT_BLE_REQUIRE_MITM true #define MICROBIT_PAIRING_FADE_SPEED 4 +#define MICROBIT_BLE_POWER_LEVELS 8 const char* MICROBIT_BLE_MANUFACTURER = "The Cast of W1A"; @@ -32,7 +33,7 @@ const char* MICROBIT_BLE_MODEL = "BBC micro:bit"; const char* MICROBIT_BLE_HARDWARE_VERSION = "1.0"; const char* MICROBIT_BLE_FIRMWARE_VERSION = MICROBIT_DAL_VERSION; const char* MICROBIT_BLE_SOFTWARE_VERSION = NULL; - +const int8_t MICROBIT_BLE_POWER_LEVEL[] = {-30, -20, -16, -12, -8, -4, 0, 4}; /* * Many of the mbed interfaces we need to use only support callbacks to plain C functions, rather than C++ methods. @@ -220,9 +221,27 @@ void MicroBitBLEManager::init(ManagedString deviceName, ManagedString serialNumb if (whitelist.size > 0) #endif ble->startAdvertising(); - } +/** + * Change the output power level of the transmitter to the given value. + * + * @param power a value in the range 0..7, where 0 is the lowest power and 7 is the highest. + * @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER if the value is out of range. + * + */ +int MicroBitBLEManager::setTransmitPower(int power) +{ + if (power < 0 || power >= MICROBIT_BLE_POWER_LEVELS) + return MICROBIT_INVALID_PARAMETER; + + if (ble->gap().setTxPower(MICROBIT_BLE_POWER_LEVEL[power]) != NRF_SUCCESS) + return MICROBIT_NOT_SUPPORTED; + + return MICROBIT_OK; +} + + /** * A request to pair has been received from a BLE device. * If we're in pairing mode, display the passkey to the user.