Skip to content

Add MSA311 support #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Introduction
:target: https://github.com/psf/black
:alt: Code Style: Black

CircuitPython library for the MSA301 Accelerometer
CircuitPython library for the MSA301/MSA311 Accelerometers


Dependencies
Expand Down Expand Up @@ -63,10 +63,10 @@ Usage Example

import time
import board
import adafruit_msa301
from adafruit_msa3xx import MSA301

i2c = board.I2C() # uses board.SCL and board.SDA
msa = adafruit_msa301.MSA301(i2c)
msa = MSA301(i2c)

while True:
print("%f %f %f"%msa.acceleration)
Expand Down
158 changes: 79 additions & 79 deletions adafruit_msa301.py → adafruit_msa3xx.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
# SPDX-License-Identifier: MIT

"""
`MSA301`
`adafruit_msa3xx`
================================================================================

CircuitPython library for the MSA301 Accelerometer
CircuitPython library for the MSA301 and MSA311 Accelerometers


* Author(s): Bryan Siepert
Expand All @@ -18,6 +18,8 @@

* Adafruit `MSA301 Triple Axis Accelerometer
<https://www.adafruit.com/product/4344>`_
* Adafruit `MSA311 Triple Axis Accelerometer
<https://www.adafruit.com/product/5309>`_

**Software and Dependencies:**

Expand All @@ -39,25 +41,26 @@
import adafruit_bus_device.i2c_device as i2cdevice

_MSA301_I2CADDR_DEFAULT = const(0x26)

_MSA301_REG_PARTID = const(0x01)
_MSA301_REG_OUT_X_L = const(0x02)
_MSA301_REG_OUT_X_H = const(0x03)
_MSA301_REG_OUT_Y_L = const(0x04)
_MSA301_REG_OUT_Y_H = const(0x05)
_MSA301_REG_OUT_Z_L = const(0x06)
_MSA301_REG_OUT_Z_H = const(0x07)
_MSA301_REG_MOTIONINT = const(0x09)
_MSA301_REG_DATAINT = const(0x0A)
_MSA301_REG_RESRANGE = const(0x0F)
_MSA301_REG_ODR = const(0x10)
_MSA301_REG_POWERMODE = const(0x11)
_MSA301_REG_INTSET0 = const(0x16)
_MSA301_REG_INTSET1 = const(0x17)
_MSA301_REG_INTMAP0 = const(0x19)
_MSA301_REG_INTMAP1 = const(0x1A)
_MSA301_REG_TAPDUR = const(0x2A)
_MSA301_REG_TAPTH = const(0x2B)
_MSA311_I2CADDR_DEFAULT = const(0x62)

_REG_PARTID = const(0x01)
_REG_OUT_X_L = const(0x02)
_REG_OUT_X_H = const(0x03)
_REG_OUT_Y_L = const(0x04)
_REG_OUT_Y_H = const(0x05)
_REG_OUT_Z_L = const(0x06)
_REG_OUT_Z_H = const(0x07)
_REG_MOTIONINT = const(0x09)
_REG_DATAINT = const(0x0A)
_REG_RESRANGE = const(0x0F)
_REG_ODR = const(0x10)
_REG_POWERMODE = const(0x11)
_REG_INTSET0 = const(0x16)
_REG_INTSET1 = const(0x17)
_REG_INTMAP0 = const(0x19)
_REG_INTMAP1 = const(0x1A)
_REG_TAPDUR = const(0x2A)
_REG_TAPTH = const(0x2B)


_STANDARD_GRAVITY = 9.806
Expand Down Expand Up @@ -200,45 +203,38 @@ class TapDuration: # pylint: disable=too-few-public-methods,too-many-instance-a
DURATION_700_MS = 0b111 # < 50 millis700 millis


class MSA301: # pylint: disable=too-many-instance-attributes
"""Driver for the MSA301 Accelerometer.

:param ~busio.I2C i2c_bus: The I2C bus the MSA is connected to.


