UPM

The UPM API is a high level sensor library for IoT devices using MRAA. See examples here. Back to index page.
SparkFun sensor images provided under CC BY-NC-SA-3.0.

KX122 Class

Module: common

Methods

KX122

(
  • bus
  • addr
  • chip_select
  • spi_bus_frequency
)
Number

KX122 constructor Set addr to -1 if using SPI. When using I2C, set chip_select_pin to -1
If no errors occur, the device gets initialized with default values and gets set to active state.

Parameters:

  • bus Number

    I2C or SPI bus to use.

  • addr Number

    I2C address of the sensor.

  • chip_select Number

    Chip select pin for SPI.

  • spi_bus_frequency Number

    Speed of the SPI communication bus in Hz.

Returns:

Number:

deviceInit

(
  • odr
  • res
  • grange
)

Initializes the sensor with given sampling rate, resolution and acceleration range. This gets called in the constructor, so it will not need to be called unless the device is reset.
Sensor is automatically set into standby mode during the initialization. Sensor is set to active mode after initialization.

Parameters:

  • odr KX122_ODR_T

    One of the KX122_ODR_T values.

  • res KX122_RES_T

    One of the KX122_RES_T values.

  • grange KX122_RANGE_T

    One of the KX122_RANGE_T values.

getSamplePeriod

() Number

Gets the length of one sample period depending on the sampling rate of the sensor.

Returns:

Number:

Floating point value of the sampling period, or if error occurs returns -1.

getWhoAmI

() Number

Gets who am i value of the sensor.

Returns:

Number:

Who am I value of the sensor.

getRawAccelerationData

(
  • x
  • y
  • z
)

Gets raw accelerometer data from the sensor.

Parameters:

  • x Float *

    Pointer to a floating point variable to store the x-axis value. Can be set to NULL if not wanted.

  • y Float *

    Pointer to a floating point variable to store the y-axis value. Can be set to NULL if not wanted.

  • z Float *

    Pointer to a floating point variable to store the z-axis value. Can be set to NULL if not wanted.

getAccelerationData

(
  • x
  • y
  • z
)

Gets converted (m/s^2) accelerometer data from the sensor.

Parameters:

  • x Float *

    Pointer to a floating point variable to store the x-axis value. Can be set to NULL if not wanted.

  • y Float *

    Pointer to a floating point variable to store the y-axis value. Can be set to NULL if not wanted.

  • z Float *

    Pointer to a floating point variable to store the z-axis value. Can be set to NULL if not wanted.

getAccelerationDataVector

() Std::vector float

Gets converted (m/s^2) accelerometer data from the sensor.

Returns:

Std::vector float :

Acceleration vector [X, Y, Z]

softwareReset

()

Performs a sensor software reset. The software reset clears the RAM of the sensor and resets all registers to pre-defined values.
You should call deviceInit() after the software reset.
See the datasheet for more details.

enableIIR

()

Enables sensor filter bypass.
Sensor needs to be in standby mode when enabling the filter bypass.

disableIIR

()

Disables sensor filter bypass.
Sensor needs to be in standby mode when enabling the filter bypass.

selfTest

()

Performs a self-test of the sensor. The test applies an electrostatic force to the sensor, simulating input acceleration. The test compares samples from all axis before and after applying the electrostatic force to the sensor. If the amount of acceleration increases according to the values defined in TABLE 1 of the datasheet, the test passes.
The function prints out the minimum, maximum and values calculated during the test for each axis, and the result of the test for each axis.
See the datasheet for more information.

setSensorStandby

()

Sets the sensor to the standby mode.

setSensorActive

()

Sets the sensor to the active mode.

setODR

(
  • odr
)

Sets the ODR of the sensor.
Sensor needs to be in standby mode when setting the ODR.

Parameters:

  • odr KX122_ODR_T

    One of the KX122_ODR_T values.

setGrange

(
  • grange
)

Sets the acceleration range of the sensor.
Sensor needs to be in standby mode when setting the acceleration range value.

