upm  1.7.1
Sensor/Actuator repository for libmraa (v2.0.0)
Data Structures | Functions | Typedefs
Include dependency graph for lsm303agr.h:

API Description

/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2017 Intel Corporation.
*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include "upm_utilities.h"
#include "lsm303agr.h"
bool shouldRun = true;
void sig_handler(int signo)
{
if (signo == SIGINT)
shouldRun = false;
}
int main(int argc, char **argv)
{
signal(SIGINT, sig_handler);
// Instantiate a LSM303AGR instance using default i2c bus and addresses
lsm303agr_context sensor = lsm303agr_init(LSM303AGR_DEFAULT_I2C_BUS,
LSM303AGR_DEFAULT_ACC_ADDR,
LSM303AGR_DEFAULT_MAG_ADDR);
if (!sensor)
{
printf("lsm303agr_init() failed.\n");
return 1;
}
// now output data every 250 milliseconds
while (shouldRun)
{
float x, y, z;
if (lsm303agr_update(sensor))
{
printf("lsm303agr_update() failed\n");
return 1;
}
lsm303agr_get_accelerometer(sensor, &x, &y, &z);
printf("Accelerometer x: %f y: %f z: %f g\n",
x, y, z);
lsm303agr_get_magnetometer(sensor, &x, &y, &z);
printf("Magnetometer x: %f y: %f z: %f uT\n",
x, y, z);
printf("Temperature: %f C\n\n", lsm303agr_get_temperature(sensor));
upm_delay_ms(250);
}
printf("Exiting...\n");
lsm303agr_close(sensor);
return 0;
}

Go to the source code of this file.

Data Structures

struct  _lsm303agr_context
 

Functions

lsm303agr_context lsm303agr_init (int bus, int acc_addr, int mag_addr)
 
void lsm303agr_close (lsm303agr_context dev)
 
upm_result_t lsm303agr_update (const lsm303agr_context dev)
 
void lsm303agr_get_magnetometer (const lsm303agr_context dev, float *x, float *y, float *z)
 
void lsm303agr_get_accelerometer (const lsm303agr_context dev, float *x, float *y, float *z)
 
float lsm303agr_get_temperature (const lsm303agr_context dev)
 
upm_result_t lsm303agr_set_full_scale (const lsm303agr_context dev, LSM303AGR_A_FS_T fs)
 
upm_result_t lsm303agr_devinit (const lsm303agr_context dev, LSM303AGR_POWER_MODE_T mode)
 
upm_result_t lsm303agr_set_power_mode (const lsm303agr_context dev, LSM303AGR_POWER_MODE_T mode)
 
upm_result_t lsm303agr_set_acc_odr (const lsm303agr_context dev, LSM303AGR_A_ODR_T odr)
 
upm_result_t lsm303agr_set_mag_odr (const lsm303agr_context dev, LSM303AGR_CFG_A_M_ODR_T odr)
 
uint8_t lsm303agr_get_acc_int1_config (const lsm303agr_context dev)
 
uint8_t lsm303agr_get_acc_int2_config (const lsm303agr_context dev)
 
upm_result_t lsm303agr_set_acc_int1_config (const lsm303agr_context dev, uint8_t bits)
 
upm_result_t lsm303agr_set_acc_int2_config (const lsm303agr_context dev, uint8_t bits)
 
uint8_t lsm303agr_get_mag_int_config (const lsm303agr_context dev)
 
upm_result_t lsm303agr_set_mag_int_config (const lsm303agr_context dev, uint8_t bits)
 
uint8_t lsm303agr_get_acc_int1_src (const lsm303agr_context dev)
 
uint8_t lsm303agr_get_acc_int2_src (const lsm303agr_context dev)
 
uint8_t lsm303agr_get_mag_int_src (const lsm303agr_context dev)
 
upm_result_t lsm303agr_install_isr (const lsm303agr_context dev, LSM303AGR_INTERRUPT_PINS_T intr, int gpio, mraa_gpio_edge_t level, void(*isr)(void *), void *arg)
 
void lsm303agr_uninstall_isr (const lsm303agr_context dev, LSM303AGR_INTERRUPT_PINS_T intr)
 
uint8_t lsm303agr_read_reg (const lsm303agr_context dev, uint8_t reg)
 
int lsm303agr_read_regs (const lsm303agr_context dev, uint8_t reg, uint8_t *buffer, int len)
 
upm_result_t lsm303agr_write_reg (const lsm303agr_context dev, uint8_t reg, uint8_t val)
 

Typedefs

typedef struct _lsm303agr_contextlsm303agr_context
 

Function Documentation

lsm303agr_context lsm303agr_init ( int  bus,
int  acc_addr,
int  mag_addr 
)

LSM303AGR initialization

This driver can only support I2C. SPI requires support for 3-wire SPI which we cannot currently handle. Only the basic capabilities of the device are supported, however there is a full register map defined (lsm303agr_defs.h), and with access to the bus read/write functions, any desired additional functionality can be implemented.

