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

API Description

/*
* Author: Alex Tereschenko <alext.mkrs@gmail.com>
* Copyright (c) 2018 Alex Tereschenko.
*
* Based on LIS2DS12 example by
* 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 <signal.h>
#include <stdio.h>
#include <unistd.h>
#include "lis3dh.h"
#include "upm_utilities.h"
bool shouldRun = true;
void
sig_handler(int signo)
{
if (signo == SIGINT)
shouldRun = false;
}
int
main(int argc, char** argv)
{
signal(SIGINT, sig_handler);
#if defined(CONFIG_BOARD_ARDUINO_101_SSS)
// ARDUINO_101_SSS (ARC core) must use I2C
// Instantiate a LIS3DH instance using default i2c bus and address
lis3dh_context sensor = lis3dh_init(LIS3DH_DEFAULT_I2C_BUS, LIS3DH_DEFAULT_I2C_ADDR, -1);
#elif defined(CONFIG_BOARD_ARDUINO_101)
// ARDUINO_101 (Quark core) must use SPI
// Instantiate a LIS3DH instance using default SPI bus and pin 10 as CS
lis3dh_context sensor = lis3dh_init(LIS3DH_DEFAULT_SPI_BUS, -1, 10);
#else
// everything else use I2C by default
// Instantiate a LIS3DH instance using default i2c bus and address
lis3dh_context sensor = lis3dh_init(LIS3DH_DEFAULT_I2C_BUS, LIS3DH_DEFAULT_I2C_ADDR, -1);
#endif
if (!sensor) {
printf("lis3dh_init() failed.\n");
return 1;
}
// now output data every 250 milliseconds
while (shouldRun) {
float x, y, z;
if (lis3dh_update(sensor)) {
printf("lis3dh_update() failed\n");
lis3dh_close(sensor);
return 1;
}
lis3dh_get_accelerometer(sensor, &x, &y, &z);
printf("Acceleration x: %f y: %f z: %f g\n", x, y, z);
printf("Compensation Temperature: %f C\n\n", lis3dh_get_temperature(sensor));
upm_delay_ms(250);
}
printf("Exiting...\n");
lis3dh_close(sensor);
return 0;
}

Go to the source code of this file.

Data Structures

struct  _lis3dh_context
 

Functions

lis3dh_context lis3dh_init (int bus, int addr, int cs)
 
void lis3dh_close (lis3dh_context dev)
 
upm_result_t lis3dh_devinit (const lis3dh_context dev, LIS3DH_ODR_T odr, LIS3DH_FS_T fs, bool high_res)
 
upm_result_t lis3dh_enable_axes (const lis3dh_context dev, bool x_axis_enable, bool y_axis_enable, bool z_axis_enable)
 
upm_result_t lis3dh_enable_bdu_mode (const lis3dh_context dev, bool bdu_enable)
 
upm_result_t lis3dh_enable_lp_mode (const lis3dh_context dev, bool lp_enable)
 
upm_result_t lis3dh_enable_hr_mode (const lis3dh_context dev, bool hr_enable)
 
upm_result_t lis3dh_enable_normal_mode (const lis3dh_context dev)
 
upm_result_t lis3dh_enable_hp_filtering (const lis3dh_context dev, bool filter)
 
upm_result_t lis3dh_enable_adc (const lis3dh_context dev, bool adc_enable)
 
upm_result_t lis3dh_enable_temperature (const lis3dh_context dev, bool temperature_enable)
 
upm_result_t lis3dh_enable_interrupt_latching (const lis3dh_context dev, bool int1_latch, bool int2_latch)
 
upm_result_t lis3dh_set_odr (const lis3dh_context dev, LIS3DH_ODR_T odr)
 
upm_result_t lis3dh_set_full_scale (const lis3dh_context dev, LIS3DH_FS_T fs)
 
upm_result_t lis3dh_set_interrupt_active_high (const lis3dh_context dev, bool high)
 
upm_result_t lis3dh_set_int1_config (const lis3dh_context dev, uint8_t cfg)
 
upm_result_t lis3dh_set_int2_config (const lis3dh_context dev, uint8_t cfg)
 
uint8_t lis3dh_read_reg (const lis3dh_context dev, uint8_t reg)
 
int lis3dh_read_regs (const lis3dh_context dev, uint8_t reg, uint8_t *buffer, int len)
 
upm_result_t lis3dh_write_reg (const lis3dh_context dev, uint8_t reg, uint8_t val)
 
upm_result_t lis3dh_update (const lis3dh_context dev)
 
uint8_t lis3dh_get_chip_id (const lis3dh_context dev)
 
void lis3dh_get_accelerometer (const lis3dh_context dev, float *x, float *y, float *z)
 
float lis3dh_get_temperature (const lis3dh_context dev)
 
uint8_t lis3dh_get_status (const lis3dh_context dev)
 
uint8_t lis3dh_get_status_aux (const lis3dh_context dev)
 
upm_result_t lis3dh_install_isr (const lis3dh_context dev, LIS3DH_INTERRUPT_PINS_T intr, int gpio, mraa_gpio_edge_t level, void(*isr)(void *), void *arg)
 
void lis3dh_uninstall_isr (const lis3dh_context dev, LIS3DH_INTERRUPT_PINS_T intr)
 

Typedefs

typedef struct _lis3dh_contextlis3dh_context
 

Function Documentation

lis3dh_context lis3dh_init ( int  bus,
int  addr,
int  cs 
)

LIS3DH initialization.

This device can support both I2C and SPI. For SPI, set the addr to -1, and specify a positive integer representing the Chip Select (CS) pin for the cs argument. If you are using a hardware CS pin (like Intel Edison with Arduino breakout), then you can connect the proper pin to the hardware CS pin on your MCU and supply -1 for cs.

Parameters
busI2C or SPI bus to use
addrThe address for this device, or -1 for SPI
csThe gpio pin to use for the SPI Chip Select. Use -1 for I2C or for SPI with a hardware controlled pin.
Returns
The device context, or NULL on error

Here is the call graph for this function:

void lis3dh_close ( lis3dh_context  dev)

LIS3DH Destructor

Parameters
devThe device context

Here is the call graph for this function:

Here is the caller graph for this function:

upm_result_t lis3dh_devinit ( const lis3dh_context  dev,
LIS3DH_ODR_T  odr,
LIS3DH_FS_T  fs,
bool  high_res 
)

Initialize the device and start operation. This function is called from lis3dh_init(), so it will not need to be called by a user unless the device is reset.

Parameters
devThe device context
odrOne of the LIS3DH_ODR_T values
fsOne of the LIS3DH_FS_T values
high_restrue to enable high resolution mode, false to disable
Returns
UPM result

Here is the call graph for this function:

Here is the caller graph for this function:

upm_result_t lis3dh_enable_axes ( const lis3dh_context  dev,
bool  x_axis_enable,
bool  y_axis_enable,
bool  z_axis_enable 
)

Enable or disable specific axes.

Parameters
devThe device context
x_axis_enabletrue to enable X axis, false to disable
y_axis_enabletrue to enable Y axis, false to disable
z_axis_enabletrue to enable Z axis, false to disable
Returns
UPM result

Here is the call graph for this function:

Here is the caller graph for this function:

upm_result_t lis3dh_enable_bdu_mode ( const lis3dh_context  dev,
bool  bdu_enable 
)

Enable or disable Block Data Update (BDU) mode.

Parameters
devThe device context
bdu_enabletrue to enable BDU mode, false to disable
Returns
UPM result

Here is the call graph for this function:

Here is the caller graph for this function:

upm_result_t lis3dh_enable_lp_mode ( const lis3dh_context  dev,
bool  lp_enable 
)

Enable or disable Low Power (LP) mode. Checks if mutually exclusive High Resolution (HR) mode is enabled and bails out if yes.

Parameters
devThe device context
lp_enabletrue to enable LP mode, false to disable
Returns
UPM result

Here is the call graph for this function:

Here is the caller graph for this function:

upm_result_t lis3dh_enable_hr_mode ( const lis3dh_context  dev,
bool  hr_enable 
)

Enable or disable High Resolution (HR) mode. Checks if mutually exclusive Low Power (LP) mode is enabled and bails out if yes.

Parameters
devThe device context
hr_enabletrue to enable HR mode, false to disable
Returns
UPM result

Here is the call graph for this function:

Here is the caller graph for this function:

upm_result_t lis3dh_enable_normal_mode ( const lis3dh_context  dev)

Enable Normal mode by explicitly disabling LP and HR ones. Note that there's no "disable" part as it's generally unknown, which mode we were in previously. To get out of Normal mode, just enable HR or LP one.

Parameters
devThe device context
Returns
UPM result

Here is the call graph for this function:

Here is the caller graph for this function:

upm_result_t lis3dh_enable_hp_filtering ( const lis3dh_context  dev,
bool  filter 
)

Enable high pass filtering of the accelerometer axis data. lis3dh_devinit() disables this by default. See the datasheet for details.

Parameters
devThe device context
filtertrue to enable filtering, false to disable
Returns
UPM result

Here is the call graph for this function:

Here is the caller graph for this function:

upm_result_t lis3dh_enable_adc ( const lis3dh_context  dev,
bool  adc_enable 
)

Enable or disable built-in Analog-to-Digital Converter (ADC).

Parameters
devThe device context
adc_enabletrue to enable ADC, false to disable
Returns
UPM result

Here is the call graph for this function:

Here is the caller graph for this function:

upm_result_t lis3dh_enable_temperature ( const lis3dh_context  dev,
bool  temperature_enable 
)

Enable or disable built-in temperature sensor. It depends on ADC being enabled, so we enable it unconditionally. See datasheet for details.

Parameters
devThe device context
temperature_enabletrue to enable temp sensor, false to disable
Returns
UPM result

Here is the call graph for this function:

Here is the caller graph for this function:

upm_result_t lis3dh_enable_interrupt_latching ( const lis3dh_context  dev,
bool  int1_latch,
bool  int2_latch 
)

Enable or disable interrupt latching for INT1 and INT2. See the datasheet for details.

Parameters
devThe device context
int1_latchtrue to enable latching for INT1, false to disable
int2_latchtrue to enable latching for INT2, false to disable
Returns
UPM result

Here is the call graph for this function:

Here is the caller graph for this function:

upm_result_t lis3dh_set_odr ( const lis3dh_context  dev,
LIS3DH_ODR_T  odr 
)

Set the output data rate (ODR) of the device

Parameters
devThe device context
odrOne of the LIS3DH_ODR_T values
Returns
UPM result

Here is the call graph for this function:

Here is the caller graph for this function:

upm_result_t lis3dh_set_full_scale ( const lis3dh_context  dev,
LIS3DH_FS_T  fs 
)

Set the full scale (FS) of the device. This device supports a full scale of 2, 4, 8, and 16G.

Parameters
devThe device context
fsOne of the LIS3DH_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 lis3dh_set_interrupt_active_high ( const lis3dh_context  dev,
bool  high 
)

Indicate whether INT1 and INT2 interrupts should be active high (default) or active low. See the datasheet for details.

Parameters
devThe device context
hightrue for active high, false for active low
Returns
UPM result

Here is the call graph for this function:

Here is the caller graph for this function:

upm_result_t lis3dh_set_int1_config ( const lis3dh_context  dev,
uint8_t  cfg 
)

Set interrupt 1 configuration. See the datasheet for details.

Parameters
devThe device context
cfgA bitmask of values from LIS3DH_CTRL_REG3_BITS_T
Returns
UPM result

Here is the call graph for this function:

Here is the caller graph for this function:

upm_result_t lis3dh_set_int2_config ( const lis3dh_context  dev,
uint8_t  cfg 
)

Set interrupt 2 configuration. See the datasheet for details.

Parameters
devThe device context
cfgA bitmask of values from LIS3DH_CTRL_REG6_BITS_T
Returns
UPM result

Here is the call graph for this function:

Here is the caller graph for this function:

uint8_t lis3dh_read_reg ( const lis3dh_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 lis3dh_read_regs ( const lis3dh_context  dev,
uint8_t  reg,
uint8_t *  buffer,
int  len 
)

Read contiguous registers into a buffer

Parameters
devThe device context
regThe register to start the read from
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 lis3dh_write_reg ( const lis3dh_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:

upm_result_t lis3dh_update ( const lis3dh_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:

uint8_t lis3dh_get_chip_id ( const lis3dh_context  dev)

Return the chip ID

Parameters
devThe device context
Returns
The chip ID (LIS3DH_CHIPID)

Here is the call graph for this function:

Here is the caller graph for this function:

void lis3dh_get_accelerometer ( const lis3dh_context  dev,
float *  x,
float *  y,
float *  z 
)

Return accelerometer data gravities (g). lis3dh_update() must have been called prior to calling this method.

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 lis3dh_get_temperature ( const lis3dh_context  dev)

Return the current measured temperature. Note, this is not ambient temperature. lis3dh_update() must have been called prior to calling this method.

Parameters
devThe device context
Returns
The temperature in degrees Celsius

Here is the caller graph for this function:

uint8_t lis3dh_get_status ( const lis3dh_context  dev)

Return the contents of the status register

Parameters
devThe device context
Returns
A bitmask of values from LIS3DH_STATUS_REG_BITS_T

Here is the call graph for this function:

Here is the caller graph for this function:

uint8_t lis3dh_get_status_aux ( const lis3dh_context  dev)

Return the contents of the status aux register

Parameters
devThe device context
Returns
A bitmask of values from LIS3DH_STATUS_REG_AUX_BITS_T

Here is the call graph for this function:

Here is the caller graph for this function:

upm_result_t lis3dh_install_isr ( const lis3dh_context  dev,
LIS3DH_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 LIS3DH_INTERRUPT_PINS_T values specifying which interrupt pin you are installing
gpioGPIO pin to use as interrupt pin
levelThe interrupt trigger level (one of 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 lis3dh_uninstall_isr ( const lis3dh_context  dev,
LIS3DH_INTERRUPT_PINS_T  intr 
)

Uninstall a previously installed interrupt handler

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

Here is the caller graph for this function:

Typedef Documentation

typedef struct _lis3dh_context * lis3dh_context

Device context