Merge pull request #75 from lancaster-university/idle-system-issue-patch

Formatting corrections and further fixes for #73
This commit is contained in:
James Devine 2016-01-13 16:37:20 +00:00
commit 32faae520d
8 changed files with 443 additions and 395 deletions

View file

@ -57,7 +57,7 @@
#define MICROBIT_ACCELEROMETER_EVT_FACE_UP 5
#define MICROBIT_ACCELEROMETER_EVT_FACE_DOWN 6
#define MICROBIT_ACCELEROMETER_EVT_FREEFALL 7
#define MICROBIT_ACCELEROMETER_EVT_3G 8
#define MICROBIT_ACCELEROMETER_EVT_3G 8
#define MICROBIT_ACCELEROMETER_EVT_6G 9
#define MICROBIT_ACCELEROMETER_EVT_8G 10
#define MICROBIT_ACCELEROMETER_EVT_SHAKE 11
@ -140,10 +140,10 @@ struct ShakeHistory
class MicroBitAccelerometer : public MicroBitComponent
{
/**
* Unique, enumerated ID for this component.
* Unique, enumerated ID for this component.
* Used to track asynchronous events in the event bus.
*/
uint16_t address; // I2C address of this accelerometer.
uint16_t samplePeriod; // The time between samples, in milliseconds.
uint8_t sampleRange; // The sample range of the accelerometer in g.
@ -152,20 +152,20 @@ class MicroBitAccelerometer : public MicroBitComponent
float pitch; // Pitch of the device, in radians.
float roll; // Roll of the device, in radians.
uint8_t sigma; // the number of ticks that the instantaneous gesture has been stable.
BasicGesture lastGesture; // the last, stable gesture recorded.
BasicGesture lastGesture; // the last, stable gesture recorded.
BasicGesture currentGesture; // the instantaneous, unfiltered gesture detected.
ShakeHistory shake; // State information needed to detect shake events.
public:
/**
* Constructor.
* Constructor.
* Create an accelerometer representation with the given ID.
* @param id the ID of the new object.
* @param address the default base address of the accelerometer.
* @param address the default base address of the accelerometer.
*
* Example:
* @code
* @code
* accelerometer(MICROBIT_ID_ACCELEROMETER, MMA8653_DEFAULT_ADDR)
* @endcode
*/
@ -199,7 +199,7 @@ class MicroBitAccelerometer : public MicroBitComponent
int setPeriod(int period);
/**
* Reads the currently configured sample rate of the accelerometer.
* Reads the currently configured sample rate of the accelerometer.
* @return The time between samples, in milliseconds.
*/
int getPeriod();
@ -214,17 +214,17 @@ class MicroBitAccelerometer : public MicroBitComponent
int setRange(int range);
/**
* Reads the currently configured sample range of the accelerometer.
* Reads the currently configured sample range of the accelerometer.
* @return The sample range, in g.
*/
int getRange();
/**
* Attempts to determine the 8 bit ID from the accelerometer.
* Attempts to determine the 8 bit ID from the accelerometer.
* @return the 8 bit ID returned by the accelerometer, or MICROBIT_I2C_ERROR if the request fails.
*
* Example:
* @code
* @code
* uBit.accelerometer.whoAmI();
* @endcode
*/
@ -236,32 +236,32 @@ class MicroBitAccelerometer : public MicroBitComponent
* @return The force measured in the X axis, in milli-g.
*
* Example:
* @code
* @code
* uBit.accelerometer.getX();
* @endcode
*/
int getX(MicroBitCoordinateSystem system = SIMPLE_CARTESIAN);
/**
* Reads the Y axis value of the latest update from the accelerometer.
* @return The force measured in the Y axis, in milli-g.
*
* Example:
* @code
* @code
* uBit.accelerometer.getY();
* @endcode
*/
*/
int getY(MicroBitCoordinateSystem system = SIMPLE_CARTESIAN);
/**
* Reads the Z axis value of the latest update from the accelerometer.
* @return The force measured in the Z axis, in milli-g.
*
* Example:
* @code
* @code
* uBit.accelerometer.getZ();
* @endcode
*/
*/
int getZ(MicroBitCoordinateSystem system = SIMPLE_CARTESIAN);
/**
@ -269,10 +269,10 @@ class MicroBitAccelerometer : public MicroBitComponent
* @return The pitch of the device, in degrees.
*
* Example:
* @code
* @code
* uBit.accelerometer.getPitch();
* @endcode
*/
*/
int getPitch();
float getPitchRadians();
@ -281,10 +281,10 @@ class MicroBitAccelerometer : public MicroBitComponent
* @return The roll of the device, in degrees.
*
* Example:
* @code
* @code
* uBit.accelerometer.getRoll();
* @endcode
*/
*/
int getRoll();
float getRollRadians();
@ -293,16 +293,16 @@ class MicroBitAccelerometer : public MicroBitComponent
* @return The last gesture detected.
*
* Example:
* @code
* @code
* if (uBit.accelerometer.getGesture() == SHAKE)
* @endcode
*/
*/
BasicGesture getGesture();
/**
* periodic callback from MicroBit idle thread.
* Check if any data is ready for reading by checking the interrupt flag on the accelerometer
*/
*/
virtual void idleTick();
/**
@ -310,6 +310,11 @@ class MicroBitAccelerometer : public MicroBitComponent
*/
virtual int isIdleCallbackNeeded();
/**
* Destructor for MicroBitButton, so that we deregister ourselves as an idleComponent
*/
~MicroBitAccelerometer();
private:
/**
* Issues a standard, 2 byte I2C command write to the accelerometer.
@ -341,7 +346,7 @@ class MicroBitAccelerometer : public MicroBitComponent
/**
*
* Updates the basic gesture recognizer. This performs instantaneous pose recognition, and also some low pass filtering to promote
* Updates the basic gesture recognizer. This performs instantaneous pose recognition, and also some low pass filtering to promote
* stability.
*/
void updateGesture();

View file

@ -48,7 +48,7 @@ struct MAG3110SampleRateConfig
extern const MAG3110SampleRateConfig MAG3110SampleRate[];
#define MAG3110_SAMPLE_RATES 11
#define MAG3110_SAMPLE_RATES 11
/*
* Compass events
@ -66,7 +66,7 @@ extern const MAG3110SampleRateConfig MAG3110SampleRate[];
*/
#define MICROBIT_COMPASS_STATUS_CALIBRATED 1
#define MICROBIT_COMPASS_STATUS_CALIBRATING 2
/*
* Term to convert sample data into SI units
*/
@ -83,19 +83,19 @@ struct CompassSample
int x;
int y;
int z;
CompassSample()
{
this->x = 0;
this->y = 0;
this->z = 0;
this->z = 0;
}
CompassSample(int x, int y, int z)
{
this->x = x;
this->y = y;
this->z = z;
this->z = z;
}
};
@ -108,11 +108,11 @@ struct CompassSample
class MicroBitCompass : public MicroBitComponent
{
/**
* Unique, enumerated ID for this component.
* Unique, enumerated ID for this component.
* Used to track asynchronous events in the event bus.
*/
uint16_t address; // I2C address of the magnetmometer.
uint16_t address; // I2C address of the magnetmometer.
uint16_t samplePeriod; // The time between samples, in millseconds.
CompassSample average; // Centre point of sample data.
@ -120,9 +120,9 @@ class MicroBitCompass : public MicroBitComponent
DigitalIn int1; // Data ready interrupt.
public:
/**
* Constructor.
* Constructor.
* Create a compass representation with the given ID.
* @param id the event ID of the compass object.
* @param address the default address for the compass register
@ -140,7 +140,7 @@ class MicroBitCompass : public MicroBitComponent
* @endcode
*/
MicroBitCompass(uint16_t id, uint16_t address);
/**
* Configures the compass for the sample rate defined
* in this object. The nearest values are chosen to those defined
@ -160,7 +160,7 @@ class MicroBitCompass : public MicroBitComponent
int setPeriod(int period);
/**
* Reads the currently configured sample rate of the compass.
* Reads the currently configured sample rate of the compass.
* @return The time between samples, in milliseconds.
*/
int getPeriod();
@ -169,10 +169,10 @@ class MicroBitCompass : public MicroBitComponent
* Gets the current heading of the device, relative to magnetic north.
* If the compass is not calibrated, it will raise the MICROBIT_COMPASS_EVT_CALIBRATE event.
* Users wishing to implement their own calibration algorithms should listen for this event,
* using MESSAGE_BUS_LISTENER_IMMEDIATE model. This ensures that calibration is complete before
* the user program continues.
*
* @return the current heading, in degrees. Or MICROBIT_CALIBRATION_IN_PROGRESS if the compass is calibrating.
* using MESSAGE_BUS_LISTENER_IMMEDIATE model. This ensures that calibration is complete before
* the user program continues.
*
* @return the current heading, in degrees. Or MICROBIT_CALIBRATION_IN_PROGRESS if the compass is calibrating.
*
* Example:
* @code
@ -182,7 +182,7 @@ class MicroBitCompass : public MicroBitComponent
int heading();
/**
* Attempts to determine the 8 bit ID from the magnetometer.
* Attempts to determine the 8 bit ID from the magnetometer.
* @return the id of the compass (magnetometer), or MICROBIT_I2C_ERROR if the magnetometer could not be updated.
*
* Example:
@ -202,7 +202,7 @@ class MicroBitCompass : public MicroBitComponent
* @endcode
*/
int getX(MicroBitCoordinateSystem system = SIMPLE_CARTESIAN);
/**
* Reads the Y axis value of the latest update from the compass.
* @return The magnetic force measured in the Y axis, in nano teslas.
@ -211,9 +211,9 @@ class MicroBitCompass : public MicroBitComponent
* @code
* uBit.compass.getY();
* @endcode
*/
*/
int getY(MicroBitCoordinateSystem system = SIMPLE_CARTESIAN);
/**
* Reads the Z axis value of the latest update from the compass.
* @return The magnetic force measured in the Z axis, in nano teslas.
@ -222,8 +222,8 @@ class MicroBitCompass : public MicroBitComponent
* @code
* uBit.compass.getZ();
* @endcode
*/
int getZ(MicroBitCoordinateSystem system = SIMPLE_CARTESIAN);
*/
int getZ(MicroBitCoordinateSystem system = SIMPLE_CARTESIAN);
/**
* Determines the overall magnetic field strength based on the latest update from the compass.
@ -233,21 +233,21 @@ class MicroBitCompass : public MicroBitComponent
* @code
* uBit.compass.getFieldStrength();
* @endcode
*/
*/
int getFieldStrength();
/**
* Reads the current die temperature of the compass.
* Reads the current die temperature of the compass.
* @return the temperature in degrees celsius, or MICROBIT_I2C_ERROR if the magnetometer could not be updated.
*/
int readTemperature();
/**
* Perform a calibration of the compass.
*
*
* This method will be called automatically if a user attempts to read a compass value when
* the compass is uncalibrated. It can also be called at any time by the user.
*
*
* Any old calibration data is deleted.
* The method will only return once the compass has been calibrated.
*
@ -265,7 +265,7 @@ class MicroBitCompass : public MicroBitComponent
* @note *** THIS FUNCITON IS NOW DEPRECATED AND WILL BE REMOVED IN THE NEXT MAJOR RELEASE ***
* @note *** PLEASE USE THE calibrate() FUNCTION INSTEAD ***
*/
void calibrateAsync();
void calibrateAsync();
/**
* Perform a calibration of the compass.
@ -274,62 +274,67 @@ class MicroBitCompass : public MicroBitComponent
* @note *** THIS FUNCITON IS NOW DEPRECATED AND WILL BE REMOVED IN THE NEXT MAJOR RELEASE ***
* @note *** PLEASE USE THE calibrate() FUNCTION INSTEAD ***
*/
int calibrateStart();
int calibrateStart();
/**
* Complete the calibration of the compass.
* This will fire MICROBIT_COMPASS_EVT_CAL_END.
*
* @note *** THIS FUNCITON IS NOW DEPRECATED AND WILL BE REMOVED IN THE NEXT MAJOR RELEASE ***
*/
void calibrateEnd();
*/
void calibrateEnd();
/**
* Configure the compass to use the given calibration data.
* Calibration data is comprised of the perceived zero offset of each axis of the compass.
* After calibration this should now take into account trimming errors in the magnetometer,
* After calibration this should now take into account trimming errors in the magnetometer,
* and any "hard iron" offsets on the device.
*
* @param The x, y and z zero offsets to use as calibration data.
*/
*/
void setCalibration(CompassSample calibration);
/**
* Provides the calibration data currently in use by the compass.
* More specifically, the x, y and z zero offsets of the compass.
*
* @return The x, y and z xero offsets of the compass.
*/
* @return The x, y and z xero offsets of the compass.
*/
CompassSample getCalibration();
/**
* Periodic callback from MicroBit idle thread.
* Check if any data is ready for reading by checking the interrupt.
*/
*/
virtual void idleTick();
/**
* Returns 0 or 1. 1 indicates that the compass is calibrated, zero means the compass requires calibration.
*/
int isCalibrated();
/**
* Returns 0 or 1. 1 indicates that the compass is calibrating, zero means the compass is not currently calibrating.
*/
int isCalibrating();
/**
* Clears the calibration held in persistent storage, and sets the calibrated flag to zero.
*/
void clearCalibration();
/**
* Returns 0 or 1. 1 indicates data is waiting to be read, zero means data is not ready to be read.
*/
virtual int isIdleCallbackNeeded();
/**
* Destructor for MicroBitCompass, so that we deregister ourselves as an idleComponent
*/
~MicroBitCompass();
private:
/**
* Issues a standard, 2 byte I2C command write to the magnetometer.
* Blocks the calling thread until complete.
@ -339,7 +344,7 @@ class MicroBitCompass : public MicroBitComponent
* @return MICROBIT_OK on success, MICROBIT_I2C_ERROR if the magnetometer could not be accessed.
*/
int writeCommand(uint8_t reg, uint8_t value);
/**
* Issues a read command into the specified buffer.
* Blocks the calling thread until complete.
@ -350,7 +355,7 @@ class MicroBitCompass : public MicroBitComponent
* @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER or MICROBIT_I2C_ERROR if the magnetometer could not be accessed.
*/
int readCommand(uint8_t reg, uint8_t* buffer, int length);
/**
* Issues a read of a given address, and returns the value.
* Blocks the calling thread until complete.
@ -359,8 +364,8 @@ class MicroBitCompass : public MicroBitComponent
* @return The register value, interpreted as a 16 but signed value, or MICROBIT_I2C_ERROR if the magnetometer could not be accessed.
*/
int read16(uint8_t reg);
/**
* Issues a read of a given address, and returns the value.
* Blocks the calling thread until complete.

View file

@ -73,7 +73,7 @@ enum AnimationMode {
enum DisplayMode {
DISPLAY_MODE_BLACK_AND_WHITE,
DISPLAY_MODE_GREYSCALE
DISPLAY_MODE_GREYSCALE
};
enum DisplayRotation {
@ -172,24 +172,24 @@ class MicroBitDisplay : public MicroBitComponent
* Periodic callback, that we use to perform any animations we have running.
*/
void animationUpdate();
/**
* Called by the display in an interval determined by the brightness of the display, to give an impression
* of brightness.
*/
void renderFinish();
/**
* Translates a bit mask to a bit mask suitable for the nrf PORT0 and PORT1.
* Brightness has two levels on, or off.
*/
void render();
/**
* Translates a bit mask into a timer interrupt that gives the appearence of greyscale.
*/
void renderGreyscale();
/**
* Internal scrollText update method.
* Shift the screen image by one pixel to the left. If necessary, paste in the next char.
@ -224,7 +224,7 @@ class MicroBitDisplay : public MicroBitComponent
/**
* Blocks the current fiber until the display is available (i.e. not effect is being displayed).
* Animations are queued until their time to display.
*/
*/
void waitForFreeDisplay();
public:
@ -255,16 +255,16 @@ public:
* Frame update method, invoked periodically to strobe the display.
*/
virtual void systemTick();
/**
* Prints the given character to the display, if it is not in use.
*
* @param c The character to display.
* @param delay Optional parameter - the time for which to show the character. Zero displays the character forever.
* @return MICROBIT_OK, MICROBIT_BUSY is the screen is in use, or MICROBIT_INVALID_PARAMETER.
*
*
* Example:
* @code
* @code
* uBit.display.printAsync('p');
* uBit.display.printAsync('p',100);
* @endcode
@ -294,7 +294,7 @@ public:
* @param i The image to display.
* @param x The horizontal position on the screen to display the image (default 0)
* @param y The vertical position on the screen to display the image (default 0)
* @param alpha Treats the brightness level '0' as transparent (default 0)
* @param alpha Treats the brightness level '0' as transparent (default 0)
* @param delay The time to delay between characters, in milliseconds. set to 0 to display forever. (default 0).
*
* Example:
@ -334,7 +334,7 @@ public:
* @endcode
*/
int print(ManagedString s, int delay = MICROBIT_DEFAULT_PRINT_SPEED);
/**
* Prints the given image to the display.
* Blocks the calling thread until all the text has been displayed.
@ -350,7 +350,7 @@ public:
* @endcode
*/
int print(MicroBitImage i, int x, int y, int alpha, int delay = 0);
/**
* Scrolls the given string to the display, from right to left.
* Uses the given delay between characters.
@ -474,14 +474,14 @@ public:
/**
* Sets the mode of the display.
* @param mode The mode to swap the display into. (can be either DISPLAY_MODE_GREYSCALE, or DISPLAY_MODE_NORMAL)
*
*
* Example:
* @code
* @code
* uBit.display.setDisplayMode(DISPLAY_MODE_GREYSCALE); //per pixel brightness
* @endcode
*/
*/
void setDisplayMode(DisplayMode mode);
/**
* Fetches the current brightness of this display.
* @return the brightness of this display, in the range 0..255.
@ -557,12 +557,16 @@ public:
* Retreives the font object used for rendering characters on the display.
*/
MicroBitFont getFont();
/**
* Captures the bitmap currently being rendered on the display.
*/
MicroBitImage screenShot();
/**
* Destructor for MicroBitDisplay, so that we deregister ourselves as a systemComponent
*/
~MicroBitDisplay();
};
#endif

