33
33
from micropython import const
34
34
from adafruit_bus_device import i2c_device
35
35
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
+
36
44
__version__ = "0.0.0-auto.0"
37
45
__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
39
47
40
48
# 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
79
87
80
88
81
89
class DataRate : # pylint: disable=too-few-public-methods
@@ -101,22 +109,22 @@ class DataRate: # pylint: disable=too-few-public-methods
101
109
102
110
"""
103
111
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)
120
128
121
129
122
130
class Range : # pylint: disable=too-few-public-methods
@@ -131,10 +139,10 @@ class Range: # pylint: disable=too-few-public-methods
131
139
132
140
"""
133
141
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)
138
146
139
147
140
148
class ADXL345 :
@@ -169,7 +177,7 @@ class ADXL345:
169
177
170
178
"""
171
179
172
- def __init__ (self , i2c , address = _ADXL345_DEFAULT_ADDRESS ):
180
+ def __init__ (self , i2c : busio . I2C , address : int = _ADXL345_DEFAULT_ADDRESS ):
173
181
174
182
self ._i2c = i2c_device .I2CDevice (i2c , address )
175
183
self ._buffer = bytearray (6 )
@@ -181,7 +189,7 @@ def __init__(self, i2c, address=_ADXL345_DEFAULT_ADDRESS):
181
189
self ._event_status = {}
182
190
183
191
@property
184
- def acceleration (self ):
192
+ def acceleration (self ) -> Tuple [ int , int , int ] :
185
193
"""The x, y, z acceleration values returned in a 3-tuple in :math:`m / s ^ 2`"""
186
194
x , y , z = unpack ("<hhh" , self ._read_register (_REG_DATAX0 , 6 ))
187
195
x = x * _ADXL345_MG2G_MULTIPLIER * _STANDARD_GRAVITY
@@ -190,7 +198,7 @@ def acceleration(self):
190
198
return (x , y , z )
191
199
192
200
@property
193
- def events (self ):
201
+ def events (self ) -> Dict [ str , bool ] :
194
202
"""
195
203
:attr:`events` will return a dictionary with a key for each
196
204
event type that has been enabled.
@@ -237,7 +245,7 @@ def events(self):
237
245
238
246
return self ._event_status
239
247
240
- def enable_motion_detection (self , * , threshold = 18 ):
248
+ def enable_motion_detection (self , * , threshold : int = 18 ):
241
249
"""
242
250
The activity detection parameters.
243
251
@@ -263,7 +271,7 @@ def enable_motion_detection(self, *, threshold=18):
263
271
self ._write_register_byte (_REG_INT_ENABLE , active_interrupts )
264
272
self ._enabled_interrupts ["motion" ] = True
265
273
266
- def disable_motion_detection (self ):
274
+ def disable_motion_detection (self ) -> None :
267
275
"""
268
276
Disable motion detection
269
277
"""
@@ -272,7 +280,7 @@ def disable_motion_detection(self):
272
280
self ._write_register_byte (_REG_INT_ENABLE , active_interrupts )
273
281
self ._enabled_interrupts .pop ("motion" )
274
282
275
- def enable_freefall_detection (self , * , threshold = 10 , time = 25 ):
283
+ def enable_freefall_detection (self , * , threshold : int = 10 , time : int = 25 ) -> None :
276
284
"""
277
285
Freefall detection parameters:
278
286
@@ -303,16 +311,22 @@ def enable_freefall_detection(self, *, threshold=10, time=25):
303
311
self ._write_register_byte (_REG_INT_ENABLE , active_interrupts )
304
312
self ._enabled_interrupts ["freefall" ] = True
305
313
306
- def disable_freefall_detection (self ):
314
+ def disable_freefall_detection (self ) -> None :
307
315
"Disable freefall detection"
308
316
active_interrupts = self ._read_register_unpacked (_REG_INT_ENABLE )
309
317
active_interrupts &= ~ _INT_FREE_FALL
310
318
self ._write_register_byte (_REG_INT_ENABLE , active_interrupts )
311
319
self ._enabled_interrupts .pop ("freefall" )
312
320
313
321
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
+ ):
316
330
"""
317
331
The tap detection parameters.
318
332
@@ -364,7 +378,7 @@ def enable_tap_detection(
364
378
"tap must be 0 to disable, 1 for single tap, or 2 for double tap"
365
379
)
366
380
367
- def disable_tap_detection (self ):
381
+ def disable_tap_detection (self ) -> None :
368
382
"Disable tap detection"
369
383
active_interrupts = self ._read_register_unpacked (_REG_INT_ENABLE )
370
384
active_interrupts &= ~ _INT_SINGLE_TAP
@@ -373,23 +387,23 @@ def disable_tap_detection(self):
373
387
self ._enabled_interrupts .pop ("tap" )
374
388
375
389
@property
376
- def data_rate (self ):
390
+ def data_rate (self ) -> int :
377
391
"""The data rate of the sensor."""
378
392
rate_register = self ._read_register_unpacked (_REG_BW_RATE )
379
393
return rate_register & 0x0F
380
394
381
395
@data_rate .setter
382
- def data_rate (self , val ) :
396
+ def data_rate (self , val : int ) -> None :
383
397
self ._write_register_byte (_REG_BW_RATE , val )
384
398
385
399
@property
386
- def range (self ):
400
+ def range (self ) -> int :
387
401
"""The measurement range of the sensor."""
388
402
range_register = self ._read_register_unpacked (_REG_DATA_FORMAT )
389
403
return range_register & 0x03
390
404
391
405
@range .setter
392
- def range (self , val ) :
406
+ def range (self , val : int ) -> None :
393
407
# read the current value of the data format register
394
408
format_register = self ._read_register_unpacked (_REG_DATA_FORMAT )
395
409
@@ -403,20 +417,20 @@ def range(self, val):
403
417
# write the updated values
404
418
self ._write_register_byte (_REG_DATA_FORMAT , format_register )
405
419
406
- def _read_clear_interrupt_source (self ):
420
+ def _read_clear_interrupt_source (self ) -> int :
407
421
return self ._read_register_unpacked (_REG_INT_SOURCE )
408
422
409
- def _read_register_unpacked (self , register ) :
423
+ def _read_register_unpacked (self , register : int ) -> int :
410
424
return unpack ("<b" , self ._read_register (register , 1 ))[0 ]
411
425
412
- def _read_register (self , register , length ) :
426
+ def _read_register (self , register : int , length : int ) -> int :
413
427
self ._buffer [0 ] = register & 0xFF
414
428
with self ._i2c as i2c :
415
429
i2c .write (self ._buffer , start = 0 , end = 1 )
416
430
i2c .readinto (self ._buffer , start = 0 , end = length )
417
431
return self ._buffer [0 :length ]
418
432
419
- def _write_register_byte (self , register , value ) :
433
+ def _write_register_byte (self , register : int , value : int ) -> None :
420
434
self ._buffer [0 ] = register & 0xFF
421
435
self ._buffer [1 ] = value & 0xFF
422
436
with self ._i2c as i2c :
0 commit comments