**Quickstart: Importing and using the device**

Here is an example of using the :class:`MSA301` class.
First you will need to import the libraries to use the sensor

.. code-block:: python

import board
import adafruit_msa301

Once this is done you can define your `board.I2C` object and define your sensor object

.. code-block:: python

i2c = board.I2C() # uses board.SCL and board.SDA
msa = adafruit_msa301.MSA301(i2c)

Now you have access to the :attr:`acceleration` attribute
class MSA3XX: # pylint: disable=too-many-instance-attributes
"""Base driver class for the MSA301/311 Accelerometers."""

.. code-block:: python
_part_id = ROUnaryStruct(_REG_PARTID, "<B")

acc_x, acc_y, acc_z = msa.acceleration
_disable_x = RWBit(_REG_ODR, 7)
_disable_y = RWBit(_REG_ODR, 6)
_disable_z = RWBit(_REG_ODR, 5)

"""
# _xyz_raw = ROBits(48, _REG_OUT_X_L, 0, 6)
_xyz_raw = Struct(_REG_OUT_X_L, "<hhh")

_part_id = ROUnaryStruct(_MSA301_REG_PARTID, "<B")
# tap INT enable and status
_single_tap_int_en = RWBit(_REG_INTSET0, 5)
_double_tap_int_en = RWBit(_REG_INTSET0, 4)
_motion_int_status = ROUnaryStruct(_REG_MOTIONINT, "B")

def __init__(self, i2c_bus):
self.i2c_device = i2cdevice.I2CDevice(i2c_bus, _MSA301_I2CADDR_DEFAULT)
# tap interrupt knobs
_tap_quiet = RWBit(_REG_TAPDUR, 7)
_tap_shock = RWBit(_REG_TAPDUR, 6)
_tap_duration = RWBits(3, _REG_TAPDUR, 0)
_tap_threshold = RWBits(5, _REG_TAPTH, 0)
reg_tapdur = ROUnaryStruct(_REG_TAPDUR, "B")

if self._part_id != 0x13:
raise AttributeError("Cannot find a MSA301")
# general settings knobs
power_mode = RWBits(2, _REG_POWERMODE, 6)
bandwidth = RWBits(4, _REG_POWERMODE, 1)
data_rate = RWBits(4, _REG_ODR, 0)
range = RWBits(2, _REG_RESRANGE, 0)
resolution = RWBits(2, _REG_RESRANGE, 2)

def __init__(self):
self._disable_x = self._disable_y = self._disable_z = False
self.power_mode = Mode.NORMAL
self.data_rate = DataRate.RATE_500_HZ
Expand All @@ -247,32 +243,6 @@ def __init__(self, i2c_bus):
self.resolution = Resolution.RESOLUTION_14_BIT
self._tap_count = 0

_disable_x = RWBit(_MSA301_REG_ODR, 7)
_disable_y = RWBit(_MSA301_REG_ODR, 6)
_disable_z = RWBit(_MSA301_REG_ODR, 5)

# _xyz_raw = ROBits(48, _MSA301_REG_OUT_X_L, 0, 6)
_xyz_raw = Struct(_MSA301_REG_OUT_X_L, "<hhh")

# tap INT enable and status
_single_tap_int_en = RWBit(_MSA301_REG_INTSET0, 5)
_double_tap_int_en = RWBit(_MSA301_REG_INTSET0, 4)
_motion_int_status = ROUnaryStruct(_MSA301_REG_MOTIONINT, "B")

# tap interrupt knobs
_tap_quiet = RWBit(_MSA301_REG_TAPDUR, 7)
_tap_shock = RWBit(_MSA301_REG_TAPDUR, 6)
_tap_duration = RWBits(3, _MSA301_REG_TAPDUR, 0)
_tap_threshold = RWBits(5, _MSA301_REG_TAPTH, 0)
reg_tapdur = ROUnaryStruct(_MSA301_REG_TAPDUR, "B")

# general settings knobs
power_mode = RWBits(2, _MSA301_REG_POWERMODE, 6)
bandwidth = RWBits(4, _MSA301_REG_POWERMODE, 1)
data_rate = RWBits(4, _MSA301_REG_ODR, 0)
range = RWBits(2, _MSA301_REG_RESRANGE, 0)
resolution = RWBits(2, _MSA301_REG_RESRANGE, 2)

@property
def acceleration(self):
"""The x, y, z acceleration values returned in a
Expand Down Expand Up @@ -368,3 +338,33 @@ def tapped(self):
return True

