initial commit
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
/*
|
||||
* 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];
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* MSA301ForStuduino
|
||||
*
|
||||
* Adafruit_MSA301 v1.1.0 (https://github.com/adafruit/Adafruit_MSA301) を
|
||||
* 株式会社アーテックが Studuino 向けに改変したものです。
|
||||
* 改変内容は同梱の README.md を参照してください。
|
||||
* オリジナルの著作権表示およびライセンスは license.txt のとおりです。
|
||||
*/
|
||||
/*!
|
||||
* @file MSA301ForStuduino.h
|
||||
*/
|
||||
|
||||
#ifndef MSA301FORSTUDUINO_H
|
||||
#define MSA301FORSTUDUINO_H
|
||||
|
||||
#include <BusIOForStuduino_Register.h>
|
||||
#include <BusIOForStuduino_I2CDevice.h>
|
||||
#include <Wire.h>
|
||||
|
||||
/*=========================================================================
|
||||
I2C ADDRESS/BITS
|
||||
-----------------------------------------------------------------------*/
|
||||
#define MSA301_I2CADDR_DEFAULT (0x26) ///< Fixed I2C address
|
||||
#define MSA311_I2CADDR_DEFAULT (0x62) ///< Fixed I2C address
|
||||
/*=========================================================================*/
|
||||
|
||||
#define MSA301_REG_PARTID 0x01 ///< Register that contains the part ID
|
||||
#define MSA301_REG_OUT_X_L 0x02 ///< Register address for X axis lower byte
|
||||
#define MSA301_REG_RESRANGE 0x0F ///< Register address for resolution range
|
||||
#define MSA301_REG_ODR 0x10 ///< Register address for data rate setting
|
||||
#define MSA301_REG_POWERMODE 0x11 ///< Register address for power mode setting
|
||||
|
||||
/** The accelerometer ranges */
|
||||
#define MSA301_RANGE_2_G 0b00 ///< +/- 2g (default value)
|
||||
/** The accelerometer data rate */
|
||||
#define MSA301_DATARATE_125_HZ 0b0111 ///< 125 Hz
|
||||
/** The accelerometer bandwidth */
|
||||
#define MSA301_BANDWIDTH_250_HZ 0b1001 ///< 250 Hz
|
||||
/** The accelerometer power mode */
|
||||
#define MSA301_NORMALMODE 0b00 ///< Normal (high speed) mode
|
||||
/** The accelerometer read resolution */
|
||||
#define MSA301_RESOLUTION_8 0b11 ///< 8-bit resolution
|
||||
|
||||
/** Class for hardware interfacing with an MSA301 accelerometer */
|
||||
class Adafruit_MSA301 { // : public Adafruit_Sensor {
|
||||
public:
|
||||
Adafruit_MSA301(void);
|
||||
bool begin(uint8_t i2c_addr = MSA301_I2CADDR_DEFAULT, TwoWire *wire = &Wire);
|
||||
|
||||
void read();
|
||||
int8_t x, ///< The last read X acceleration in raw units
|
||||
y, ///< The last read Y acceleration in raw units
|
||||
z; ///< The last read Z acceleration in raw units
|
||||
|
||||
private:
|
||||
Adafruit_I2CDevice *i2c_dev;
|
||||
};
|
||||
|
||||
/** Class for hardware interfacing with an MSA311 accelerometer */
|
||||
class Adafruit_MSA311 : public Adafruit_MSA301 {
|
||||
public:
|
||||
Adafruit_MSA311(void);
|
||||
bool begin(uint8_t i2c_addr = MSA311_I2CADDR_DEFAULT, TwoWire *wire = &Wire);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,103 @@
|
||||
# MSA301ForStuduino
|
||||
|
||||
Adafruit MSA301 ライブラリ v1.1.0 を、株式会社アーテックが Studuino 向けに改変したものです。
|
||||
MSA301 / MSA311 加速度センサーを I2C 経由で読み出します。
|
||||
|
||||
オリジナル: https://github.com/adafruit/Adafruit_MSA301
|
||||
オリジナルのライセンス(BSD)は `license.txt` を参照してください。
|
||||
|
||||
本ライブラリは Adafruit が配布する公式版とは互換性がありません。
|
||||
公式版の `Adafruit_MSA301` と同時にインストールしても衝突しないよう、
|
||||
ライブラリ名・フォルダ名・ファイル名・インクルードガードを変更しています。
|
||||
|
||||
## 使い方
|
||||
|
||||
```cpp
|
||||
#include <MSA301ForStuduino.h>
|
||||
|
||||
Adafruit_MSA311 msa; // MSA311 (I2C 0x62)
|
||||
// Adafruit_MSA301 msa; // MSA301 (I2C 0x26)
|
||||
|
||||
void setup() {
|
||||
Wire.begin();
|
||||
if (!msa.begin()) {
|
||||
// センサーが見つからない
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
msa.read();
|
||||
// msa.x, msa.y, msa.z
|
||||
}
|
||||
```
|
||||
|
||||
依存ライブラリは `BusIOForStuduino` のみです。
|
||||
Adafruit Unified Sensor および Adafruit SSD1306 は必要ありません。
|
||||
|
||||
## オリジナルからの変更点
|
||||
|
||||
### 1. ライブラリ名・ファイル名の変更
|
||||
|
||||
公式版との衝突を避けるため、次のように改名しました。クラス名は変更していません。
|
||||
|
||||
| オリジナル | 本ライブラリ |
|
||||
|:--|:--|
|
||||
| `Adafruit_MSA301`(フォルダ) | `MSA301ForStuduino` |
|
||||
| `Adafruit_MSA301.h` | `MSA301ForStuduino.h` |
|
||||
| `Adafruit_MSA301.cpp` | `MSA301ForStuduino.cpp` |
|
||||
| `ADAFRUIT_MSA301_H`(インクルードガード) | `MSA301FORSTUDUINO_H` |
|
||||
| `name=Adafruit MSA301` | `name=MSA301ForStuduino` |
|
||||
|
||||
### 2. Adafruit Unified Sensor への依存を削除
|
||||
|
||||
`Adafruit_MSA301` クラスの `Adafruit_Sensor` 継承を外し、`getEvent()` と
|
||||
`getSensor()`、`sensors_event_t` を用いる API を削除しました。
|
||||
ATmega168(SRAM 1KB)ではメモリに余裕がないためです。
|
||||
|
||||
### 3. 未使用 API の削除
|
||||
|
||||
Studuino が使用しない設定用 API(`setDataRate()` / `getDataRate()` /
|
||||
`setPowerMode()` / `getPowerMode()` / `setBandwidth()` / `getBandwidth()` /
|
||||
`setRange()` / `getRange()` / `setResolution()` / `getResolution()` /
|
||||
`enableAxes()` / `setClick()` / `enableInterrupts()` /
|
||||
`getMotionInterruptStatus()` など)と、対応する enum 定義を削除しました。
|
||||
これらの設定は `begin()` の中で固定値(125Hz / 250Hz / ±2G / 8bit /
|
||||
ノーマルモード)を直接書き込む形に置き換えています。
|
||||
|
||||
### 4. Adafruit_MSA311 クラスの追加
|
||||
|
||||
MSA311(I2C アドレス `0x62`)に対応する `Adafruit_MSA311` クラスを追加しました。
|
||||
`Adafruit_MSA301` を継承し、既定 I2C アドレスのみが異なります。
|
||||
|
||||
### 5. begin() を複数回呼び出せるよう修正
|
||||
|
||||
オリジナルの `begin()` は呼ばれるたびに `Adafruit_I2CDevice` を `new` していました。
|
||||
Studuino のセンサー値確認モードはセンサーの抜き差しからの復帰のため
|
||||
一定周期で `begin()` を呼び直す設計になっており、この実装のままでは
|
||||
呼び出しのたびにメモリリークが発生し、SRAM 1KB の ATmega168 では
|
||||
数十秒で枯渇して動作が破綻します。
|
||||
|
||||
そのため、確保済みの場合は再確保しないよう変更しました。
|
||||
あわせて、コンストラクタで `i2c_dev` を `nullptr` に初期化しています。
|
||||
|
||||
```cpp
|
||||
Adafruit_MSA301::Adafruit_MSA301() : i2c_dev(nullptr) {}
|
||||
|
||||
bool Adafruit_MSA301::begin(uint8_t i2c_address, TwoWire *wire) {
|
||||
if (i2c_dev == nullptr) {
|
||||
i2c_dev = new Adafruit_I2CDevice(i2c_address, wire);
|
||||
}
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
なお、この変更により `i2c_dev` は初回の `begin()` で確保された I2C アドレスを
|
||||
保持し続けます。2回目以降の `begin()` に渡した `i2c_address` は無視されます。
|
||||
|
||||
### 6. 配布物からの除外
|
||||
|
||||
ビルドに不要なファイルを削除しました(`examples/`、`assets/`、
|
||||
`MSA301-V1.0-ENG.PDF`、`.github/`、`.travis.yml`、`code-of-conduct.md`)。
|
||||
|
||||
`examples/` は上記 2. と 3. の API 削除により、そのままではコンパイルが
|
||||
通らなくなっていたため削除しています。
|
||||
@@ -0,0 +1,9 @@
|
||||
name=MSA301ForStuduino
|
||||
version=1.0.0
|
||||
author=Limor Fried for Adafruit Industries
|
||||
maintainer=Artec Co., Ltd. <planning@artec-kk.co.jp>
|
||||
sentence=MSA301/MSA311 accelerometer library for Studuino.
|
||||
paragraph=Fork of the Adafruit MSA301 library v1.1.0, modified by Artec Co., Ltd. for Studuino. Requires BusIOForStuduino. Adafruit Unified Sensor and Adafruit SSD1306 are NOT required. See README.md for the list of modifications.
|
||||
category=Sensors
|
||||
url=https://github.com/adafruit/Adafruit_MSA301
|
||||
architectures=*
|
||||
@@ -0,0 +1,26 @@
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2019, Limor Fried for 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