This commit is contained in:
ladyada 2019-07-21 16:51:23 -04:00
commit 0482f293c9
5 changed files with 612 additions and 0 deletions

46
.github/ISSUE_TEMPLATE.md vendored Normal file
View file

@ -0,0 +1,46 @@
Thank you for opening an issue on an Adafruit Arduino library repository. To
improve the speed of resolution please review the following guidelines and
common troubleshooting steps below before creating the issue:
- **Do not use GitHub issues for troubleshooting projects and issues.** Instead use
the forums at http://forums.adafruit.com to ask questions and troubleshoot why
something isn't working as expected. In many cases the problem is a common issue
that you will more quickly receive help from the forum community. GitHub issues
are meant for known defects in the code. If you don't know if there is a defect
in the code then start with troubleshooting on the forum first.
- **If following a tutorial or guide be sure you didn't miss a step.** Carefully
check all of the steps and commands to run have been followed. Consult the
forum if you're unsure or have questions about steps in a guide/tutorial.
- **For Arduino projects check these very common issues to ensure they don't apply**:
- For uploading sketches or communicating with the board make sure you're using
a **USB data cable** and **not** a **USB charge-only cable**. It is sometimes
very hard to tell the difference between a data and charge cable! Try using the
cable with other devices or swapping to another cable to confirm it is not
the problem.
- **Be sure you are supplying adequate power to the board.** Check the specs of
your board and plug in an external power supply. In many cases just
plugging a board into your computer is not enough to power it and other
peripherals.
- **Double check all soldering joints and connections.** Flakey connections
cause many mysterious problems. See the [guide to excellent soldering](https://learn.adafruit.com/adafruit-guide-excellent-soldering/tools) for examples of good solder joints.
- **Ensure you are using an official Arduino or Adafruit board.** We can't
guarantee a clone board will have the same functionality and work as expected
with this code and don't support them.
If you're sure this issue is a defect in the code and checked the steps above
please fill in the following fields to provide enough troubleshooting information.
You may delete the guideline and text above to just leave the following details:
- Arduino board: **INSERT ARDUINO BOARD NAME/TYPE HERE**
- Arduino IDE version (found in Arduino -> About Arduino menu): **INSERT ARDUINO
VERSION HERE**
- List the steps to reproduce the problem below (if possible attach a sketch or
copy the sketch code in too): **LIST REPRO STEPS BELOW**

26
.github/PULL_REQUEST_TEMPLATE.md vendored Normal file
View file

@ -0,0 +1,26 @@
Thank you for creating a pull request to contribute to Adafruit's GitHub code!
Before you open the request please review the following guidelines and tips to
help it be more easily integrated:
- **Describe the scope of your change--i.e. what the change does and what parts
of the code were modified.** This will help us understand any risks of integrating
the code.
- **Describe any known limitations with your change.** For example if the change
doesn't apply to a supported platform of the library please mention it.
- **Please run any tests or examples that can exercise your modified code.** We
strive to not break users of the code and running tests/examples helps with this
process.
Thank you again for contributing! We will try to test and integrate the change
as soon as we can, but be aware we have many GitHub repositories to manage and
can't immediately respond to every request. There is no need to bump or check in
on a pull request (it will clutter the discussion of the request).
Also don't be worried if the request is closed or not integrated--sometimes the
priorities of Adafruit's GitHub code (education, ease of use) might not match the
priorities of the pull request. Don't fret, the open source community thrives on
forks and GitHub makes it easy to keep your changes in a forked repo.
After reviewing the guidelines above you can delete this text from the pull request.

358
Adafruit_MSA301.cpp Normal file
View file

@ -0,0 +1,358 @@
/**************************************************************************/
/*!
@file Adafruit_MSA301.cpp
@author Limor Fried (Adafruit Industries)
@license MIT (see license.txt)
This is a library for the Adafruit MSA301 Accel breakout board
----> https://www.adafruit.com
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
*/
/**************************************************************************/
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <Wire.h>
#include <Adafruit_MSA301.h>
/**************************************************************************/
/*!
@brief Instantiates a new MSA301 class
*/
/**************************************************************************/
Adafruit_MSA301::Adafruit_MSA301() {}
/*!
* @brief Sets up the hardware and initializes I2C
* @param i2c_address
* The I2C address to be used.
* @param wire
* The Wire object to be used for I2C connections.
* @return True if initialization was successful, otherwise false.
*/
bool Adafruit_MSA301::begin(uint8_t i2c_address, TwoWire *wire) {
i2c_dev = new Adafruit_I2CDevice(i2c_address, wire);
if (!i2c_dev->begin()) {
Serial.println("Failed to init i2c address");
return false;
}
// Check connection
Adafruit_BusIO_Register chip_id =
Adafruit_BusIO_Register(i2c_dev, MSA301_REG_PARTID, 1);
// make sure we're talking to the right chip
if (chip_id.read() != 0x13) {
// No MSA301 detected ... return false
return false;
}
// enable all axes
enableAxes(true, true, true);
// normal mode
setPowerMode(MSA301_NORMALMODE);
// 500Hz rate
setDataRate(MSA301_DATARATE_500_HZ);
// 250Hz bw
setBandwidth(MSA301_BANDWIDTH_250_HZ);
setRange(MSA301_RANGE_4_G);
setResolution(MSA301_RESOLUTION_14);
/*
// DRDY on INT1
writeRegister8(MSA301_REG_CTRL3, 0x10);
// Turn on orientation config
//writeRegister8(MSA301_REG_PL_CFG, 0x40);
*/
/*
for (uint8_t i=0; i<0x30; i++) {
Serial.print("$");
Serial.print(i, HEX); Serial.print(" = 0x");
Serial.println(readRegister8(i), HEX);
}
*/
return true;
}
/**************************************************************************/
/*!
@brief Sets the data rate for the MSA301 (controls power consumption)
*/
/**************************************************************************/
void Adafruit_MSA301::setDataRate(msa301_dataRate_t dataRate)
{
Adafruit_BusIO_Register ODR =
Adafruit_BusIO_Register(i2c_dev, MSA301_REG_ODR, 1);
Adafruit_BusIO_RegisterBits dataratebits = Adafruit_BusIO_RegisterBits(&ODR, 4, 0);
dataratebits.write((uint8_t)dataRate);
}
/**************************************************************************/
/*!
@brief Sets the data rate for the MSA301 (controls power consumption)
*/
/**************************************************************************/
msa301_dataRate_t Adafruit_MSA301::getDataRate(void)
{
Adafruit_BusIO_Register ODR =
Adafruit_BusIO_Register(i2c_dev, MSA301_REG_ODR, 1);
Adafruit_BusIO_RegisterBits dataratebits = Adafruit_BusIO_RegisterBits(&ODR, 4, 0);
return (msa301_dataRate_t)dataratebits.read();
}
void Adafruit_MSA301::enableAxes(bool enableX, bool enableY, bool enableZ) {
Adafruit_BusIO_Register ODR =
Adafruit_BusIO_Register(i2c_dev, MSA301_REG_ODR, 1);
Adafruit_BusIO_RegisterBits x = Adafruit_BusIO_RegisterBits(&ODR, 1, 7);
Adafruit_BusIO_RegisterBits y = Adafruit_BusIO_RegisterBits(&ODR, 1, 6);
Adafruit_BusIO_RegisterBits z = Adafruit_BusIO_RegisterBits(&ODR, 1, 5);
x.write(!enableX); // these are *disable* bits, yeah...
y.write(!enableY);
z.write(!enableZ);
}
void Adafruit_MSA301::setPowerMode(msa301_powermode_t mode)
{
Adafruit_BusIO_Register PowerMode =
Adafruit_BusIO_Register(i2c_dev, MSA301_REG_POWERMODE, 1);
Adafruit_BusIO_RegisterBits powermodebits = Adafruit_BusIO_RegisterBits(&PowerMode, 2, 6);
powermodebits.write((uint8_t)mode);
}
msa301_powermode_t Adafruit_MSA301::getPowerMode(void)
{
Adafruit_BusIO_Register PowerMode =
Adafruit_BusIO_Register(i2c_dev, MSA301_REG_POWERMODE, 1);
Adafruit_BusIO_RegisterBits powermodebits = Adafruit_BusIO_RegisterBits(&PowerMode, 2, 6);
return (msa301_powermode_t)powermodebits.read();
}
void Adafruit_MSA301::setBandwidth(msa301_bandwidth_t bandwidth)
{
Adafruit_BusIO_Register PowerMode =
Adafruit_BusIO_Register(i2c_dev, MSA301_REG_POWERMODE, 1);
Adafruit_BusIO_RegisterBits bandwidthbits = Adafruit_BusIO_RegisterBits(&PowerMode, 4, 1);
bandwidthbits.write((uint8_t)bandwidth);
}
msa301_bandwidth_t Adafruit_MSA301::getBandwidth(void)
{
Adafruit_BusIO_Register PowerMode =
Adafruit_BusIO_Register(i2c_dev, MSA301_REG_POWERMODE, 1);
Adafruit_BusIO_RegisterBits bandwidthbits = Adafruit_BusIO_RegisterBits(&PowerMode, 4, 1);
return (msa301_bandwidth_t)bandwidthbits.read();
}
/**************************************************************************/
/*!
@brief Sets the g range for the accelerometer
*/
/**************************************************************************/
void Adafruit_MSA301::setRange(msa301_range_t range)
{
Adafruit_BusIO_Register ResRange =
Adafruit_BusIO_Register(i2c_dev, MSA301_REG_RESRANGE, 1);
Adafruit_BusIO_RegisterBits rangebits = Adafruit_BusIO_RegisterBits(&ResRange, 2, 0);
rangebits.write((uint8_t)range);
}
/**************************************************************************/
/*!
@brief Gets the g range for the accelerometer
*/
/**************************************************************************/
msa301_range_t Adafruit_MSA301::getRange(void)
{
Adafruit_BusIO_Register ResRange =
Adafruit_BusIO_Register(i2c_dev, MSA301_REG_RESRANGE, 1);
Adafruit_BusIO_RegisterBits rangebits = Adafruit_BusIO_RegisterBits(&ResRange, 2, 0);
return (msa301_range_t)rangebits.read();
}
/**************************************************************************/
/*!
@brief
*/
/**************************************************************************/
void Adafruit_MSA301::setResolution(msa301_resolution_t resolution)
{
Adafruit_BusIO_Register ResRange =
Adafruit_BusIO_Register(i2c_dev, MSA301_REG_RESRANGE, 1);
Adafruit_BusIO_RegisterBits resbits = Adafruit_BusIO_RegisterBits(&ResRange, 2, 2);
resbits.write((uint8_t)resolution);
}
/**************************************************************************/
/*!
@brief
*/
/**************************************************************************/
msa301_resolution_t Adafruit_MSA301::getResolution(void)
{
Adafruit_BusIO_Register ResRange =
Adafruit_BusIO_Register(i2c_dev, MSA301_REG_RESRANGE, 1);
Adafruit_BusIO_RegisterBits resbits = Adafruit_BusIO_RegisterBits(&ResRange, 2, 2);
return (msa301_resolution_t)resbits.read();
}
void Adafruit_MSA301::read(void) {
uint8_t buffer[6];
Adafruit_BusIO_Register XYZDataReg =
Adafruit_BusIO_Register(i2c_dev, MSA301_REG_OUT_X_L, 6);
XYZDataReg.read(buffer, 6);
x = buffer[0]; x |= buffer[1] << 8;
y = buffer[2]; y |= buffer[3] << 8;
z = buffer[4]; z |= buffer[5] << 8;
msa301_range_t range = getRange();
float divider = 3.9; // 3.9mg in 2g
if (range == MSA301_RANGE_16_G) divider = 31.3 * 1000.0;
if (range == MSA301_RANGE_8_G) divider = 15.6 * 1000.0;
if (range == MSA301_RANGE_4_G) divider = 7.8 * 1000.0;
if (range == MSA301_RANGE_2_G) divider = 3.9 * 1000.0;
x_g = (float)x / divider;
y_g = (float)y / divider;
z_g = (float)z / divider;
}
/**************************************************************************/
/*!
@brief Set INT to output for single or double click
*/
/**************************************************************************/
void Adafruit_MSA301::setClick(bool tap_quiet, bool tap_shock, msa301_tapduration_t tapduration, uint8_t tapthresh) {
Adafruit_BusIO_Register TapDur =
Adafruit_BusIO_Register(i2c_dev, MSA301_REG_TAPDUR, 1);
Adafruit_BusIO_RegisterBits quietbit = Adafruit_BusIO_RegisterBits(&TapDur, 1, 7);
quietbit.write(tap_quiet);
Adafruit_BusIO_RegisterBits shockbit = Adafruit_BusIO_RegisterBits(&TapDur, 1, 6);
shockbit.write(tap_shock);
Adafruit_BusIO_RegisterBits durationbits = Adafruit_BusIO_RegisterBits(&TapDur, 3, 0);
durationbits.write(tapduration);
Adafruit_BusIO_Register TapTh =
Adafruit_BusIO_Register(i2c_dev, MSA301_REG_TAPTH, 1);
Adafruit_BusIO_RegisterBits threshbits = Adafruit_BusIO_RegisterBits(&TapTh, 5, 0);
threshbits.write(tapthresh);
}
uint8_t Adafruit_MSA301::getClick(void) {
Adafruit_BusIO_Register ClickStatus =
Adafruit_BusIO_Register(i2c_dev, MSA301_REG_CLICKSTATUS, 1);
return ClickStatus.read();
}
void Adafruit_MSA301::enableInterrupts(bool orient, bool singletap, bool doubletap,
bool activeX, bool activeY, bool activeZ,
bool newData, bool freefall) {
Adafruit_BusIO_Register IntSet0 =
Adafruit_BusIO_Register(i2c_dev, MSA301_REG_INTSET0, 1);
Adafruit_BusIO_Register IntSet1 =
Adafruit_BusIO_Register(i2c_dev, MSA301_REG_INTSET1, 1);
uint8_t irqs = 0;
irqs |= orient << 6;
irqs |= singletap << 5;
irqs |= doubletap << 4;
irqs |= activeX << 0;
irqs |= activeY << 1;
irqs |= activeZ << 2;
IntSet0.write(irqs);
irqs = 0;
irqs |= newData << 4;
irqs |= freefall << 3;
IntSet1.write(irqs);
}
uint8_t Adafruit_MSA301::getMotionInterruptStatus(void) {
Adafruit_BusIO_Register IntStatus =
Adafruit_BusIO_Register(i2c_dev, MSA301_REG_MOTIONINT, 1);
return IntStatus.read();
}
uint8_t Adafruit_MSA301::getDataInterruptStatus(void) {
Adafruit_BusIO_Register IntStatus =
Adafruit_BusIO_Register(i2c_dev, MSA301_REG_DATAINT, 1);
return IntStatus.read();
}
/**************************************************************************/
/*!
@brief Gets the most recent sensor event
*/
/**************************************************************************/
bool Adafruit_MSA301::getEvent(sensors_event_t *event) {
/* 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 = 0;
read();
event->acceleration.x = x_g * SENSORS_GRAVITY_STANDARD;
event->acceleration.y = y_g * SENSORS_GRAVITY_STANDARD;
event->acceleration.z = z_g * SENSORS_GRAVITY_STANDARD;
return true;
}
/**************************************************************************/
/*!
@brief Gets the sensor_t data
*/
/**************************************************************************/
void Adafruit_MSA301::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, "MSA301", 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->max_value = 0;
sensor->min_value = 0;
sensor->resolution = 0;
}

173
Adafruit_MSA30X.h Normal file
View file

@ -0,0 +1,173 @@
/**************************************************************************/
/*!
@file Adafruit_MSA301.h
@author Limor Fried (Adafruit Industries)
@license MIT (see license.txt)
This is a library for the Adafruit MSA301 Accel breakout board
----> https://www.adafruit.com/
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
*/
/**************************************************************************/
#ifndef ADAFRUIT_MSA301_H
#define ADAFRUIT_MSA301_H
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include <Adafruit_I2CDevice.h>
#include <Adafruit_BusIO_Register.h>
/*=========================================================================
I2C ADDRESS/BITS
-----------------------------------------------------------------------*/
#define MSA301_I2CADDR_DEFAULT (0x26)
/*=========================================================================*/
#define MSA301_REG_PARTID 0x01
#define MSA301_REG_OUT_X_L 0x02
#define MSA301_REG_OUT_X_H 0x03
#define MSA301_REG_OUT_Y_L 0x04
#define MSA301_REG_OUT_Y_H 0x05
#define MSA301_REG_OUT_Z_L 0x06
#define MSA301_REG_OUT_Z_H 0x07
#define MSA301_REG_MOTIONINT 0x09
#define MSA301_REG_DATAINT 0x0A
#define MSA301_REG_CLICKSTATUS 0x0B
#define MSA301_REG_RESRANGE 0x0F
#define MSA301_REG_ODR 0x10
#define MSA301_REG_POWERMODE 0x11
#define MSA301_REG_INTSET0 0x16
#define MSA301_REG_INTSET1 0x17
#define MSA301_REG_TAPDUR 0x2A
#define MSA301_REG_TAPTH 0x2B
typedef enum {
MSA301_RANGE_2_G = 0b00, // +/- 2g (default value)
MSA301_RANGE_4_G = 0b01, // +/- 4g
MSA301_RANGE_8_G = 0b10, // +/- 8g
MSA301_RANGE_16_G = 0b11, // +/- 16g
} msa301_range_t;
typedef enum {
MSA301_AXIS_X = 0x0,
MSA301_AXIS_Y = 0x1,
MSA301_AXIS_Z = 0x2,
} msa301_axis_t;
/* Used with register 0x10 (MSA301_REG_ODR) to set datarate */
typedef enum {
MSA301_DATARATE_1_HZ = 0b0000, // 1 Hz
MSA301_DATARATE_1_95_HZ = 0b0001, // 1.95 Hz
MSA301_DATARATE_3_9_HZ = 0b0010, // 3.9 Hz
MSA301_DATARATE_7_81_HZ = 0b0011, // 7.81 Hz
MSA301_DATARATE_15_63_HZ = 0b0100, // 15.63 Hz
MSA301_DATARATE_31_25_HZ = 0b0101, // 31.25 Hz
MSA301_DATARATE_62_5_HZ = 0b0110, // 62.5 Hz
MSA301_DATARATE_125_HZ = 0b0111, // 125 Hz
MSA301_DATARATE_250_HZ = 0b1000, // 250 Hz
MSA301_DATARATE_500_HZ = 0b1001, // 500 Hz
MSA301_DATARATE_1000_HZ = 0b1010, // 1000 Hz
} msa301_dataRate_t;
typedef enum {
MSA301_BANDWIDTH_1_95_HZ = 0b0000, // 1.95 Hz
MSA301_BANDWIDTH_3_9_HZ = 0b0011, // 3.9 Hz
MSA301_BANDWIDTH_7_81_HZ = 0b0100, // 7.81 Hz
MSA301_BANDWIDTH_15_63_HZ = 0b0101, // 15.63 Hz
MSA301_BANDWIDTH_31_25_HZ = 0b0110, // 31.25 Hz
MSA301_BANDWIDTH_62_5_HZ = 0b0111, // 62.5 Hz
MSA301_BANDWIDTH_125_HZ = 0b1000, // 125 Hz
MSA301_BANDWIDTH_250_HZ = 0b1001, // 250 Hz
MSA301_BANDWIDTH_500_HZ = 0b1010, // 500 Hz
} msa301_bandwidth_t;
typedef enum {
MSA301_NORMALMODE = 0b00,
MSA301_LOWPOWERMODE = 0b01,
MSA301_SUSPENDMODE = 0b010,
} msa301_powermode_t;
typedef enum {
MSA301_RESOLUTION_14 = 0b00,
MSA301_RESOLUTION_12 = 0b01,
MSA301_RESOLUTION_10 = 0b10,
MSA301_RESOLUTION_8 = 0b11,
} msa301_resolution_t;
typedef enum {
MSA301_TAPDUR_50_MS = 0b000,
MSA301_TAPDUR_100_MS = 0b001,
MSA301_TAPDUR_150_MS = 0b010,
MSA301_TAPDUR_200_MS = 0b011,
MSA301_TAPDUR_250_MS = 0b100,
MSA301_TAPDUR_375_MS = 0b101,
MSA301_TAPDUR_500_MS = 0b110,
MSA301_TAPDUR_700_MS = 0b111,
} msa301_tapduration_t;
typedef enum {
MSA301_INT_ORIENT = 0b100000,
MSA301_INT_SINGLETAP,
MSA301_INT_DOUBLETAP,
MSA301_INT_ACTIVETAP,
MSA301_INT_NEWDATA,
} msa301_interrupt_t;
class Adafruit_MSA301 : public Adafruit_Sensor {
public:
Adafruit_MSA301(void);
bool begin(uint8_t i2c_addr=MSA301_I2CADDR_DEFAULT, TwoWire *wire = &Wire);
void setDataRate(msa301_dataRate_t dataRate);
msa301_dataRate_t getDataRate(void);
void enableAxes(bool x, bool y, bool z);
void setPowerMode(msa301_powermode_t mode);
msa301_powermode_t getPowerMode(void);
void setBandwidth(msa301_bandwidth_t bandwidth);
msa301_bandwidth_t getBandwidth(void);
void setRange(msa301_range_t range);
msa301_range_t getRange(void);
void setResolution(msa301_resolution_t resolution);
msa301_resolution_t getResolution(void);
void read();
bool getEvent(sensors_event_t *event);
void getSensor(sensor_t *sensor);
void enableInterrupts(bool singletap=false, bool doubletap=false,
bool activeX=false, bool activeY=false, bool activeZ=false,
bool newData=false, bool freefall=false, bool orient=false);
uint8_t getClick(void);
uint8_t getMotionInterruptStatus(void) ;
uint8_t getDataInterruptStatus(void);
void setClick(bool tap_quiet, bool tap_shock, msa301_tapduration_t tapduration, uint8_t tapthresh);
/*
uint8_t getOrientation(void);
*/
int16_t x, y, z;
float x_g, y_g, z_g;
private:
Adafruit_I2CDevice *i2c_dev;
int32_t _sensorID;
};
#endif

9
library.properties Normal file
View file

@ -0,0 +1,9 @@
name=Adafruit MSA301
version=1.0.0
author=Adafruit <info@adafruit.com>
maintainer=Adafruit <info@adafruit.com>
sentence=Library for the Adafruit MSA301 Accelerometer.
paragraph=Designed specifically to work with the Adafruit MSA301 Breakout, and is based on Adafruit's Unified Sensor Library.
category=Sensors
url=https://github.com/adafruit/Adafruit_MSA301
architectures=*