Skip to content

Commit ecc3582

Browse files
authored
Merge pull request #2 from kattni/offset
Add offsets, example, fix acceleration.
2 parents 1cb3e85 + ebbbb74 commit ecc3582

File tree

3 files changed

+64
-5
lines changed

3 files changed

+64
-5
lines changed

adafruit_adxl37x.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,33 @@
2828
2929
"""
3030

31+
from struct import unpack
3132
from micropython import const
3233
import adafruit_adxl34x
3334

35+
try:
36+
from typing import Tuple, Optional
37+
38+
# This is only needed for typing
39+
import busio
40+
except ImportError:
41+
pass
42+
3443
__version__ = "0.0.0-auto.0"
3544
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ADXL37x.git"
3645

3746
_ADXL375_DEFAULT_ADDRESS = const(0x53)
3847

48+
_ADXL347_MULTIPLIER: float = 0.049 # 49mg per lsb
49+
_STANDARD_GRAVITY: float = 9.80665 # earth standard gravity
50+
51+
_REG_DATAX0: int = const(0x32) # X-axis data 0
52+
_REG_DATAX1: int = const(0x33) # X-axis data 1
53+
_REG_DATAY0: int = const(0x34) # Y-axis data 0
54+
_REG_DATAY1: int = const(0x35) # Y-axis data 1
55+
_REG_DATAZ0: int = const(0x36) # Z-axis data 0
56+
_REG_DATAZ1: int = const(0x37) # Z-axis data 1
57+
3958

4059
class DataRate(adafruit_adxl34x.DataRate): # pylint: disable=too-few-public-methods
4160
"""Stub class for data rate."""
@@ -79,17 +98,26 @@ class ADXL375(adafruit_adxl34x.ADXL345):
7998
8099
"""
81100

82-
def __init__(self, i2c, address=None):
101+
def __init__(self, i2c: busio.I2C, address: Optional[int] = None):
83102
super().__init__(
84103
i2c, address if address is not None else _ADXL375_DEFAULT_ADDRESS
85104
)
86105

87106
@property
88-
def range(self):
107+
def acceleration(self) -> Tuple[int, int, int]:
108+
"""The x, y, z acceleration values returned in a 3-tuple in :math:`m / s ^ 2`"""
109+
x, y, z = unpack("<hhh", self._read_register(_REG_DATAX0, 6))
110+
x = x * _ADXL347_MULTIPLIER * _STANDARD_GRAVITY
111+
y = y * _ADXL347_MULTIPLIER * _STANDARD_GRAVITY
112+
z = z * _ADXL347_MULTIPLIER * _STANDARD_GRAVITY
113+
return x, y, z
114+
115+
@property
116+
def range(self) -> int:
89117
"""Range is fixed. Updating the range is not implemented."""
90-
return
118+
raise NotImplementedError("Range not implemented. ADXL375 is fixed at 200G.")
91119

92120
@range.setter
93-
def range(self, val):
121+
def range(self, val: int) -> None:
94122
"""Range is fixed. Updating the range is not implemented."""
95123
raise NotImplementedError("Range not implemented. ADXL375 is fixed at 200G.")
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# SPDX-FileCopyrightText: 2022 Kattni Rembor for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
import time
5+
import board
6+
import adafruit_adxl37x
7+
8+
i2c = board.STEMMA_I2C() # uses board.SCL and board.SDA
9+
accelerometer = adafruit_adxl37x.ADXL375(i2c)
10+
11+
accelerometer.offset = 0, 0, 0
12+
13+
print("Hold accelerometer flat to set offsets to 0, 0, and -1g...")
14+
time.sleep(1)
15+
x = accelerometer.raw_x
16+
y = accelerometer.raw_y
17+
z = accelerometer.raw_z
18+
print("Raw x: ", x)
19+
print("Raw y: ", y)
20+
print("Raw z: ", z)
21+
22+
accelerometer.offset = (
23+
round(-x / 4),
24+
round(-y / 4),
25+
round(-(z - 20) / 4), # Z should be '20' at 1g (49mg per bit)
26+
)
27+
print("Calibrated offsets: ", accelerometer.offset)
28+
29+
while True:
30+
print("%f %f %f m/s^2" % accelerometer.acceleration)
31+
time.sleep(0.2)

examples/adxl37x_simpletest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010
accelerometer = adafruit_adxl37x.ADXL375(i2c)
1111

1212
while True:
13-
print("%f %f %f" % accelerometer.acceleration)
13+
print("%f %f %f m/s^2" % accelerometer.acceleration)
1414
time.sleep(0.2)

0 commit comments

Comments
 (0)