Factored idle/interrupt callback events out of MicroBit.cpp

This commit is contained in:
Joe Finney 2015-11-24 09:21:46 +00:00
parent 5231b5f526
commit 000351b599
7 changed files with 23 additions and 20 deletions

View File

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

View File

@ -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;
}
}
/**

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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);
}
}
/**

View File

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

View File

@ -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;
}