Parameters:

  • grange KX122_RANGE_T

    One of the KX122_RANGE_T values.

setResolution

(
  • res
)

Sets the resolution of the sensor. High resolution (16 bits) or low resolution (8 bits).
Sensor needs to be in standby mode when setting the sensor resolution.
When sensor is set to low resolution mode, the sensor runs in low power mode, which in turn enables features such as averaging.( setAverage() ).

Parameters:

  • res KX122_RES_T

    One of the KX122_RES_T values.

setBW

(
  • lpro
)

Sets the low pass filter roll off
Sensor needs to be in standby mode when setting the filter roll off value.

Parameters:

  • lpro LPRO_STATE_T

    One of the LPRO_STATE_T values.

setAverage

(
  • avg
)

Set the amount of samples to be averaged in low power mode.
Sensor needs to be in standby mode when setting the average value.

Parameters:

  • avg KX122_AVG_T

    One of the KX122_AVG_T values.

installISR

(
  • edge
  • intp
  • pin
  • isr
  • arg
)

Installs an interrupt handler to be executed when an interrupt is detected on the interrupt pin.

Parameters:

  • edge Mraa::Edge

    One of the mraa::Edge values. Interrupt trigger edge.

  • intp KX122_INTERRUPT_PIN_T

    One of the KX122_INTERRUPT_PIN_T values. Specifies which interrupt pin you are setting.

  • pin Number

    The GPIO pin to use as the interrupt pin.

  • isr Function

    Pointer to the function to be called, when the interrupt occurs.

  • arg Void *

    The arguments to be passed to the function.

uninstallISR

(
  • intp
)

Uninstalls a previously installed interrupt handler.

Parameters:

  • intp KX122_INTERRUPT_PIN_T

    One of the KX122_INTERRUPT_PIN_T values. Specifies which interrupt pin handler is uninstalled.

enableInterrupt1

(
  • polarity
)

Enables interrupts on the interrupt pin 1 of the sensor.
Pulse width = 50us (10us if data sampling rate > 1600Hz). Using pulse mode.
Sensor needs to be in standby mode when enabling the interrupt.
See datasheet for more details.

Parameters:

  • polarity KX122_INTERRUPT_POLARITY_T

    One of the KX122_INTERRUPT_POLARITY_T values. Select the polarity of the interrupt.

enableInterrupt2

(
  • polarity
)

Enables interrupts on the interrupt pin 2 of the sensor.
Pulse width = 50us (10us if data sampling rate > 1600Hz). Using pulse mode.
Sensor needs to be in standby mode when enabling the interrupt.
See datasheet for more details.

Parameters:

  • polarity KX122_INTERRUPT_POLARITY_T

    One of the KX122_INTERRUPT_POLARITY_T values. Select the polarity of the interrupt.

disableInterrupt1

()

Disables interrupts on the interrupt pin 1 of the sensor.
Sensor needs to be in standby mode when disabling the interrupt pin 1.

disableInterrupt2

()

Disables interrupts on the interrupt pin 2 of the sensor.
Sensor needs to be in standby mode when disabling the interrupt pin 2.

routeInterrupt1

(
  • bits
)

Routes the interrupts to the interrupt pin 1 of the sensor.
Sensor needs to be in standby mode when routing the interrupts.
See datasheet for more details.

Parameters:

  • bits Number

    One or more of the KX122_INTERRUPT_T values. Combine with bitwise OR (|)

routeInterrupt2

(
  • bits
)

Routes the interrupts to the interrupt pin 2 of the sensor.
Sensor needs to be in standby mode when routing the interrupts.
See datasheet for more details.

Parameters:

  • bits Number

    One or more of the KX122_INTERRUPT_T values. Combine with bitwise OR (|)

getInterruptStatus

() Boolean

Gets the status of the interrupts. (Has an interrupt occurred)
See datasheet for more details.

Returns:

Boolean:

