A compilation of some hardware sensors and their shared programming interfaces.

Embed: (wiki syntax)

« Back to documentation index

MMA8451Q Class Reference

This class allows for easy control of an MMA8451Q accelerometer IC. More...

#include <MMA8451Q.h>

Inherits I2CSensor, PeriodicSensor, and SleepableSensor.

Public Types

enum  smpl_rate_t
 

Enumeration of allowed output data sampling rates.

More...
enum  scale_t
 

Enumeration of allowed dynamic ranges (full scale) of the accelerometer data.

More...

Public Member Functions

 MMA8451Q (PinName sda, PinName scl, int i2c_addr)
void selfInit ()
 Self-initialization to some nice preset.
void reset ()
 Does a soft reset of the device.
uint8_t whoAmI ()
 Implements the pure virtual method of the parent I2CSensor class.
bool isActive ()
void setActive (bool activate)
uint8_t getSystemMode ()
bool is14bDataEnabled ()
void set14bData (bool enable)
smpl_rate_t getOutputDataRate ()
void setOutputDataRate (smpl_rate_t rate)
scale_t getScale ()
void setScale (scale_t scale)
void enableDataReadyInterrupt (bool enable, bool pinSelect)
 Enables an interrupt output signal from the device whenever data (XYZ) is generated.
int16_t getX (bool sampleNow)
int16_t getY (bool sampleNow)
int16_t getZ (bool sampleNow)
float getFloatX (bool sampleNow)
float getFloatY (bool sampleNow)
float getFloatZ (bool sampleNow)
PinName getSDAPin ()
PinName getSCLPin ()
uint8_t getDeviceI2CAddress ()
uint8_t getRegister (const uint8_t reg_addr)
 Read an 8-bit register.
uint16_t getRegister16b (const uint8_t reg_addr)
 Read a 16-bit register.
void setRegister (const uint8_t reg_addr, const uint8_t data)
 Set an 8-bit register.
void setRegister16b (const uint8_t reg_addr, const uint16_t data)
 Set a 16-bit register.
bool isDataReady ()
void enableBackgroundSampling (bool enable, float sample_period)
 Control the background sampling of the device via interrupts.
bool isBackgroundSamplingEnabled ()
float getSamplePeriod ()
float getMinSamplePeriod ()

Protected Member Functions

int __readReg (const uint8_t reg_addr, uint8_t *data, int len)
int __writeReg (const uint8_t *data, int total_len)

Detailed Description

This class allows for easy control of an MMA8451Q accelerometer IC.

Definition at line 18 of file MMA8451Q.h.


Member Enumeration Documentation

enum scale_t

Enumeration of allowed dynamic ranges (full scale) of the accelerometer data.

G2: +/- 2G (0.25 mg/level at 14b, 15.6 mg/level at 8b) G4: +/- 4G (0.5 mg/level at 14b, 31.25 mg/level at 8b) G8: +/- 8G (1.0 mg/level at 14b, 62.5 mg/level at 8b)

Definition at line 40 of file MMA8451Q.h.

Enumeration of allowed output data sampling rates.

Definition at line 23 of file MMA8451Q.h.


Constructor & Destructor Documentation

MMA8451Q ( PinName  sda,
PinName  scl,
int  i2c_addr 
)
Parameters:
sdathe pin identifier for SDA I2C signal
sclthe pin identifier for SCL I2C signal
i2c_addrthe 8-bit I2C address for this device. Note that LSB is a don't care.

Definition at line 17 of file MMA8451Q.cpp.


Member Function Documentation

int __readReg ( const uint8_t  reg_addr,
uint8_t *  data,
int  len 
) [protected, inherited]
Parameters:
reg_addr8-bit register address inside the device
data8-bit data that will be read from the register. This pointer MUST be valid. This array MUST be at least as long as len.
lentotal number of bytes to read. len must be >= 1.
Returns:
0 on success, otherwise error code from I2C

Definition at line 57 of file I2CSensor.cpp.

int __writeReg ( const uint8_t *  data,
int  total_len 
) [protected, inherited]
Parameters:
data8-bit data that will be written to the register. This pointer MUST be valid. This array MUST be at least as long as total_len. data[0] should be set to the 8-bit register address. The data payload should start at index 1.
total_lentotal length of the data array, which is the length of the payload + 1 for the register address. total_len must be >= 2.
Returns:
0 on success, otherwise error code from I2C

Definition at line 72 of file I2CSensor.cpp.

void enableBackgroundSampling ( bool  enable,
float  sample_period 
) [inherited]

Control the background sampling of the device via interrupts.

If disabled, the device can still be sampled using the getter methods directly.

Parameters:
enableif true, enables background sampling with the given sample period.
sample_periodsampling period in seconds. Must be at least 0.005 sec unless enable is false, in which case it is don't care.

Definition at line 36 of file PeriodicSensor.cpp.

void enableDataReadyInterrupt ( bool  enable,
bool  pinSelect 
)

Enables an interrupt output signal from the device whenever data (XYZ) is generated.

Parameters:
enableEnables/disables interrupt
pinSelectif false, INT2. if true, INT1

Definition at line 170 of file MMA8451Q.cpp.

