developer.mbed.org branch of Lancaster University's microbit library. The real home for this is https://github.com/lancaster-university/microbit

Dependencies:   microbit-dal

Dependents:   microbit-samples microbit_snake microbit_snake microbit-letter-game ... more

Embed: (wiki syntax)

« Back to documentation index

MicroBit Class Reference

Class definition for a MicroBit device. More...

#include <MicroBit.h>

Public Member Functions

 MicroBit ()
 Constructor.
void init ()
 Post constructor initialisation method.
void reset ()
 Will reset the micro:bit when called.
void sleep (uint32_t milliseconds)
 Delay execution for the given amount of time.
void seedRandom ()
 Seed the pseudo random number generator using the hardware random number generator.
void seedRandom (uint32_t seed)
 Seed the pseudo random number generator using the given value.
int random (int max)
 Generate a random number in the given range.
unsigned long systemTime ()
 Determine the time since this MicroBit was last reset.
const char * systemVersion ()
 Determine the version of the micro:bit runtime currently in use.
void panic (int statusCode=0)
 Triggers a microbit panic where an loop will display a panic face and the status code, if provided.
int addSystemComponent (MicroBitComponent *component)
 Add a component to the array of system components.
int removeSystemComponent (MicroBitComponent *component)
 Remove a component from the array of system components.
int addIdleComponent (MicroBitComponent *component)
 Adds a component to the array of idle thread components, which are processed when the run queue is empty.
int removeIdleComponent (MicroBitComponent *component)
 Remove a component from the array of idle thread components.

Static Public Member Functions

static ManagedString getName ()
 Return the friendly name for this device.
static ManagedString getSerial ()
 Return the serial number of this device.

Detailed Description

Class definition for a MicroBit device.

Represents the device as a whole, and includes member variables that represent various device drivers used to control aspects of the micro:bit.

Definition at line 78 of file MicroBit.h.


Constructor & Destructor Documentation

MicroBit (  )

Constructor.

Create a representation of a MicroBit device, which includes member variables that represent various device drivers used to control aspects of the micro:bit.

Definition at line 61 of file MicroBit.cpp.


Member Function Documentation

int addIdleComponent ( MicroBitComponent *  component )

Adds a component to the array of idle thread components, which are processed when the run queue is empty.

The system timer will poll isIdleCallbackNeeded on each component to determine if the scheduler should schedule the idle_task imminently.

Parameters:
componentThe component to add to the array.
Returns:
MICROBIT_OK on success or MICROBIT_NO_RESOURCES if the fiber components array is full.
 MicroBitI2C i2c(I2C_SDA0, I2C_SCL0);

 // heap allocated - otherwise it will be paged out!
 MicroBitAccelerometer* accelerometer = new MicroBitAccelerometer(i2c);

 fiber_add_idle_component(accelerometer);
Note:
This interface is now deprecated, and will be removed in the next major release. Please use fiber_add_idle_component().

Definition at line 580 of file MicroBit.h.

int addSystemComponent ( MicroBitComponent *  component )

Add a component to the array of system components.

This component will then receive periodic callbacks, once every tick period in interrupt context.

Parameters:
componentThe component to add.
Returns:
MICROBIT_OK on success or MICROBIT_NO_RESOURCES if the component array is full.
 // heap allocated - otherwise it will be paged out!
 MicroBitDisplay* display = new MicroBitDisplay();

 uBit.addSystemComponent(display);
Note:
This interface is now deprecated, and will be removed in the next major release. Please use system_timer_add_component().

Definition at line 529 of file MicroBit.h.

ManagedString getName (  ) [static]

Return the friendly name for this device.

Returns:
A ManagedString representing the friendly name of this device.
 ManagedString name = uBit.getName();

Definition at line 384 of file MicroBit.h.

ManagedString getSerial (  ) [static]

Return the serial number of this device.

Returns:
A ManagedString representing the serial number of this device.
 ManagedString serialNumber = uBit.getSerial();

Definition at line 398 of file MicroBit.h.

void init (  )

Post constructor initialisation method.

This call will initialised the scheduler, memory allocator and Bluetooth stack.

This is required as the Bluetooth stack can't be brought up in a static context i.e. in a constructor.

 uBit.init();
Note:
This method must be called before user code utilises any functionality contained by uBit.

