140 lines
4.6 KiB
C++
140 lines
4.6 KiB
C++
/*
|
|
* MSA301ForStuduino
|
|
*
|
|
* Adafruit_MSA301 v1.1.0 を株式会社アーテックが Studuino 向けに改変したものです。
|
|
* 改変内容は同梱の README.md を参照してください。
|
|
*/
|
|
/*!
|
|
* @file MSA301ForStuduino.cpp
|
|
*
|
|
* @mainpage Adafruit MSA301 Accelerometer Breakout
|
|
*
|
|
* @section intro_sec Introduction
|
|
*
|
|
* 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!
|
|
*
|
|
* @section author Author
|
|
*
|
|
* Limor Fried (Adafruit Industries)
|
|
*
|
|
* @section license License
|
|
*
|
|
* BSD license, all text here must be included in any redistribution.
|
|
*
|
|
*/
|
|
|
|
#include <MSA301ForStuduino.h>
|
|
|
|
/**************************************************************************/
|
|
/*!
|
|
@brief Instantiates a new MSA301 class
|
|
*/
|
|
/**************************************************************************/
|
|
Adafruit_MSA301::Adafruit_MSA301() : i2c_dev(nullptr) {}
|
|
|
|
/**************************************************************************/
|
|
/*!
|
|
@brief Instantiates a new MSA311 class
|
|
*/
|
|
/**************************************************************************/
|
|
Adafruit_MSA311::Adafruit_MSA311() {}
|
|
|
|
/*!
|
|
* @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_MSA311::begin(uint8_t i2c_address, TwoWire *wire) {
|
|
return Adafruit_MSA301::begin(i2c_address, wire);
|
|
}
|
|
|
|
/*!
|
|
* @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) {
|
|
if (i2c_dev == nullptr) {
|
|
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;
|
|
}
|
|
|
|
// enableAxes(true, true, true);
|
|
// setDataRate(MSA301_DATARATE_125_HZ);
|
|
Adafruit_BusIO_Register ODR = Adafruit_BusIO_Register(i2c_dev, MSA301_REG_ODR, 1);
|
|
// enable all axes
|
|
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(false); // these are *disable* bits, yeah...
|
|
y.write(false);
|
|
z.write(false);
|
|
// 125Hz rate
|
|
Adafruit_BusIO_RegisterBits dataratebits = Adafruit_BusIO_RegisterBits(&ODR, 4, 0);
|
|
dataratebits.write(MSA301_DATARATE_125_HZ);
|
|
|
|
// setPowerMode(MSA301_NORMALMODE);
|
|
// setBandwidth(MSA301_BANDWIDTH_250_HZ);
|
|
Adafruit_BusIO_Register PowerMode = Adafruit_BusIO_Register(i2c_dev, MSA301_REG_POWERMODE, 1);
|
|
// normal mode
|
|
Adafruit_BusIO_RegisterBits powermodebits = Adafruit_BusIO_RegisterBits(&PowerMode, 2, 6);
|
|
powermodebits.write(MSA301_NORMALMODE);
|
|
// 250Hz bw
|
|
Adafruit_BusIO_RegisterBits bandwidthbits = Adafruit_BusIO_RegisterBits(&PowerMode, 4, 1);
|
|
bandwidthbits.write(MSA301_BANDWIDTH_250_HZ);
|
|
|
|
// setRange(MSA301_RANGE_2_G);
|
|
// setResolution(MSA301_RESOLUTION_8);
|
|
Adafruit_BusIO_Register ResRange = Adafruit_BusIO_Register(i2c_dev, MSA301_REG_RESRANGE, 1);
|
|
// 2G
|
|
Adafruit_BusIO_RegisterBits rangebits = Adafruit_BusIO_RegisterBits(&ResRange, 2, 0);
|
|
rangebits.write(MSA301_RANGE_2_G);
|
|
// 8bit
|
|
Adafruit_BusIO_RegisterBits resbits = Adafruit_BusIO_RegisterBits(&ResRange, 2, 2);
|
|
resbits.write(MSA301_RESOLUTION_8);
|
|
|
|
return true;
|
|
}
|
|
|
|
/**************************************************************************/
|
|
/*!
|
|
@brief Read the XYZ data from the accelerometer and store in the internal
|
|
x, y and z (and x_g, y_g, z_g) member variables.
|
|
*/
|
|
/**************************************************************************/
|
|
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);
|
|
|
|
y = 255 - buffer[1];
|
|
x = 255 - buffer[3];
|
|
z = buffer[5];
|
|
}
|