microbit: BUGFIX to MicroBtDFU Service

- Updates to UI handling to avoid starving out the processor.
  - Clears screen before entering DFU bootloader
This commit is contained in:
Joe Finney 2015-10-09 01:14:36 +01:00
parent 122d885ee8
commit 6764c732ea
2 changed files with 43 additions and 33 deletions

View file

@ -92,6 +92,10 @@ class MicroBitDFUService
// Update BLE characteristic to release our flash code.
void releaseFlashCode();
// Event handlers for button clicks.
void onButtonA(MicroBitEvent e);
void onButtonB(MicroBitEvent e);
};
#endif

View file

@ -46,7 +46,7 @@ MicroBitDFUService::MicroBitDFUService(BLEDevice &_ble) :
microBitDFUServiceControlCharacteristicHandle = microBitDFUServiceControlCharacteristic.getValueHandle();
microBitDFUServiceFlashCodeCharacteristicHandle = microBitDFUServiceFlashCodeCharacteristic.getValueHandle();
ble.onDataWritten(this, &MicroBitDFUService::onDataWritten);
ble.gattServer().onDataWritten(this, &MicroBitDFUService::onDataWritten);
}
@ -88,54 +88,54 @@ int MicroBitDFUService::getName(char *name)
return MICROBIT_DFU_HISTOGRAM_WIDTH;
}
void MicroBitDFUService::onButtonA(MicroBitEvent e)
{
if (flashCodeRequested)
{
releaseFlashCode();
uBit.display.scroll("");
showTick();
flashCodeRequested = false;
authenticated = true;
}
}
void MicroBitDFUService::onButtonB(MicroBitEvent e)
{
uBit.display.scroll("VERSION: TODO");
showNameHistogram();
}
/**
* Begin the pairing process. Typically called when device is powered up with buttons held down.
* Scroll a description on the display, then displays the device ID code as a histogram on the matrix display.
*/
void MicroBitDFUService::pair()
{
ManagedString blueZoneString("BLUE ZONE...");
ManagedString pairString("PAIR?");
uBit.display.scroll(blueZoneString);
uBit.MessageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, this, &MicroBitDFUService::onButtonA);
uBit.MessageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, this, &MicroBitDFUService::onButtonB);
uBit.display.scroll("BLUE ZONE...");
showNameHistogram();
while(1)
{
for (int i=0; i<100; i++)
{
if (flashCodeRequested)
{
uBit.display.scrollAsync(pairString);
for (int j=0; j<40; j++)
{
if (uBit.buttonA.isPressed())
{
i=100;
releaseFlashCode();
showTick();
flashCodeRequested = false;
authenticated = true;
break;
}
wait(0.1);
}
}
wait (0.1);
if (flashCodeRequested)
uBit.display.scroll("PAIR?");
// If our peer disconnects, drop all state.
if ((authenticated || flashCodeRequested) && !ble.getGapState().connected)
{
authenticated = false;
flashCodeRequested = false;
flashCode = 0x00;
}
// If our peer disconnects, drop all state.
if ((authenticated || flashCodeRequested) && !ble.getGapState().connected)
{
authenticated = false;
flashCodeRequested = false;
flashCode = 0x00;
}
uBit.sleep(500);
}
}
/**
* Callback. Invoked when any of our attributes are written via BLE.
*/
@ -152,6 +152,9 @@ void MicroBitDFUService::onDataWritten(const GattWriteCallbackParams *params)
if (authenticated)
{
uBit.display.scroll("");
uBit.display.clear();
#if CONFIG_ENABLED(MICROBIT_DBG)
pc.printf(" ACTIVATING BOOTLOADER.\n");
#endif
@ -161,6 +164,9 @@ void MicroBitDFUService::onDataWritten(const GattWriteCallbackParams *params)
break;
case MICROBIT_DFU_OPCODE_START_PAIR:
#if CONFIG_ENABLED(MICROBIT_DBG)
pc.printf(" PAIRING REQUESTED.\n");
#endif
flashCodeRequested = true;
break;