microbit-dal: optimisation to MicroBitCompass

The Compass now only adds itself to the idle callback when it is first "used".
This commit is contained in:
James Devine 2016-03-11 12:07:46 +00:00
parent f3e6503e45
commit bf9fdad3c6
2 changed files with 21 additions and 2 deletions

View File

@ -67,6 +67,7 @@ extern const MAG3110SampleRateConfig MAG3110SampleRate[];
*/
#define MICROBIT_COMPASS_STATUS_CALIBRATED 1
#define MICROBIT_COMPASS_STATUS_CALIBRATING 2
#define MICROBIT_COMPASS_STATUS_ADDED_TO_IDLE 4
/*
* Term to convert sample data into SI units

View File

@ -33,8 +33,6 @@ 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;
}
@ -184,6 +182,16 @@ int MicroBitCompass::heading()
*/
int MicroBitCompass::updateSample()
{
/**
* Adds the compass to idle, if it hasn't been added already.
* This is an optimisation so that the compass is only added on first 'use'.
*/
if(!(status & MICROBIT_COMPASS_STATUS_ADDED_TO_IDLE))
{
fiber_add_idle_component(this);
status |= MICROBIT_COMPASS_STATUS_ADDED_TO_IDLE;
}
// Poll interrupt line from compass (Active HI).
// Interrupt is cleared on data read of MAG_OUT_X_MSB.
if(int1)
@ -441,6 +449,16 @@ int MicroBitCompass::readTemperature()
*/
int MicroBitCompass::calibrate()
{
/**
* Adds the compass to idle, if it hasn't been added already.
* This is an optimisation so that the compass is only added on first 'use'.
*/
if(!(status & MICROBIT_COMPASS_STATUS_ADDED_TO_IDLE))
{
fiber_add_idle_component(this);
status |= MICROBIT_COMPASS_STATUS_ADDED_TO_IDLE;
}
// Only perform one calibration process at a time.
if(isCalibrating())
return MICROBIT_CALIBRATION_IN_PROGRESS;