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.
This commit is contained in:
Joe Finney 2016-01-16 17:07:20 +00:00
parent f17151a0bf
commit 07852b0cd6
2 changed files with 30 additions and 2 deletions

View file

@ -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.

View file

@ -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.