mbed.org local branch of microbit-dal. The real version lives in git at https://github.com/lancaster-university/microbit-dal

Dependencies:   BLE_API nRF51822 mbed-dev-bin

Dependents:   microbit Microbit IoTChallenge1 microbit ... more

Embed: (wiki syntax)

« Back to documentation index

MicroBitAccelerometer Class Reference

MicroBitAccelerometer Class Reference

Class definition for MicroBit Accelerometer. More...

#include <MicroBitAccelerometer.h>

Inherits MicroBitComponent.

Public Member Functions

 MicroBitAccelerometer (MicroBitI2C &_i2c, uint16_t address=MMA8653_DEFAULT_ADDR, uint16_t id=MICROBIT_ID_ACCELEROMETER)
 Constructor.
int configure ()
 Configures the accelerometer for G range and sample rate defined in this object.
int updateSample ()
 Reads the acceleration data from the accelerometer, and stores it in our buffer.
int setPeriod (int period)
 Attempts to set the sample rate of the accelerometer to the specified value (in ms).
int getPeriod ()
 Reads the currently configured sample rate of the accelerometer.
int setRange (int range)
 Attempts to set the sample range of the accelerometer to the specified value (in g).
int getRange ()
 Reads the currently configured sample range of the accelerometer.
int whoAmI ()
 Attempts to read the 8 bit ID from the accelerometer, this can be used for validation purposes.
int getX (MicroBitCoordinateSystem system=SIMPLE_CARTESIAN)
 Reads the value of the X axis from the latest update retrieved from the accelerometer.
int getY (MicroBitCoordinateSystem system=SIMPLE_CARTESIAN)
 Reads the value of the Y axis from the latest update retrieved from the accelerometer.
int getZ (MicroBitCoordinateSystem system=SIMPLE_CARTESIAN)
 Reads the value of the Z axis from the latest update retrieved from the accelerometer.
int getPitch ()
 Provides a rotation compensated pitch of the device, based on the latest update retrieved from the accelerometer.
float getPitchRadians ()
 Provides a rotation compensated pitch of the device, based on the latest update retrieved from the accelerometer.
int getRoll ()
 Provides a rotation compensated roll of the device, based on the latest update retrieved from the accelerometer.
float getRollRadians ()
 Provides a rotation compensated roll of the device, based on the latest update retrieved from the accelerometer.
uint16_t getGesture ()
 Retrieves the last recorded gesture.
virtual void idleTick ()
 A periodic callback invoked by the fiber scheduler idle thread.
 ~MicroBitAccelerometer ()
 Destructor for MicroBitButton, where we deregister this instance from the array of fiber components.
virtual void systemTick ()
 The system timer will call this member function once the component has been added to the array of system components using system_timer_add_component.

Detailed Description

Class definition for MicroBit Accelerometer.

Represents an implementation of the Freescale MMA8653 3 axis accelerometer Also includes basic data caching and on demand activation.

Definition at line 159 of file MicroBitAccelerometer.h.


Constructor & Destructor Documentation

MicroBitAccelerometer ( MicroBitI2C _i2c,
uint16_t  address = MMA8653_DEFAULT_ADDR,
uint16_t  id = MICROBIT_ID_ACCELEROMETER 
)

Constructor.

Create a software abstraction of an accelerometer.

Parameters:
_i2can instance of MicroBitI2C used to communicate with the onboard accelerometer.
addressthe default I2C address of the accelerometer. Defaults to: MMA8653_DEFAULT_ADDR.
idthe unique EventModel id of this component. Defaults to: MICROBIT_ID_ACCELEROMETER
 MicroBitI2C i2c = MicroBitI2C(I2C_SDA0, I2C_SCL0);

 MicroBitAccelerometer accelerometer = MicroBitAccelerometer(i2c);

Definition at line 179 of file MicroBitAccelerometer.cpp.

Destructor for MicroBitButton, where we deregister this instance from the array of fiber components.

Destructor for MicroBitAccelerometer, where we deregister from the array of fiber components.

Definition at line 736 of file MicroBitAccelerometer.cpp.


Member Function Documentation

int configure (  )

Configures the accelerometer for G range and sample rate defined in this object.

Class definition for MicroBit Accelerometer.

The nearest values are chosen to those defined that are supported by the hardware. The instance variables are then updated to reflect reality.

Returns:
MICROBIT_OK on success, MICROBIT_I2C_ERROR if the accelerometer could not be configured.

Represents an implementation of the Freescale MMA8653 3 axis accelerometer Also includes basic data caching and on demand activation. Configures the accelerometer for G range and sample rate defined in this object. The nearest values are chosen to those defined that are supported by the hardware. The instance variables are then updated to reflect reality.

Returns:
MICROBIT_OK on success, MICROBIT_I2C_ERROR if the accelerometer could not be configured.

Definition at line 48 of file MicroBitAccelerometer.cpp.

uint16_t getGesture (  )

Retrieves the last recorded gesture.

Returns:
The last gesture that was detected.