Parameters
busI2C bus to use
acc_addrThe I2C address of the accelerometer. Use -1 if you do not wish to use the accelerometer.
mag_addrThe I2C address of the magnetometer. Use -1 if you do not wish to use the magnetometer.
Returns
The device context, or NULL if an error occurred

Here is the call graph for this function:

void lsm303agr_close ( lsm303agr_context  dev)

LSM303AGR Destructor

Parameters
devThe device context

Here is the call graph for this function:

Here is the caller graph for this function:

upm_result_t lsm303agr_update ( const lsm303agr_context  dev)

Update the internal stored values from sensor data

Parameters
devThe device context
Returns
UPM result

Here is the call graph for this function:

Here is the caller graph for this function:

void lsm303agr_get_magnetometer ( const lsm303agr_context  dev,
float *  x,
float *  y,
float *  z 
)

Return magnetometer data in micro-Teslas (uT). update() must have been called prior to calling this function.

Parameters
devThe device context
xPointer to a floating point value that will have the current x component placed into it
yPointer to a floating point value that will have the current y component placed into it
zPointer to a floating point value that will have the current z component placed into it

Here is the caller graph for this function:

void lsm303agr_get_accelerometer ( const lsm303agr_context  dev,
float *  x,
float *  y,
float *  z 
)

Return accelerometer data in gravities. update() must have been called prior to calling this function.

Parameters
devThe device context
xPointer to a floating point value that will have the current x component placed into it
yPointer to a floating point value that will have the current y component placed into it
zPointer to a floating point value that will have the current z component placed into it

Here is the caller graph for this function:

float lsm303agr_get_temperature ( const lsm303agr_context  dev)

Return temperature data in degrees Celsius.. update() must have been called prior to calling this function.

Parameters
devThe device context
Returns
Temperature in degrees Celsius

Here is the caller graph for this function:

upm_result_t lsm303agr_set_full_scale ( const lsm303agr_context  dev,
LSM303AGR_A_FS_T  fs 
)

Set the full scale (sensitivity) value for the accelerometer. This device supports 2G, 4G, 8G, and 16G full scale modes.

Parameters
devThe device context
fsOne of the LSM303AGR_A_FS_T values
Returns
UPM result

Here is the call graph for this function:

Here is the caller graph for this function:

upm_result_t lsm303agr_devinit ( const lsm303agr_context  dev,
LSM303AGR_POWER_MODE_T  mode 
)

Initialize the device and start operation. This function is called from the constructor so it will not typically need to be called by a user unless the device is reset. It will initialize the accelerometer and magnetometer (if enabled) to certain default running modes.

For the accelerometer, the full scale will be set to 2G, mode continuous, all axes enabled, BDU enabled, temperature measurement enabled, and an output data rate (ODR) of 100Hz with the power mode set at high resolution.

For the magnetometer, temperature compensation will be enabled, mode continuous, and an output data rate of 10Hz with the power mode set at high resolution.

Parameters
devThe device context
modeOne of the LSM303AGR_POWER_MODE_T values. This parameter is passed to a call to lsm303agr_set_power_mode() and will set the appropriate mode for both the accelerometer and magnetometer. The default set by the constructor is LSM303AGR_POWER_HIGH_RESOLUTION.
Returns
UPM result

Here is the call graph for this function:

Here is the caller graph for this function:

upm_result_t lsm303agr_set_power_mode ( const lsm303agr_context  dev,
LSM303AGR_POWER_MODE_T  mode 
)

Set an operating power mode. There are 3 modes available: low power, normal, and high resolution.

Parameters
devThe device context
modeOne of the LSM303AGR_POWER_MODE_T values. The default set at initialization time is LSM303AGR_POWER_HIGH_RESOLUTION.
Returns
UPM result

Here is the call graph for this function:

Here is the caller graph for this function:

upm_result_t lsm303agr_set_acc_odr ( const lsm303agr_context  dev,
LSM303AGR_A_ODR_T  odr 
)

Set the accelerometer (acc) output data rate (odr)

Parameters
devThe device context
odrOne of the LSM303AGR_A_ODR_T values. The default set at initialization time is LSM303AGR_A_ODR_100HZ.
Returns
UPM result

Here is the call graph for this function:

Here is the caller graph for this function:

upm_result_t lsm303agr_set_mag_odr ( const lsm303agr_context  dev,
LSM303AGR_CFG_A_M_ODR_T  odr 
)

Set the magnetometer (mag) output data rate (odr)

Parameters
devThe device context
odrOne of the LSM303AGR_CFG_A_M_ODR_T values. The default set at initialization time is LSM303AGR_CFG_A_M_ODR_10HZ.
Returns
UPM result

Here is the call graph for this function:

Here is the caller graph for this function:

uint8_t lsm303agr_get_acc_int1_config ( const lsm303agr_context  dev)

Return the accelerometer (acc) interrupt 1 config register. This register allows you to enable various interrupt conditions. See the datasheet for details.

Parameters
devThe device context
Returns
A bitmask of LSM303AGR_INT_CFG_A_BITS_T bits

Here is the call graph for this function:

Here is the caller graph for this function:

uint8_t lsm303agr_get_acc_int2_config ( const lsm303agr_context  dev)

