diff --git a/source/MicroBit.cpp b/source/MicroBit.cpp index ebc42b0..2a0acc3 100644 --- a/source/MicroBit.cpp +++ b/source/MicroBit.cpp @@ -109,13 +109,11 @@ MicroBit::MicroBit() : */ void MicroBit::init() { - // Bring up our nested heap allocator. + // Bring up the heap allocator, and reclaim as much memory from SoftDevice as possible. microbit_heap_init(); - // Bring up fiber scheduler + // Bring up fiber scheduler. Wait a little while for state to stabilise. scheduler_init(&messageBus); - sleep(10); - // Load any stored calibration data from persistent storage. MicroBitStorage s = MicroBitStorage(); MicroBitConfigurationBlock *b = s.getConfigurationBlock(); @@ -129,15 +127,6 @@ void MicroBit::init() delete b; - // TODO: YUCK!!! MOVE THESE INTO THE RELEVANT COMPONENTS!! - - //add the display to the systemComponent array - addSystemComponent(&display); - - //add the compass and accelerometer to the idle array - addIdleComponent(&accelerometer); - addIdleComponent(&compass); - addIdleComponent(&messageBus); // Seed our random number generator seedRandom(); @@ -229,6 +218,9 @@ void MicroBit::compassCalibrator(MicroBitEvent) int x = accelerometer.getX(); int y = accelerometer.getY(); + // Wait a little whie for the button state to stabilise (one scheduler tick). + sleep(10); + // Deterine the position of the user controlled pixel on the screen. if (x < -PIXEL2_THRESHOLD) cursor.x = 0; diff --git a/source/MicroBitAccelerometer.cpp b/source/MicroBitAccelerometer.cpp index bf2fde9..944e4e4 100644 --- a/source/MicroBitAccelerometer.cpp +++ b/source/MicroBitAccelerometer.cpp @@ -157,7 +157,10 @@ MicroBitAccelerometer::MicroBitAccelerometer(uint16_t id, uint16_t address, Micr // Configure and enable the accelerometer. if (this->configure() == MICROBIT_OK) + { + fiber_add_idle_component(this); status |= MICROBIT_COMPONENT_RUNNING; + } } /** diff --git a/source/MicroBitCompass.cpp b/source/MicroBitCompass.cpp index 49bcf60..c65aa3f 100644 --- a/source/MicroBitCompass.cpp +++ b/source/MicroBitCompass.cpp @@ -33,6 +33,8 @@ MicroBitCompass::MicroBitCompass(uint16_t id, uint16_t address, MicroBitI2C& _i2 // Assume that we have no calibraiton information. status &= ~MICROBIT_COMPASS_STATUS_CALIBRATED; + fiber_add_idle_component(this); + // Indicate that we're up and running. status |= MICROBIT_COMPONENT_RUNNING; } diff --git a/source/MicroBitDisplay.cpp b/source/MicroBitDisplay.cpp index 707f85d..cc8cae0 100644 --- a/source/MicroBitDisplay.cpp +++ b/source/MicroBitDisplay.cpp @@ -49,7 +49,9 @@ MicroBitDisplay::MicroBitDisplay(uint16_t id, uint8_t x, uint8_t y) : if (!this->defaultDisplay) this->defaultDisplay = this; - + + fiber_add_system_component(this); + status |= MICROBIT_COMPONENT_RUNNING; } diff --git a/source/MicroBitEvent.cpp b/source/MicroBitEvent.cpp index b6294a9..d9c0d04 100644 --- a/source/MicroBitEvent.cpp +++ b/source/MicroBitEvent.cpp @@ -42,11 +42,13 @@ MicroBitEvent::MicroBitEvent() */ void MicroBitEvent::fire(MicroBitEventLaunchMode mode) { - if (mode == CREATE_AND_QUEUE) - MicroBitMessageBus::defaultMessageBus->send(*this); - - else if (mode == CREATE_AND_FIRE) - MicroBitMessageBus::defaultMessageBus->process(*this); + if(MicroBitMessageBus::defaultMessageBus) + { + if (mode == CREATE_AND_QUEUE) + MicroBitMessageBus::defaultMessageBus->send(*this); + else if (mode == CREATE_AND_FIRE) + MicroBitMessageBus::defaultMessageBus->process(*this); + } } /** diff --git a/source/MicroBitFiber.cpp b/source/MicroBitFiber.cpp index 0aef7a0..1f4387f 100644 --- a/source/MicroBitFiber.cpp +++ b/source/MicroBitFiber.cpp @@ -884,7 +884,7 @@ void idle() // If the above did create any useful work, enter power efficient sleep. if(scheduler_runqueue_empty()) - __WFI(); + __WFE(); } /** * IDLE task. diff --git a/source/MicroBitMessageBus.cpp b/source/MicroBitMessageBus.cpp index 6492a13..86a3bdc 100644 --- a/source/MicroBitMessageBus.cpp +++ b/source/MicroBitMessageBus.cpp @@ -19,6 +19,8 @@ MicroBitMessageBus::MicroBitMessageBus() this->evt_queue_tail = NULL; this->queueLength = 0; + fiber_add_idle_component(this); + if(MicroBitMessageBus::defaultMessageBus == NULL) MicroBitMessageBus::defaultMessageBus = this; }