45
45
46
46
from micropython import const
47
47
from adafruit_bus_device import i2c_device
48
+
48
49
try :
49
50
from struct import unpack
50
51
except ImportError :
51
52
from ustruct import unpack
52
53
__version__ = "0.0.0-auto.0"
53
54
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ADXL34x.git"
54
- #pylint: disable=bad-whitespace
55
- _ADXL345_DEFAULT_ADDRESS = const (0x53 ) # Assumes ALT address pin low
55
+ # pylint: disable=bad-whitespace
56
+ _ADXL345_DEFAULT_ADDRESS = const (0x53 ) # Assumes ALT address pin low
56
57
57
58
# Conversion factors
58
- _ADXL345_MG2G_MULTIPLIER = 0.004 # 4mg per lsb
59
- _STANDARD_GRAVITY = 9.80665 # earth standard gravity
60
-
61
- _REG_DEVID = const (0x00 ) # Device ID
62
- _REG_THRESH_TAP = const (0x1D ) # Tap threshold
63
- _REG_OFSX = const (0x1E ) # X-axis offset
64
- _REG_OFSY = const (0x1F ) # Y-axis offset
65
- _REG_OFSZ = const (0x20 ) # Z-axis offset
66
- _REG_DUR = const (0x21 ) # Tap duration
67
- _REG_LATENT = const (0x22 ) # Tap latency
68
- _REG_WINDOW = const (0x23 ) # Tap window
69
- _REG_THRESH_ACT = const (0x24 ) # Activity threshold
70
- _REG_THRESH_INACT = const (0x25 ) # Inactivity threshold
71
- _REG_TIME_INACT = const (0x26 ) # Inactivity time
72
- _REG_ACT_INACT_CTL = const (0x27 ) # Axis enable control for [in]activity detection
73
- _REG_THRESH_FF = const (0x28 ) # Free-fall threshold
74
- _REG_TIME_FF = const (0x29 ) # Free-fall time
75
- _REG_TAP_AXES = const (0x2A ) # Axis control for single/double tap
76
- _REG_ACT_TAP_STATUS = const (0x2B ) # Source for single/double tap
77
- _REG_BW_RATE = const (0x2C ) # Data rate and power mode control
78
- _REG_POWER_CTL = const (0x2D ) # Power-saving features control
79
- _REG_INT_ENABLE = const (0x2E ) # Interrupt enable control
80
- _REG_INT_MAP = const (0x2F ) # Interrupt mapping control
81
- _REG_INT_SOURCE = const (0x30 ) # Source of interrupts
82
- _REG_DATA_FORMAT = const (0x31 ) # Data format control
83
- _REG_DATAX0 = const (0x32 ) # X-axis data 0
84
- _REG_DATAX1 = const (0x33 ) # X-axis data 1
85
- _REG_DATAY0 = const (0x34 ) # Y-axis data 0
86
- _REG_DATAY1 = const (0x35 ) # Y-axis data 1
87
- _REG_DATAZ0 = const (0x36 ) # Z-axis data 0
88
- _REG_DATAZ1 = const (0x37 ) # Z-axis data 1
89
- _REG_FIFO_CTL = const (0x38 ) # FIFO control
90
- _REG_FIFO_STATUS = const (0x39 ) # FIFO status
91
- _INT_SINGLE_TAP = const (0b01000000 ) # SINGLE_TAP bit
92
- _INT_DOUBLE_TAP = const (0b00100000 ) # DOUBLE_TAP bit
93
- _INT_ACT = const (0b00010000 ) # ACT bit
94
- _INT_INACT = const (0b00001000 ) # INACT bit
95
- _INT_FREE_FALL = const (0b00000100 ) # FREE_FALL bit
96
-
97
- class DataRate : #pylint: disable=too-few-public-methods
59
+ _ADXL345_MG2G_MULTIPLIER = 0.004 # 4mg per lsb
60
+ _STANDARD_GRAVITY = 9.80665 # earth standard gravity
61
+
62
+ _REG_DEVID = const (0x00 ) # Device ID
63
+ _REG_THRESH_TAP = const (0x1D ) # Tap threshold
64
+ _REG_OFSX = const (0x1E ) # X-axis offset
65
+ _REG_OFSY = const (0x1F ) # Y-axis offset
66
+ _REG_OFSZ = const (0x20 ) # Z-axis offset
67
+ _REG_DUR = const (0x21 ) # Tap duration
68
+ _REG_LATENT = const (0x22 ) # Tap latency
69
+ _REG_WINDOW = const (0x23 ) # Tap window
70
+ _REG_THRESH_ACT = const (0x24 ) # Activity threshold
71
+ _REG_THRESH_INACT = const (0x25 ) # Inactivity threshold
72
+ _REG_TIME_INACT = const (0x26 ) # Inactivity time
73
+ _REG_ACT_INACT_CTL = const (0x27 ) # Axis enable control for [in]activity detection
74
+ _REG_THRESH_FF = const (0x28 ) # Free-fall threshold
75
+ _REG_TIME_FF = const (0x29 ) # Free-fall time
76
+ _REG_TAP_AXES = const (0x2A ) # Axis control for single/double tap
77
+ _REG_ACT_TAP_STATUS = const (0x2B ) # Source for single/double tap
78
+ _REG_BW_RATE = const (0x2C ) # Data rate and power mode control
79
+ _REG_POWER_CTL = const (0x2D ) # Power-saving features control
80
+ _REG_INT_ENABLE = const (0x2E ) # Interrupt enable control
81
+ _REG_INT_MAP = const (0x2F ) # Interrupt mapping control
82
+ _REG_INT_SOURCE = const (0x30 ) # Source of interrupts
83
+ _REG_DATA_FORMAT = const (0x31 ) # Data format control
84
+ _REG_DATAX0 = const (0x32 ) # X-axis data 0
85
+ _REG_DATAX1 = const (0x33 ) # X-axis data 1
86
+ _REG_DATAY0 = const (0x34 ) # Y-axis data 0
87
+ _REG_DATAY1 = const (0x35 ) # Y-axis data 1
88
+ _REG_DATAZ0 = const (0x36 ) # Z-axis data 0
89
+ _REG_DATAZ1 = const (0x37 ) # Z-axis data 1
90
+ _REG_FIFO_CTL = const (0x38 ) # FIFO control
91
+ _REG_FIFO_STATUS = const (0x39 ) # FIFO status
92
+ _INT_SINGLE_TAP = const (0b01000000 ) # SINGLE_TAP bit
93
+ _INT_DOUBLE_TAP = const (0b00100000 ) # DOUBLE_TAP bit
94
+ _INT_ACT = const (0b00010000 ) # ACT bit
95
+ _INT_INACT = const (0b00001000 ) # INACT bit
96
+ _INT_FREE_FALL = const (0b00000100 ) # FREE_FALL bit
97
+
98
+
99
+ class DataRate : # pylint: disable=too-few-public-methods
98
100
"""An enum-like class representing the possible data rates.
99
101
Possible values are
100
102
@@ -116,25 +118,26 @@ class DataRate: #pylint: disable=too-few-public-methods
116
118
- ``DataRate.RATE_0_10_HZ``
117
119
118
120
"""
119
- RATE_3200_HZ = const (0b1111 ) # 1600Hz Bandwidth 140mA IDD
120
- RATE_1600_HZ = const (0b1110 ) # 800Hz Bandwidth 90mA IDD
121
- RATE_800_HZ = const (0b1101 ) # 400Hz Bandwidth 140mA IDD
122
- RATE_400_HZ = const (0b1100 ) # 200Hz Bandwidth 140mA IDD
123
- RATE_200_HZ = const (0b1011 ) # 100Hz Bandwidth 140mA IDD
124
- RATE_100_HZ = const (0b1010 ) # 50Hz Bandwidth 140mA IDD
125
- RATE_50_HZ = const (0b1001 ) # 25Hz Bandwidth 90mA IDD
126
- RATE_25_HZ = const (0b1000 ) # 12.5Hz Bandwidth 60mA IDD
127
- RATE_12_5_HZ = const (0b0111 ) # 6.25Hz Bandwidth 50mA IDD
128
- RATE_6_25HZ = const (0b0110 ) # 3.13Hz Bandwidth 45mA IDD
129
- RATE_3_13_HZ = const (0b0101 ) # 1.56Hz Bandwidth 40mA IDD
130
- RATE_1_56_HZ = const (0b0100 ) # 0.78Hz Bandwidth 34mA IDD
131
- RATE_0_78_HZ = const (0b0011 ) # 0.39Hz Bandwidth 23mA IDD
132
- RATE_0_39_HZ = const (0b0010 ) # 0.20Hz Bandwidth 23mA IDD
133
- RATE_0_20_HZ = const (0b0001 ) # 0.10Hz Bandwidth 23mA IDD
134
- RATE_0_10_HZ = const (0b0000 ) # 0.05Hz Bandwidth 23mA IDD (default value)
135
-
136
-
137
- class Range : #pylint: disable=too-few-public-methods
121
+
122
+ RATE_3200_HZ = const (0b1111 ) # 1600Hz Bandwidth 140mA IDD
123
+ RATE_1600_HZ = const (0b1110 ) # 800Hz Bandwidth 90mA IDD
124
+ RATE_800_HZ = const (0b1101 ) # 400Hz Bandwidth 140mA IDD
125
+ RATE_400_HZ = const (0b1100 ) # 200Hz Bandwidth 140mA IDD
126
+ RATE_200_HZ = const (0b1011 ) # 100Hz Bandwidth 140mA IDD
127
+ RATE_100_HZ = const (0b1010 ) # 50Hz Bandwidth 140mA IDD
128
+ RATE_50_HZ = const (0b1001 ) # 25Hz Bandwidth 90mA IDD
129
+ RATE_25_HZ = const (0b1000 ) # 12.5Hz Bandwidth 60mA IDD
130
+ RATE_12_5_HZ = const (0b0111 ) # 6.25Hz Bandwidth 50mA IDD
131
+ RATE_6_25HZ = const (0b0110 ) # 3.13Hz Bandwidth 45mA IDD
132
+ RATE_3_13_HZ = const (0b0101 ) # 1.56Hz Bandwidth 40mA IDD
133
+ RATE_1_56_HZ = const (0b0100 ) # 0.78Hz Bandwidth 34mA IDD
134
+ RATE_0_78_HZ = const (0b0011 ) # 0.39Hz Bandwidth 23mA IDD
135
+ RATE_0_39_HZ = const (0b0010 ) # 0.20Hz Bandwidth 23mA IDD
136
+ RATE_0_20_HZ = const (0b0001 ) # 0.10Hz Bandwidth 23mA IDD
137
+ RATE_0_10_HZ = const (0b0000 ) # 0.05Hz Bandwidth 23mA IDD (default value)
138
+
139
+
140
+ class Range : # pylint: disable=too-few-public-methods
138
141
"""An enum-like class representing the possible measurement ranges in +/- G.
139
142
140
143
Possible values are
@@ -145,10 +148,12 @@ class Range: #pylint: disable=too-few-public-methods
145
148
- ``Range.RANGE_2_G``
146
149
147
150
"""
148
- RANGE_16_G = const (0b11 ) # +/- 16g
149
- RANGE_8_G = const (0b10 ) # +/- 8g
150
- RANGE_4_G = const (0b01 ) # +/- 4g
151
- RANGE_2_G = const (0b00 ) # +/- 2g (default value)
151
+
152
+ RANGE_16_G = const (0b11 ) # +/- 16g
153
+ RANGE_8_G = const (0b10 ) # +/- 8g
154
+ RANGE_4_G = const (0b01 ) # +/- 4g
155
+ RANGE_2_G = const (0b00 ) # +/- 2g (default value)
156
+
152
157
153
158
# pylint: enable=bad-whitespace
154
159
class ADXL345 :
@@ -158,8 +163,8 @@ class ADXL345:
158
163
:param address: The I2C device address for the sensor. Default is ``0x53``.
159
164
160
165
"""
161
- def __init__ (self , i2c , address = _ADXL345_DEFAULT_ADDRESS ):
162
166
167
+ def __init__ (self , i2c , address = _ADXL345_DEFAULT_ADDRESS ):
163
168
164
169
self ._i2c = i2c_device .I2CDevice (i2c , address )
165
170
self ._buffer = bytearray (6 )
@@ -173,7 +178,7 @@ def __init__(self, i2c, address=_ADXL345_DEFAULT_ADDRESS):
173
178
@property
174
179
def acceleration (self ):
175
180
"""The x, y, z acceleration values returned in a 3-tuple in m / s ^ 2."""
176
- x , y , z = unpack (' <hhh' , self ._read_register (_REG_DATAX0 , 6 ))
181
+ x , y , z = unpack (" <hhh" , self ._read_register (_REG_DATAX0 , 6 ))
177
182
x = x * _ADXL345_MG2G_MULTIPLIER * _STANDARD_GRAVITY
178
183
y = y * _ADXL345_MG2G_MULTIPLIER * _STANDARD_GRAVITY
179
184
z = z * _ADXL345_MG2G_MULTIPLIER * _STANDARD_GRAVITY
@@ -207,14 +212,22 @@ def events(self):
207
212
208
213
for event_type , value in self ._enabled_interrupts .items ():
209
214
if event_type == "motion" :
210
- self ._event_status [event_type ] = interrupt_source_register & _INT_ACT > 0
215
+ self ._event_status [event_type ] = (
216
+ interrupt_source_register & _INT_ACT > 0
217
+ )
211
218
if event_type == "tap" :
212
219
if value == 1 :
213
- self ._event_status [event_type ] = interrupt_source_register & _INT_SINGLE_TAP > 0
220
+ self ._event_status [event_type ] = (
221
+ interrupt_source_register & _INT_SINGLE_TAP > 0
222
+ )
214
223
else :
215
- self ._event_status [event_type ] = interrupt_source_register & _INT_DOUBLE_TAP > 0
224
+ self ._event_status [event_type ] = (
225
+ interrupt_source_register & _INT_DOUBLE_TAP > 0
226
+ )
216
227
if event_type == "freefall" :
217
- self ._event_status [event_type ] = interrupt_source_register & _INT_FREE_FALL > 0
228
+ self ._event_status [event_type ] = (
229
+ interrupt_source_register & _INT_FREE_FALL > 0
230
+ )
218
231
219
232
return self ._event_status
220
233
@@ -233,11 +246,12 @@ def enable_motion_detection(self, *, threshold=18):
233
246
"""
234
247
active_interrupts = self ._read_register_unpacked (_REG_INT_ENABLE )
235
248
236
-
237
- self ._write_register_byte (_REG_INT_ENABLE , 0x0 ) # disable interrupts for setup
238
- self ._write_register_byte (_REG_ACT_INACT_CTL , 0b01110000 ) # enable activity on X,Y,Z
249
+ self ._write_register_byte (_REG_INT_ENABLE , 0x0 ) # disable interrupts for setup
250
+ self ._write_register_byte (
251
+ _REG_ACT_INACT_CTL , 0b01110000
252
+ ) # enable activity on X,Y,Z
239
253
self ._write_register_byte (_REG_THRESH_ACT , threshold )
240
- self ._write_register_byte (_REG_INT_ENABLE , _INT_ACT ) # Inactive interrupt only
254
+ self ._write_register_byte (_REG_INT_ENABLE , _INT_ACT ) # Inactive interrupt only
241
255
242
256
active_interrupts |= _INT_ACT
243
257
self ._write_register_byte (_REG_INT_ENABLE , active_interrupts )
@@ -252,7 +266,6 @@ def disable_motion_detection(self):
252
266
self ._write_register_byte (_REG_INT_ENABLE , active_interrupts )
253
267
self ._enabled_interrupts .pop ("motion" )
254
268
255
-
256
269
def enable_freefall_detection (self , * , threshold = 10 , time = 25 ):
257
270
"""
258
271
Freefall detection parameters:
@@ -273,7 +286,7 @@ def enable_freefall_detection(self, *, threshold=10, time=25):
273
286
274
287
active_interrupts = self ._read_register_unpacked (_REG_INT_ENABLE )
275
288
276
- self ._write_register_byte (_REG_INT_ENABLE , 0x0 ) # disable interrupts for setup
289
+ self ._write_register_byte (_REG_INT_ENABLE , 0x0 ) # disable interrupts for setup
277
290
self ._write_register_byte (_REG_THRESH_FF , threshold )
278
291
self ._write_register_byte (_REG_TIME_FF , time )
279
292
@@ -289,7 +302,9 @@ def disable_freefall_detection(self):
289
302
self ._write_register_byte (_REG_INT_ENABLE , active_interrupts )
290
303
self ._enabled_interrupts .pop ("freefall" )
291
304
292
- def enable_tap_detection (self , * , tap_count = 1 , threshold = 20 , duration = 50 , latency = 20 , window = 255 ):#pylint: disable=line-too-long
305
+ def enable_tap_detection (
306
+ self , * , tap_count = 1 , threshold = 20 , duration = 50 , latency = 20 , window = 255
307
+ ): # pylint: disable=line-too-long
293
308
"""
294
309
The tap detection parameters.
295
310
@@ -317,8 +332,10 @@ def enable_tap_detection(self, *, tap_count=1, threshold=20, duration=50, latenc
317
332
"""
318
333
active_interrupts = self ._read_register_unpacked (_REG_INT_ENABLE )
319
334
320
- self ._write_register_byte (_REG_INT_ENABLE , 0x0 ) # disable interrupts for setup
321
- self ._write_register_byte (_REG_TAP_AXES , 0b00000111 ) # enable X, Y, Z axes for tap
335
+ self ._write_register_byte (_REG_INT_ENABLE , 0x0 ) # disable interrupts for setup
336
+ self ._write_register_byte (
337
+ _REG_TAP_AXES , 0b00000111
338
+ ) # enable X, Y, Z axes for tap
322
339
self ._write_register_byte (_REG_THRESH_TAP , threshold )
323
340
self ._write_register_byte (_REG_DUR , duration )
324
341
@@ -334,7 +351,9 @@ def enable_tap_detection(self, *, tap_count=1, threshold=20, duration=50, latenc
334
351
self ._write_register_byte (_REG_INT_ENABLE , active_interrupts )
335
352
self ._enabled_interrupts ["tap" ] = 2
336
353
else :
337
- raise ValueError ("tap must be 0 to disable, 1 for single tap, or 2 for double tap" )
354
+ raise ValueError (
355
+ "tap must be 0 to disable, 1 for single tap, or 2 for double tap"
356
+ )
338
357
339
358
def disable_tap_detection (self ):
340
359
"Disable tap detection"
@@ -394,6 +413,7 @@ def _write_register_byte(self, register, value):
394
413
with self ._i2c as i2c :
395
414
i2c .write (self ._buffer , start = 0 , end = 2 )
396
415
416
+
397
417
class ADXL343 (ADXL345 ):
398
418
"""
399
419
Stub class for the ADXL343 3-axis accelerometer
0 commit comments