Skip to content

Commit 02a08dd

Browse files
authored
Merge pull request #33 from tammymakesthings/issue30-add-type-annotations
Issue30 add type annotations
2 parents 8c72e4a + 19d4e16 commit 02a08dd

File tree

1 file changed

+91
-77
lines changed

1 file changed

+91
-77
lines changed

adafruit_adxl34x.py

Lines changed: 91 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -33,49 +33,57 @@
3333
from micropython import const
3434
from adafruit_bus_device import i2c_device
3535

36+
try:
37+
from typing import Tuple, Dict
38+
39+
# This is only needed for typing
40+
import busio # pylint: disable=unused-import
41+
except ImportError:
42+
pass
43+
3644
__version__ = "0.0.0-auto.0"
3745
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ADXL34x.git"
38-
_ADXL345_DEFAULT_ADDRESS = const(0x53) # Assumes ALT address pin low
46+
_ADXL345_DEFAULT_ADDRESS: int = const(0x53) # Assumes ALT address pin low
3947

4048
# Conversion factors
41-
_ADXL345_MG2G_MULTIPLIER = 0.004 # 4mg per lsb
42-
_STANDARD_GRAVITY = 9.80665 # earth standard gravity
43-
44-
_REG_DEVID = const(0x00) # Device ID
45-
_REG_THRESH_TAP = const(0x1D) # Tap threshold
46-
_REG_OFSX = const(0x1E) # X-axis offset
47-
_REG_OFSY = const(0x1F) # Y-axis offset
48-
_REG_OFSZ = const(0x20) # Z-axis offset
49-
_REG_DUR = const(0x21) # Tap duration
50-
_REG_LATENT = const(0x22) # Tap latency
51-
_REG_WINDOW = const(0x23) # Tap window
52-
_REG_THRESH_ACT = const(0x24) # Activity threshold
53-
_REG_THRESH_INACT = const(0x25) # Inactivity threshold
54-
_REG_TIME_INACT = const(0x26) # Inactivity time
55-
_REG_ACT_INACT_CTL = const(0x27) # Axis enable control for [in]activity detection
56-
_REG_THRESH_FF = const(0x28) # Free-fall threshold
57-
_REG_TIME_FF = const(0x29) # Free-fall time
58-
_REG_TAP_AXES = const(0x2A) # Axis control for single/double tap
59-
_REG_ACT_TAP_STATUS = const(0x2B) # Source for single/double tap
60-
_REG_BW_RATE = const(0x2C) # Data rate and power mode control
61-
_REG_POWER_CTL = const(0x2D) # Power-saving features control
62-
_REG_INT_ENABLE = const(0x2E) # Interrupt enable control
63-
_REG_INT_MAP = const(0x2F) # Interrupt mapping control
64-
_REG_INT_SOURCE = const(0x30) # Source of interrupts
65-
_REG_DATA_FORMAT = const(0x31) # Data format control
66-
_REG_DATAX0 = const(0x32) # X-axis data 0
67-
_REG_DATAX1 = const(0x33) # X-axis data 1
68-
_REG_DATAY0 = const(0x34) # Y-axis data 0
69-
_REG_DATAY1 = const(0x35) # Y-axis data 1
70-
_REG_DATAZ0 = const(0x36) # Z-axis data 0
71-
_REG_DATAZ1 = const(0x37) # Z-axis data 1
72-
_REG_FIFO_CTL = const(0x38) # FIFO control
73-
_REG_FIFO_STATUS = const(0x39) # FIFO status
74-
_INT_SINGLE_TAP = const(0b01000000) # SINGLE_TAP bit
75-
_INT_DOUBLE_TAP = const(0b00100000) # DOUBLE_TAP bit
76-
_INT_ACT = const(0b00010000) # ACT bit
77-
_INT_INACT = const(0b00001000) # INACT bit
78-
_INT_FREE_FALL = const(0b00000100) # FREE_FALL bit
49+
_ADXL345_MG2G_MULTIPLIER: float = 0.004 # 4mg per lsb
50+
_STANDARD_GRAVITY: float = 9.80665 # earth standard gravity
51+
52+
_REG_DEVID: int = const(0x00) # Device ID
53+
_REG_THRESH_TAP: int = const(0x1D) # Tap threshold
54+
_REG_OFSX: int = const(0x1E) # X-axis offset
55+
_REG_OFSY: int = const(0x1F) # Y-axis offset
56+
_REG_OFSZ: int = const(0x20) # Z-axis offset
57+
_REG_DUR: int = const(0x21) # Tap duration
58+
_REG_LATENT: int = const(0x22) # Tap latency
59+
_REG_WINDOW: int = const(0x23) # Tap window
60+
_REG_THRESH_ACT: int = const(0x24) # Activity threshold
61+
_REG_THRESH_INACT: int = const(0x25) # Inactivity threshold
62+
_REG_TIME_INACT: int = const(0x26) # Inactivity time
63+
_REG_ACT_INACT_CTL: int = const(0x27) # Axis enable control for [in]activity detection
64+
_REG_THRESH_FF: int = const(0x28) # Free-fall threshold
65+
_REG_TIME_FF: int = const(0x29) # Free-fall time
66+
_REG_TAP_AXES: int = const(0x2A) # Axis control for single/double tap
67+
_REG_ACT_TAP_STATUS: int = const(0x2B) # Source for single/double tap
68+
_REG_BW_RATE: int = const(0x2C) # Data rate and power mode control
69+
_REG_POWER_CTL: int = const(0x2D) # Power-saving features control
70+
_REG_INT_ENABLE: int = const(0x2E) # Interrupt enable control
71+
_REG_INT_MAP: int = const(0x2F) # Interrupt mapping control
72+
_REG_INT_SOURCE: int = const(0x30) # Source of interrupts
73+
_REG_DATA_FORMAT: int = const(0x31) # Data format control
74+
_REG_DATAX0: int = const(0x32) # X-axis data 0
75+
_REG_DATAX1: int = const(0x33) # X-axis data 1
76+
_REG_DATAY0: int = const(0x34) # Y-axis data 0
77+
_REG_DATAY1: int = const(0x35) # Y-axis data 1
78+
_REG_DATAZ0: int = const(0x36) # Z-axis data 0
79+
_REG_DATAZ1: int = const(0x37) # Z-axis data 1
80+
_REG_FIFO_CTL: int = const(0x38) # FIFO control
81+
_REG_FIFO_STATUS: int = const(0x39) # FIFO status
82+
_INT_SINGLE_TAP: int = const(0b01000000) # SINGLE_TAP bit
83+
_INT_DOUBLE_TAP: int = const(0b00100000) # DOUBLE_TAP bit
84+
_INT_ACT: int = const(0b00010000) # ACT bit
85+
_INT_INACT: int = const(0b00001000) # INACT bit
86+
_INT_FREE_FALL: int = const(0b00000100) # FREE_FALL bit
7987

