Attempting to publish a tree

Dependencies:   BLE_API mbed-dev-bin nRF51822

Fork of microbit-dal by Lancaster University

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.
BasicGesture getGesture ()
 Retrieves the last recorded gesture.
virtual void idleTick ()
 A periodic callback invoked by the fiber scheduler idle thread.
virtual int isIdleCallbackNeeded ()
 Returns 0 or 1.
 ~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 168 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 691 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.

BasicGesture 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 663 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 453 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 579 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 593 of file MicroBitAccelerometer.cpp.

int getRange (  )

Reads the currently configured sample range of the accelerometer.

Returns:
The sample range, in g.

Definition at line 484 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 610 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 624 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 500 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 527 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 554 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 673 of file MicroBitAccelerometer.cpp.

int isIdleCallbackNeeded (  ) [virtual]

Returns 0 or 1.

1 indicates data is waiting to be read, zero means data is not ready to be read.

We check if any data is ready for reading by checking the interrupt flag on the accelerometer.

Reimplemented from MicroBitComponent.

Definition at line 683 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 442 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 473 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 118 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 240 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 215 of file MicroBitAccelerometer.cpp.