2015-08-12 10:53:41 +00:00
|
|
|
#include "MicroBit.h"
|
|
|
|
|
2015-09-02 11:42:24 +00:00
|
|
|
#if CONFIG_ENABLED(MICROBIT_DBG)
|
2015-08-12 10:53:41 +00:00
|
|
|
Serial pc(USBTX, USBRX);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
MicroBit uBit;
|
|
|
|
InterruptIn resetButton(MICROBIT_PIN_BUTTON_RESET);
|
|
|
|
|
|
|
|
void
|
2015-08-14 15:51:45 +00:00
|
|
|
reset()
|
2015-08-12 10:53:41 +00:00
|
|
|
{
|
|
|
|
NVIC_SystemReset();
|
|
|
|
}
|
|
|
|
|
2015-09-02 11:42:24 +00:00
|
|
|
|
2015-08-12 10:53:41 +00:00
|
|
|
int main()
|
|
|
|
{
|
|
|
|
// Bring up soft reset button.
|
|
|
|
resetButton.mode(PullUp);
|
2015-08-14 15:51:45 +00:00
|
|
|
resetButton.fall(reset);
|
2015-08-12 10:53:41 +00:00
|
|
|
|
2015-09-02 11:42:24 +00:00
|
|
|
#if CONFIG_ENABLED(MICROBIT_DBG)
|
2015-08-12 10:53:41 +00:00
|
|
|
pc.baud(115200);
|
|
|
|
|
|
|
|
// For diagnostics. Gives time to open the console window. :-)
|
2015-08-31 22:25:10 +00:00
|
|
|
for (int i=3; i>0; i--)
|
2015-08-12 10:53:41 +00:00
|
|
|
{
|
|
|
|
pc.printf("=== SUPERMAIN: Starting in %d ===\n", i);
|
|
|
|
wait(1.0);
|
|
|
|
}
|
2015-08-31 22:25:10 +00:00
|
|
|
#endif
|
2015-08-12 10:53:41 +00:00
|
|
|
|
2015-08-31 22:25:10 +00:00
|
|
|
// Bring up our nested heap allocator.
|
|
|
|
microbit_heap_init();
|
2015-08-12 10:53:41 +00:00
|
|
|
|
|
|
|
// Bring up fiber scheduler
|
|
|
|
scheduler_init();
|
|
|
|
|
2015-08-31 22:25:10 +00:00
|
|
|
// Bring up random number generator, BLE, display and system timers.
|
2015-08-12 10:53:41 +00:00
|
|
|
uBit.init();
|
2015-08-31 22:25:10 +00:00
|
|
|
|
|
|
|
// Provide time for all threaded initialisers to complete.
|
2015-08-12 10:53:41 +00:00
|
|
|
uBit.sleep(100);
|
2015-08-12 23:49:48 +00:00
|
|
|
|
2015-09-02 11:42:24 +00:00
|
|
|
#if CONFIG_ENABLED(MICROBIT_BLE_BLUEZONE)
|
2015-08-12 10:53:41 +00:00
|
|
|
// Test if we need to enter BLE pairing mode...
|
|
|
|
int i=0;
|
|
|
|
while (uBit.buttonA.isPressed() && uBit.buttonB.isPressed() && i<10)
|
|
|
|
{
|
|
|
|
uBit.sleep(100);
|
|
|
|
i++;
|
|
|
|
|
2015-08-31 22:25:10 +00:00
|
|
|
if (i == 10)
|
|
|
|
{
|
|
|
|
// OK - we need to enter BLUE ZONE mode.
|
|
|
|
// Test to see if BLE and the necessary services have been brought up already.
|
|
|
|
// If not, start them.
|
|
|
|
if (!uBit.ble)
|
|
|
|
{
|
|
|
|
uBit.ble = new BLEDevice();
|
|
|
|
uBit.ble->init();
|
|
|
|
uBit.ble->onDisconnection(bleDisconnectionCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!uBit.ble_firmware_update_service)
|
|
|
|
{
|
|
|
|
uBit.ble_firmware_update_service = new MicroBitDFUService(*uBit.ble);
|
|
|
|
uBit.ble_firmware_update_service->getName(MICROBIT_BLE_DEVICE_NAME+14);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we're advertising.
|
|
|
|
uBit.ble->accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
|
|
|
|
uBit.ble->accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)MICROBIT_BLE_DEVICE_NAME, strlen(MICROBIT_BLE_DEVICE_NAME)+1);
|
|
|
|
uBit.ble->setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
|
|
|
|
uBit.ble->setAdvertisingInterval(Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(1000));
|
|
|
|
uBit.ble->startAdvertising();
|
|
|
|
|
|
|
|
// enter BLUE ZONE mode.
|
2015-08-12 10:53:41 +00:00
|
|
|
uBit.ble_firmware_update_service->pair();
|
2015-08-31 22:25:10 +00:00
|
|
|
}
|
2015-08-12 10:53:41 +00:00
|
|
|
}
|
2015-08-12 23:49:48 +00:00
|
|
|
#endif
|
2015-08-31 22:25:10 +00:00
|
|
|
|
2015-08-12 10:53:41 +00:00
|
|
|
app_main();
|
|
|
|
|
2015-08-31 22:25:10 +00:00
|
|
|
// If app_main exits, there may still be other fibers running, registered event handlers etc.
|
|
|
|
// Simply release this fiber, which will mean we enter the scheduler. Worse case, we then
|
|
|
|
// sit in the idle task forever, in a power efficient sleep.
|
|
|
|
release_fiber();
|
|
|
|
|
|
|
|
// We should never get here, but just in case.
|
|
|
|
while(1);
|
2015-08-12 10:53:41 +00:00
|
|
|
}
|