Definition at line 109 of file MicroBit.cpp.

void panic ( int  statusCode = 0 )

Triggers a microbit panic where an loop will display a panic face and the status code, if provided.

This loop will continue for panic_timeout iterations, defaults to 0 (infinite).

panic_timeout can be configured via a call to microbit_panic_timeout.

Parameters:
statusCodethe status code of the associated error.
 microbit_panic_timeout(4);

 // will display loop for 4 iterations.
 uBit.panic(10);

Definition at line 652 of file MicroBit.h.

int random ( int  max )

Generate a random number in the given range.

We use a simple Galois LFSR random number generator here, as a Galois LFSR is sufficient for our applications, and much more lightweight than the hardware random number generator built int the processor, which takes a long time and uses a lot of energy.

KIDS: You shouldn't use this is the real world to generate cryptographic keys though... have a think why not. :-)

Parameters:
maxthe upper range to generate a number for. This number cannot be negative.
Returns:
A random, natural number between 0 and the max-1. Or MICROBIT_INVALID_VALUE if max is <= 0.
 uBit.random(200); //a number between 0 and 199

Definition at line 479 of file MicroBit.h.

int removeIdleComponent ( MicroBitComponent *  component )

Remove a component from the array of idle thread components.

Parameters:
componentThe component to remove from the idle component array.
Returns:
MICROBIT_OK on success. MICROBIT_INVALID_PARAMETER is returned if the given component has not been previously added.
 MicroBitI2C i2c(I2C_SDA0, I2C_SCL0);

 // heap allocated - otherwise it will be paged out!
 MicroBitAccelerometer* accelerometer = new MicroBitAccelerometer(i2c);

 uBit.addIdleComponent(accelerometer);

 uBit.removeIdleComponent(accelerometer);
Note:
This interface is now deprecated, and will be removed in the next major release. Please use fiber_remove_idle_component().

Definition at line 605 of file MicroBit.h.

int removeSystemComponent ( MicroBitComponent *  component )

Remove a component from the array of system components.

This component will no longer receive periodic callbacks.

Parameters:
componentThe component to remove.
Returns:
MICROBIT_OK on success or MICROBIT_INVALID_PARAMETER is returned if the given component has not been previously added.
 // heap allocated - otherwise it will be paged out!
 MicroBitDisplay* display = new MicroBitDisplay();

 uBit.addSystemComponent(display);

 uBit.removeSystemComponent(display);
Note:
This interface is now deprecated, and will be removed in the next major release. Please use system_timer_remove_component().

Definition at line 553 of file MicroBit.h.

void reset (  )

Will reset the micro:bit when called.

 uBit.reset();

Definition at line 418 of file MicroBit.h.

void seedRandom (  )

Seed the pseudo random number generator using the hardware random number generator.

 uBit.seedRandom();

Definition at line 491 of file MicroBit.h.

void seedRandom ( uint32_t  seed )

Seed the pseudo random number generator using the given value.

Parameters:
seedThe 32-bit value to seed the generator with.
 uBit.seedRandom(0xBB5EED);

Definition at line 506 of file MicroBit.h.

void sleep ( uint32_t  milliseconds )

Delay execution for the given amount of time.

If the scheduler is running, this will deschedule the current fiber and perform a power efficient, concurrent sleep operation.

If the scheduler is disabled or we're running in an interrupt context, this will revert to a busy wait.

Alternatively: wait, wait_ms, wait_us can be used which will perform a blocking sleep operation.

Parameters:
millisecondsthe amount of time, in ms, to wait for. This number cannot be negative.
Returns:
MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER milliseconds is less than zero.
 uBit.sleep(20); //sleep for 20ms
Note:
This operation is currently limited by the rate of the system timer, therefore the granularity of the sleep operation is limited to 6 ms unless the rate of the system timer is modified.

Definition at line 456 of file MicroBit.h.

unsigned long systemTime (  )

Determine the time since this MicroBit was last reset.

Returns:
The time since the last reset, in milliseconds.
Note:
This will value overflow after 1.6 months.

Definition at line 618 of file MicroBit.h.

const char * systemVersion (  )

Determine the version of the micro:bit runtime currently in use.

Returns:
A textual description of the version of the micro:bit runtime that is currently running on this device.

Definition at line 630 of file MicroBit.h.