78
78
79
79
80
80
_STANDARD_GRAVITY = 9.806
81
- class Mode : # pylint: disable=too-few-public-methods
81
+
82
+
83
+ class Mode : # pylint: disable=too-few-public-methods
82
84
"""An enum-like class representing the different modes that the MSA301 can
83
85
use. The values can be referenced like ``Mode.NORMAL`` or ``Mode.SUSPEND``
84
86
Possible values are
@@ -87,12 +89,14 @@ class Mode: # pylint: disable=too-few-public-methods
87
89
- ``Mode.LOW_POWER``
88
90
- ``Mode.SUSPEND``
89
91
"""
92
+
90
93
# pylint: disable=invalid-name
91
94
NORMAL = 0b00
92
95
LOWPOWER = 0b01
93
96
SUSPEND = 0b010
94
97
95
- class DataRate : # pylint: disable=too-few-public-methods
98
+
99
+ class DataRate : # pylint: disable=too-few-public-methods
96
100
"""An enum-like class representing the different data rates that the MSA301 can
97
101
use. The values can be referenced like ``DataRate.RATE_1_HZ`` or ``DataRate.RATE_1000_HZ``
98
102
Possible values are
@@ -109,19 +113,21 @@ class DataRate: # pylint: disable=too-few-public-methods
109
113
- ``DataRate.RATE_500_HZ``
110
114
- ``DataRate.RATE_1000_HZ``
111
115
"""
112
- RATE_1_HZ = 0b0000 # 1 Hz
116
+
117
+ RATE_1_HZ = 0b0000 # 1 Hz
113
118
RATE_1_95_HZ = 0b0001 # 1.95 Hz
114
- RATE_3_9_HZ = 0b0010 # 3.9 Hz
119
+ RATE_3_9_HZ = 0b0010 # 3.9 Hz
115
120
RATE_7_81_HZ = 0b0011 # 7.81 Hz
116
- RATE_15_63_HZ = 0b0100 # 15.63 Hz
117
- RATE_31_25_HZ = 0b0101 # 31.25 Hz
121
+ RATE_15_63_HZ = 0b0100 # 15.63 Hz
122
+ RATE_31_25_HZ = 0b0101 # 31.25 Hz
118
123
RATE_62_5_HZ = 0b0110 # 62.5 Hz
119
- RATE_125_HZ = 0b0111 # 125 Hz
120
- RATE_250_HZ = 0b1000 # 250 Hz
121
- RATE_500_HZ = 0b1001 # 500 Hz
124
+ RATE_125_HZ = 0b0111 # 125 Hz
125
+ RATE_250_HZ = 0b1000 # 250 Hz
126
+ RATE_500_HZ = 0b1001 # 500 Hz
122
127
RATE_1000_HZ = 0b1010 # 1000 Hz
123
128
124
- class BandWidth : # pylint: disable=too-few-public-methods
129
+
130
+ class BandWidth : # pylint: disable=too-few-public-methods
125
131
"""An enum-like class representing the different bandwidths that the MSA301 can
126
132
use. The values can be referenced like ``BandWidth.WIDTH_1_HZ`` or ``BandWidth.RATE_500_HZ``
127
133
Possible values are
@@ -138,17 +144,19 @@ class BandWidth: # pylint: disable=too-few-public-methods
138
144
- ``BandWidth.RATE_500_HZ``
139
145
- ``BandWidth.RATE_1000_HZ``
140
146
"""
147
+
141
148
WIDTH_1_95_HZ = 0b0000 # 1.95 Hz
142
- WIDTH_3_9_HZ = 0b0011 # 3.9 Hz
149
+ WIDTH_3_9_HZ = 0b0011 # 3.9 Hz
143
150
WIDTH_7_81_HZ = 0b0100 # 7.81 Hz
144
- WIDTH_15_63_HZ = 0b0101 # 15.63 Hz
145
- WIDTH_31_25_HZ = 0b0110 # 31.25 Hz
151
+ WIDTH_15_63_HZ = 0b0101 # 15.63 Hz
152
+ WIDTH_31_25_HZ = 0b0110 # 31.25 Hz
146
153
WIDTH_62_5_HZ = 0b0111 # 62.5 Hz
147
- WIDTH_125_HZ = 0b1000 # 125 Hz
148
- WIDTH_250_HZ = 0b1001 # 250 Hz
149
- WIDTH_500_HZ = 0b1010 # 500 Hz
154
+ WIDTH_125_HZ = 0b1000 # 125 Hz
155
+ WIDTH_250_HZ = 0b1001 # 250 Hz
156
+ WIDTH_500_HZ = 0b1010 # 500 Hz
157
+
150
158
151
- class Range : # pylint: disable=too-few-public-methods
159
+ class Range : # pylint: disable=too-few-public-methods
152
160
"""An enum-like class representing the different acceleration measurement ranges that the
153
161
MSA301 can use. The values can be referenced like ``Range.RANGE_2_G`` or ``Range.RANGE_16_G``
154
162
Possible values are
@@ -158,12 +166,14 @@ class Range: # pylint: disable=too-few-public-methods
158
166
- ``Range.RANGE_8_G``
159
167
- ``Range.RANGE_16_G``
160
168
"""
169
+
161
170
RANGE_2_G = 0b00 # +/- 2g (default value)
162
171
RANGE_4_G = 0b01 # +/- 4g
163
172
RANGE_8_G = 0b10 # +/- 8g
164
- RANGE_16_G = 0b11 # +/- 16g
173
+ RANGE_16_G = 0b11 # +/- 16g
174
+
165
175
166
- class Resolution : # pylint: disable=too-few-public-methods
176
+ class Resolution : # pylint: disable=too-few-public-methods
167
177
"""An enum-like class representing the different measurement ranges that the MSA301 can
168
178
use. The values can be referenced like ``Range.RANGE_2_G`` or ``Range.RANGE_16_G``
169
179
Possible values are
@@ -173,29 +183,33 @@ class Resolution: # pylint: disable=too-few-public-methods
173
183
- ``Resolution.RESOLUTION_10_BIT``
174
184
- ``Resolution.RESOLUTION_8_BIT``
175
185
"""
186
+
176
187
RESOLUTION_14_BIT = 0b00
177
188
RESOLUTION_12_BIT = 0b01
178
189
RESOLUTION_10_BIT = 0b10
179
190
RESOLUTION_8_BIT = 0b11
180
191
181
- class TapDuration : #pylint: disable=too-few-public-methods,too-many-instance-attributes
192
+
193
+ class TapDuration : # pylint: disable=too-few-public-methods,too-many-instance-attributes
182
194
"""An enum-like class representing the options for the "double_tap_window" parameter of
183
195
`enable_tap_detection`"""
184
- DURATION_50_MS = 0b000 #< 50 millis
185
- DURATION_100_MS = 0b001 #< 100 millis
186
- DURATION_150_MS = 0b010 #< 150 millis
187
- DURATION_200_MS = 0b011 #< 200 millis
188
- DURATION_250_MS = 0b100 #< 250 millis
189
- DURATION_375_MS = 0b101 #< 375 millis
190
- DURATION_500_MS = 0b110 #< 500 millis
191
- DURATION_700_MS = 0b111 #< 50 millis700 millis
192
196
197
+ DURATION_50_MS = 0b000 # < 50 millis
198
+ DURATION_100_MS = 0b001 # < 100 millis
199
+ DURATION_150_MS = 0b010 # < 150 millis
200
+ DURATION_200_MS = 0b011 # < 200 millis
201
+ DURATION_250_MS = 0b100 # < 250 millis
202
+ DURATION_375_MS = 0b101 # < 375 millis
203
+ DURATION_500_MS = 0b110 # < 500 millis
204
+ DURATION_700_MS = 0b111 # < 50 millis700 millis
193
205
194
- class MSA301 :#pylint: disable=too-many-instance-attributes
206
+
207
+ class MSA301 : # pylint: disable=too-many-instance-attributes
195
208
"""Driver for the MSA301 Accelerometer.
196
209
197
210
:param ~busio.I2C i2c_bus: The I2C bus the MSA is connected to.
198
211
"""
212
+
199
213
_part_id = ROUnaryStruct (_MSA301_REG_PARTID , "<B" )
200
214
201
215
def __init__ (self , i2c_bus ):
@@ -204,7 +218,6 @@ def __init__(self, i2c_bus):
204
218
if self ._part_id != 0x13 :
205
219
raise AttributeError ("Cannot find a MSA301" )
206
220
207
-
208
221
self ._disable_x = self ._disable_y = self ._disable_z = False
209
222
self .power_mode = Mode .NORMAL
210
223
self .data_rate = DataRate .RATE_500_HZ
@@ -247,7 +260,7 @@ def acceleration(self):
247
260
acc_bytes = bytearray ()
248
261
# shift out bytes, reversing the order
249
262
for shift in range (6 ):
250
- bottom_byte = ( raw_data >> ( 8 * shift ) & 0xFF )
263
+ bottom_byte = raw_data >> ( 8 * shift ) & 0xFF
251
264
acc_bytes .append (bottom_byte )
252
265
253
266
# unpack three LE, signed shorts
@@ -265,18 +278,21 @@ def acceleration(self):
265
278
scale = 4096.0
266
279
267
280
# shift down to the actual 14 bits and scale based on the range
268
- x_acc = ((x >> 2 ) / scale ) * _STANDARD_GRAVITY
269
- y_acc = ((y >> 2 ) / scale ) * _STANDARD_GRAVITY
270
- z_acc = ((z >> 2 ) / scale ) * _STANDARD_GRAVITY
281
+ x_acc = ((x >> 2 ) / scale ) * _STANDARD_GRAVITY
282
+ y_acc = ((y >> 2 ) / scale ) * _STANDARD_GRAVITY
283
+ z_acc = ((z >> 2 ) / scale ) * _STANDARD_GRAVITY
271
284
272
285
return (x_acc , y_acc , z_acc )
273
286
274
- def enable_tap_detection (self , * ,
275
- tap_count = 1 ,
276
- threshold = 25 ,
277
- long_initial_window = True ,
278
- long_quiet_window = True ,
279
- double_tap_window = TapDuration .DURATION_250_MS ):
287
+ def enable_tap_detection ( # pylint: disable=bad-continuation
288
+ self ,
289
+ * ,
290
+ tap_count = 1 ,
291
+ threshold = 25 ,
292
+ long_initial_window = True ,
293
+ long_quiet_window = True ,
294
+ double_tap_window = TapDuration .DURATION_250_MS
295
+ ):
280
296
"""
281
297
Enables tap detection with configurable parameters.
282
298
@@ -332,12 +348,12 @@ def tapped(self):
332
348
333
349
motion_int_status = self ._motion_int_status
334
350
335
- if motion_int_status == 0 : # no interrupts triggered
351
+ if motion_int_status == 0 : # no interrupts triggered
336
352
return False
337
353
338
- if self ._tap_count == 1 and motion_int_status & 1 << 5 :
354
+ if self ._tap_count == 1 and motion_int_status & 1 << 5 :
339
355
return True
340
- if self ._tap_count == 2 and motion_int_status & 1 << 4 :
356
+ if self ._tap_count == 2 and motion_int_status & 1 << 4 :
341
357
return True
342
358
343
359
return False
0 commit comments