diff --git a/adafruit_adxl37x.py b/adafruit_adxl37x.py index a063b30..50904f2 100644 --- a/adafruit_adxl37x.py +++ b/adafruit_adxl37x.py @@ -28,14 +28,33 @@ """ +from struct import unpack from micropython import const import adafruit_adxl34x +try: + from typing import Tuple, Optional + + # This is only needed for typing + import busio +except ImportError: + pass + __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ADXL37x.git" _ADXL375_DEFAULT_ADDRESS = const(0x53) +_ADXL347_MULTIPLIER: float = 0.049 # 49mg per lsb +_STANDARD_GRAVITY: float = 9.80665 # earth standard gravity + +_REG_DATAX0: int = const(0x32) # X-axis data 0 +_REG_DATAX1: int = const(0x33) # X-axis data 1 +_REG_DATAY0: int = const(0x34) # Y-axis data 0 +_REG_DATAY1: int = const(0x35) # Y-axis data 1 +_REG_DATAZ0: int = const(0x36) # Z-axis data 0 +_REG_DATAZ1: int = const(0x37) # Z-axis data 1 + class DataRate(adafruit_adxl34x.DataRate): # pylint: disable=too-few-public-methods """Stub class for data rate.""" @@ -79,17 +98,26 @@ class ADXL375(adafruit_adxl34x.ADXL345): """ - def __init__(self, i2c, address=None): + def __init__(self, i2c: busio.I2C, address: Optional[int] = None): super().__init__( i2c, address if address is not None else _ADXL375_DEFAULT_ADDRESS ) @property - def range(self): + def acceleration(self) -> Tuple[int, int, int]: + """The x, y, z acceleration values returned in a 3-tuple in :math:`m / s ^ 2`""" + x, y, z = unpack(" int: """Range is fixed. Updating the range is not implemented.""" - return + raise NotImplementedError("Range not implemented. ADXL375 is fixed at 200G.") @range.setter - def range(self, val): + def range(self, val: int) -> None: """Range is fixed. Updating the range is not implemented.""" raise NotImplementedError("Range not implemented. ADXL375 is fixed at 200G.") diff --git a/examples/adxl37x_offset_calibration.py b/examples/adxl37x_offset_calibration.py new file mode 100644 index 0000000..30e95ab --- /dev/null +++ b/examples/adxl37x_offset_calibration.py @@ -0,0 +1,31 @@ +# SPDX-FileCopyrightText: 2022 Kattni Rembor for Adafruit Industries +# SPDX-License-Identifier: MIT + +import time +import board +import adafruit_adxl37x + +i2c = board.STEMMA_I2C() # uses board.SCL and board.SDA +accelerometer = adafruit_adxl37x.ADXL375(i2c) + +accelerometer.offset = 0, 0, 0 + +print("Hold accelerometer flat to set offsets to 0, 0, and -1g...") +time.sleep(1) +x = accelerometer.raw_x +y = accelerometer.raw_y +z = accelerometer.raw_z +print("Raw x: ", x) +print("Raw y: ", y) +print("Raw z: ", z) + +accelerometer.offset = ( + round(-x / 4), + round(-y / 4), + round(-(z - 20) / 4), # Z should be '20' at 1g (49mg per bit) +) +print("Calibrated offsets: ", accelerometer.offset) + +while True: + print("%f %f %f m/s^2" % accelerometer.acceleration) + time.sleep(0.2) diff --git a/examples/adxl37x_simpletest.py b/examples/adxl37x_simpletest.py index 2cbcade..d1289a1 100644 --- a/examples/adxl37x_simpletest.py +++ b/examples/adxl37x_simpletest.py @@ -10,5 +10,5 @@ accelerometer = adafruit_adxl37x.ADXL375(i2c) while True: - print("%f %f %f" % accelerometer.acceleration) + print("%f %f %f m/s^2" % accelerometer.acceleration) time.sleep(0.2)