Return true if an interrupt event has occurred, returns false if no interrupts have occurred.

getInterruptSource

() Number

Gets the source of the interrupt.
The value stored is one or more of the KX122_INTERRUPT_T values, depending on the interrupts that have occurred.
If multiple interrupts have occurred, and you need to determine a specific interrupt, you can use masking to get the state of the specific interrupt: (int_source & KX122_DATA_READY_INT) == KX122_DATA_READY_INT)
See datasheet for more details.

Returns:

Number:

Interrupt source value.

clearInterrupt

()

Clears latching interrupts (Wakeup, Data Ready).
See datasheet for more details.

enableDataReadyInterrupt

()

Enables the data ready interrupt. Availability of new acceleration data is reflected as an interrupt.
Sensor needs to be in standby mode when enabling the interrupt.

disableDataReadyInterrupt

()

Disables the data ready interrupt.
Sensor needs to be in standby mode when disabling the interrupt.

enableBufferFullInterrupt

()

Enables the buffer full interrupt. Buffer can hold 681 sets of values (8 bit, low resolution mode) or 340 sets of values (16 bit, high resolution mode).
Sensor needs to be in standby mode when enabling the interrupt.

disableBufferFullInterrupt

()

Disables the buffer full interrupt.
Sensor needs to be in standby mode when disabling the interrupt.

enableBuffer

()

Enables the buffer.
Sensor needs to be in standby mode when enabling the buffer.

disableBuffer

()

Disables the buffer.
Sensor needs to be in standby mode when disabling the buffer.

bufferInit

(
  • samples
  • res
  • mode
)

Initializes the buffer with the given sample watermark level, buffer resolution and buffer operating mode. Buffer is enabled after the initialization.
Sensor is automatically set into standby mode during the buffer initialization. Sensor is set to active mode after initialization.
See the other buffer functions for details about the different parameter values.

Parameters:

  • samples Uint

    Amount of samples to trigger the watermark interrupt.

  • res KX122_RES_T

    One of the KX122_RES_T values.

  • mode KX122_BUFFER_MODE_T

    One of the KX122_BUFFER_MODE_T values.

setBufferResolution

(
  • res
)

Sets the buffer resolution. Buffer resolution is independent of the sensor resolution.
Sensor needs to be in standby mode when setting the buffer resolution.

Parameters:

  • res KX122_RES_T

    undefined

setBufferThreshold

(
  • samples
)

Sets the buffer watermark interrupt threshold. When the buffer sample count reaches the watermark, watermark interrupt will be given.
In low resolution mode, maximum number of samples is 681. In high resolution, the maximum number is 340.
See datasheet for more details.
Sensor needs to be in standby mode when setting the buffer threshold.

Parameters:

  • samples Uint

    Amount of samples to trigger the watermark interrupt.

setBufferMode

(
  • mode
)

Sets the buffer operating mode. The buffer can operate in FIFO,FILO or Stream mode. The buffer gathers data, reports data and interacts with the status indicators in a slightly different way depending on the operating mode.
See datasheet for more details.
Sensor needs to be in standby mode when setting the buffer mode.

Parameters:

  • mode KX122_BUFFER_MODE_T

    One of the KX122_BUFFER_MODE_T values.

getBufferStatus

() Uint

Gets the current amount of samples in the buffer.

Returns:

Uint:

number of samples in the buffer.

getRawBufferSamples

(
  • len
)
Std::vector float

Gets the specified amount of raw acceleration samples from the buffer.

Parameters:

  • len Uint

    The amount of samples to read from the buffer.

Returns:

Std::vector float :

vector containing x, y & z-axis data

getBufferSamples

(
  • len
)
Std::vector float

Gets the specified amount of converted (m/s^2) acceleration samples from the buffer.

Parameters:

  • len Uint

    The amount of samples to read from the buffer.

Returns:

Std::vector float :

vector containing x, y & z-axis data

clearBuffer

()

Clears the buffer, removing all existing samples from the buffer.