Skip to content

Commit

Permalink
Merge pull request #2 from kattni/offset
Browse files Browse the repository at this point in the history
Add offsets, example, fix acceleration.
  • Loading branch information
tekktrik committed Feb 16, 2022
2 parents 1cb3e85 + ebbbb74 commit ecc3582
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
36 changes: 32 additions & 4 deletions adafruit_adxl37x.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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("<hhh", self._read_register(_REG_DATAX0, 6))
x = x * _ADXL347_MULTIPLIER * _STANDARD_GRAVITY
y = y * _ADXL347_MULTIPLIER * _STANDARD_GRAVITY
z = z * _ADXL347_MULTIPLIER * _STANDARD_GRAVITY
return x, y, z

@property
def range(self) -> 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.")
31 changes: 31 additions & 0 deletions examples/adxl37x_offset_calibration.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion examples/adxl37x_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit ecc3582

Please sign in to comment.