return False


class MSA301(MSA3XX):
"""Driver for the MSA301 Accelerometer.

:param ~busio.I2C i2c_bus: The I2C bus the MSA is connected to.
"""

def __init__(self, i2c_bus):
self.i2c_device = i2cdevice.I2CDevice(i2c_bus, _MSA301_I2CADDR_DEFAULT)

if self._part_id != 0x13:
raise AttributeError("Cannot find a MSA301")

super().__init__()


class MSA311(MSA3XX):
"""Driver for the MSA311 Accelerometer.

:param ~busio.I2C i2c_bus: The I2C bus the MSA is connected to.
"""

def __init__(self, i2c_bus):
self.i2c_device = i2cdevice.I2CDevice(i2c_bus, _MSA311_I2CADDR_DEFAULT)

if self._part_id != 0x13:
raise AttributeError("Cannot find a MSA311")

super().__init__()
2 changes: 1 addition & 1 deletion docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
.. If your library file(s) are nested in a directory (e.g. /adafruit_foo/foo.py)
.. use this format as the module name: "adafruit_foo.foo"

.. automodule:: adafruit_msa301
.. automodule:: adafruit_msa3xx
:members:
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Table of Contents
:caption: Related Products

Adafruit MSA301 Triple Axis Accelerometer <https://www.adafruit.com/product/4344>
Adafruit MSA311 Triple Axis Accelerometer <https://www.adafruit.com/product/5309>

.. toctree::
:caption: Other Links
Expand Down
4 changes: 2 additions & 2 deletions examples/msa301_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

import time
import board
import adafruit_msa301
from adafruit_msa3xx import MSA301

i2c = board.I2C() # uses board.SCL and board.SDA
msa = adafruit_msa301.MSA301(i2c)
msa = MSA301(i2c)

while True:
print("%f %f %f" % msa.acceleration)
Expand Down
4 changes: 2 additions & 2 deletions examples/msa301_tap_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

import time
import board
import adafruit_msa301
from adafruit_msa3xx import MSA301

i2c = board.I2C() # uses board.SCL and board.SDA
msa = adafruit_msa301.MSA301(i2c)
msa = MSA301(i2c)

msa.enable_tap_detection()

Expand Down
13 changes: 13 additions & 0 deletions examples/msa311_simpletest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import time
import board
from adafruit_msa3xx import MSA311

i2c = board.I2C() # uses board.SCL and board.SDA
msa = MSA311(i2c)

while True:
print("%f %f %f" % msa.acceleration)
time.sleep(0.5)
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ requires = [

[project]
name = "adafruit-circuitpython-msa301"
description = "CircuitPython library for the MSA301 Accelerometer"
description = "CircuitPython library for the MSA301/MSA311 Accelerometers"
version = "0.0.0+auto.0"
readme = "README.rst"
authors = [
Expand All @@ -24,6 +24,7 @@ keywords = [
"circuitpython",
"micropython",
"msa301",
"msa311",
"accelerometer",
"msa",
]
Expand All @@ -39,7 +40,7 @@ classifiers = [
dynamic = ["dependencies", "optional-dependencies"]

[tool.setuptools]
py-modules = ["adafruit_msa301"]
py-modules = ["adafruit_msa3xx"]

[tool.setuptools.dynamic]
dependencies = {file = ["requirements.txt"]}
Expand Down