Return the accelerometer (acc) interrupt 2 config register. This register allows you to enable various interrupt conditions. See the datasheet for details.

Parameters
devThe device context
Returns
A bitmask of LSM303AGR_INT_CFG_A_BITS_T bits

Here is the call graph for this function:

Here is the caller graph for this function:

upm_result_t lsm303agr_set_acc_int1_config ( const lsm303agr_context  dev,
uint8_t  bits 
)

Set the accelerometer (acc) interrupt 1 config register. See the datasheet for details.

Parameters
devThe device context
bitsA bitmask of LSM303AGR_INT_CFG_A_BITS_T bits
Returns
UPM result

Here is the call graph for this function:

Here is the caller graph for this function:

upm_result_t lsm303agr_set_acc_int2_config ( const lsm303agr_context  dev,
uint8_t  bits 
)

Set the accelerometer (acc) interrupt 2 config register. See the datasheet for details.

Parameters
devThe device context
bitsA bitmask of LSM303AGR_INT_CFG_A_BITS_T bits
Returns
UPM result

Here is the call graph for this function:

Here is the caller graph for this function:

uint8_t lsm303agr_get_mag_int_config ( const lsm303agr_context  dev)

Return the magnetometer (mag) interrupt config register. See the datasheet for details.

Parameters
devThe device context
Returns
A bitmask of LSM303AGR_INT_CTRL_REG_M_BITS_T bits

Here is the call graph for this function:

Here is the caller graph for this function:

upm_result_t lsm303agr_set_mag_int_config ( const lsm303agr_context  dev,
uint8_t  bits 
)

Set the magnetometer (mag) interrupt config register. See the datasheet for details.

Parameters
devThe device context
bitsA bitmask of LSM303AGR_INT_CTRL_REG_M_BITS_T bits

Here is the call graph for this function:

Here is the caller graph for this function:

uint8_t lsm303agr_get_acc_int1_src ( const lsm303agr_context  dev)

Return the accelerometer (acc) interrupt 1 source register. This register indicates which interrupts have been triggered. See the datasheet for details.

Parameters
devThe device context
Returns
a bitmask of LSM303AGR_INT_SRC_A_BITS_T bits

Here is the call graph for this function:

Here is the caller graph for this function:

uint8_t lsm303agr_get_acc_int2_src ( const lsm303agr_context  dev)

Return the accelerometer (acc) interrupt 2 source register. This register indicates which interrupts have been triggered. See the datasheet for details.

Parameters
devThe device context
Returns
a bitmask of LSM303AGR_INT_SRC_A_BITS_T bits

Here is the call graph for this function:

Here is the caller graph for this function:

uint8_t lsm303agr_get_mag_int_src ( const lsm303agr_context  dev)

Return the magnetometer (mag) interrupt source register. This register indicates which interrupts have been triggered. See the datasheet for details.

Parameters
devThe device context
Returns
a bitmask of LSM303AGR_INT_SRC_REG_M_BITS_T bits

Here is the call graph for this function:

Here is the caller graph for this function:

upm_result_t lsm303agr_install_isr ( const lsm303agr_context  dev,
LSM303AGR_INTERRUPT_PINS_T  intr,
int  gpio,
mraa_gpio_edge_t  level,
void(*)(void *)  isr,
void *  arg 
)

Install an interrupt handler

Parameters
devThe device context
intrOne of the LSM303AGR_INTERRUPT_PINS_T values specifying which interrupt pin you are installing
gpioGPIO pin to use as interrupt pin
levelThe interrupt trigger level (one of the mraa_gpio_edge_t values). Make sure that you have configured the interrupt pin properly for whatever level you choose.
isrThe interrupt handler, accepting a void * argument
argThe argument to pass the the interrupt handler
Returns
UPM result

Here is the call graph for this function:

Here is the caller graph for this function:

void lsm303agr_uninstall_isr ( const lsm303agr_context  dev,
LSM303AGR_INTERRUPT_PINS_T  intr 
)

Uninstall a previously installed interrupt handler

Parameters
devThe device context
intrOne of the LSM303AGR_INTERRUPT_PINS_T values specifying which interrupt pin you are removing

Here is the caller graph for this function:

uint8_t lsm303agr_read_reg ( const lsm303agr_context  dev,
uint8_t  reg 
)

Read a register.

Parameters
devThe device context
regThe register to read
Returns
The value of the register

Here is the caller graph for this function:

int lsm303agr_read_regs ( const lsm303agr_context  dev,
uint8_t  reg,
uint8_t *  buffer,
int  len 
)

Read contiguous registers into a buffer

Parameters
devThe device context
bufferThe buffer to store the results
lenThe number of registers to read
Returns
The number of bytes read, or -1 on error

Here is the caller graph for this function:

upm_result_t lsm303agr_write_reg ( const lsm303agr_context  dev,
uint8_t  reg,
uint8_t  val 
)

Write to a register

Parameters
devThe device context
regThe register to write to
valThe value to write
Returns
UPM result

Here is the caller graph for this function:

Typedef Documentation

Device context