@ -3,12 +3,11 @@
char MICROBIT_BLE_DEVICE_NAME [ ] = " BBC micro:bit [xxxxx] " ;
# if CONFIG_ENABLED(MICROBIT_BLE_ENABLED) && CONFIG_ENABLED(MICROBIT_BLE_DEVICE_INFORMATION_SERVICE)
const char MICROBIT_BLE_MANUFACTURER [ ] = " The Cast of W1A " ;
const char MICROBIT_BLE_MODEL [ ] = " micro:bit " ;
const char MICROBIT_BLE_SERIAL [ ] = " SN1 " ;
const char MICROBIT_BLE_HARDWARE_VERSION [ ] = " 0.2 " ;
const char MICROBIT_BLE_FIRMWARE_VERSION [ ] = " 1.1 " ;
const char MICROBIT_BLE_SOFTWARE_VERSION [ ] = " 1.0 " ;
const char * MICROBIT_BLE_MANUFACTURER = " The Cast of W1A " ;
const char * MICROBIT_BLE_MODEL = " BBC micro:bit " ;
const char * MICROBIT_BLE_HARDWARE_VERSION = " 1.0 " ;
const char * MICROBIT_BLE_FIRMWARE_VERSION = " 1.1 " ;
const char * MICROBIT_BLE_SOFTWARE_VERSION = NULL ;
# endif
/**
@ -94,59 +93,65 @@ void MicroBit::init()
// Seed our random number generator
seedRandom ( ) ;
// Generate the name for our device.
this - > deriveName ( ) ;
# if CONFIG_ENABLED(MICROBIT_BLE_ENABLED)
// Start the BLE stack.
ble = new BLEDevice ( ) ;
ble - > init ( ) ;
ble - > onDisconnection ( bleDisconnectionCallback ) ;
// Bring up any configured auxiliary services.
# if CONFIG_ENABLED(MICROBIT_BLE_DFU_SERVICE)
ble_firmware_update_service = new MicroBitDFUService ( * ble ) ;
// Compute our auto-generated MicroBit device name.
ble_firmware_update_service - > getName ( MICROBIT_BLE_DEVICE_NAME + 15 ) ;
# endif
# if CONFIG_ENABLED(MICROBIT_BLE_DEVICE_INFORMATION_SERVICE)
DeviceInformationService ble_device_information_service ( * ble , MICROBIT_BLE_MANUFACTURER , MICROBIT_BLE_MODEL , MICROBIT_BLE_SERIAL , MICROBIT_BLE_HARDWARE_VERSION , MICROBIT_BLE_FIRMWARE_VERSION , MICROBIT_BLE_SOFTWARE_VERSION ) ;
DeviceInformationService ble_device_information_service ( * ble , MICROBIT_BLE_MANUFACTURER , MICROBIT_BLE_MODEL , getSerial( ) . toCharArray ( ) , MICROBIT_BLE_HARDWARE_VERSION , MICROBIT_BLE_FIRMWARE_VERSION , MICROBIT_BLE_SOFTWARE_VERSION ) ;
# endif
# if CONFIG_ENABLED(MICROBIT_BLE_EVENT_SERVICE)
ble_event_service = new MicroBitEventService( * ble ) ;
new MicroBitEventService( * ble ) ;
# endif
# if CONFIG_ENABLED(MICROBIT_BLE_LED_SERVICE)
ble_led_service = new MicroBitLEDService( * ble ) ;
new MicroBitLEDService( * ble ) ;
# endif
# if CONFIG_ENABLED(MICROBIT_BLE_ACCELEROMETER_SERVICE)
ble_accelerometer_service = new MicroBitAccelerometerService( * ble ) ;
new MicroBitAccelerometerService( * ble ) ;
# endif
# if CONFIG_ENABLED(MICROBIT_BLE_MAGNETOMETER_SERVICE)
ble_magnetometer_service = new MicroBitMagnetometerService( * ble ) ;
new MicroBitMagnetometerService( * ble ) ;
# endif
# if CONFIG_ENABLED(MICROBIT_BLE_BUTTON_SERVICE)
ble_button_service = new MicroBitButtonService( * ble ) ;
new MicroBitButtonService( * ble ) ;
# endif
# if CONFIG_ENABLED(MICROBIT_BLE_IO_PIN_SERVICE)
ble_io_pin_service = new MicroBitIOPinService( * ble ) ;
new MicroBitIOPinService( * ble ) ;
# endif
# if CONFIG_ENABLED(MICROBIT_BLE_TEMPERATURE_SERVICE)
ble_temperature_service = new MicroBitTemperatureService( * ble ) ;
new MicroBitTemperatureService( * ble ) ;
# endif
// Configure for high speed mode where possible.
Gap : : ConnectionParams_t fast ;
ble - > getPreferredConnectionParams ( & fast ) ;
fast . minConnectionInterval = 8 ; // 10 ms
fast . maxConnectionInterval = 16 ; // 20 ms
fast . slaveLatency = 0 ;
ble - > setPreferredConnectionParams ( & fast ) ;
// Setup advertising.
ble - > accumulateAdvertisingPayload ( GapAdvertisingData : : BREDR_NOT_SUPPORTED | GapAdvertisingData : : LE_GENERAL_DISCOVERABLE ) ;
ble - > accumulateAdvertisingPayload ( GapAdvertisingData : : COMPLETE_LOCAL_NAME , ( uint8_t * ) MICROBIT_BLE_DEVICE_NAME , sizeof ( MICROBIT_BLE_DEVICE_NAME ) ) ;
ble - > setAdvertisingType ( GapAdvertisingParams : : ADV_CONNECTABLE_UNDIRECTED ) ;
ble - > setAdvertisingInterval ( Gap : : MSEC_TO_ADVERTISEMENT_DURATION_UNITS ( 1000 ) ) ;
ble - > setAdvertisingInterval ( Gap : : MSEC_TO_ADVERTISEMENT_DURATION_UNITS ( 2 00) ) ;
ble - > startAdvertising ( ) ;
# endif
@ -154,6 +159,69 @@ void MicroBit::init()
systemTicker . attach ( this , & MicroBit : : systemTick , MICROBIT_DISPLAY_REFRESH_PERIOD ) ;
}
/**
* Derives the friendly name for this device , autogenerated from our hardware Device ID .
*/
void MicroBit : : deriveName ( )
{
const uint8_t codebook [ MICROBIT_NAME_LENGTH ] [ MICROBIT_NAME_CODE_LETTERS ] =
{
{ ' z ' , ' v ' , ' g ' , ' p ' , ' t ' } ,
{ ' u ' , ' o ' , ' i ' , ' e ' , ' a ' } ,
{ ' z ' , ' v ' , ' g ' , ' p ' , ' t ' } ,
{ ' u ' , ' o ' , ' i ' , ' e ' , ' a ' } ,
{ ' z ' , ' v ' , ' g ' , ' p ' , ' t ' }
} ;
char * name = MICROBIT_BLE_DEVICE_NAME + 15 ;
// We count right to left, so fast forward the pointer.
name + = MICROBIT_NAME_LENGTH ;
uint32_t n = NRF_FICR - > DEVICEID [ 1 ] ;
int ld = 1 ;
int d = MICROBIT_NAME_CODE_LETTERS ;
int h ;
for ( int i = 0 ; i < MICROBIT_NAME_LENGTH ; i + + )
{
h = ( n % d ) / ld ;
n - = h ;
d * = MICROBIT_NAME_CODE_LETTERS ;
ld * = MICROBIT_NAME_CODE_LETTERS ;
* - - name = codebook [ i ] [ h ] ;
}
}
/**
* Return the friendly name for this device .
*
* @ return A string representing the friendly name of this device .
*/
ManagedString MicroBit : : getName ( )
{
return ManagedString ( MICROBIT_BLE_DEVICE_NAME + 15 , MICROBIT_NAME_LENGTH ) ;
}
/**
* Return the serial number of this device .
*
* @ return A string representing the serial number of this device .
*/
ManagedString MicroBit : : getSerial ( )
{
// We take to 16 bit numbers here, as we want the full range of ID bits, but don't want negative numbers...
int n1 = NRF_FICR - > DEVICEID [ 1 ] & 0xffff ;
int n2 = ( NRF_FICR - > DEVICEID [ 1 ] > > 16 ) & 0xffff ;
// Simply concat the two numbers.
ManagedString s1 = ManagedString ( n1 ) ;
ManagedString s2 = ManagedString ( n2 ) ;
return s1 + s2 ;
}
/**
* Will reset the micro : bit when called .
*