8088

8189
class DataRate: # pylint: disable=too-few-public-methods
@@ -101,22 +109,22 @@ class DataRate: # pylint: disable=too-few-public-methods
101109
102110
"""
103111

104-
RATE_3200_HZ = const(0b1111) # 1600Hz Bandwidth 140mA IDD
105-
RATE_1600_HZ = const(0b1110) # 800Hz Bandwidth 90mA IDD
106-
RATE_800_HZ = const(0b1101) # 400Hz Bandwidth 140mA IDD
107-
RATE_400_HZ = const(0b1100) # 200Hz Bandwidth 140mA IDD
108-
RATE_200_HZ = const(0b1011) # 100Hz Bandwidth 140mA IDD
109-
RATE_100_HZ = const(0b1010) # 50Hz Bandwidth 140mA IDD
110-
RATE_50_HZ = const(0b1001) # 25Hz Bandwidth 90mA IDD
111-
RATE_25_HZ = const(0b1000) # 12.5Hz Bandwidth 60mA IDD
112-
RATE_12_5_HZ = const(0b0111) # 6.25Hz Bandwidth 50mA IDD
113-
RATE_6_25HZ = const(0b0110) # 3.13Hz Bandwidth 45mA IDD
114-
RATE_3_13_HZ = const(0b0101) # 1.56Hz Bandwidth 40mA IDD
115-
RATE_1_56_HZ = const(0b0100) # 0.78Hz Bandwidth 34mA IDD
116-
RATE_0_78_HZ = const(0b0011) # 0.39Hz Bandwidth 23mA IDD
117-
RATE_0_39_HZ = const(0b0010) # 0.20Hz Bandwidth 23mA IDD
118-
RATE_0_20_HZ = const(0b0001) # 0.10Hz Bandwidth 23mA IDD
119-
RATE_0_10_HZ = const(0b0000) # 0.05Hz Bandwidth 23mA IDD (default value)
112+
RATE_3200_HZ: int = const(0b1111) # 1600Hz Bandwidth 140mA IDD
113+
RATE_1600_HZ: int = const(0b1110) # 800Hz Bandwidth 90mA IDD
114+
RATE_800_HZ: int = const(0b1101) # 400Hz Bandwidth 140mA IDD
115+
RATE_400_HZ: int = const(0b1100) # 200Hz Bandwidth 140mA IDD
116+
RATE_200_HZ: int = const(0b1011) # 100Hz Bandwidth 140mA IDD
117+
RATE_100_HZ: int = const(0b1010) # 50Hz Bandwidth 140mA IDD
118+
RATE_50_HZ: int = const(0b1001) # 25Hz Bandwidth 90mA IDD
119+
RATE_25_HZ: int = const(0b1000) # 12.5Hz Bandwidth 60mA IDD
120+
RATE_12_5_HZ: int = const(0b0111) # 6.25Hz Bandwidth 50mA IDD
121+
RATE_6_25HZ: int = const(0b0110) # 3.13Hz Bandwidth 45mA IDD
122+
RATE_3_13_HZ: int = const(0b0101) # 1.56Hz Bandwidth 40mA IDD
123+
RATE_1_56_HZ: int = const(0b0100) # 0.78Hz Bandwidth 34mA IDD
124+
RATE_0_78_HZ: int = const(0b0011) # 0.39Hz Bandwidth 23mA IDD
125+
RATE_0_39_HZ: int = const(0b0010) # 0.20Hz Bandwidth 23mA IDD
126+
RATE_0_20_HZ: int = const(0b0001) # 0.10Hz Bandwidth 23mA IDD
127+
RATE_0_10_HZ: int = const(0b0000) # 0.05Hz Bandwidth 23mA IDD (default value)
120128

121129

122130
class Range: # pylint: disable=too-few-public-methods
@@ -131,10 +139,10 @@ class Range: # pylint: disable=too-few-public-methods
131139
132140
"""
133141