View file

@ -18,7 +18,7 @@
* MicroBit platform. It serves a number of purposes:
*
* 1) It provides an eventing abstraction that is independent of the underlying substrate.
* 2) It provides a mechanism to decouple user code from trusted system code
* 2) It provides a mechanism to decouple user code from trusted system code
* i.e. the basis of a message passing nano kernel.
* 3) It allows a common high level eventing abstraction across a range of hardware types.e.g. buttons, BLE...
* 4) It provides a mechanims for extensibility - new devices added via I/O pins can have OO based
@ -34,23 +34,23 @@ class MicroBitMessageBus : public MicroBitComponent
public:
/**
* Default constructor.
* Default constructor.
* Anticipating only one MessageBus per device, as filtering is handled within the class.
*/
MicroBitMessageBus();
MicroBitMessageBus();
/**
* Queues the given event to be sent to all registered recipients.
*
* @param The event to send.
* @param The event to send.
*
* n.b. THIS IS NOW WRAPPED BY THE MicroBitEvent CLASS FOR CONVENIENCE...
*
* Example:
* @code
* @code
* MicroBitEvent evt(id,MICROBIT_BUTTON_EVT_DOWN,ticks,false);
* evt.fire();
* //OR YOU CAN DO THIS...
* //OR YOU CAN DO THIS...
* MicroBitEvent evt(id,MICROBIT_BUTTON_EVT_DOWN);
* @endcode
*/
@ -63,7 +63,7 @@ class MicroBitMessageBus : public MicroBitComponent
* IT IS RECOMMENDED THAT ALL EXTERNAL CODE USE THE send() FUNCTIONS INSTEAD OF THIS FUNCTION,
* or the constructors provided by MicroBitEvent.
*
* @param evt The event to send.
* @param evt The event to send.
* @param urgent The type of listeners to process (optional). If set to true, only listeners defined as urgent and non-blocking will be processed
* otherwise, all other (standard) listeners will be processed.
* @return 1 if all matching listeners were processed, 0 if further processing is required.
@ -72,19 +72,19 @@ class MicroBitMessageBus : public MicroBitComponent
/**
* Register a listener function.
*
* @param id The source of messages to listen for. Events sent from any other IDs will be filtered.
*
* @param id The source of messages to listen for. Events sent from any other IDs will be filtered.
* Use MICROBIT_ID_ANY to receive events from all components.
*
* @param value The value of messages to listen for. Events with any other values will be filtered.
* @param value The value of messages to listen for. Events with any other values will be filtered.
* Use MICROBIT_EVT_ANY to receive events of any value.
*
* @param handler The function to call when an event is received.
*
* @return MICROBIT_OK on success MICROBIT_INVALID_PARAMETER
*
*
* Example:
* @code
* @code
* void onButtonBClick()
* {
* //do something
@ -93,22 +93,22 @@ class MicroBitMessageBus : public MicroBitComponent
* @endcode
*/
int listen(int id, int value, void (*handler)(MicroBitEvent), uint16_t flags = MESSAGE_BUS_LISTENER_DEFAULT_FLAGS);
/**
* Register a listener function.
*
* @param id The source of messages to listen for. Events sent from any other IDs will be filtered.
*
* @param id The source of messages to listen for. Events sent from any other IDs will be filtered.
* Use MICROBIT_ID_ANY to receive events from all components.
*
* @param value The value of messages to listen for. Events with any other values will be filtered.
* @param value The value of messages to listen for. Events with any other values will be filtered.
* Use MICROBIT_EVT_ANY to receive events of any value.
*
* @param hander The function to call when an event is received.
*
* @return MICROBIT_OK on success MICROBIT_INVALID_PARAMETER
*
*
* Example:
* @code
* @code
* void onButtonBClick(void *arg)
* {
* //do something
@ -120,26 +120,26 @@ class MicroBitMessageBus : public MicroBitComponent
/**
* Register a listener function.
*
* @param id The source of messages to listen for. Events sent from any other IDs will be filtered.
*
* @param id The source of messages to listen for. Events sent from any other IDs will be filtered.
* Use MICROBIT_ID_ANY to receive events from all components.
*
* @param value The value of messages to listen for. Events with any other values will be filtered.
* @param value The value of messages to listen for. Events with any other values will be filtered.
* Use MICROBIT_EVT_ANY to receive events of any value.
*
* @param hander The function to call when an event is received.
*
* @return MICROBIT_OK on success MICROBIT_INVALID_PARAMETER
*
*
* Example:
* @code
* @code
* void SomeClass::onButtonBClick()
* {
* //do something
* }
*
* SomeClass s = new SomeClass();
* uBit.MessageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, s, &SomeClass::onButtonBClick);
* uBit.MessageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, s, &SomeClass::onButtonBClick);
* @endcode
*/
template <typename T>
@ -149,7 +149,7 @@ class MicroBitMessageBus : public MicroBitComponent
/**
* Unregister a listener function.
* Listners are identified by the Event ID, Event VALUE and handler registered using listen().
*
*
* @param id The Event ID used to register the listener.
* @param value The Event VALUE used to register the listener.
* @param handler The function used to register the listener.
@ -158,21 +158,21 @@ class MicroBitMessageBus : public MicroBitComponent
*
*
* Example:
* @code
* @code
* void onButtonBClick()
* {
* //do something
* }
*
* uBit.MessageBus.ignore(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonBClick);
* uBit.MessageBus.ignore(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonBClick);
* @endcode
*/
int ignore(int id, int value, void (*handler)(MicroBitEvent));
/**
* Unregister a listener function.
* Listners are identified by the Event ID, Event VALUE and handler registered using listen().
*
*
* @param id The Event ID used to register the listener.
* @param value The Event VALUE used to register the listener.
* @param handler The function used to register the listener.
@ -180,13 +180,13 @@ class MicroBitMessageBus : public MicroBitComponent
* @return MICROBIT_OK on success MICROBIT_INVALID_PARAMETER
*
* Example:
* @code
* @code
* void onButtonBClick(void *arg)
* {
* //do something
* }
*
* uBit.MessageBus.ignore(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonBClick);
* uBit.MessageBus.ignore(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonBClick);
* @endcode
*/
int ignore(int id, int value, void (*handler)(MicroBitEvent, void*));
@ -194,7 +194,7 @@ class MicroBitMessageBus : public MicroBitComponent
/**
* Unregister a listener function.
* Listners are identified by the Event ID, Event VALUE and handler registered using listen().
*
*
* @param id The Event ID used to register the listener.
* @param value The Event VALUE used to register the listener.
* @param handler The function used to register the listener.
@ -202,15 +202,15 @@ class MicroBitMessageBus : public MicroBitComponent
* @return MICROBIT_OK on success MICROBIT_INVALID_PARAMETER
*
* Example:
* @code
*
* @code
*
* void SomeClass::onButtonBClick()
* {
* //do something
* }
*
* SomeClass s = new SomeClass();
* uBit.MessageBus.ignore(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, s, &SomeClass::onButtonBClick);
* uBit.MessageBus.ignore(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, s, &SomeClass::onButtonBClick);
* @endcode
*/
template <typename T>
@ -223,6 +223,11 @@ class MicroBitMessageBus : public MicroBitComponent
*/
MicroBitListener *elementAt(int n);
/**
* Destructor for MicroBitMessageBus, so that we deregister ourselves as an idleComponent
*/
~MicroBitMessageBus();
private:
/**
@ -250,7 +255,7 @@ class MicroBitMessageBus : public MicroBitComponent
MicroBitEventQueueItem *evt_queue_tail; // Tail of queued events to be processed.
uint16_t nonce_val; // The last nonce issued.
uint16_t queueLength; // The number of events currently waiting to be processed.
void queueEvent(MicroBitEvent &evt);
MicroBitEventQueueItem* dequeueEvent();
@ -262,10 +267,10 @@ class MicroBitMessageBus : public MicroBitComponent
* A registration function to allow C++ member funcitons (methods) to be registered as an event
* listener.
*
* @param id The source of messages to listen for. Events sent from any other IDs will be filtered.
* @param id The source of messages to listen for. Events sent from any other IDs will be filtered.
* Use MICROBIT_ID_ANY to receive events from all components.
*
* @param value The value of messages to listen for. Events with any other values will be filtered.
* @param value The value of messages to listen for. Events with any other values will be filtered.
* Use MICROBIT_EVT_ANY to receive events of any value.
*
* @param object The object on which the method should be invoked.
@ -291,7 +296,7 @@ int MicroBitMessageBus::listen(uint16_t id, uint16_t value, T* object, void (T::
/**
* Unregister a listener function.
* Listners are identified by the Event ID, Event VALUE and handler registered using listen().
*
*
* @param id The Event ID used to register the listener.
* @param value The Event VALUE used to register the listener.
* @param handler The function used to register the listener.
@ -299,13 +304,13 @@ int MicroBitMessageBus::listen(uint16_t id, uint16_t value, T* object, void (T::
* @return MICROBIT_OK on success MICROBIT_INVALID_PARAMETER
*
* Example:
* @code
* @code
* void onButtonBClick(void *arg)
* {
* //do something
* }
*
* uBit.MessageBus.ignore(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonBClick);
* uBit.MessageBus.ignore(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonBClick);
* @endcode
*/
template <typename T>
@ -322,5 +327,3 @@ int MicroBitMessageBus::ignore(uint16_t id, uint16_t value, T* object, void (T::
#endif

View file

@ -48,32 +48,32 @@ int MicroBitAccelerometer::configure()
// Now configure the accelerometer accordingly.
// First place the device into standby mode, so it can be configured.
result = writeCommand(MMA8653_CTRL_REG1, 0x00);
if (result != 0)
if (result != 0)
return MICROBIT_I2C_ERROR;
// Enable high precisiosn mode. This consumes a bit more power, but still only 184 uA!
result = writeCommand(MMA8653_CTRL_REG2, 0x10);
if (result != 0)
if (result != 0)
return MICROBIT_I2C_ERROR;
// Enable the INT1 interrupt pin.
result = writeCommand(MMA8653_CTRL_REG4, 0x01);
if (result != 0)
if (result != 0)
return MICROBIT_I2C_ERROR;
// Select the DATA_READY event source to be routed to INT1
result = writeCommand(MMA8653_CTRL_REG5, 0x01);
if (result != 0)
if (result != 0)
return MICROBIT_I2C_ERROR;
// Configure for the selected g range.
result = writeCommand(MMA8653_XYZ_DATA_CFG, actualSampleRange->xyz_data_cfg);
if (result != 0)
if (result != 0)
return MICROBIT_I2C_ERROR;
// Bring the device back online, with 10bit wide samples at the requested frequency.
result = writeCommand(MMA8653_CTRL_REG1, actualSampleRate->ctrl_reg1 | 0x01);
if (result != 0)
if (result != 0)
return MICROBIT_I2C_ERROR;
return MICROBIT_OK;
@ -124,13 +124,13 @@ int MicroBitAccelerometer::readCommand(uint8_t reg, uint8_t* buffer, int length)
}
/**
* Constructor.
* Constructor.
* Create an accelerometer representation with the given ID.
* @param id the ID of the new object.
* @param address the default base address of the accelerometer.
* @param address the default base address of the accelerometer.
*
* Example:
* @code
* @code
* accelerometer(MICROBIT_ID_ACCELEROMETER, MMA8653_DEFAULT_ADDR)
* @endcode
*/
@ -161,11 +161,11 @@ MicroBitAccelerometer::MicroBitAccelerometer(uint16_t id, uint16_t address) : sa
}
/**
* Attempts to determine the 8 bit ID from the accelerometer.
* Attempts to determine the 8 bit ID from the accelerometer.
* @return the 8 bit ID returned by the accelerometer, or MICROBIT_I2C_ERROR if the request fails.
*
* Example:
* @code
* @code
* uBit.accelerometer.whoAmI();
* @endcode
*/
@ -174,7 +174,7 @@ int MicroBitAccelerometer::whoAmI()
uint8_t data;
int result;
result = readCommand(MMA8653_WHOAMI, &data, 1);
result = readCommand(MMA8653_WHOAMI, &data, 1);
if (result !=0)
return MICROBIT_I2C_ERROR;
@ -197,7 +197,7 @@ int MicroBitAccelerometer::update()
return MICROBIT_I2C_ERROR;
// read MSB values...
sample.x = data[0];
sample.x = data[0];
sample.y = data[2];
sample.z = data[4];
@ -221,7 +221,7 @@ int MicroBitAccelerometer::update()
// Indicate that pitch and roll data is now stale, and needs to be recalculated if needed.
status &= ~MICROBIT_ACCEL_PITCH_ROLL_VALID;
// Update gesture tracking
// Update gesture tracking
updateGesture();
// Indicate that a new sample is available
@ -257,9 +257,9 @@ BasicGesture MicroBitAccelerometer::instantaneousPosture()
// Test for shake events.
// We detect a shake by measuring zero crossings in each axis. In other words, if we see a strong acceleration to the left followed by
// a string acceleration to the right, then we can infer a shake. Similarly, we can do this for each acxis (left/right, up/down, in/out).
//
//
// If we see enough zero crossings in succession (MICROBIT_ACCELEROMETER_SHAKE_COUNT_THRESHOLD), then we decide that the device
// has been shaken.
// has been shaken.
if ((getX() < -MICROBIT_ACCELEROMETER_SHAKE_TOLERANCE && shake.x) || (getX() > MICROBIT_ACCELEROMETER_SHAKE_TOLERANCE && !shake.x))
{
shakeDetected = true;
@ -289,7 +289,7 @@ BasicGesture MicroBitAccelerometer::instantaneousPosture()
if(--shake.count == 0)
shake.shaken = 0;
}
}
}
if (shake.shaken)
return GESTURE_SHAKE;
@ -334,10 +334,10 @@ void MicroBitAccelerometer::updateGesture()
BasicGesture g = instantaneousPosture();
// Perform some low pass filtering to reduce jitter from any detected effects
if (g == currentGesture)
if (g == currentGesture)
{
if (sigma < MICROBIT_ACCELEROMETER_GESTURE_DAMPING)
sigma++;
sigma++;
}
else
{
@ -350,7 +350,7 @@ void MicroBitAccelerometer::updateGesture()
{
lastGesture = currentGesture;
MicroBitEvent e(MICROBIT_ID_GESTURE, lastGesture);
}
}
}
/**
@ -367,7 +367,7 @@ int MicroBitAccelerometer::setPeriod(int period)
}
/**
* Reads the currently configured sample rate of the accelerometer.
* Reads the currently configured sample rate of the accelerometer.
* @return The time between samples, in milliseconds.
*/
int MicroBitAccelerometer::getPeriod()
@ -389,7 +389,7 @@ int MicroBitAccelerometer::setRange(int range)
}
/**
* Reads the currently configured sample range of the accelerometer.
* Reads the currently configured sample range of the accelerometer.
* @return The sample range, in g.
*/
int MicroBitAccelerometer::getRange()
@ -403,7 +403,7 @@ int MicroBitAccelerometer::getRange()
* @return The force measured in the X axis, in milli-g.
*
* Example:
* @code
* @code
* uBit.accelerometer.getX();
* uBit.accelerometer.getX(RAW);
* @endcode
@ -414,7 +414,7 @@ int MicroBitAccelerometer::getX(MicroBitCoordinateSystem system)
{
case SIMPLE_CARTESIAN:
return -sample.x;
case NORTH_EAST_DOWN:
return sample.y;
@ -430,18 +430,18 @@ int MicroBitAccelerometer::getX(MicroBitCoordinateSystem system)
* @return The force measured in the Y axis, in milli-g.
*
* Example:
* @code
* @code
* uBit.accelerometer.getY();
* uBit.accelerometer.getY(RAW);
* @endcode
*/
*/
int MicroBitAccelerometer::getY(MicroBitCoordinateSystem system)
{
switch (system)
{
case SIMPLE_CARTESIAN:
return -sample.y;
case NORTH_EAST_DOWN:
return -sample.x;
@ -457,11 +457,11 @@ int MicroBitAccelerometer::getY(MicroBitCoordinateSystem system)
* @return The force measured in the Z axis, in milli-g.
*
* Example:
* @code
* @code
* uBit.accelerometer.getZ();
* uBit.accelerometer.getZ(RAW);
* @endcode
*/
*/
int MicroBitAccelerometer::getZ(MicroBitCoordinateSystem system)
{
switch (system)
@ -475,16 +475,16 @@ int MicroBitAccelerometer::getZ(MicroBitCoordinateSystem system)
return sample.z;
}
}
/**
* Provides a rotation compensated pitch of the device, based on the latest update from the accelerometer.
* @return The pitch of the device, in degrees.
*
* Example:
* @code
* @code
* uBit.accelerometer.getPitch();
* @endcode
*/
*/
int MicroBitAccelerometer::getPitch()
{
return (int) ((360*getPitchRadians()) / (2*PI));
@ -503,10 +503,10 @@ float MicroBitAccelerometer::getPitchRadians()
* @return The roll of the device, in degrees.
*
* Example:
* @code
* @code
* uBit.accelerometer.getRoll();
* @endcode
*/
*/
int MicroBitAccelerometer::getRoll()
{
return (int) ((360*getRollRadians()) / (2*PI));
@ -541,10 +541,10 @@ void MicroBitAccelerometer::recalculatePitchRoll()
* @return The last gesture detected.
*
* Example:
* @code
* @code
* if (uBit.accelerometer.getGesture() == SHAKE)
* @endcode
*/
*/
BasicGesture MicroBitAccelerometer::getGesture()
{
return lastGesture;
@ -553,7 +553,7 @@ BasicGesture MicroBitAccelerometer::getGesture()
/**
* periodic callback from MicroBit clock.
* Check if any data is ready for reading by checking the interrupt flag on the accelerometer
*/
*/
void MicroBitAccelerometer::idleTick()
{
// Poll interrupt line from accelerometer.
@ -571,19 +571,27 @@ int MicroBitAccelerometer::isIdleCallbackNeeded()
return !int1;
}
/**
* Destructor for MicroBitAccelerometer, so that we deregister ourselves as an idleComponent
*/
MicroBitAccelerometer::~MicroBitAccelerometer()
{
uBit.removeIdleComponent(this);
}
const MMA8653SampleRangeConfig MMA8653SampleRange[MMA8653_SAMPLE_RANGES] = {
{2, 0},
{4, 1},
{2, 0},
{4, 1},
{8, 2}
};
const MMA8653SampleRateConfig MMA8653SampleRate[MMA8653_SAMPLE_RATES] = {
{1250, 0x00},
{2500, 0x08},
{5000, 0x10},
{10000, 0x18},
{20000, 0x20},
{80000, 0x28},
{1250, 0x00},
{2500, 0x08},
{5000, 0x10},
{10000, 0x18},
{20000, 0x20},
{80000, 0x28},
{160000, 0x30},
{640000, 0x38}
};

View file

@ -1,7 +1,7 @@
#include "MicroBit.h"
/**
* Constructor.
* Constructor.
* Create a compass representation with the given ID.
* @param id the event ID of the compass object.
* @param address the default address for the compass register
@ -22,17 +22,17 @@ MicroBitCompass::MicroBitCompass(uint16_t id, uint16_t address) : average(), sam
{
this->id = id;
this->address = address;
// We presume the device calibrated until the average values are read.
this->status = 0x01;
// Select 10Hz update rate, with oversampling, and enable the device.
this->samplePeriod = 100;
this->configure();
// Assume that we have no calibraiton information.
// Assume that we have no calibraiton information.
status &= ~MICROBIT_COMPASS_STATUS_CALIBRATED;
// Indicate that we're up and running.
uBit.flags |= MICROBIT_FLAG_COMPASS_RUNNING;
}
@ -50,7 +50,7 @@ int MicroBitCompass::writeCommand(uint8_t reg, uint8_t value)
uint8_t command[2];
command[0] = reg;
command[1] = value;
return uBit.i2c.write(address, (const char *)command, 2);
}
@ -100,7 +100,7 @@ int MicroBitCompass::read16(uint8_t reg)
cmd[0] = 0x00;
cmd[1] = 0x00;
result = uBit.i2c.read(address, (char *)cmd, 2);
if (result !=0)
return MICROBIT_I2C_ERROR;
@ -132,10 +132,10 @@ int MicroBitCompass::read8(uint8_t reg)
* Gets the current heading of the device, relative to magnetic north.
* If the compass is not calibrated, it will raise the MICROBIT_COMPASS_EVT_CALIBRATE event.
* Users wishing to implement their own calibration algorithms should listen for this event,
* using MESSAGE_BUS_LISTENER_IMMEDIATE model. This ensures that calibration is complete before
* the user program continues.
*
* @return the current heading, in degrees. Or MICROBIT_CALIBRATION_IN_PROGRESS if the compass is calibrating.
* using MESSAGE_BUS_LISTENER_IMMEDIATE model. This ensures that calibration is complete before
* the user program continues.
*
* @return the current heading, in degrees. Or MICROBIT_CALIBRATION_IN_PROGRESS if the compass is calibrating.
*
* Example:
* @code
@ -147,7 +147,7 @@ int MicroBitCompass::heading()
float bearing;
if(status & MICROBIT_COMPASS_STATUS_CALIBRATING)
return MICROBIT_CALIBRATION_IN_PROGRESS;
return MICROBIT_CALIBRATION_IN_PROGRESS;
if(!(status & MICROBIT_COMPASS_STATUS_CALIBRATED))
calibrate();
@ -165,11 +165,11 @@ int MicroBitCompass::heading()
float sinTheta = sin(theta);
float cosTheta = cos(theta);
bearing = (360*atan2(z*sinPhi - y*cosPhi, x*cosTheta + y*sinTheta*sinPhi + z*sinTheta*cosPhi)) / (2*PI);
bearing = (360*atan2(z*sinPhi - y*cosPhi, x*cosTheta + y*sinTheta*sinPhi + z*sinTheta*cosPhi)) / (2*PI);
if (bearing < 0)
bearing += 360.0;
return (int) bearing;
}
@ -179,7 +179,7 @@ int MicroBitCompass::heading()
*/
void MicroBitCompass::idleTick()
{
// Poll interrupt line from accelerometer (Active HI).
// Poll interrupt line from accelerometer (Active HI).
// Interrupt is cleared on data read of MAG_OUT_X_MSB.
if(int1)
{
@ -225,7 +225,7 @@ int MicroBitCompass::getX(MicroBitCoordinateSystem system)
* @code
* uBit.compass.getY();
* @endcode
*/
*/
int MicroBitCompass::getY(MicroBitCoordinateSystem system)
{
switch (system)
@ -235,7 +235,7 @@ int MicroBitCompass::getY(MicroBitCoordinateSystem system)
case NORTH_EAST_DOWN:
return (sample.x - average.x);