upm  1.7.1
Sensor/Actuator repository for libmraa (v2.0.0)
Data Structures | Functions | Typedefs
Include dependency graph for lsm303d.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 "lsm303d.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 LSM303D instance using default i2c bus and addresses
lsm303d_context sensor = lsm303d_init(LSM303D_DEFAULT_I2C_BUS,
LSM303D_DEFAULT_I2C_ADDR);
if (!sensor)
{
printf("lsm303d_init() failed.\n");
return 1;
}
// now output data every 250 milliseconds
while (shouldRun)
{
float x, y, z;
if (lsm303d_update(sensor))
{
printf("lsm303d_update() failed\n");
return 1;
}
lsm303d_get_accelerometer(sensor, &x, &y, &z);
printf("Accelerometer x: %f y: %f z: %f g\n",
x, y, z);
lsm303d_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", lsm303d_get_temperature(sensor));
upm_delay_ms(250);
}
printf("Exiting...\n");
lsm303d_close(sensor);
return 0;
}

Go to the source code of this file.

Data Structures

struct  _lsm303d_context
 

Functions

lsm303d_context lsm303d_init (int bus, int addr)
 
void lsm303d_close (lsm303d_context dev)
 
upm_result_t lsm303d_update (const lsm303d_context dev)
 
void lsm303d_get_magnetometer (const lsm303d_context dev, float *x, float *y, float *z)
 
void lsm303d_get_accelerometer (const lsm303d_context dev, float *x, float *y, float *z)
 
float lsm303d_get_temperature (const lsm303d_context dev)
 
upm_result_t lsm303d_set_acc_full_scale (const lsm303d_context dev, LSM303D_AFS_T fs)
 
upm_result_t lsm303d_set_mag_full_scale (const lsm303d_context dev, LSM303D_MFS_T fs)
 
upm_result_t lsm303d_devinit (const lsm303d_context dev, LSM303D_M_RES_T res)
 
upm_result_t lsm303d_set_acc_odr (const lsm303d_context dev, LSM303D_AODR_T odr)
 
upm_result_t lsm303d_set_mag_odr (const lsm303d_context dev, LSM303D_MODR_T odr)
 
uint8_t lsm303d_read_reg (const lsm303d_context dev, uint8_t reg)
 
int lsm303d_read_regs (const lsm303d_context dev, uint8_t reg, uint8_t *buffer, int len)
 
upm_result_t lsm303d_write_reg (const lsm303d_context dev, uint8_t reg, uint8_t val)
 

Typedefs

typedef struct _lsm303d_contextlsm303d_context
 

Function Documentation

lsm303d_context lsm303d_init ( int  bus,
int  addr 
)

LSM303D initialization

This driver only supports I2C.

Due to the fact that this chip is currently obsolete, we only support minimum functionality.

Parameters
busI2C bus to use
addrThe I2C address of the device
Returns
The device context, or NULL if an error occurred

Here is the call graph for this function:

void lsm303d_close ( lsm303d_context  dev)

LSM303D Destructor

Parameters
devThe device context

Here is the caller graph for this function:

upm_result_t lsm303d_update ( const lsm303d_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 lsm303d_get_magnetometer ( const lsm303d_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 lsm303d_get_accelerometer ( const lsm303d_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 lsm303d_get_temperature ( const lsm303d_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 lsm303d_set_acc_full_scale ( const lsm303d_context  dev,
LSM303D_AFS_T  fs 
)

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

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

Here is the call graph for this function:

Here is the caller graph for this function:

upm_result_t lsm303d_set_mag_full_scale ( const lsm303d_context  dev,
LSM303D_MFS_T  fs 
)

Set the full scale (sensitivity) value for the magnetometer. This device supports 2, 4, 8, and 16 Gauss.

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

Here is the call graph for this function:

Here is the caller graph for this function:

upm_result_t lsm303d_devinit ( const lsm303d_context  dev,
LSM303D_M_RES_T  res 
)

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.

Parameters
devThe device context
resOne of the LSM303D_M_RES_T values. This value sets the resolution of the magnetometer. At init time, this value is set to LSM303D_M_RES_HIGH.
Returns
UPM result

Here is the call graph for this function:

Here is the caller graph for this function:

upm_result_t lsm303d_set_acc_odr ( const lsm303d_context  dev,
LSM303D_AODR_T  odr 
)

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

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

Here is the call graph for this function:

Here is the caller graph for this function:

upm_result_t lsm303d_set_mag_odr ( const lsm303d_context  dev,
LSM303D_MODR_T  odr 
)

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

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

Here is the call graph for this function:

Here is the caller graph for this function:

uint8_t lsm303d_read_reg ( const lsm303d_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 lsm303d_read_regs ( const lsm303d_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 lsm303d_write_reg ( const lsm303d_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