134-
RANGE_16_G = const(0b11) # +/- 16g
135-
RANGE_8_G = const(0b10) # +/- 8g
136-
RANGE_4_G = const(0b01) # +/- 4g
137-
RANGE_2_G = const(0b00) # +/- 2g (default value)
142+
RANGE_16_G: int = const(0b11) # +/- 16g
143+
RANGE_8_G: int = const(0b10) # +/- 8g
144+
RANGE_4_G: int = const(0b01) # +/- 4g
145+
RANGE_2_G: int = const(0b00) # +/- 2g (default value)
138146

139147

140148
class ADXL345:
@@ -169,7 +177,7 @@ class ADXL345:
169177
170178
"""
171179

172-
def __init__(self, i2c, address=_ADXL345_DEFAULT_ADDRESS):
180+
def __init__(self, i2c: busio.I2C, address: int = _ADXL345_DEFAULT_ADDRESS):
173181

174182
self._i2c = i2c_device.I2CDevice(i2c, address)
175183
self._buffer = bytearray(6)
@@ -181,7 +189,7 @@ def __init__(self, i2c, address=_ADXL345_DEFAULT_ADDRESS):
181189
self._event_status = {}
182190

183191
@property
184-
def acceleration(self):
192+
def acceleration(self) -> Tuple[int, int, int]:
185193
"""The x, y, z acceleration values returned in a 3-tuple in :math:`m / s ^ 2`"""
186194
x, y, z = unpack("<hhh", self._read_register(_REG_DATAX0, 6))
187195
x = x * _ADXL345_MG2G_MULTIPLIER * _STANDARD_GRAVITY
@@ -190,7 +198,7 @@ def acceleration(self):
190198
return (x, y, z)
191199

192200
@property
193-
def events(self):
201+
def events(self) -> Dict[str, bool]:
194202
"""
195203
:attr:`events` will return a dictionary with a key for each
196204
event type that has been enabled.
@@ -237,7 +245,7 @@ def events(self):
237245

238246
return self._event_status
239247