uint8_t getDeviceI2CAddress (  ) [inherited]
Returns:
Device I2C address (LSB always 0 in this case, it is don't care)

Definition at line 28 of file I2CSensor.cpp.

float getFloatX ( bool  sampleNow )
Parameters:
sampleNowif true, queries the device for the sample and returns it. if false, gets the last queried value. The latter is preferred if this object is set up to sample using interrupts. Returns the latest X data reading as a float in Gs

Definition at line 284 of file MMA8451Q.cpp.

float getFloatY ( bool  sampleNow )
Parameters:
sampleNowif true, queries the device for the sample and returns it. if false, gets the last queried value. The latter is preferred if this object is set up to sample using interrupts. Returns the latest Y data reading as a float in Gs

Definition at line 288 of file MMA8451Q.cpp.

float getFloatZ ( bool  sampleNow )
Parameters:
sampleNowif true, queries the device for the sample and returns it. if false, gets the last queried value. The latter is preferred if this object is set up to sample using interrupts. Returns the latest Z data reading as a float in Gs

Definition at line 292 of file MMA8451Q.cpp.

float getMinSamplePeriod (  ) [inherited]
Returns:
the minimum sample period in seconds

Definition at line 57 of file PeriodicSensor.cpp.

MMA8451Q::smpl_rate_t getOutputDataRate (  )
Returns:
an enumerated value corresponding to the current output data sample rate

Definition at line 90 of file MMA8451Q.cpp.

uint8_t getRegister ( const uint8_t  reg_addr ) [inherited]

Read an 8-bit register.

Parameters:
reg_addrthe register in the device
Returns:
The raw value from the register specified by reg_addr.

Definition at line 30 of file I2CSensor.cpp.

uint16_t getRegister16b ( const uint8_t  reg_addr ) [inherited]

Read a 16-bit register.

Parameters:
reg_addrthe register in the device
Returns:
The raw value from the register specified by reg_addr.

Definition at line 36 of file I2CSensor.cpp.

float getSamplePeriod (  ) [inherited]
Returns:
the sample period in seconds, if background sampling is enabled. Else, non-positive return.

Definition at line 53 of file PeriodicSensor.cpp.

MMA8451Q::scale_t getScale (  )
Returns:
an enumerated value corresponding to the full scale (range) of the output data

Definition at line 139 of file MMA8451Q.cpp.

PinName getSCLPin (  ) [inherited]
Returns:
I2C SCL pin ID

Definition at line 26 of file I2CSensor.cpp.

PinName getSDAPin (  ) [inherited]
Returns:
I2C SDA pin ID

Definition at line 24 of file I2CSensor.cpp.

uint8_t getSystemMode (  )
Returns:
the 8-bit system mode status

Definition at line 67 of file MMA8451Q.cpp.

int16_t getX ( bool  sampleNow )
Parameters:
sampleNowif true, queries the device for the sample and returns it. if false, gets the last queried value. The latter is preferred if this object is set up to sample using interrupts.
Returns:
a 14-bit value representing the latest data sample for the X dimension, centered at 0.

Definition at line 203 of file MMA8451Q.cpp.

int16_t getY ( bool  sampleNow )
Parameters:
sampleNowif true, queries the device for the sample and returns it. if false, gets the last queried value. The latter is preferred if this object is set up to sample using interrupts.
Returns:
a 14-bit value representing the latest data sample for the Y dimension, centered at 0.

Definition at line 230 of file MMA8451Q.cpp.

int16_t getZ ( bool  sampleNow )
Parameters:
sampleNowif true, queries the device for the sample and returns it. if false, gets the last queried value. The latter is preferred if this object is set up to sample using interrupts.
Returns:
a 14-bit value representing the latest data sample for the Z dimension, centered at 0.

Definition at line 257 of file MMA8451Q.cpp.

bool is14bDataEnabled (  )
Returns:
true if the device is generating 14b resolution data, otherwise 8b

Definition at line 71 of file MMA8451Q.cpp.

bool isActive (  )
Returns:
true if the device is active

Definition at line 52 of file MMA8451Q.cpp.

bool isBackgroundSamplingEnabled (  ) [inherited]
Returns:
true if background sampling is enabled.

Definition at line 49 of file PeriodicSensor.cpp.

bool isDataReady (  ) [inherited]
Returns:
true if there is new data ready to be retrieved.

Definition at line 32 of file PeriodicSensor.cpp.

void reset (  )

Does a soft reset of the device.

Definition at line 42 of file MMA8451Q.cpp.

void selfInit (  )

Self-initialization to some nice preset.

You must ensure the device is first deactivated using setActive().

Definition at line 33 of file MMA8451Q.cpp.

void set14bData ( bool  enable )
Parameters:
enableif true, sets 14b data, otherwise 8b data

Definition at line 75 of file MMA8451Q.cpp.

void setActive ( bool  activate )
Parameters:
activateif true, enables the device, else disables it

Definition at line 56 of file MMA8451Q.cpp.

void setOutputDataRate ( MMA8451Q::smpl_rate_t  rate )
Parameters:
theenumerated value corresponding to the output data sample rate to use

Definition at line 94 of file MMA8451Q.cpp.

void setRegister ( const uint8_t  reg_addr,
const uint8_t  data 
) [inherited]

Set an 8-bit register.

Parameters:
reg_addrthe register in the device
datathe byte to write to the register

Definition at line 43 of file I2CSensor.cpp.

void setRegister16b ( const uint8_t  reg_addr,
const uint16_t  data 
) [inherited]

Set a 16-bit register.

Parameters:
reg_addrthe register in the device
datathe byte to write to the register

Definition at line 48 of file I2CSensor.cpp.

void setScale ( MMA8451Q::scale_t  scale )
Parameters:
scalesets the sample scale via the enumerated value

Definition at line 143 of file MMA8451Q.cpp.

uint8_t whoAmI (  )

Implements the pure virtual method of the parent I2CSensor class.

Returns:
the 8-bit device identifier.

Definition at line 48 of file MMA8451Q.cpp.