61
61
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LSM303_Accel.git"
62
62
63
63
# pylint: disable=bad-whitespace
64
- _ADDRESS_ACCEL = const (0x19 ) # (0x32 >> 1) // 0011001x
65
- _ADDRESS_MAG = const (0x1E ) # (0x3C >> 1) // 0011110x
66
- _ID = const (0xD4 ) # (0b11010100)
64
+ _ADDRESS_ACCEL = const (0x19 ) # (0x32 >> 1) // 0011001x
65
+ _ADDRESS_MAG = const (0x1E ) # (0x3C >> 1) // 0011110x
66
+ _ID = const (0xD4 ) # (0b11010100)
67
67
68
68
# Accelerometer registers
69
69
70
- _REG_ACCEL_WHO_AM_I = const (0x0F )
71
- _REG_ACCEL_CTRL_REG1_A = const (0x20 )
72
- _REG_ACCEL_CTRL_REG2_A = const (0x21 )
73
- _REG_ACCEL_CTRL_REG3_A = const (0x22 )
74
- _REG_ACCEL_CTRL_REG4_A = const (0x23 )
75
- _REG_ACCEL_CTRL_REG5_A = const (0x24 )
76
- _REG_ACCEL_CTRL_REG6_A = const (0x25 )
77
- _REG_ACCEL_REFERENCE_A = const (0x26 )
78
- _REG_ACCEL_STATUS_REG_A = const (0x27 )
79
- _REG_ACCEL_OUT_X_L_A = const (0x28 )
80
- _REG_ACCEL_OUT_X_H_A = const (0x29 )
81
- _REG_ACCEL_OUT_Y_L_A = const (0x2A )
82
- _REG_ACCEL_OUT_Y_H_A = const (0x2B )
83
- _REG_ACCEL_OUT_Z_L_A = const (0x2C )
84
- _REG_ACCEL_OUT_Z_H_A = const (0x2D )
70
+ _REG_ACCEL_WHO_AM_I = const (0x0F )
71
+ _REG_ACCEL_CTRL_REG1_A = const (0x20 )
72
+ _REG_ACCEL_CTRL_REG2_A = const (0x21 )
73
+ _REG_ACCEL_CTRL_REG3_A = const (0x22 )
74
+ _REG_ACCEL_CTRL_REG4_A = const (0x23 )
75
+ _REG_ACCEL_CTRL_REG5_A = const (0x24 )
76
+ _REG_ACCEL_CTRL_REG6_A = const (0x25 )
77
+ _REG_ACCEL_REFERENCE_A = const (0x26 )
78
+ _REG_ACCEL_STATUS_REG_A = const (0x27 )
79
+ _REG_ACCEL_OUT_X_L_A = const (0x28 )
80
+ _REG_ACCEL_OUT_X_H_A = const (0x29 )
81
+ _REG_ACCEL_OUT_Y_L_A = const (0x2A )
82
+ _REG_ACCEL_OUT_Y_H_A = const (0x2B )
83
+ _REG_ACCEL_OUT_Z_L_A = const (0x2C )
84
+ _REG_ACCEL_OUT_Z_H_A = const (0x2D )
85
85
_REG_ACCEL_FIFO_CTRL_REG_A = const (0x2E )
86
- _REG_ACCEL_FIFO_SRC_REG_A = const (0x2F )
87
- _REG_ACCEL_INT1_CFG_A = const (0x30 )
88
- _REG_ACCEL_INT1_SOURCE_A = const (0x31 )
89
- _REG_ACCEL_INT1_THS_A = const (0x32 )
86
+ _REG_ACCEL_FIFO_SRC_REG_A = const (0x2F )
87
+ _REG_ACCEL_INT1_CFG_A = const (0x30 )
88
+ _REG_ACCEL_INT1_SOURCE_A = const (0x31 )
89
+ _REG_ACCEL_INT1_THS_A = const (0x32 )
90
90
_REG_ACCEL_INT1_DURATION_A = const (0x33 )
91
- _REG_ACCEL_INT2_CFG_A = const (0x34 )
92
- _REG_ACCEL_INT2_SOURCE_A = const (0x35 )
93
- _REG_ACCEL_INT2_THS_A = const (0x36 )
91
+ _REG_ACCEL_INT2_CFG_A = const (0x34 )
92
+ _REG_ACCEL_INT2_SOURCE_A = const (0x35 )
93
+ _REG_ACCEL_INT2_THS_A = const (0x36 )
94
94
_REG_ACCEL_INT2_DURATION_A = const (0x37 )
95
- _REG_ACCEL_CLICK_CFG_A = const (0x38 )
96
- _REG_ACCEL_CLICK_SRC_A = const (0x39 )
97
- _REG_ACCEL_CLICK_THS_A = const (0x3A )
98
- _REG_ACCEL_TIME_LIMIT_A = const (0x3B )
99
- _REG_ACCEL_TIME_LATENCY_A = const (0x3C )
100
- _REG_ACCEL_TIME_WINDOW_A = const (0x3D )
101
- _REG_ACCEL_ACT_THS_A = const (0x3E )
102
- _REG_ACCEL_ACT_DUR_A = const (0x3F )
95
+ _REG_ACCEL_CLICK_CFG_A = const (0x38 )
96
+ _REG_ACCEL_CLICK_SRC_A = const (0x39 )
97
+ _REG_ACCEL_CLICK_THS_A = const (0x3A )
98
+ _REG_ACCEL_TIME_LIMIT_A = const (0x3B )
99
+ _REG_ACCEL_TIME_LATENCY_A = const (0x3C )
100
+ _REG_ACCEL_TIME_WINDOW_A = const (0x3D )
101
+ _REG_ACCEL_ACT_THS_A = const (0x3E )
102
+ _REG_ACCEL_ACT_DUR_A = const (0x3F )
103
103
# note:: Tap related registers are called ``CLICK_`` in the datasheet
104
104
# Conversion constants
105
- _LSM303ACCEL_MG_LSB = 16704.0 # magic!
106
- _GRAVITY_STANDARD = 9.80665 # Earth's gravity in m/s^2
107
- _SMOLLER_GRAVITY = 0.00980665
108
- #pylint:disable=too-few-public-methods
105
+ _LSM303ACCEL_MG_LSB = 16704.0 # magic!
106
+ _GRAVITY_STANDARD = 9.80665 # Earth's gravity in m/s^2
107
+ _SMOLLER_GRAVITY = 0.00980665
108
+ # pylint:disable=too-few-public-methods
109
109
class Rate :
110
110
"""Options for `data_rate`"""
111
+
111
112
RATE_SHUTDOWN = const (0 )
112
- RATE_1_HZ = const (1 )
113
- RATE_10_HZ = const (2 )
114
- RATE_25_HZ = const (3 )
115
- RATE_50_HZ = const (4 )
116
- RATE_100_HZ = const (5 )
117
- RATE_200_HZ = const (6 )
118
- RATE_400_HZ = const (7 )
119
- RATE_1620_HZ = const (8 )
120
- RATE_1344_HZ = const (9 )
113
+ RATE_1_HZ = const (1 )
114
+ RATE_10_HZ = const (2 )
115
+ RATE_25_HZ = const (3 )
116
+ RATE_50_HZ = const (4 )
117
+ RATE_100_HZ = const (5 )
118
+ RATE_200_HZ = const (6 )
119
+ RATE_400_HZ = const (7 )
120
+ RATE_1620_HZ = const (8 )
121
+ RATE_1344_HZ = const (9 )
122
+
121
123
122
124
class Mode :
123
125
"""Options for `mode`"""
124
- MODE_NORMAL = const (0 )
126
+
127
+ MODE_NORMAL = const (0 )
125
128
MODE_HIGH_RESOLUTION = const (1 )
126
- MODE_LOW_POWER = const (2 )
129
+ MODE_LOW_POWER = const (2 )
130
+
127
131
128
132
class Range :
129
133
"""Options for `range`"""
134
+
130
135
RANGE_2G = const (0 )
131
136
RANGE_4G = const (1 )
132
137
RANGE_8G = const (2 )
133
138
RANGE_16G = const (3 )
134
139
140
+
135
141
# pylint: enable=bad-whitespace,too-few-public-methods
136
142
137
- class LSM303_Accel : #pylint:disable=too-many-instance-attributes
143
+
144
+ class LSM303_Accel : # pylint:disable=too-many-instance-attributes
138
145
"""Driver for the LSM303's accelerometer."""
139
146
140
147
# Class-level buffer for reading and writing data with the sensor.
@@ -189,6 +196,7 @@ class LSM303_Accel: #pylint:disable=too-many-instance-attributes
189
196
_tap_time_window = UnaryStruct (_REG_ACCEL_TIME_WINDOW_A , "B" )
190
197
191
198
_BUFFER = bytearray (6 )
199
+
192
200
def __init__ (self , i2c ):
193
201
self ._accel_device = I2CDevice (i2c , _ADDRESS_ACCEL )
194
202
self .i2c_device = self ._accel_device
@@ -203,8 +211,16 @@ def __init__(self, i2c):
203
211
self ._cached_mode = 0
204
212
self ._cached_range = 0
205
213
206
- def set_tap (self , tap , threshold , * ,
207
- time_limit = 10 , time_latency = 20 , time_window = 255 , tap_cfg = None ):
214
+ def set_tap (
215
+ self ,
216
+ tap ,
217
+ threshold ,
218
+ * ,
219
+ time_limit = 10 ,
220
+ time_latency = 20 ,
221
+ time_window = 255 ,
222
+ tap_cfg = None
223
+ ):
208
224
"""
209
225
The tap detection parameters.
210
226
@@ -221,10 +237,11 @@ def set_tap(self, tap, threshold, *,
221
237
"""
222
238
223
239
if (tap < 0 or tap > 2 ) and tap_cfg is None :
224
- raise ValueError ('Tap must be 0 (disabled), 1 (single tap), or 2 (double tap)!' )
240
+ raise ValueError (
241
+ "Tap must be 0 (disabled), 1 (single tap), or 2 (double tap)!"
242
+ )
225
243
if threshold > 127 or threshold < 0 :
226
- raise ValueError ('Threshold out of range (0-127)' )
227
-
244
+ raise ValueError ("Threshold out of range (0-127)" )
228
245
229
246
if tap == 0 and tap_cfg is None :
230
247
# Disable click interrupt.
@@ -242,7 +259,7 @@ def set_tap(self, tap, threshold, *,
242
259
# Or, if a custom tap configuration register value specified, use it.
243
260
self ._tap_config = tap_cfg
244
261
245
- self ._tap_threshold = threshold # why and?
262
+ self ._tap_threshold = threshold # why and?
246
263
self ._tap_time_limit = time_limit
247
264
self ._tap_time_latency = time_latency
248
265
self ._tap_time_window = time_window
@@ -259,8 +276,10 @@ def tapped(self):
259
276
260
277
@property
261
278
def _raw_acceleration (self ):
262
- self ._read_bytes (self ._accel_device , _REG_ACCEL_OUT_X_L_A | 0x80 , 6 , self ._BUFFER )
263
- return struct .unpack_from ('<hhh' , self ._BUFFER [0 :6 ])
279
+ self ._read_bytes (
280
+ self ._accel_device , _REG_ACCEL_OUT_X_L_A | 0x80 , 6 , self ._BUFFER
281
+ )
282
+ return struct .unpack_from ("<hhh" , self ._BUFFER [0 :6 ])
264
283
265
284
@property
266
285
def acceleration (self ):
@@ -279,14 +298,14 @@ def acceleration(self):
279
298
def _scale_data (self , raw_measurement ):
280
299
lsb , shift = self ._lsb_shift ()
281
300
282
- return (raw_measurement >> shift ) * lsb * _SMOLLER_GRAVITY
301
+ return (raw_measurement >> shift ) * lsb * _SMOLLER_GRAVITY
283
302
284
- def _lsb_shift (self ): # pylint:disable=too-many-branches
303
+ def _lsb_shift (self ): # pylint:disable=too-many-branches
285
304
# the bit depth of the data depends on the mode, and the lsb value
286
305
# depends on the mode and range
287
- lsb = - 1 # the default, normal mode @ 2G
306
+ lsb = - 1 # the default, normal mode @ 2G
288
307
289
- if self ._cached_mode is Mode .MODE_HIGH_RESOLUTION : # 12-bit
308
+ if self ._cached_mode is Mode .MODE_HIGH_RESOLUTION : # 12-bit
290
309
shift = 4
291
310
if self ._cached_range is Range .RANGE_2G :
292
311
lsb = 0.98
@@ -296,7 +315,7 @@ def _lsb_shift(self): #pylint:disable=too-many-branches
296
315
lsb = 3.9
297
316
elif self ._cached_range is Range .RANGE_16G :
298
317
lsb = 11.72
299
- elif self ._cached_mode is Mode .MODE_NORMAL : # 10-bit
318
+ elif self ._cached_mode is Mode .MODE_NORMAL : # 10-bit
300
319
shift = 6
301
320
if self ._cached_range is Range .RANGE_2G :
302
321
lsb = 3.9
@@ -307,8 +326,7 @@ def _lsb_shift(self): #pylint:disable=too-many-branches
307
326
elif self ._cached_range is Range .RANGE_16G :
308
327
lsb = 46.9
309
328
310
-
311
- elif self ._cached_mode is Mode .MODE_LOW_POWER : # 8-bit
329
+ elif self ._cached_mode is Mode .MODE_LOW_POWER : # 8-bit
312
330
shift = 8
313
331
if self ._cached_range is Range .RANGE_2G :
314
332
lsb = 15.63
@@ -320,8 +338,10 @@ def _lsb_shift(self): #pylint:disable=too-many-branches
320
338
lsb = 187.58
321
339
322
340
if lsb is - 1 :
323
- raise AttributeError ("'impossible' range or mode detected: range: %d mode: %d" %
324
- (self ._cached_range , self ._cached_mode ))
341
+ raise AttributeError (
342
+ "'impossible' range or mode detected: range: %d mode: %d"
343
+ % (self ._cached_range , self ._cached_mode )
344
+ )
325
345
return (lsb , shift )
326
346
327
347
@property
@@ -360,14 +380,13 @@ def mode(self, value):
360
380
if value < 0 or value > 2 :
361
381
raise AttributeError ("mode must be a `Mode`" )
362
382
self ._high_resolution = value & 0b01
363
- self ._low_power = (value & 0b10 ) >> 1
383
+ self ._low_power = (value & 0b10 ) >> 1
364
384
self ._cached_mode = value
365
385
366
386
def _read_u8 (self , device , address ):
367
387
with device as i2c :
368
388
self ._BUFFER [0 ] = address & 0xFF
369
- i2c .write_then_readinto (self ._BUFFER , self ._BUFFER ,
370
- out_end = 1 , in_end = 1 )
389
+ i2c .write_then_readinto (self ._BUFFER , self ._BUFFER , out_end = 1 , in_end = 1 )
371
390
return self ._BUFFER [0 ]
372
391
373
392
def _write_u8 (self , device , address , val ):
0 commit comments