Skip to content

simplify xyz to just use one struct read and do the scaling in a listcomp #13

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
Jan 2, 2021
Merged
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
27 changes: 7 additions & 20 deletions adafruit_msa301.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,10 @@
__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MSA301.git"

import struct

from micropython import const
from adafruit_register.i2c_struct import ROUnaryStruct
from adafruit_register.i2c_struct import Struct, ROUnaryStruct
from adafruit_register.i2c_bit import RWBit
from adafruit_register.i2c_bits import RWBits, ROBits
from adafruit_register.i2c_bits import RWBits
import adafruit_bus_device.i2c_device as i2cdevice

_MSA301_I2CADDR_DEFAULT = const(0x26)
Expand Down Expand Up @@ -207,7 +205,7 @@ class TapDuration: # pylint: disable=too-few-public-methods,too-many-instance-a
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.
:param ~busio.I2C i2c_bus: The I2C bus the MSA is connected to.
"""

_part_id = ROUnaryStruct(_MSA301_REG_PARTID, "<B")
Expand All @@ -230,7 +228,8 @@ def __init__(self, i2c_bus):
_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 = 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)
Expand All @@ -255,16 +254,6 @@ def __init__(self, i2c_bus):
def acceleration(self):
"""The x, y, z acceleration values returned in a 3-tuple and are in m / s ^ 2."""
# read the 6 bytes of acceleration data
# zh, zl, yh, yl, xh, xl
raw_data = self._xyz_raw
acc_bytes = bytearray()
# shift out bytes, reversing the order
for shift in range(6):
bottom_byte = raw_data >> (8 * shift) & 0xFF
acc_bytes.append(bottom_byte)

# unpack three LE, signed shorts
x, y, z = struct.unpack_from("<hhh", acc_bytes)

current_range = self.range
scale = 1.0
Expand All @@ -278,11 +267,9 @@ def acceleration(self):
scale = 4096.0

# shift down to the actual 14 bits and scale based on the range
x_acc = ((x >> 2) / scale) * _STANDARD_GRAVITY
y_acc = ((y >> 2) / scale) * _STANDARD_GRAVITY
z_acc = ((z >> 2) / scale) * _STANDARD_GRAVITY
x, y, z = [((i >> 2) / scale) * _STANDARD_GRAVITY for i in self._xyz_raw]

return (x_acc, y_acc, z_acc)
return (x, y, z)

def enable_tap_detection(
self,
Expand Down