An access library for the MMA8452 accelerometer
Controlled by I2C
Functions | Variables
mma8452.c File Reference

I2C based access library for the MMA8452 accelerometer using pigpio for I2C access
Copyright U. Raich Nov. 2018
This program is part of a course on embedded systems held at the University of Cape Coast, Ghana in 2018
It is released under the GNU Public License For details please see https://www.gnu.org/licenses/gpl.html. More...

#include <stdio.h>
#include "mma8452.h"
Include dependency graph for mma8452.c:

Functions

void mma8452SetDebug (bool onOff)
 switching debug messages on or off More...
 
int mma8452Setup (void)
 Setup the accelerometer on the I2C bus of the RPi Opens the connection to the pigoio daemon. More...
 
void mma8452Close (void)
 Closes the program in an orderly fashion. Disconnects from the pigpiod daemon.
 
int mma8452Init (void)
 Checks if the accelerometer's device WHO_AM_I register shows the right value of 0x2a. More...
 
int mma8452ReadXYZ (int16_t *buf)
 Function to read raw X,Y,Z acceleration values. The mma8452 is put into active mode. More...
 
int mma8452ReadXYZCounts (int16_t *buf)
 Function to read X,Y,Z axes values as integer counts. This calls mma8452ReadXYZ and transforms the result to right adjusted signed integer values. More...
 
int mma8452ReadXYZ_G (float *buf)
 Function to read X,Y,Z axes values in units of g. This calls mma8452ReadXYZ and calculates the corresponding floating point values in units if g. More...
 
bool mma8452GetFastRead ()
 Checks if the mma8452 is in fast read mode. In fast read mode only the 8 most significant bits are read out. More...
 
int mma8452SetFastRead (bool onOff)
 Sets or removes mma8452 fast read mode. In fast read mode only the 8 most significant bits are read out. More...
 
int mma8452GetRange ()
 Gets the dynamic range The mma8452 has 3 dynamic ranges: 2g, 4g, 8g full scale. More...
 
int mma8452SetRange (int range)
 Sets the dynamic range The mma8452 has 3 dynamic ranges: 2g, 4g, 8g full scale. More...
 
char * mma8452Range2String (int range)
 Converts the dynamic range code into a humanly readable text. More...
 
char * mma8452DataRate2String (int dataRate)
 Converts the data rate code into a humanly readable text. More...
 
int mma8452GetDataRate (void)
 Gets the data rate code. More...
 
int mma8452SetDataRate (int dataRate)
 Sets the data rate code. More...
 
char * mma8452DataErrorCode2String (int errorCode)
 Converts the error code into a humanly readable text. More...
 
int mma8452GetMode (void)
 Gets the running mode. More...
 
char * mma8452Mode2String (int mode)
 Converts the running mode code into a humanly readable string. More...
 
int mma8452GetActiveStandby ()
 Checks if the mma8452 is in standby or active mode. More...
 
int mma8452SetActive ()
 Sets the mma8452 to active mode. In this mode acceleration data are aquired. More...
 
int mma8452SetStandby ()
 Sets the mma8452 to standby mode. In this mode configuration registers can be programmed. More...
 
int mma8452EnablePL (bool onOff)
 Enables or disables Portrait / Landscape mode. PL mode allows to find out if the device is horizontal or vertical and if is is upside down. More...
 
bool mma8452IsPLEnabled ()
 Checks if Portrait / Landscape mode is enabled. PL mode allows to find out if the device is horizontal or vertical and if is is upside down. More...
 
int mma8452GetOrientation (void)
 Get a code for the orientation. More...
 
char * mma8452Orientation2String (int orientation)
 Converts the orientation code into a humanly readable string. More...
 

Variables

int handle
 
int pi
 
bool mma8452Debug = true
 
unsigned char temp
 
char * mm8452DataRangeString []
 
char * mm8452ErrorString []
 
char * mma8452ModeString []
 
char * mma8452RangeString []
 
char * mma8452OrientationString []
 

Detailed Description

I2C based access library for the MMA8452 accelerometer using pigpio for I2C access
Copyright U. Raich Nov. 2018
This program is part of a course on embedded systems held at the University of Cape Coast, Ghana in 2018
It is released under the GNU Public License For details please see https://www.gnu.org/licenses/gpl.html.

Reads out the mma8452 accelerometer using the libmm8542.so shared library. It Copyright U. Raich Nov. 2018 This program is part of a course on embedded systems held at the University of Cape Coast, Ghana in 2018 It is released under the GNU Public License For details please see https://www.gnu.org/licenses/gpl.html.

:

Author
Uli Raich

Function Documentation

◆ mma8452DataErrorCode2String()

char* mma8452DataErrorCode2String ( int  errorCode)

Converts the error code into a humanly readable text.

Parameters
datarate: error code
Return values
dynamic_range_stringa string describing the error

◆ mma8452DataRate2String()

char* mma8452DataRate2String ( int  dataRate)

Converts the data rate code into a humanly readable text.

Parameters
data_ratethe data rate code
Return values
dynamic_range_stringa string describing the dynamic range

◆ mma8452EnablePL()

int mma8452EnablePL ( bool  onOff)

Enables or disables Portrait / Landscape mode. PL mode allows to find out if the device is horizontal or vertical and if is is upside down.

Parameters

◆ mma8452GetActiveStandby()

int mma8452GetActiveStandby ( void  )

Checks if the mma8452 is in standby or active mode.

Return values
active_standbyMMA8452_ACTIVE if active, MMA8452_STANDBY if in standby mode or sleep

◆ mma8452GetDataRate()

int mma8452GetDataRate ( void  )

Gets the data rate code.

Return values
data_ratecode for the data rate

