diff --git a/inc/MicroBit.h b/inc/MicroBit.h index 135f15a..bb80f6b 100644 --- a/inc/MicroBit.h +++ b/inc/MicroBit.h @@ -46,12 +46,14 @@ #define MICROBIT_NAME_CODE_LETTERS 5 // Random number generator -#define NRF51822_RNG_ADDRESS 0x4000D000 +#define NRF51822_RNG_ADDRESS 0x4000D000 // mbed pin assignments of core components. -#define MICROBIT_PIN_SDA P0_30 -#define MICROBIT_PIN_SCL P0_0 +#define MICROBIT_PIN_SDA P0_30 +#define MICROBIT_PIN_SCL P0_0 + +#define MICROBIT_DEFAULT_TICK_PERIOD FIBER_TICK_PERIOD_MS /** * Class definition for a MicroBit device. @@ -74,6 +76,8 @@ class MicroBit // Periodic callback Ticker systemTicker; + int tickPeriod; + // I2C Interface MicroBitI2C i2c; @@ -259,6 +263,21 @@ class MicroBit */ int removeIdleComponent(MicroBitComponent *component); + + /* + * Reconfigures the ticker to the given speed in milliseconds. + * @param speedMs the speed in milliseconds + * @return MICROBIT_OK on success. MICROBIT_INVALID_PARAMETER is returned if speedUs < 1 + * + * @note this will also modify the value that is added to ticks in MiroBitFiber:scheduler_tick() + */ + int setTickPeriod(int speedMs); + + /* + * Returns the currently used tick speed in milliseconds + */ + int getTickPeriod(); + /** * Determine the time since this MicroBit was last reset. * diff --git a/inc/MicroBitDisplay.h b/inc/MicroBitDisplay.h index a96e497..cbaee86 100644 --- a/inc/MicroBitDisplay.h +++ b/inc/MicroBitDisplay.h @@ -1,12 +1,6 @@ #ifndef MICROBIT_DISPLAY_H #define MICROBIT_DISPLAY_H - -/** - * Core Configuration settings. - */ -#define MICROBIT_DISPLAY_REFRESH_PERIOD ((float)FIBER_TICK_PERIOD_MS / (float)1000) - /** * MessageBus Event Codes */ diff --git a/source/MicroBit.cpp b/source/MicroBit.cpp index e783c67..651d54a 100644 --- a/source/MicroBit.cpp +++ b/source/MicroBit.cpp @@ -1,4 +1,4 @@ -/* +/* * The underlying Nordic libraries that support BLE do not compile cleanly with the stringent GCC settings we employ * If we're compiling under GCC, then we suppress any warnings generated from this code (but not the rest of the DAL) * The ARM cc compiler is more tolerant. We don't test __GNUC__ here to detect GCC as ARMCC also typically sets this @@ -133,8 +133,10 @@ void MicroBit::init() // Seed our random number generator seedRandom(); + tickPeriod = MICROBIT_DEFAULT_TICK_PERIOD; + // Start refreshing the Matrix Display - systemTicker.attach(this, &MicroBit::systemTick, MICROBIT_DISPLAY_REFRESH_PERIOD); + systemTicker.attach_us(this, &MicroBit::systemTick, tickPeriod * 1000); // Register our compass calibration algorithm. MessageBus.listen(MICROBIT_ID_COMPASS, MICROBIT_COMPASS_EVT_CALIBRATE, this, &MicroBit::compassCalibrator, MESSAGE_BUS_LISTENER_IMMEDIATE); @@ -607,6 +609,35 @@ int MicroBit::removeIdleComponent(MicroBitComponent *component) return MICROBIT_OK; } +/* + * Reconfigures the ticker to the given speed in milliseconds. + * @param speedMs the speed in milliseconds + * @return MICROBIT_OK on success. MICROBIT_INVALID_PARAMETER is returned if speedUs < 1 + * + * @note this will also modify the value that is added to ticks in MiroBitFiber:scheduler_tick() + */ +int MicroBit::setTickPeriod(int speedMs) +{ + if(speedMs < 1) + return MICROBIT_INVALID_PARAMETER; + + uBit.systemTicker.detach(); + + uBit.systemTicker.attach_us(this, &MicroBit::systemTick, speedMs * 1000); + + tickPeriod = speedMs; + + return MICROBIT_OK; +} + +/* + * Returns the currently used tick speed in milliseconds + */ +int MicroBit::getTickPeriod() +{ + return tickPeriod; +} + /** * Determine the time since this MicroBit was last reset. * diff --git a/source/MicroBitDisplay.cpp b/source/MicroBitDisplay.cpp index 0ef5014..4303563 100644 --- a/source/MicroBitDisplay.cpp +++ b/source/MicroBitDisplay.cpp @@ -145,7 +145,7 @@ void MicroBitDisplay::render() //timer does not have enough resolution for brightness of 1. 23.53 us if(brightness != MICROBIT_DISPLAY_MAXIMUM_BRIGHTNESS && brightness > MICROBIT_DISPLAY_MINIMUM_BRIGHTNESS) - renderTimer.attach(this, &MicroBitDisplay::renderFinish, (((float)brightness) / ((float)MICROBIT_DISPLAY_MAXIMUM_BRIGHTNESS)) * (float)MICROBIT_DISPLAY_REFRESH_PERIOD); + renderTimer.attach_us(this, &MicroBitDisplay::renderFinish, (((brightness * 100) / (MICROBIT_DISPLAY_MAXIMUM_BRIGHTNESS)) * uBit.getTickPeriod())); //this will take around 23us to execute if(brightness <= MICROBIT_DISPLAY_MINIMUM_BRIGHTNESS) diff --git a/source/MicroBitFiber.cpp b/source/MicroBitFiber.cpp index aabea0f..bebe05a 100644 --- a/source/MicroBitFiber.cpp +++ b/source/MicroBitFiber.cpp @@ -181,8 +181,8 @@ void scheduler_tick() Fiber *t; // increment our real-time counter. - ticks += FIBER_TICK_PERIOD_MS; - + ticks += uBit.getTickPeriod(); + // Check the sleep queue, and wake up any fibers as necessary. while (f != NULL) {