43
43
44
44
# Register addresses:
45
45
# pylint: disable=bad-whitespace
46
- _REG_OUTADC1_L = const (0x08 )
47
- _REG_WHOAMI = const (0x0F )
48
- _REG_TEMPCFG = const (0x1F )
49
- _REG_CTRL1 = const (0x20 )
50
- _REG_CTRL3 = const (0x22 )
51
- _REG_CTRL4 = const (0x23 )
52
- _REG_CTRL5 = const (0x24 )
53
- _REG_OUT_X_L = const (0x28 )
54
- _REG_INT1SRC = const (0x31 )
55
- _REG_CLICKCFG = const (0x38 )
56
- _REG_CLICKSRC = const (0x39 )
57
- _REG_CLICKTHS = const (0x3A )
58
- _REG_TIMELIMIT = const (0x3B )
46
+ _REG_OUTADC1_L = const (0x08 )
47
+ _REG_WHOAMI = const (0x0F )
48
+ _REG_TEMPCFG = const (0x1F )
49
+ _REG_CTRL1 = const (0x20 )
50
+ _REG_CTRL3 = const (0x22 )
51
+ _REG_CTRL4 = const (0x23 )
52
+ _REG_CTRL5 = const (0x24 )
53
+ _REG_OUT_X_L = const (0x28 )
54
+ _REG_INT1SRC = const (0x31 )
55
+ _REG_CLICKCFG = const (0x38 )
56
+ _REG_CLICKSRC = const (0x39 )
57
+ _REG_CLICKTHS = const (0x3A )
58
+ _REG_TIMELIMIT = const (0x3B )
59
59
_REG_TIMELATENCY = const (0x3C )
60
- _REG_TIMEWINDOW = const (0x3D )
60
+ _REG_TIMEWINDOW = const (0x3D )
61
61
62
62
# Register value constants:
63
- RANGE_16_G = const (0b11 ) # +/- 16g
64
- RANGE_8_G = const (0b10 ) # +/- 8g
65
- RANGE_4_G = const (0b01 ) # +/- 4g
66
- RANGE_2_G = const (0b00 ) # +/- 2g (default value)
67
- DATARATE_1344_HZ = const (0b1001 ) # 1.344 KHz
68
- DATARATE_400_HZ = const (0b0111 ) # 400Hz
69
- DATARATE_200_HZ = const (0b0110 ) # 200Hz
70
- DATARATE_100_HZ = const (0b0101 ) # 100Hz
71
- DATARATE_50_HZ = const (0b0100 ) # 50Hz
72
- DATARATE_25_HZ = const (0b0011 ) # 25Hz
73
- DATARATE_10_HZ = const (0b0010 ) # 10 Hz
74
- DATARATE_1_HZ = const (0b0001 ) # 1 Hz
75
- DATARATE_POWERDOWN = const (0 )
76
- DATARATE_LOWPOWER_1K6HZ = const (0b1000 )
77
- DATARATE_LOWPOWER_5KHZ = const (0b1001 )
63
+ RANGE_16_G = const (0b11 ) # +/- 16g
64
+ RANGE_8_G = const (0b10 ) # +/- 8g
65
+ RANGE_4_G = const (0b01 ) # +/- 4g
66
+ RANGE_2_G = const (0b00 ) # +/- 2g (default value)
67
+ DATARATE_1344_HZ = const (0b1001 ) # 1.344 KHz
68
+ DATARATE_400_HZ = const (0b0111 ) # 400Hz
69
+ DATARATE_200_HZ = const (0b0110 ) # 200Hz
70
+ DATARATE_100_HZ = const (0b0101 ) # 100Hz
71
+ DATARATE_50_HZ = const (0b0100 ) # 50Hz
72
+ DATARATE_25_HZ = const (0b0011 ) # 25Hz
73
+ DATARATE_10_HZ = const (0b0010 ) # 10 Hz
74
+ DATARATE_1_HZ = const (0b0001 ) # 1 Hz
75
+ DATARATE_POWERDOWN = const (0 )
76
+ DATARATE_LOWPOWER_1K6HZ = const (0b1000 )
77
+ DATARATE_LOWPOWER_5KHZ = const (0b1001 )
78
78
79
79
# Other constants
80
80
STANDARD_GRAVITY = 9.806
86
86
87
87
class LIS3DH :
88
88
"""Driver base for the LIS3DH accelerometer."""
89
+
89
90
def __init__ (self , int1 = None , int2 = None ):
90
91
# Check device ID.
91
92
device_id = self ._read_register_byte (_REG_WHOAMI )
92
93
if device_id != 0x33 :
93
- raise RuntimeError (' Failed to find LIS3DH!' )
94
+ raise RuntimeError (" Failed to find LIS3DH!" )
94
95
# Reboot
95
96
self ._write_register_byte (_REG_CTRL5 , 0x80 )
96
97
time .sleep (0.01 ) # takes 5ms
@@ -156,7 +157,7 @@ def acceleration(self):
156
157
elif accel_range == RANGE_2_G :
157
158
divider = 16380
158
159
159
- x , y , z = struct .unpack (' <hhh' , self ._read_register (_REG_OUT_X_L | 0x80 , 6 ))
160
+ x , y , z = struct .unpack (" <hhh" , self ._read_register (_REG_OUT_X_L | 0x80 , 6 ))
160
161
161
162
# convert from Gs to m / s ^ 2 and adjust for the range
162
163
x = (x / divider ) * STANDARD_GRAVITY
@@ -201,12 +202,13 @@ def read_adc_raw(self, adc):
201
202
value 1, 2, or 3.
202
203
"""
203
204
if adc < 1 or adc > 3 :
204
- raise ValueError (' ADC must be a value 1 to 3!' )
205
+ raise ValueError (" ADC must be a value 1 to 3!" )
205
206
206
- return struct .unpack ('<h' ,
207
- self ._read_register ((_REG_OUTADC1_L + ((adc - 1 )* 2 )) | 0x80 , 2 )[0 :2 ])[0 ]
207
+ return struct .unpack (
208
+ "<h" , self ._read_register ((_REG_OUTADC1_L + ((adc - 1 ) * 2 )) | 0x80 , 2 )[0 :2 ]
209
+ )[0 ]
208
210
209
- def read_adc_mV (self , adc ): # pylint: disable=invalid-name
211
+ def read_adc_mV (self , adc ): # pylint: disable=invalid-name
210
212
"""Read the specified analog to digital converter value in millivolts.
211
213
ADC must be a value 1, 2, or 3. NOTE the ADC can only measure voltages
212
214
in the range of ~900-1200mV!
@@ -222,7 +224,7 @@ def read_adc_mV(self, adc): # pylint: disable=invalid-name
222
224
# x1 = 32512
223
225
# y0 = 1800
224
226
# y1 = 900
225
- return 1800 + (raw + 32512 )* (- 900 / 65024 )
227
+ return 1800 + (raw + 32512 ) * (- 900 / 65024 )
226
228
227
229
@property
228
230
def tapped (self ):
@@ -250,8 +252,16 @@ def tapped(self):
250
252
raw = self ._read_register_byte (_REG_CLICKSRC )
251
253
return raw & 0x40 > 0
252
254
253
- def set_tap (self , tap , threshold , * ,
254
- time_limit = 10 , time_latency = 20 , time_window = 255 , click_cfg = None ):
255
+ def set_tap (
256
+ self ,
257
+ tap ,
258
+ threshold ,
259
+ * ,
260
+ time_limit = 10 ,
261
+ time_latency = 20 ,
262
+ time_window = 255 ,
263
+ click_cfg = None
264
+ ):
255
265
"""
256
266
The tap detection parameters.
257
267
@@ -271,9 +281,11 @@ def set_tap(self, tap, threshold, *,
271
281
:param int click_cfg: CLICK_CFG register value.
272
282
"""
273
283
if (tap < 0 or tap > 2 ) and click_cfg is None :
274
- raise ValueError ('Tap must be 0 (disabled), 1 (single tap), or 2 (double tap)!' )
284
+ raise ValueError (
285
+ "Tap must be 0 (disabled), 1 (single tap), or 2 (double tap)!"
286
+ )
275
287
if threshold > 127 or threshold < 0 :
276
- raise ValueError (' Threshold out of range (0-127)' )
288
+ raise ValueError (" Threshold out of range (0-127)" )
277
289
278
290
ctrl3 = self ._read_register_byte (_REG_CTRL3 )
279
291
if tap == 0 and click_cfg is None :
@@ -315,7 +327,8 @@ class LIS3DH_I2C(LIS3DH):
315
327
"""Driver for the LIS3DH accelerometer connected over I2C."""
316
328
317
329
def __init__ (self , i2c , * , address = 0x18 , int1 = None , int2 = None ):
318
- import adafruit_bus_device .i2c_device as i2c_device
330
+ import adafruit_bus_device .i2c_device as i2c_device # pylint: disable=import-outside-toplevel
331
+
319
332
self ._i2c = i2c_device .I2CDevice (i2c , address )
320
333
self ._buffer = bytearray (6 )
321
334
super ().__init__ (int1 = int1 , int2 = int2 )
@@ -338,7 +351,8 @@ class LIS3DH_SPI(LIS3DH):
338
351
"""Driver for the LIS3DH accelerometer connected over SPI."""
339
352
340
353
def __init__ (self , spi , cs , * , baudrate = 100000 , int1 = None , int2 = None ):
341
- import adafruit_bus_device .spi_device as spi_device
354
+ import adafruit_bus_device .spi_device as spi_device # pylint: disable=import-outside-toplevel
355
+
342
356
self ._spi = spi_device .SPIDevice (spi , cs , baudrate = baudrate )
343
357
self ._buffer = bytearray (6 )
344
358
super ().__init__ (int1 = int1 , int2 = int2 )
@@ -349,12 +363,12 @@ def _read_register(self, register, length):
349
363
else :
350
364
self ._buffer [0 ] = (register | 0xC0 ) & 0xFF # Read multiple, bit 6&7 high.
351
365
with self ._spi as spi :
352
- spi .write (self ._buffer , start = 0 , end = 1 ) # pylint: disable=no-member
353
- spi .readinto (self ._buffer , start = 0 , end = length ) # pylint: disable=no-member
366
+ spi .write (self ._buffer , start = 0 , end = 1 ) # pylint: disable=no-member
367
+ spi .readinto (self ._buffer , start = 0 , end = length ) # pylint: disable=no-member
354
368
return self ._buffer
355
369
356
370
def _write_register_byte (self , register , value ):
357
371
self ._buffer [0 ] = register & 0x7F # Write, bit 7 low.
358
372
self ._buffer [1 ] = value & 0xFF
359
373
with self ._spi as spi :
360
- spi .write (self ._buffer , start = 0 , end = 2 ) # pylint: disable=no-member
374
+ spi .write (self ._buffer , start = 0 , end = 2 ) # pylint: disable=no-member
0 commit comments