Example:

 MicroBitDisplay display;

 if (accelerometer.getGesture() == SHAKE)
     display.scroll("SHAKE!");

Definition at line 718 of file MicroBitAccelerometer.cpp.

int getPeriod (  )

Reads the currently configured sample rate of the accelerometer.

Returns:
The time between samples, in milliseconds.

Definition at line 508 of file MicroBitAccelerometer.cpp.

int getPitch (  )

Provides a rotation compensated pitch of the device, based on the latest update retrieved from the accelerometer.

Returns:
The pitch of the device, in degrees.
 accelerometer.getPitch();

Definition at line 634 of file MicroBitAccelerometer.cpp.

float getPitchRadians (  )

Provides a rotation compensated pitch of the device, based on the latest update retrieved from the accelerometer.

Returns:
The pitch of the device, in radians.
 accelerometer.getPitchRadians();

Definition at line 648 of file MicroBitAccelerometer.cpp.

int getRange (  )

Reads the currently configured sample range of the accelerometer.

Returns:
The sample range, in g.

Definition at line 539 of file MicroBitAccelerometer.cpp.

int getRoll (  )

Provides a rotation compensated roll of the device, based on the latest update retrieved from the accelerometer.

Returns:
The roll of the device, in degrees.
 accelerometer.getRoll();

Definition at line 665 of file MicroBitAccelerometer.cpp.

float getRollRadians (  )

Provides a rotation compensated roll of the device, based on the latest update retrieved from the accelerometer.

Returns:
The roll of the device, in radians.
 accelerometer.getRollRadians();

Definition at line 679 of file MicroBitAccelerometer.cpp.

int getX ( MicroBitCoordinateSystem  system = SIMPLE_CARTESIAN )

Reads the value of the X axis from the latest update retrieved from the accelerometer.

Parameters:
systemThe coordinate system to use. By default, a simple cartesian system is provided.
Returns:
The force measured in the X axis, in milli-g.
 accelerometer.getX();

Definition at line 555 of file MicroBitAccelerometer.cpp.

int getY ( MicroBitCoordinateSystem  system = SIMPLE_CARTESIAN )

Reads the value of the Y axis from the latest update retrieved from the accelerometer.

Returns:
The force measured in the Y axis, in milli-g.
 accelerometer.getY();

Definition at line 582 of file MicroBitAccelerometer.cpp.

int getZ ( MicroBitCoordinateSystem  system = SIMPLE_CARTESIAN )

Reads the value of the Z axis from the latest update retrieved from the accelerometer.

Returns:
The force measured in the Z axis, in milli-g.
 accelerometer.getZ();

Definition at line 609 of file MicroBitAccelerometer.cpp.

void idleTick (  ) [virtual]

A periodic callback invoked by the fiber scheduler idle thread.

Internally calls updateSample().

Reimplemented from MicroBitComponent.

Definition at line 728 of file MicroBitAccelerometer.cpp.

int setPeriod ( int  period )

Attempts to set the sample rate of the accelerometer to the specified value (in ms).

Parameters:
periodthe requested time between samples, in milliseconds.
Returns:
MICROBIT_OK on success, MICROBIT_I2C_ERROR is the request fails.
 // sample rate is now 20 ms.
 accelerometer.setPeriod(20);
Note:
The requested rate may not be possible on the hardware. In this case, the nearest lower rate is chosen.

Definition at line 497 of file MicroBitAccelerometer.cpp.

int setRange ( int  range )

Attempts to set the sample range of the accelerometer to the specified value (in g).

Parameters:
rangeThe requested sample range of samples, in g.
Returns:
MICROBIT_OK on success, MICROBIT_I2C_ERROR is the request fails.
 // the sample range of the accelerometer is now 8G.
 accelerometer.setRange(8);
Note:
The requested range may not be possible on the hardware. In this case, the nearest lower range is chosen.

Definition at line 528 of file MicroBitAccelerometer.cpp.

virtual void systemTick (  ) [virtual, inherited]

The system timer will call this member function once the component has been added to the array of system components using system_timer_add_component.

This callback will be in interrupt context.

Reimplemented in MicroBitSystemTimerCallback, MicroBitButton, and MicroBitDisplay.

Definition at line 125 of file MicroBitComponent.h.

int updateSample (  )

Reads the acceleration data from the accelerometer, and stores it in our buffer.

This only happens if the accelerometer indicates that it has new data via int1.

On first use, this member function will attempt to add this component to the list of fiber components in order to constantly update the values stored by this object.

This technique is called lazy instantiation, and it means that we do not obtain the overhead from non-chalantly adding this component to fiber components.

Returns:
MICROBIT_OK on success, MICROBIT_I2C_ERROR if the read request fails.

Definition at line 244 of file MicroBitAccelerometer.cpp.

int whoAmI (  )

Attempts to read the 8 bit ID from the accelerometer, this can be used for validation purposes.

Returns:
the 8 bit ID returned by the accelerometer, or MICROBIT_I2C_ERROR if the request fails.
 accelerometer.whoAmI();

Definition at line 219 of file MicroBitAccelerometer.cpp.