first commit
This commit is contained in:
508
libraries/Adafruit_ADXL343/Adafruit_ADXL343.cpp
Normal file
508
libraries/Adafruit_ADXL343/Adafruit_ADXL343.cpp
Normal file
@@ -0,0 +1,508 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file Adafruit_ADXL343.cpp
|
||||
@author Bryan Siepert and K.Townsend (Adafruit Industries)
|
||||
|
||||
BSD License (see license.txt)
|
||||
|
||||
The ADXL343 is a digital accelerometer with 13-bit resolution, capable
|
||||
of measuring up to +/-16g. This driver communicates using I2C.
|
||||
|
||||
This is a library for the Adafruit ADXL343 breakout
|
||||
----> https://www.adafruit.com/product/4097
|
||||
or the Adafruit ADXL343 + ADT7410 FeatherWing
|
||||
----> https://www.adafruit.com/product/4147
|
||||
|
||||
Adafruit invests time and resources providing this open source code,
|
||||
please support Adafruit and open-source hardware by purchasing
|
||||
products from Adafruit!
|
||||
|
||||
v1.0 - First release
|
||||
*/
|
||||
/**************************************************************************/
|
||||
#if ARDUINO >= 100
|
||||
#include "Arduino.h"
|
||||
#else
|
||||
#include "WProgram.h"
|
||||
#endif
|
||||
|
||||
#include <Wire.h>
|
||||
#include <limits.h>
|
||||
|
||||
#include "Adafruit_ADXL343.h"
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Writes 8-bits to the specified destination register
|
||||
|
||||
@param reg The register to write to
|
||||
@param value The value to write to the register
|
||||
*/
|
||||
/**************************************************************************/
|
||||
|
||||
void Adafruit_ADXL343::writeRegister(uint8_t reg, uint8_t value) {
|
||||
Adafruit_BusIO_Register reg_obj = Adafruit_BusIO_Register(
|
||||
i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC, reg, 1);
|
||||
reg_obj.write(value);
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Reads 8-bits from the specified register
|
||||
|
||||
@param reg register to read
|
||||
|
||||
@return The results of the register read request
|
||||
*/
|
||||
/**************************************************************************/
|
||||
uint8_t Adafruit_ADXL343::readRegister(uint8_t reg) {
|
||||
Adafruit_BusIO_Register reg_obj = Adafruit_BusIO_Register(
|
||||
i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC, reg, 1);
|
||||
return ((uint8_t)reg_obj.read());
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Reads 16-bits from the specified register
|
||||
|
||||
@param reg The register to read two bytes from
|
||||
|
||||
@return The 16-bit value read from the reg starting address
|
||||
*/
|
||||
/**************************************************************************/
|
||||
|
||||
int16_t Adafruit_ADXL343::read16(uint8_t reg) {
|
||||
Adafruit_BusIO_Register reg_obj = Adafruit_BusIO_Register(
|
||||
i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC, reg, 2);
|
||||
return ((uint16_t)reg_obj.read());
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Read the device ID (can be used to check connection)
|
||||
|
||||
@return The 8-bit device ID
|
||||
*/
|
||||
/**************************************************************************/
|
||||
uint8_t Adafruit_ADXL343::getDeviceID(void) {
|
||||
// Check device ID register
|
||||
return readRegister(ADXL3XX_REG_DEVID);
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Enables (1) or disables (0) the interrupts on the specified
|
||||
interrupt pin.
|
||||
|
||||
@param cfg The bitfield of the interrupts to enable or disable.
|
||||
|
||||
@return True if the operation was successful, otherwise false.
|
||||
*/
|
||||
/**************************************************************************/
|
||||
bool Adafruit_ADXL343::enableInterrupts(int_config cfg) {
|
||||
/* Update the INT_ENABLE register with 'config'. */
|
||||
writeRegister(ADXL3XX_REG_INT_ENABLE, cfg.value);
|
||||
|
||||
/* ToDo: Add proper error checking! */
|
||||
return true;
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief 'Maps' the specific interrupt to either pin INT1 (bit=0),
|
||||
of pin INT2 (bit=1).
|
||||
|
||||
@param cfg The bitfield of the interrupts to enable or disable.
|
||||
|
||||
@return True if the operation was successful, otherwise false.
|
||||
*/
|
||||
/**************************************************************************/
|
||||
bool Adafruit_ADXL343::mapInterrupts(int_config cfg) {
|
||||
/* Update the INT_MAP register with 'config'. */
|
||||
writeRegister(ADXL3XX_REG_INT_MAP, cfg.value);
|
||||
|
||||
/* ToDo: Add proper error checking! */
|
||||
return true;
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Reads the status of the interrupt pins. Reading this register
|
||||
also clears or deasserts any currently active interrupt.
|
||||
|
||||
@return The 8-bit content of the INT_SOURCE register.
|
||||
*/
|
||||
/**************************************************************************/
|
||||
uint8_t Adafruit_ADXL343::checkInterrupts(void) {
|
||||
return readRegister(ADXL3XX_REG_INT_SOURCE);
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Gets the most recent X axis value
|
||||
|
||||
@return The 16-bit signed value for the X axis
|
||||
*/
|
||||
/**************************************************************************/
|
||||
int16_t Adafruit_ADXL343::getX(void) { return read16(ADXL3XX_REG_DATAX0); }
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Gets the most recent Y axis value
|
||||
|
||||
@return The 16-bit signed value for the Y axis
|
||||
*/
|
||||
/**************************************************************************/
|
||||
int16_t Adafruit_ADXL343::getY(void) { return read16(ADXL3XX_REG_DATAY0); }
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Gets the most recent Z axis value
|
||||
|
||||
@return The 16-bit signed value for the Z axis
|
||||
*/
|
||||
/**************************************************************************/
|
||||
int16_t Adafruit_ADXL343::getZ(void) { return read16(ADXL3XX_REG_DATAZ0); }
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Reads 3x16-bits from the x, y, and z data register
|
||||
@param x reference to return x acceleration data
|
||||
@param y reference to return y acceleration data
|
||||
@param z reference to return z acceleration data
|
||||
@return True if the operation was successful, otherwise false.
|
||||
*/
|
||||
/**************************************************************************/
|
||||
bool Adafruit_ADXL343::getXYZ(int16_t &x, int16_t &y, int16_t &z) {
|
||||
int16_t buffer[] = {0, 0, 0};
|
||||
Adafruit_BusIO_Register reg_obj = Adafruit_BusIO_Register(
|
||||
i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC, ADXL3XX_REG_DATAX0, 6);
|
||||
if (!reg_obj.read((uint8_t *)&buffer, 6))
|
||||
return false;
|
||||
x = buffer[0];
|
||||
y = buffer[1];
|
||||
z = buffer[2];
|
||||
return true;
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
* @brief Instantiates a new ADXL343 class
|
||||
*
|
||||
* @param sensorID An optional ID # so you can track this sensor, it will
|
||||
* tag sensorEvents you create.
|
||||
*/
|
||||
/**************************************************************************/
|
||||
Adafruit_ADXL343::Adafruit_ADXL343(int32_t sensorID) {
|
||||
_sensorID = sensorID;
|
||||
_wire = &Wire;
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
* @brief Instantiates a new ADXL343 class
|
||||
*
|
||||
* @param sensorID An optional ID # so you can track this sensor, it will
|
||||
* tag sensorEvents you create.
|
||||
* @param wireBus TwoWire instance to use for I2C communication.
|
||||
*/
|
||||
/**************************************************************************/
|
||||
Adafruit_ADXL343::Adafruit_ADXL343(int32_t sensorID, TwoWire *wireBus) {
|
||||
_sensorID = sensorID;
|
||||
_wire = wireBus;
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Instantiates a new ADXL343 class in software SPI mode
|
||||
|
||||
@param clock The SCK pin
|
||||
@param miso The MISO pin
|
||||
@param mosi The MOSI pin
|
||||
@param cs The CS/SSEL pin
|
||||
@param sensorID An optional ID # so you can track this sensor, it will tag
|
||||
sensorEvents you create.
|
||||
*/
|
||||
/**************************************************************************/
|
||||
Adafruit_ADXL343::Adafruit_ADXL343(uint8_t clock, uint8_t miso, uint8_t mosi,
|
||||
uint8_t cs, int32_t sensorID) {
|
||||
_sensorID = sensorID;
|
||||
_cs = cs;
|
||||
_clk = clock;
|
||||
_do = mosi;
|
||||
_di = miso;
|
||||
_wire = NULL;
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Instantiates a new ADXL343 class in hardware SPI mode
|
||||
|
||||
@param cs The CS/SSEL pin
|
||||
@param theSPI SPIClass instance to use for SPI communication.
|
||||
@param sensorID An optional ID # so you can track this sensor, it will tag
|
||||
sensorEvents you create.
|
||||
*/
|
||||
/**************************************************************************/
|
||||
Adafruit_ADXL343::Adafruit_ADXL343(uint8_t cs, SPIClass *theSPI,
|
||||
int32_t sensorID) {
|
||||
_sensorID = sensorID;
|
||||
_cs = cs;
|
||||
_spi = theSPI;
|
||||
_wire = NULL;
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Setups the HW (reads coefficients values, etc.)
|
||||
@param i2caddr The 7-bit I2C address to find the ADXL on
|
||||
@return True if the sensor was successfully initialised.
|
||||
*/
|
||||
/**************************************************************************/
|
||||
bool Adafruit_ADXL343::begin(uint8_t i2caddr) {
|
||||
|
||||
if (_wire) {
|
||||
//-- I2C --------------
|
||||
if (i2c_dev) {
|
||||
delete i2c_dev; // remove old interface
|
||||
}
|
||||
i2c_dev = new Adafruit_I2CDevice(i2caddr, _wire);
|
||||
|
||||
if (!i2c_dev->begin()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
} else {
|
||||
//-- SPI --------------
|
||||
i2c_dev = NULL;
|
||||
|
||||
if (spi_dev) {
|
||||
delete spi_dev; // remove old interface
|
||||
}
|
||||
if (_spi) {
|
||||
// hardware spi
|
||||
spi_dev = new Adafruit_SPIDevice(_cs,
|
||||
1000000, // frequency
|
||||
SPI_BITORDER_MSBFIRST, // bit order
|
||||
SPI_MODE3, // data mode
|
||||
_spi); // hardware SPI
|
||||
} else {
|
||||
// software spi
|
||||
spi_dev = new Adafruit_SPIDevice(_cs, _clk, _di, _do,
|
||||
1000000, // frequency
|
||||
SPI_BITORDER_MSBFIRST, // bit order
|
||||
SPI_MODE3); // data mode
|
||||
}
|
||||
|
||||
if (!spi_dev->begin()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/* Check connection */
|
||||
uint8_t deviceid = getDeviceID();
|
||||
if (deviceid != 0xE5) {
|
||||
/* No ADXL343 detected ... return false */
|
||||
return false;
|
||||
}
|
||||
|
||||
_range = ADXL343_RANGE_2_G;
|
||||
// Default tap detection level (2G, 31.25ms duration, single tap only)
|
||||
// If only the single tap function is in use, the single tap interrupt
|
||||
// is triggered when the acceleration goes below the threshold, as
|
||||
// long as DUR has not been exceeded.
|
||||
writeRegister(ADXL3XX_REG_INT_ENABLE, 0); // Disable interrupts to start
|
||||
writeRegister(ADXL3XX_REG_THRESH_TAP, 20); // 62.5 mg/LSB (so 0xFF = 16 g)
|
||||
writeRegister(ADXL3XX_REG_DUR, 50); // Max tap duration, 625 µs/LSB
|
||||
writeRegister(ADXL3XX_REG_LATENT,
|
||||
0); // Tap latency, 1.25 ms/LSB, 0=no double tap
|
||||
writeRegister(ADXL3XX_REG_WINDOW,
|
||||
0); // Waiting period, 1.25 ms/LSB, 0=no double tap
|
||||
writeRegister(ADXL3XX_REG_TAP_AXES, 0x7); // Enable the XYZ axis for tap
|
||||
|
||||
// Enable measurements
|
||||
writeRegister(ADXL3XX_REG_POWER_CTL, 0x08);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Sets the g range for the accelerometer
|
||||
|
||||
@param range The range to set, based on adxl34x_range_t
|
||||
*/
|
||||
/**************************************************************************/
|
||||
void Adafruit_ADXL343::setRange(adxl34x_range_t range) {
|
||||
Adafruit_BusIO_Register data_format_reg =
|
||||
Adafruit_BusIO_Register(i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC,
|
||||
ADXL3XX_REG_DATA_FORMAT, 1);
|
||||
|
||||
Adafruit_BusIO_RegisterBits range_bits =
|
||||
Adafruit_BusIO_RegisterBits(&data_format_reg, 2, 0);
|
||||
|
||||
Adafruit_BusIO_RegisterBits full_range_bit =
|
||||
Adafruit_BusIO_RegisterBits(&data_format_reg, 1, 3);
|
||||
|
||||
/* Update the data rate */
|
||||
range_bits.write(range);
|
||||
/* Make sure that the FULL-RES bit is enabled for range scaling */
|
||||
full_range_bit.write(true);
|
||||
|
||||
/* Keep track of the current range (to avoid readbacks) */
|
||||
_range = range;
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Sets the g range for the accelerometer
|
||||
|
||||
@return The adxl34x_range_t value corresponding to the sensors range
|
||||
*/
|
||||
/**************************************************************************/
|
||||
adxl34x_range_t Adafruit_ADXL343::getRange(void) {
|
||||
Adafruit_BusIO_Register data_format_reg =
|
||||
Adafruit_BusIO_Register(i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC,
|
||||
ADXL3XX_REG_DATA_FORMAT, 1);
|
||||
|
||||
Adafruit_BusIO_RegisterBits range_bits =
|
||||
Adafruit_BusIO_RegisterBits(&data_format_reg, 2, 0);
|
||||
return (adxl34x_range_t)range_bits.read();
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Sets the data rate for the ADXL343 (controls power consumption)
|
||||
|
||||
@param dataRate The data rate to set, based on adxl3xx_dataRate_t
|
||||
*/
|
||||
/**************************************************************************/
|
||||
void Adafruit_ADXL343::setDataRate(adxl3xx_dataRate_t dataRate) {
|
||||
/* Note: The LOW_POWER bits are currently ignored and we always keep
|
||||
the device in 'normal' mode */
|
||||
writeRegister(ADXL3XX_REG_BW_RATE, dataRate);
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Sets the data rate for the ADXL343 (controls power consumption)
|
||||
|
||||
@return The current data rate, based on adxl3xx_dataRate_t
|
||||
*/
|
||||
/**************************************************************************/
|
||||
adxl3xx_dataRate_t Adafruit_ADXL343::getDataRate(void) {
|
||||
Adafruit_BusIO_Register bw_rate_reg = Adafruit_BusIO_Register(
|
||||
i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC, ADXL3XX_REG_BW_RATE, 1);
|
||||
|
||||
Adafruit_BusIO_RegisterBits rate_bits =
|
||||
Adafruit_BusIO_RegisterBits(&bw_rate_reg, 4, 0);
|
||||
|
||||
return (adxl3xx_dataRate_t)rate_bits.read();
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Retrieves the X Y Z trim offsets, note that they are 4 bits signed
|
||||
but we use int8_t to store and 'extend' the sign bit!
|
||||
@param x Pointer to the x offset, from -5 to 4 (internally multiplied by 8
|
||||
lsb)
|
||||
@param y Pointer to the y offset, from -5 to 4 (internally multiplied by 8
|
||||
lsb)
|
||||
@param z Pointer to the z offset, from -5 to 4 (internally multiplied by 8
|
||||
lsb)
|
||||
*/
|
||||
/**************************************************************************/
|
||||
void Adafruit_ADXL343::getTrimOffsets(int8_t *x, int8_t *y, int8_t *z) {
|
||||
Adafruit_BusIO_Register x_off = Adafruit_BusIO_Register(
|
||||
i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC, ADXL3XX_REG_OFSX, 1);
|
||||
if (x != NULL)
|
||||
*x = x_off.read();
|
||||
Adafruit_BusIO_Register y_off = Adafruit_BusIO_Register(
|
||||
i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC, ADXL3XX_REG_OFSY, 1);
|
||||
if (y != NULL)
|
||||
*y = y_off.read();
|
||||
Adafruit_BusIO_Register z_off = Adafruit_BusIO_Register(
|
||||
i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC, ADXL3XX_REG_OFSZ, 1);
|
||||
if (z != NULL)
|
||||
*z = z_off.read();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Sets the X Y Z trim offsets, note that they are 4 bits signed
|
||||
but we use int8_t to store and 'extend' the sign bit!
|
||||
@param x The x offset, from -5 to 4 (internally multiplied by 8 lsb)
|
||||
@param y The y offset, from -5 to 4 (internally multiplied by 8 lsb)
|
||||
@param z The z offset, from -5 to 4 (internally multiplied by 8 lsb)
|
||||
*/
|
||||
/**************************************************************************/
|
||||
void Adafruit_ADXL343::setTrimOffsets(int8_t x, int8_t y, int8_t z) {
|
||||
Adafruit_BusIO_Register x_off = Adafruit_BusIO_Register(
|
||||
i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC, ADXL3XX_REG_OFSX, 1);
|
||||
x_off.write(x);
|
||||
|
||||
Adafruit_BusIO_Register y_off = Adafruit_BusIO_Register(
|
||||
i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC, ADXL3XX_REG_OFSY, 1);
|
||||
y_off.write(y);
|
||||
|
||||
Adafruit_BusIO_Register z_off = Adafruit_BusIO_Register(
|
||||
i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC, ADXL3XX_REG_OFSZ, 1);
|
||||
z_off.write(z);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Gets the most recent sensor event
|
||||
|
||||
@param event Pointer to the sensors_event_t placeholder
|
||||
|
||||
@return True of the read request was successful.
|
||||
*/
|
||||
/**************************************************************************/
|
||||
bool Adafruit_ADXL343::getEvent(sensors_event_t *event) {
|
||||
int16_t x, y, z;
|
||||
/* Clear the event */
|
||||
memset(event, 0, sizeof(sensors_event_t));
|
||||
|
||||
event->version = sizeof(sensors_event_t);
|
||||
event->sensor_id = _sensorID;
|
||||
event->type = SENSOR_TYPE_ACCELEROMETER;
|
||||
event->timestamp = millis();
|
||||
if (!getXYZ(x, y, z))
|
||||
return false;
|
||||
event->acceleration.x =
|
||||
x * ADXL343_MG2G_MULTIPLIER * SENSORS_GRAVITY_STANDARD;
|
||||
event->acceleration.y =
|
||||
y * ADXL343_MG2G_MULTIPLIER * SENSORS_GRAVITY_STANDARD;
|
||||
event->acceleration.z =
|
||||
z * ADXL343_MG2G_MULTIPLIER * SENSORS_GRAVITY_STANDARD;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Gets the sensor_t data
|
||||
|
||||
@param sensor Pointer to the sensor_t placeholder.
|
||||
*/
|
||||
/**************************************************************************/
|
||||
void Adafruit_ADXL343::getSensor(sensor_t *sensor) {
|
||||
/* Clear the sensor_t object */
|
||||
memset(sensor, 0, sizeof(sensor_t));
|
||||
|
||||
/* Insert the sensor name in the fixed length char array */
|
||||
strncpy(sensor->name, "ADXL343", sizeof(sensor->name) - 1);
|
||||
sensor->name[sizeof(sensor->name) - 1] = 0;
|
||||
sensor->version = 1;
|
||||
sensor->sensor_id = _sensorID;
|
||||
sensor->type = SENSOR_TYPE_ACCELEROMETER;
|
||||
sensor->min_delay = 0;
|
||||
sensor->min_value = -156.9064F; /* -16g = 156.9064 m/s^2 */
|
||||
sensor->max_value = 156.9064F; /* 16g = 156.9064 m/s^2 */
|
||||
sensor->resolution = 0.03923F; /* 4mg = 0.0392266 m/s^2 */
|
||||
}
|
||||
204
libraries/Adafruit_ADXL343/Adafruit_ADXL343.h
Normal file
204
libraries/Adafruit_ADXL343/Adafruit_ADXL343.h
Normal file
@@ -0,0 +1,204 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file Adafruit_ADXL343.h
|
||||
@author Bryan Siepert and K. Townsend (Adafruit Industries)
|
||||
|
||||
BSD license (see license.txt)
|
||||
|
||||
This is a library for the Adafruit ADS1015 breakout board
|
||||
----> https://www.adafruit.com/products/???
|
||||
|
||||
Adafruit invests time and resources providing this open source code,
|
||||
please support Adafruit and open-source hardware by purchasing
|
||||
products from Adafruit!
|
||||
|
||||
v1.0 - First release
|
||||
*/
|
||||
/**************************************************************************/
|
||||
#ifndef _ADAFRUIT_SENSOR_ADXL343_H
|
||||
#define _ADAFRUIT_SENSOR_ADXL343_H
|
||||
|
||||
#if ARDUINO >= 100
|
||||
#include "Arduino.h"
|
||||
#else
|
||||
#include "WProgram.h"
|
||||
#endif
|
||||
|
||||
#include <Adafruit_BusIO_Register.h>
|
||||
#include <Adafruit_I2CDevice.h>
|
||||
#include <Adafruit_SPIDevice.h>
|
||||
#include <Adafruit_Sensor.h>
|
||||
#include <Wire.h>
|
||||
|
||||
/*=========================================================================
|
||||
I2C ADDRESS/BITS
|
||||
-----------------------------------------------------------------------*/
|
||||
#define ADXL343_ADDRESS (0x53) /**< Assumes ALT address pin low */
|
||||
/*=========================================================================*/
|
||||
|
||||
/*=========================================================================
|
||||
REGISTERS
|
||||
-----------------------------------------------------------------------*/
|
||||
#define ADXL3XX_REG_DEVID (0x00) /**< Device ID */
|
||||
#define ADXL3XX_REG_THRESH_TAP (0x1D) /**< Tap threshold */
|
||||
#define ADXL3XX_REG_OFSX (0x1E) /**< X-axis offset */
|
||||
#define ADXL3XX_REG_OFSY (0x1F) /**< Y-axis offset */
|
||||
#define ADXL3XX_REG_OFSZ (0x20) /**< Z-axis offset */
|
||||
#define ADXL3XX_REG_DUR (0x21) /**< Tap duration */
|
||||
#define ADXL3XX_REG_LATENT (0x22) /**< Tap latency */
|
||||
#define ADXL3XX_REG_WINDOW (0x23) /**< Tap window */
|
||||
#define ADXL3XX_REG_THRESH_ACT (0x24) /**< Activity threshold */
|
||||
#define ADXL3XX_REG_THRESH_INACT (0x25) /**< Inactivity threshold */
|
||||
#define ADXL3XX_REG_TIME_INACT (0x26) /**< Inactivity time */
|
||||
#define ADXL3XX_REG_ACT_INACT_CTL \
|
||||
(0x27) /**< Axis enable control for activity and inactivity detection */
|
||||
#define ADXL3XX_REG_THRESH_FF (0x28) /**< Free-fall threshold */
|
||||
#define ADXL3XX_REG_TIME_FF (0x29) /**< Free-fall time */
|
||||
#define ADXL3XX_REG_TAP_AXES (0x2A) /**< Axis control for single/double tap */
|
||||
#define ADXL3XX_REG_ACT_TAP_STATUS (0x2B) /**< Source for single/double tap */
|
||||
#define ADXL3XX_REG_BW_RATE (0x2C) /**< Data rate and power mode control */
|
||||
#define ADXL3XX_REG_POWER_CTL (0x2D) /**< Power-saving features control */
|
||||
#define ADXL3XX_REG_INT_ENABLE (0x2E) /**< Interrupt enable control */
|
||||
#define ADXL3XX_REG_INT_MAP (0x2F) /**< Interrupt mapping control */
|
||||
#define ADXL3XX_REG_INT_SOURCE (0x30) /**< Source of interrupts */
|
||||
#define ADXL3XX_REG_DATA_FORMAT (0x31) /**< Data format control */
|
||||
#define ADXL3XX_REG_DATAX0 (0x32) /**< X-axis data 0 */
|
||||
#define ADXL3XX_REG_DATAX1 (0x33) /**< X-axis data 1 */
|
||||
#define ADXL3XX_REG_DATAY0 (0x34) /**< Y-axis data 0 */
|
||||
#define ADXL3XX_REG_DATAY1 (0x35) /**< Y-axis data 1 */
|
||||
#define ADXL3XX_REG_DATAZ0 (0x36) /**< Z-axis data 0 */
|
||||
#define ADXL3XX_REG_DATAZ1 (0x37) /**< Z-axis data 1 */
|
||||
#define ADXL3XX_REG_FIFO_CTL (0x38) /**< FIFO control */
|
||||
#define ADXL3XX_REG_FIFO_STATUS (0x39) /**< FIFO status */
|
||||
/*=========================================================================*/
|
||||
|
||||
/*=========================================================================
|
||||
REGISTERS
|
||||
-----------------------------------------------------------------------*/
|
||||
#define ADXL343_MG2G_MULTIPLIER (0.004) /**< 4mg per lsb */
|
||||
/*=========================================================================*/
|
||||
|
||||
/** Used with register 0x2C (ADXL3XX_REG_BW_RATE) to set bandwidth */
|
||||
typedef enum {
|
||||
ADXL343_DATARATE_3200_HZ = 0b1111, /**< 3200Hz Bandwidth */
|
||||
ADXL343_DATARATE_1600_HZ = 0b1110, /**< 1600Hz Bandwidth */
|
||||
ADXL343_DATARATE_800_HZ = 0b1101, /**< 800Hz Bandwidth */
|
||||
ADXL343_DATARATE_400_HZ = 0b1100, /**< 400Hz Bandwidth */
|
||||
ADXL343_DATARATE_200_HZ = 0b1011, /**< 200Hz Bandwidth */
|
||||
ADXL343_DATARATE_100_HZ = 0b1010, /**< 100Hz Bandwidth */
|
||||
ADXL343_DATARATE_50_HZ = 0b1001, /**< 50Hz Bandwidth */
|
||||
ADXL343_DATARATE_25_HZ = 0b1000, /**< 25Hz Bandwidth */
|
||||
ADXL343_DATARATE_12_5_HZ = 0b0111, /**< 12.5Hz Bandwidth */
|
||||
ADXL343_DATARATE_6_25HZ = 0b0110, /**< 6.25Hz Bandwidth */
|
||||
ADXL343_DATARATE_3_13_HZ = 0b0101, /**< 3.13Hz Bandwidth */
|
||||
ADXL343_DATARATE_1_56_HZ = 0b0100, /**< 1.56Hz Bandwidth */
|
||||
ADXL343_DATARATE_0_78_HZ = 0b0011, /**< 0.78Hz Bandwidth */
|
||||
ADXL343_DATARATE_0_39_HZ = 0b0010, /**< 0.39Hz Bandwidth */
|
||||
ADXL343_DATARATE_0_20_HZ = 0b0001, /**< 0.20Hz Bandwidth */
|
||||
ADXL343_DATARATE_0_10_HZ = 0b0000, /**< 0.10Hz Bandwidth (default value) */
|
||||
|
||||
ADXL3XX_DATARATE_3200_HZ = 0b1111, /**< 3200Hz Bandwidth */
|
||||
ADXL3XX_DATARATE_1600_HZ = 0b1110, /**< 1600Hz Bandwidth */
|
||||
ADXL3XX_DATARATE_800_HZ = 0b1101, /**< 800Hz Bandwidth */
|
||||
ADXL3XX_DATARATE_400_HZ = 0b1100, /**< 400Hz Bandwidth */
|
||||
ADXL3XX_DATARATE_200_HZ = 0b1011, /**< 200Hz Bandwidth */
|
||||
ADXL3XX_DATARATE_100_HZ = 0b1010, /**< 100Hz Bandwidth */
|
||||
ADXL3XX_DATARATE_50_HZ = 0b1001, /**< 50Hz Bandwidth */
|
||||
ADXL3XX_DATARATE_25_HZ = 0b1000, /**< 25Hz Bandwidth */
|
||||
ADXL3XX_DATARATE_12_5_HZ = 0b0111, /**< 12.5Hz Bandwidth */
|
||||
ADXL3XX_DATARATE_6_25HZ = 0b0110, /**< 6.25Hz Bandwidth */
|
||||
ADXL3XX_DATARATE_3_13_HZ = 0b0101, /**< 3.13Hz Bandwidth */
|
||||
ADXL3XX_DATARATE_1_56_HZ = 0b0100, /**< 1.56Hz Bandwidth */
|
||||
ADXL3XX_DATARATE_0_78_HZ = 0b0011, /**< 0.78Hz Bandwidth */
|
||||
ADXL3XX_DATARATE_0_39_HZ = 0b0010, /**< 0.39Hz Bandwidth */
|
||||
ADXL3XX_DATARATE_0_20_HZ = 0b0001, /**< 0.20Hz Bandwidth */
|
||||
ADXL3XX_DATARATE_0_10_HZ = 0b0000 /**< 0.10Hz Bandwidth (default value) */
|
||||
} adxl3xx_dataRate_t;
|
||||
|
||||
/** Used with register 0x31 (ADXL3XX_REG_DATA_FORMAT) to set g range */
|
||||
typedef enum {
|
||||
ADXL343_RANGE_16_G = 0b11, /**< +/- 16g */
|
||||
ADXL343_RANGE_8_G = 0b10, /**< +/- 8g */
|
||||
ADXL343_RANGE_4_G = 0b01, /**< +/- 4g */
|
||||
ADXL343_RANGE_2_G = 0b00, /**< +/- 2g (default value) */
|
||||
|
||||
ADXL34X_RANGE_16_G = 0b11, /**< +/- 16g */
|
||||
ADXL34X_RANGE_8_G = 0b10, /**< +/- 8g */
|
||||
ADXL34X_RANGE_4_G = 0b01, /**< +/- 4g */
|
||||
ADXL34X_RANGE_2_G = 0b00 /**< +/- 2g (default value) */
|
||||
} adxl34x_range_t;
|
||||
|
||||
/** Possible interrupts sources on the ADXL343. */
|
||||
union int_config {
|
||||
uint8_t value; /**< Composite 8-bit value of the bitfield.*/
|
||||
struct {
|
||||
uint8_t overrun : 1; /**< Bit 0 */
|
||||
uint8_t watermark : 1; /**< Bit 1 */
|
||||
uint8_t freefall : 1; /**< Bit 2 */
|
||||
uint8_t inactivity : 1; /**< Bit 3 */
|
||||
uint8_t activity : 1; /**< Bit 4 */
|
||||
uint8_t double_tap : 1; /**< Bit 5 */
|
||||
uint8_t single_tap : 1; /**< Bit 6 */
|
||||
uint8_t data_ready : 1; /**< Bit 7 */
|
||||
} bits; /**< Individual bits in the bitfield. */
|
||||
};
|
||||
|
||||
/** Possible interrupt pin outputs on the ADXL343. */
|
||||
typedef enum {
|
||||
ADXL343_INT1 = 0,
|
||||
ADXL343_INT2 = 1,
|
||||
ADXL3XX_INT1 = 0,
|
||||
ADXL3XX_INT2 = 1,
|
||||
} adxl3xx_int_pin;
|
||||
|
||||
/**
|
||||
* Driver for the Adafruit ADXL343 breakout.
|
||||
*/
|
||||
class Adafruit_ADXL343 : public Adafruit_Sensor {
|
||||
public:
|
||||
Adafruit_ADXL343(int32_t sensorID);
|
||||
Adafruit_ADXL343(int32_t sensorID, TwoWire *wireBus);
|
||||
Adafruit_ADXL343(uint8_t clock, uint8_t miso, uint8_t mosi, uint8_t cs,
|
||||
int32_t sensorID = -1);
|
||||
Adafruit_ADXL343(uint8_t cs, SPIClass *theSPI, int32_t sensorID = -1);
|
||||
|
||||
bool begin(uint8_t i2caddr = ADXL343_ADDRESS);
|
||||
void setRange(adxl34x_range_t range);
|
||||
adxl34x_range_t getRange(void);
|
||||
void setDataRate(adxl3xx_dataRate_t dataRate);
|
||||
adxl3xx_dataRate_t getDataRate(void);
|
||||
bool getEvent(sensors_event_t *);
|
||||
void getSensor(sensor_t *);
|
||||
|
||||
uint8_t getDeviceID(void);
|
||||
void writeRegister(uint8_t reg, uint8_t value);
|
||||
uint8_t readRegister(uint8_t reg);
|
||||
int16_t read16(uint8_t reg);
|
||||
|
||||
bool enableInterrupts(int_config cfg);
|
||||
bool mapInterrupts(int_config cfg);
|
||||
uint8_t checkInterrupts(void);
|
||||
|
||||
void getTrimOffsets(int8_t *x, int8_t *y, int8_t *z);
|
||||
void setTrimOffsets(int8_t x, int8_t y, int8_t z);
|
||||
|
||||
int16_t getX(void);
|
||||
int16_t getY(void);
|
||||
int16_t getZ(void);
|
||||
bool getXYZ(int16_t &x, int16_t &y, int16_t &z);
|
||||
|
||||
protected:
|
||||
Adafruit_SPIDevice *spi_dev = NULL; ///< BusIO SPI device
|
||||
Adafruit_I2CDevice *i2c_dev = NULL; ///< BusIO I2C device
|
||||
|
||||
TwoWire *_wire = NULL; ///< I2C hardware interface
|
||||
SPIClass *_spi = NULL; ///< SPI hardware interface
|
||||
int32_t _sensorID; ///< User-set sensor identifier
|
||||
adxl34x_range_t _range; ///< cache of range
|
||||
uint8_t _clk, ///< SPI software clock
|
||||
_do, ///< SPI software data out
|
||||
_di, ///< SPI software data in
|
||||
_cs; ///< SPI software chip select
|
||||
};
|
||||
|
||||
#endif
|
||||
29
libraries/Adafruit_ADXL343/README.md
Normal file
29
libraries/Adafruit_ADXL343/README.md
Normal file
@@ -0,0 +1,29 @@
|
||||
# Adafruit ADXL343 Accelerometer Driver 
|
||||
|
||||
This driver is for the Adafruit ADXL343 Breakout (https://www.adafruit.com/product/4097), and is based on Adafruit's Unified Sensor Library (Adafruit_Sensor).
|
||||
|
||||
## About the ADXL343 ##
|
||||
|
||||
The ADXL343 is a digital accelerometer that supports both SPI and I2C mode, with adjustable data rata and 'range' (+/-2/4/8/16g). The Adafruit_ADXL343 driver takes advantage of I2C mode to reduce the total pin count required to use the sensor.
|
||||
|
||||
More information on the ADXL345 can be found in the datasheet: http://www.analog.com/static/imported-files/data_sheets/ADXL343.pdf
|
||||
|
||||
## What is the Adafruit Unified Sensor Library? ##
|
||||
|
||||
The Adafruit Unified Sensor Library (https://github.com/adafruit/Adafruit_Sensor) provides a common interface and data type for any supported sensor. It defines some basic information about the sensor (sensor limits, etc.), and returns standard SI units of a specific type and scale for each supported sensor type.
|
||||
|
||||
It provides a simple abstraction layer between your application and the actual sensor HW, allowing you to drop in any comparable sensor with only one or two lines of code to change in your project (essentially the constructor since the functions to read sensor data and get information about the sensor are defined in the base Adafruit_Sensor class).
|
||||
|
||||
This is imporant useful for two reasons:
|
||||
|
||||
1.) You can use the data right away because it's already converted to SI units that you understand and can compare, rather than meaningless values like 0..1023.
|
||||
|
||||
2.) Because SI units are standardised in the sensor library, you can also do quick sanity checks working with new sensors, or drop in any comparable sensor if you need better sensitivity or if a lower cost unit becomes available, etc.
|
||||
|
||||
Light sensors will always report units in lux, gyroscopes will always report units in rad/s, etc. ... freeing you up to focus on the data, rather than digging through the datasheet to understand what the sensor's raw numbers really mean.
|
||||
|
||||
## About this Driver ##
|
||||
|
||||
Adafruit invests time and resources providing this open source code. Please support Adafruit and open-source hardware by purchasing products from Adafruit!
|
||||
|
||||
Written by Kevin (KTOWN) Townsend for Adafruit Industries.
|
||||
149
libraries/Adafruit_ADXL343/examples/interrupts/interrupts.ino
Normal file
149
libraries/Adafruit_ADXL343/examples/interrupts/interrupts.ino
Normal file
@@ -0,0 +1,149 @@
|
||||
#include <Wire.h>
|
||||
#include <Adafruit_Sensor.h>
|
||||
#include <Adafruit_ADXL343.h>
|
||||
|
||||
/* Assign a unique ID to this sensor at the same time */
|
||||
Adafruit_ADXL343 accel = Adafruit_ADXL343(12345);
|
||||
|
||||
/** The input pins to enable the interrupt on, connected to INT1 and INT2 on the ADXL. */
|
||||
#define INPUT_PIN_INT1 (5) // Uno = (2)
|
||||
#define INPUT_PIN_INT2 (6) // Uno = (3)
|
||||
|
||||
/**
|
||||
* This struct is used to count the number of times that specific interrutps
|
||||
* have been fired by the ADXL and detected on the MCU. They will increment
|
||||
* by one for each event associated with the specified interrupt 'bit'.
|
||||
*/
|
||||
struct adxl_int_stats {
|
||||
uint32_t data_ready;
|
||||
uint32_t single_tap;
|
||||
uint32_t double_tap;
|
||||
uint32_t activity;
|
||||
uint32_t inactivity;
|
||||
uint32_t freefall;
|
||||
uint32_t watermark;
|
||||
uint32_t overrun;
|
||||
uint32_t total;
|
||||
};
|
||||
|
||||
/** Global stats block, incremented inside the interrupt handler(s). */
|
||||
struct adxl_int_stats g_int_stats = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
|
||||
|
||||
/** Global counter to track the numbers of unused interrupts fired. */
|
||||
uint32_t g_ints_fired = 0;
|
||||
|
||||
/** Global variable to determine which interrupt(s) are enabled on the ADXL343. */
|
||||
int_config g_int_config_enabled = { 0 };
|
||||
|
||||
/** Global variables to determine which INT pin interrupt(s) are mapped to on the ADXL343. */
|
||||
int_config g_int_config_map = { 0 };
|
||||
|
||||
/** Interrupt service routine for INT1 events. */
|
||||
void int1_isr(void)
|
||||
{
|
||||
/* By default, this sketch routes the OVERRUN interrupt to INT1. */
|
||||
g_int_stats.overrun++;
|
||||
g_int_stats.total++;
|
||||
g_ints_fired++;
|
||||
|
||||
/* TODO: Toggle an LED! */
|
||||
}
|
||||
|
||||
/** Interrupt service routine for INT2 events. */
|
||||
void int2_isr(void)
|
||||
{
|
||||
/* By default, this sketch routes the DATA_READY interrupt to INT2. */
|
||||
g_int_stats.data_ready++;
|
||||
g_int_stats.total++;
|
||||
g_ints_fired++;
|
||||
|
||||
/* TODO: Toggle an LED! */
|
||||
}
|
||||
|
||||
/** Configures the HW interrupts on the ADXL343 and the target MCU. */
|
||||
void config_interrupts(void)
|
||||
{
|
||||
/* NOTE: Once an interrupt fires on the ADXL you can read a register
|
||||
* to know the source of the interrupt, but since this would likely
|
||||
* happen in the 'interrupt context' performing an I2C read is a bad
|
||||
* idea since it will block the device from handling other interrupts
|
||||
* in a timely manner.
|
||||
*
|
||||
* The best approach is to try to make use of only two interrupts on
|
||||
* two different interrupt pins, so that when an interrupt fires, based
|
||||
* on the 'isr' function that is called, you already know the int source.
|
||||
*/
|
||||
|
||||
/* Attach interrupt inputs on the MCU. */
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
pinMode(INPUT_PIN_INT1, INPUT);
|
||||
pinMode(INPUT_PIN_INT2, INPUT);
|
||||
attachInterrupt(digitalPinToInterrupt(INPUT_PIN_INT1), int1_isr, RISING);
|
||||
attachInterrupt(digitalPinToInterrupt(INPUT_PIN_INT2), int2_isr, RISING);
|
||||
|
||||
/* Enable interrupts on the accelerometer. */
|
||||
g_int_config_enabled.bits.overrun = true; /* Set the INT1 */
|
||||
g_int_config_enabled.bits.watermark = false;
|
||||
g_int_config_enabled.bits.freefall = false;
|
||||
g_int_config_enabled.bits.inactivity = false;
|
||||
g_int_config_enabled.bits.activity = false;
|
||||
g_int_config_enabled.bits.double_tap = false;
|
||||
g_int_config_enabled.bits.single_tap = false;
|
||||
g_int_config_enabled.bits.data_ready = true; /* Set to INT2 */
|
||||
accel.enableInterrupts(g_int_config_enabled);
|
||||
|
||||
/* Map specific interrupts to one of the two INT pins. */
|
||||
g_int_config_map.bits.overrun = ADXL343_INT1;
|
||||
g_int_config_map.bits.watermark = ADXL343_INT1;
|
||||
g_int_config_map.bits.freefall = ADXL343_INT1;
|
||||
g_int_config_map.bits.inactivity = ADXL343_INT1;
|
||||
g_int_config_map.bits.activity = ADXL343_INT1;
|
||||
g_int_config_map.bits.double_tap = ADXL343_INT1;
|
||||
g_int_config_map.bits.single_tap = ADXL343_INT1;
|
||||
g_int_config_map.bits.data_ready = ADXL343_INT2;
|
||||
accel.mapInterrupts(g_int_config_map);
|
||||
}
|
||||
|
||||
void setup(void)
|
||||
{
|
||||
Serial.begin(9600);
|
||||
while (!Serial);
|
||||
Serial.println("ADXL343 Interrupt Tester"); Serial.println("");
|
||||
|
||||
/* Initialise the sensor */
|
||||
if(!accel.begin())
|
||||
{
|
||||
/* There was a problem detecting the ADXL343 ... check your connections */
|
||||
Serial.println("Ooops, no ADXL343 detected ... Check your wiring!");
|
||||
while(1);
|
||||
}
|
||||
|
||||
/* Set the range to whatever is appropriate for your project */
|
||||
accel.setRange(ADXL343_RANGE_16_G);
|
||||
// displaySetRange(ADXL343_RANGE_8_G);
|
||||
// displaySetRange(ADXL343_RANGE_4_G);
|
||||
// displaySetRange(ADXL343_RANGE_2_G);
|
||||
|
||||
|
||||
/* Configure the HW interrupts. */
|
||||
config_interrupts();
|
||||
|
||||
Serial.println("ADXL343 init complete. Waiting for INT activity.");
|
||||
}
|
||||
|
||||
void loop(void)
|
||||
{
|
||||
/* Get a new sensor event */
|
||||
sensors_event_t event;
|
||||
accel.getEvent(&event);
|
||||
delay(10);
|
||||
|
||||
while (g_ints_fired) {
|
||||
Serial.println("INT detected!");
|
||||
Serial.print("\tOVERRUN Count: "); Serial.println(g_int_stats.overrun, DEC);
|
||||
Serial.print("\tDATA_READY Count: "); Serial.println(g_int_stats.data_ready, DEC);
|
||||
|
||||
/* Decrement the unhandled int counter. */
|
||||
g_ints_fired--;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Connect two identical ADXL_343 sensor breakout boards
|
||||
* to a single Arduino. By connecting SDO on one of the
|
||||
* sensors to Vcc the I2C address of this sensor changes
|
||||
* from the default (0x53) to the alternative address (0x1D).
|
||||
* The address is passed during begin().
|
||||
*
|
||||
* This example returns raw sensor output of x, y and z acceleration
|
||||
* for both sensors over serial at 115200 baud.
|
||||
*
|
||||
* Example by Rolf Hut based on sensorTest example
|
||||
*/
|
||||
|
||||
|
||||
#include <Wire.h>
|
||||
#include <Adafruit_Sensor.h>
|
||||
#include <Adafruit_ADXL343.h>
|
||||
|
||||
/* Create two sensor objects and assign unique IDs */
|
||||
Adafruit_ADXL343 accel1 = Adafruit_ADXL343(12345);
|
||||
Adafruit_ADXL343 accel2 = Adafruit_ADXL343(12346);
|
||||
|
||||
|
||||
|
||||
|
||||
void setup(void)
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while (!Serial);
|
||||
|
||||
Serial.println("Two Accelerometers on one Arduino"); Serial.println("");
|
||||
|
||||
/* Initialise the first sensors, this uses the default address */
|
||||
if(!accel1.begin())
|
||||
{
|
||||
/* There was a problem detecting the ADXL343 ... check your connections */
|
||||
Serial.println("Ooops, no ADXL343 nr1 detected ... Check your wiring!");
|
||||
while(1);
|
||||
}
|
||||
|
||||
/* Initialise the second sensors, this uses the alternative address */
|
||||
/* of 0x1D. On this sensor SDO must be connected to VCC */
|
||||
if(!accel2.begin(0x1D))
|
||||
{
|
||||
/* There was a problem detecting the ADXL343 ... check your connections */
|
||||
Serial.println("Ooops, no ADXL343 nr2 detected ... Check your wiring!");
|
||||
while(1);
|
||||
}
|
||||
|
||||
/* Set the range and data rate to whatever is appropriate for your project */
|
||||
/* See the sensortest example for more details */
|
||||
accel1.setRange(ADXL343_RANGE_2_G);
|
||||
accel2.setRange(ADXL343_RANGE_2_G);
|
||||
|
||||
accel1.setDataRate(ADXL343_DATARATE_1600_HZ);
|
||||
accel2.setDataRate(ADXL343_DATARATE_1600_HZ);
|
||||
|
||||
/* Display some basic information on these sensors */
|
||||
accel1.printSensorDetails();
|
||||
accel2.printSensorDetails();
|
||||
Serial.println("");
|
||||
}
|
||||
|
||||
void loop(void)
|
||||
{
|
||||
/* Get new sensor events */
|
||||
sensors_event_t event1;
|
||||
sensors_event_t event2;
|
||||
accel1.getEvent(&event1);
|
||||
accel2.getEvent(&event2);
|
||||
|
||||
/* Display the results (acceleration is measured in m/s^2) */
|
||||
Serial.print(event1.acceleration.x); Serial.print(",");
|
||||
Serial.print(event1.acceleration.y); Serial.print(",");
|
||||
Serial.print(event1.acceleration.z); Serial.print(",");
|
||||
Serial.print(event2.acceleration.x); Serial.print(",");
|
||||
Serial.print(event2.acceleration.y); Serial.print(",");
|
||||
Serial.println(event2.acceleration.z);
|
||||
}
|
||||
93
libraries/Adafruit_ADXL343/examples/offsets/offsets.ino
Normal file
93
libraries/Adafruit_ADXL343/examples/offsets/offsets.ino
Normal file
@@ -0,0 +1,93 @@
|
||||
/* This example shows how to use the trimmer offset registers to account for any error in the sensor
|
||||
and 'zero' out the flat reading to be 0, 0, 1g. note this is unique to each sensor so it'll have
|
||||
to be repeated and 'saved' for each ADXL you use!
|
||||
*/
|
||||
|
||||
#include <Wire.h>
|
||||
#include <Adafruit_Sensor.h>
|
||||
#include <Adafruit_ADXL343.h>
|
||||
|
||||
#define ADXL343_SCK 13
|
||||
#define ADXL343_MISO 12
|
||||
#define ADXL343_MOSI 11
|
||||
#define ADXL343_CS 10
|
||||
|
||||
/* Assign a unique ID to this sensor at the same time */
|
||||
/* Uncomment following line for default Wire bus */
|
||||
Adafruit_ADXL343 accel = Adafruit_ADXL343(12345);
|
||||
|
||||
/* NeoTrellis M4, etc. */
|
||||
/* Uncomment following line for Wire1 bus */
|
||||
//Adafruit_ADXL343 accel = Adafruit_ADXL343(12345, &Wire1);
|
||||
|
||||
/* Uncomment for SPI */
|
||||
//Adafruit_ADXL343 accel = Adafruit_ADXL343(ADXL343_SCK, ADXL343_MISO, ADXL343_MOSI, ADXL343_CS, 12345);
|
||||
|
||||
void setup(void)
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while (!Serial);
|
||||
Serial.println("Offsets Test"); Serial.println("");
|
||||
|
||||
/* Initialise the sensor */
|
||||
if(!accel.begin())
|
||||
{
|
||||
/* There was a problem detecting the ADXL343 ... check your connections */
|
||||
Serial.println("Ooops, no ADXL343 detected ... Check your wiring!");
|
||||
while(1);
|
||||
}
|
||||
|
||||
/* Set the range to whatever is appropriate for your project */
|
||||
accel.setRange(ADXL343_RANGE_16_G);
|
||||
|
||||
/* Display some basic information on this sensor */
|
||||
accel.printSensorDetails();
|
||||
Serial.println("");
|
||||
|
||||
// init offsets to zero
|
||||
accel.setTrimOffsets(0, 0, 0);
|
||||
|
||||
Serial.println("Hold accelerometer flat to set offsets to 0, 0, and -1g...");
|
||||
delay(1000);
|
||||
int16_t x, y, z;
|
||||
x = accel.getX();
|
||||
y = accel.getY();
|
||||
z = accel.getZ();
|
||||
Serial.print("Raw X: "); Serial.print(x); Serial.print(" ");
|
||||
Serial.print("Y: "); Serial.print(y); Serial.print(" ");
|
||||
Serial.print("Z: "); Serial.print(z); Serial.print(" ");Serial.println(" counts");
|
||||
|
||||
// the trim offsets are in 'multiples' of 8, we want to round, so we add 4
|
||||
accel.setTrimOffsets(-(x+4)/8,
|
||||
-(y+4)/8,
|
||||
-(z-250+4)/8); // Z should be '250' at 1g (4mg per bit)
|
||||
|
||||
int8_t x_offset, y_offset, z_offset;
|
||||
accel.getTrimOffsets(&x_offset, &y_offset, &z_offset);
|
||||
Serial.print("Current trim offsets: ");
|
||||
Serial.print(x_offset); Serial.print(", ");
|
||||
Serial.print(y_offset); Serial.print(", ");
|
||||
Serial.println(z_offset);
|
||||
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void loop(void)
|
||||
{
|
||||
/* Get a new sensor event */
|
||||
sensors_event_t event;
|
||||
accel.getEvent(&event);
|
||||
|
||||
/* Display the results (acceleration is measured in m/s^2) */
|
||||
Serial.print("X: "); Serial.print(event.acceleration.x); Serial.print(" ");
|
||||
Serial.print("Y: "); Serial.print(event.acceleration.y); Serial.print(" ");
|
||||
Serial.print("Z: "); Serial.print(event.acceleration.z); Serial.print(" ");
|
||||
Serial.println("m/s^2 ");
|
||||
|
||||
Serial.print("Raw X: "); Serial.print(accel.getX()); Serial.print(" ");
|
||||
Serial.print("Y: "); Serial.print(accel.getY()); Serial.print(" ");
|
||||
Serial.print("Z: "); Serial.print(accel.getZ()); Serial.print(" ");
|
||||
Serial.println(" counts");
|
||||
Serial.println();
|
||||
delay(500);
|
||||
}
|
||||
148
libraries/Adafruit_ADXL343/examples/sensortest/sensortest.ino
Normal file
148
libraries/Adafruit_ADXL343/examples/sensortest/sensortest.ino
Normal file
@@ -0,0 +1,148 @@
|
||||
#include <Wire.h>
|
||||
#include <Adafruit_Sensor.h>
|
||||
#include <Adafruit_ADXL343.h>
|
||||
|
||||
#define ADXL343_SCK 13
|
||||
#define ADXL343_MISO 12
|
||||
#define ADXL343_MOSI 11
|
||||
#define ADXL343_CS 10
|
||||
|
||||
/* Assign a unique ID to this sensor at the same time */
|
||||
/* Uncomment following line for default Wire bus */
|
||||
Adafruit_ADXL343 accel = Adafruit_ADXL343(12345);
|
||||
|
||||
/* NeoTrellis M4, etc. */
|
||||
/* Uncomment following line for Wire1 bus */
|
||||
//Adafruit_ADXL343 accel = Adafruit_ADXL343(12345, &Wire1);
|
||||
|
||||
/* Uncomment for software SPI */
|
||||
//Adafruit_ADXL343 accel = Adafruit_ADXL343(ADXL343_SCK, ADXL343_MISO, ADXL343_MOSI, ADXL343_CS, 12345);
|
||||
|
||||
/* Uncomment for hardware SPI */
|
||||
//Adafruit_ADXL343 accel = Adafruit_ADXL343(ADXL343_CS, &SPI, 12345);
|
||||
|
||||
void displayDataRate(void)
|
||||
{
|
||||
Serial.print ("Data Rate: ");
|
||||
|
||||
switch(accel.getDataRate())
|
||||
{
|
||||
case ADXL343_DATARATE_3200_HZ:
|
||||
Serial.print ("3200 ");
|
||||
break;
|
||||
case ADXL343_DATARATE_1600_HZ:
|
||||
Serial.print ("1600 ");
|
||||
break;
|
||||
case ADXL343_DATARATE_800_HZ:
|
||||
Serial.print ("800 ");
|
||||
break;
|
||||
case ADXL343_DATARATE_400_HZ:
|
||||
Serial.print ("400 ");
|
||||
break;
|
||||
case ADXL343_DATARATE_200_HZ:
|
||||
Serial.print ("200 ");
|
||||
break;
|
||||
case ADXL343_DATARATE_100_HZ:
|
||||
Serial.print ("100 ");
|
||||
break;
|
||||
case ADXL343_DATARATE_50_HZ:
|
||||
Serial.print ("50 ");
|
||||
break;
|
||||
case ADXL343_DATARATE_25_HZ:
|
||||
Serial.print ("25 ");
|
||||
break;
|
||||
case ADXL343_DATARATE_12_5_HZ:
|
||||
Serial.print ("12.5 ");
|
||||
break;
|
||||
case ADXL343_DATARATE_6_25HZ:
|
||||
Serial.print ("6.25 ");
|
||||
break;
|
||||
case ADXL343_DATARATE_3_13_HZ:
|
||||
Serial.print ("3.13 ");
|
||||
break;
|
||||
case ADXL343_DATARATE_1_56_HZ:
|
||||
Serial.print ("1.56 ");
|
||||
break;
|
||||
case ADXL343_DATARATE_0_78_HZ:
|
||||
Serial.print ("0.78 ");
|
||||
break;
|
||||
case ADXL343_DATARATE_0_39_HZ:
|
||||
Serial.print ("0.39 ");
|
||||
break;
|
||||
case ADXL343_DATARATE_0_20_HZ:
|
||||
Serial.print ("0.20 ");
|
||||
break;
|
||||
case ADXL343_DATARATE_0_10_HZ:
|
||||
Serial.print ("0.10 ");
|
||||
break;
|
||||
default:
|
||||
Serial.print ("???? ");
|
||||
break;
|
||||
}
|
||||
Serial.println(" Hz");
|
||||
}
|
||||
|
||||
void displayRange(void)
|
||||
{
|
||||
Serial.print ("Range: +/- ");
|
||||
|
||||
switch(accel.getRange())
|
||||
{
|
||||
case ADXL343_RANGE_16_G:
|
||||
Serial.print ("16 ");
|
||||
break;
|
||||
case ADXL343_RANGE_8_G:
|
||||
Serial.print ("8 ");
|
||||
break;
|
||||
case ADXL343_RANGE_4_G:
|
||||
Serial.print ("4 ");
|
||||
break;
|
||||
case ADXL343_RANGE_2_G:
|
||||
Serial.print ("2 ");
|
||||
break;
|
||||
default:
|
||||
Serial.print ("?? ");
|
||||
break;
|
||||
}
|
||||
Serial.println(" g");
|
||||
}
|
||||
|
||||
void setup(void)
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while (!Serial);
|
||||
Serial.println("Accelerometer Test"); Serial.println("");
|
||||
|
||||
/* Initialise the sensor */
|
||||
if(!accel.begin())
|
||||
{
|
||||
/* There was a problem detecting the ADXL343 ... check your connections */
|
||||
Serial.println("Ooops, no ADXL343 detected ... Check your wiring!");
|
||||
while(1);
|
||||
}
|
||||
|
||||
/* Set the range to whatever is appropriate for your project */
|
||||
accel.setRange(ADXL343_RANGE_16_G);
|
||||
// accel.setRange(ADXL343_RANGE_8_G);
|
||||
// accel.setRange(ADXL343_RANGE_4_G);
|
||||
// accel.setRange(ADXL343_RANGE_2_G);
|
||||
|
||||
/* Display some basic information on this sensor */
|
||||
accel.printSensorDetails();
|
||||
displayDataRate();
|
||||
displayRange();
|
||||
Serial.println("");
|
||||
}
|
||||
|
||||
void loop(void)
|
||||
{
|
||||
/* Get a new sensor event */
|
||||
sensors_event_t event;
|
||||
accel.getEvent(&event);
|
||||
|
||||
/* Display the results (acceleration is measured in m/s^2) */
|
||||
Serial.print("X: "); Serial.print(event.acceleration.x); Serial.print(" ");
|
||||
Serial.print("Y: "); Serial.print(event.acceleration.y); Serial.print(" ");
|
||||
Serial.print("Z: "); Serial.print(event.acceleration.z); Serial.print(" ");Serial.println("m/s^2 ");
|
||||
delay(500);
|
||||
}
|
||||
71
libraries/Adafruit_ADXL343/examples/singletap/singletap.ino
Normal file
71
libraries/Adafruit_ADXL343/examples/singletap/singletap.ino
Normal file
@@ -0,0 +1,71 @@
|
||||
#include <Wire.h>
|
||||
#include <Adafruit_Sensor.h>
|
||||
#include <Adafruit_ADXL343.h>
|
||||
|
||||
/* Assign a unique ID to this sensor at the same time */
|
||||
Adafruit_ADXL343 accel = Adafruit_ADXL343(12345);
|
||||
|
||||
/** The input pin to enable the interrupt on, connected to INT1 on the ADXL. */
|
||||
#define INPUT_PIN_INT1 (5) // SAMD21/SAMD51 = 5 for interrupt pin
|
||||
|
||||
uint32_t g_tap_count = 0;
|
||||
int_config g_int_config_enabled = { 0 };
|
||||
int_config g_int_config_map = { 0 };
|
||||
|
||||
/** Interrupt service routine for INT1 events. This will be called when a single tap is detected. */
|
||||
void int1_isr(void)
|
||||
{
|
||||
g_tap_count++;
|
||||
}
|
||||
|
||||
void setup(void)
|
||||
{
|
||||
Serial.begin(9600);
|
||||
while (!Serial);
|
||||
Serial.println("ADXL343 Single Tap INT Tester"); Serial.println("");
|
||||
|
||||
/* Initialise the sensor */
|
||||
if(!accel.begin())
|
||||
{
|
||||
/* There was a problem detecting the ADXL343 ... check your connections */
|
||||
Serial.println("Ooops, no ADXL343 detected ... Check your wiring!");
|
||||
while(1);
|
||||
}
|
||||
|
||||
/* Set the range to whatever is appropriate for your project */
|
||||
accel.setRange(ADXL343_RANGE_16_G);
|
||||
|
||||
/* Configure the HW interrupts. */
|
||||
pinMode(INPUT_PIN_INT1, INPUT);
|
||||
attachInterrupt(digitalPinToInterrupt(INPUT_PIN_INT1), int1_isr, RISING);
|
||||
|
||||
/* Enable single tap interrupts on the accelerometer. */
|
||||
g_int_config_enabled.bits.single_tap = true;
|
||||
accel.enableInterrupts(g_int_config_enabled);
|
||||
|
||||
/* Map single tap interrupts to INT1 pin. */
|
||||
g_int_config_map.bits.single_tap = ADXL343_INT1;
|
||||
accel.mapInterrupts(g_int_config_map);
|
||||
|
||||
/* Reset tap counter. */
|
||||
g_tap_count = 0;
|
||||
|
||||
Serial.println("ADXL343 init complete. Waiting for single tap INT activity.");
|
||||
}
|
||||
|
||||
void loop(void)
|
||||
{
|
||||
/* Get a new sensor event */
|
||||
/* Reading data clears the interrupts. */
|
||||
sensors_event_t event;
|
||||
accel.getEvent(&event);
|
||||
delay(10);
|
||||
|
||||
while (g_tap_count) {
|
||||
Serial.println("Single tap detected!");
|
||||
/* Clear the interrupt as a side-effect of reading the interrupt source register.. */
|
||||
accel.checkInterrupts();
|
||||
/* Decrement the local interrupt counter. */
|
||||
g_tap_count--;
|
||||
}
|
||||
}
|
||||
10
libraries/Adafruit_ADXL343/library.properties
Normal file
10
libraries/Adafruit_ADXL343/library.properties
Normal file
@@ -0,0 +1,10 @@
|
||||
name=Adafruit ADXL343
|
||||
version=1.6.4
|
||||
author=Adafruit
|
||||
maintainer=Adafruit <info@adafruit.com>
|
||||
sentence=Unified driver for the ADXL343 Accelerometer
|
||||
paragraph=Unified driver for the ADXL343 Accelerometer
|
||||
category=Sensors
|
||||
url=https://github.com/adafruit/Adafruit_ADXL343
|
||||
architectures=*
|
||||
depends=Adafruit Unified Sensor, Adafruit BusIO
|
||||
26
libraries/Adafruit_ADXL343/license.txt
Normal file
26
libraries/Adafruit_ADXL343/license.txt
Normal file
@@ -0,0 +1,26 @@
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2019, Adafruit Industries
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
3. Neither the name of the copyright holders nor the
|
||||
names of its contributors may be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
|
||||
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
Reference in New Issue
Block a user