◆ mma8452GetFastRead()

bool mma8452GetFastRead ( void  )

Checks if the mma8452 is in fast read mode. In fast read mode only the 8 most significant bits are read out.

Return values
fast_modetrue if in fast mode, false otherwise

◆ mma8452GetMode()

int mma8452GetMode ( void  )

Gets the running mode.

Return values
running_modecode for the running mode, which can be standby, wake or sleep

◆ mma8452GetOrientation()

int mma8452GetOrientation ( void  )

Get a code for the orientation.

Return values
orientationcode for the orientation, returns -1 of PL mode is not enabled

◆ mma8452GetRange()

int mma8452GetRange ( void  )

Gets the dynamic range The mma8452 has 3 dynamic ranges: 2g, 4g, 8g full scale.

Return values
rangea code for the dynamic range: MMA8452_FULL_SCALE_2G, MMA8452_FULL_SCALE_4G, MMA8452_FULL_SCALE_8G

◆ mma8452Init()

int mma8452Init ( void  )

Checks if the accelerometer's device WHO_AM_I register shows the right value of 0x2a.

Return values
error_codeMMA8452_SUCCESS if the identification was found MMA8452_NOT_FOUND otherwise

◆ mma8452IsPLEnabled()

bool mma8452IsPLEnabled ( void  )

Checks if Portrait / Landscape mode is enabled. PL mode allows to find out if the device is horizontal or vertical and if is is upside down.

Parameters

◆ mma8452Mode2String()

char* mma8452Mode2String ( int  mode)

Converts the running mode code into a humanly readable string.

Parameters

◆ mma8452Orientation2String()

char* mma8452Orientation2String ( int  orientation)

Converts the orientation code into a humanly readable string.

Parameters

◆ mma8452Range2String()

char* mma8452Range2String ( int  range)

Converts the dynamic range code into a humanly readable text.

Parameters
datarate: the dynamic range code
Return values
dynamic_range_stringa string describing the dynamic range

◆ mma8452ReadXYZ()

int mma8452ReadXYZ ( int16_t *  buf)

Function to read raw X,Y,Z acceleration values. The mma8452 is put into active mode.

Parameters
pointerto a buffer of 3 int16_t values buf[0] = X, buf[1] = Y, buf[2] = z The acceleration values are signed 12 bit, left adjusted values
Return values
anerror_code: MMA8452_SUCCESS if successful

◆ mma8452ReadXYZ_G()

int mma8452ReadXYZ_G ( float *  buf)

Function to read X,Y,Z axes values in units of g. This calls mma8452ReadXYZ and calculates the corresponding floating point values in units if g.

Parameters
fbufpointer to a buffer of 3 float values buf[0] = X, buf[1] = Y, buf[2] = z The acceleration values are signed 12 bit, left adjusted values
Return values
error_codeMMA8452_SUCCESS if successful

◆ mma8452ReadXYZCounts()

int mma8452ReadXYZCounts ( int16_t *  buf)

Function to read X,Y,Z axes values as integer counts. This calls mma8452ReadXYZ and transforms the result to right adjusted signed integer values.

Parameters
bufpointer to a buffer of 3 int16_t values buf[0] = X, buf[1] = Y, buf[2] = z The acceleration values are signed 12 bit, left adjusted values
Return values
error_codeMMA8452_SUCCESS if successful

◆ mma8452SetActive()

int mma8452SetActive ( )

Sets the mma8452 to active mode. In this mode acceleration data are aquired.

Return values
error_codeMMA8452_SUCCESS if successful an error code otherwise

◆ mma8452SetDataRate()

int mma8452SetDataRate ( int  dataRate)

Sets the data rate code.

Parameters

◆ mma8452SetDebug()

void mma8452SetDebug ( bool  onOff)

switching debug messages on or off

Parameters
onOffto switch debug mode on or off

◆ mma8452SetFastRead()

int mma8452SetFastRead ( bool  onOff)

Sets or removes mma8452 fast read mode. In fast read mode only the 8 most significant bits are read out.

Parameters

◆ mma8452SetRange()

int mma8452SetRange ( int  range)

Sets the dynamic range The mma8452 has 3 dynamic ranges: 2g, 4g, 8g full scale.

Parameters
rangecode for the dynamic range
Return values
error_codeMMA8452_SUCCESS if successful, a negative error code otherwise

◆ mma8452SetStandby()

int mma8452SetStandby ( )

Sets the mma8452 to standby mode. In this mode configuration registers can be programmed.

Return values
MMA8452_SUCCESSif successful an error code otherwise

◆ mma8452Setup()

int mma8452Setup ( void  )

Setup the accelerometer on the I2C bus of the RPi Opens the connection to the pigoio daemon.

Return values
error_codeMMA8452_SUCCESS if successful, MMA8452_PIGPIO_INIT_FAILURE otherwise

Variable Documentation

◆ mm8452DataRangeString

char* mm8452DataRangeString[]
Initial value:
= {
"1.25 ms",
"2.5 ms",
"5 ms",
"10 ms",
"20 ms",
"80 ms",
"160 ms",
"640 ms",
}

◆ mm8452ErrorString

char* mm8452ErrorString[]
Initial value:
= {
"success",
"pigpio init failure",
"I2C init failure",
"mma8452 not found",
"invalid parameter",
"not standby",
}

◆ mma8452ModeString

char* mma8452ModeString[]
Initial value:
= {
"standby",
"wake",
"sleep",
}

◆ mma8452OrientationString

char* mma8452OrientationString[]
Initial value:
= {
"portrait up",
"portrait down",
"landscape up",
"landscape down",
}

◆ mma8452RangeString

char* mma8452RangeString[]
Initial value:
= {
"2g","4g","8g",
}