240-
def enable_motion_detection(self, *, threshold=18):
248+
def enable_motion_detection(self, *, threshold: int = 18):
241249
"""
242250
The activity detection parameters.
243251
@@ -263,7 +271,7 @@ def enable_motion_detection(self, *, threshold=18):
263271
self._write_register_byte(_REG_INT_ENABLE, active_interrupts)
264272
self._enabled_interrupts["motion"] = True
265273

266-
def disable_motion_detection(self):
274+
def disable_motion_detection(self) -> None:
267275
"""
268276
Disable motion detection
269277
"""
@@ -272,7 +280,7 @@ def disable_motion_detection(self):
272280
self._write_register_byte(_REG_INT_ENABLE, active_interrupts)
273281
self._enabled_interrupts.pop("motion")
274282

275-
def enable_freefall_detection(self, *, threshold=10, time=25):
283+
def enable_freefall_detection(self, *, threshold: int = 10, time: int = 25) -> None:
276284
"""
277285
Freefall detection parameters:
278286
@@ -303,16 +311,22 @@ def enable_freefall_detection(self, *, threshold=10, time=25):
303311
self._write_register_byte(_REG_INT_ENABLE, active_interrupts)
304312
self._enabled_interrupts["freefall"] = True
305313

306-
def disable_freefall_detection(self):
314+
def disable_freefall_detection(self) -> None:
307315
"Disable freefall detection"
308316
active_interrupts = self._read_register_unpacked(_REG_INT_ENABLE)
309317
active_interrupts &= ~_INT_FREE_FALL
310318
self._write_register_byte(_REG_INT_ENABLE, active_interrupts)
311319
self._enabled_interrupts.pop("freefall")
312320

313321
def enable_tap_detection(
314-
self, *, tap_count=1, threshold=20, duration=50, latency=20, window=255
315-
): # pylint: disable=line-too-long
322+
self,
323+
*,
324+
tap_count: int = 1,
325+
threshold: int = 20,
326+
duration: int = 50,
327+
latency: int = 20,
328+
window: int = 255
329+
):
316330
"""
317331
The tap detection parameters.
318332
@@ -364,7 +378,7 @@ def enable_tap_detection(
364378
"tap must be 0 to disable, 1 for single tap, or 2 for double tap"
365379
)
366380

367-
def disable_tap_detection(self):
381+
def disable_tap_detection(self) -> None:
368382
"Disable tap detection"
369383
active_interrupts = self._read_register_unpacked(_REG_INT_ENABLE)
370384
active_interrupts &= ~_INT_SINGLE_TAP
@@ -373,23 +387,23 @@ def disable_tap_detection(self):
373387
self._enabled_interrupts.pop("tap")
374388

375389
@property
376-
def data_rate(self):
390+
def data_rate(self) -> int:
377391
"""The data rate of the sensor."""
378392
rate_register = self._read_register_unpacked(_REG_BW_RATE)
379393
return rate_register & 0x0F
380394

381395
@data_rate.setter
382-
def data_rate(self, val):
396+
def data_rate(self, val: int) -> None:
383397
self._write_register_byte(_REG_BW_RATE, val)
384398

385399
@property
386-
def range(self):
400+
def range(self) -> int:
387401
"""The measurement range of the sensor."""
388402
range_register = self._read_register_unpacked(_REG_DATA_FORMAT)
389403
return range_register & 0x03
390404

391405
@range.setter
392-
def range(self, val):
406+
def range(self, val: int) -> None:
393407
# read the current value of the data format register
394408
format_register = self._read_register_unpacked(_REG_DATA_FORMAT)
395409

@@ -403,20 +417,20 @@ def range(self, val):
403417
# write the updated values
404418
self._write_register_byte(_REG_DATA_FORMAT, format_register)
405419

406-
def _read_clear_interrupt_source(self):
420+
def _read_clear_interrupt_source(self) -> int:
407421
return self._read_register_unpacked(_REG_INT_SOURCE)
408422

409-
def _read_register_unpacked(self, register):
423+
def _read_register_unpacked(self, register: int) -> int:
410424
return unpack("<b", self._read_register(register, 1))[0]
411425

412-
def _read_register(self, register, length):
426+
def _read_register(self, register: int, length: int) -> int:
413427
self._buffer[0] = register & 0xFF
414428
with self._i2c as i2c:
415429
i2c.write(self._buffer, start=0, end=1)
416430
i2c.readinto(self._buffer, start=0, end=length)
417431
return self._buffer[0:length]
418432

419-
def _write_register_byte(self, register, value):
433+
def _write_register_byte(self, register: int, value: int) -> None:
420434
self._buffer[0] = register & 0xFF
421435
self._buffer[1] = value & 0xFF
422436
with self._i2c as i2c:

0 commit comments

Comments
 (0)