4343
4444# Register addresses:
4545# 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 )
5959_REG_TIMELATENCY = const (0x3C )
60- _REG_TIMEWINDOW = const (0x3D )
60+ _REG_TIMEWINDOW = const (0x3D )
6161
6262# 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 )
7878
7979# Other constants
8080STANDARD_GRAVITY = 9.806
8686
8787class LIS3DH :
8888 """Driver base for the LIS3DH accelerometer."""
89+
8990 def __init__ (self , int1 = None , int2 = None ):
9091 # Check device ID.
9192 device_id = self ._read_register_byte (_REG_WHOAMI )
9293 if device_id != 0x33 :
93- raise RuntimeError (' Failed to find LIS3DH!' )
94+ raise RuntimeError (" Failed to find LIS3DH!" )
9495 # Reboot
9596 self ._write_register_byte (_REG_CTRL5 , 0x80 )
9697 time .sleep (0.01 ) # takes 5ms
@@ -156,7 +157,7 @@ def acceleration(self):
156157 elif accel_range == RANGE_2_G :
157158 divider = 16380
158159
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 ))
160161
161162 # convert from Gs to m / s ^ 2 and adjust for the range
162163 x = (x / divider ) * STANDARD_GRAVITY
@@ -201,12 +202,13 @@ def read_adc_raw(self, adc):
201202 value 1, 2, or 3.
202203 """
203204 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!" )
205206
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 ]
208210
209- def read_adc_mV (self , adc ): # pylint: disable=invalid-name
211+ def read_adc_mV (self , adc ): # pylint: disable=invalid-name
210212 """Read the specified analog to digital converter value in millivolts.
211213 ADC must be a value 1, 2, or 3. NOTE the ADC can only measure voltages
212214 in the range of ~900-1200mV!
@@ -222,7 +224,7 @@ def read_adc_mV(self, adc): # pylint: disable=invalid-name
222224 # x1 = 32512
223225 # y0 = 1800
224226 # y1 = 900
225- return 1800 + (raw + 32512 )* (- 900 / 65024 )
227+ return 1800 + (raw + 32512 ) * (- 900 / 65024 )
226228
227229 @property
228230 def tapped (self ):
@@ -250,8 +252,16 @@ def tapped(self):
250252 raw = self ._read_register_byte (_REG_CLICKSRC )
251253 return raw & 0x40 > 0
252254
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+ ):
255265 """
256266 The tap detection parameters.
257267
@@ -271,9 +281,11 @@ def set_tap(self, tap, threshold, *,
271281 :param int click_cfg: CLICK_CFG register value.
272282 """
273283 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+ )
275287 if threshold > 127 or threshold < 0 :
276- raise ValueError (' Threshold out of range (0-127)' )
288+ raise ValueError (" Threshold out of range (0-127)" )
277289
278290 ctrl3 = self ._read_register_byte (_REG_CTRL3 )
279291 if tap == 0 and click_cfg is None :
@@ -315,7 +327,8 @@ class LIS3DH_I2C(LIS3DH):
315327 """Driver for the LIS3DH accelerometer connected over I2C."""
316328
317329 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+
319332 self ._i2c = i2c_device .I2CDevice (i2c , address )
320333 self ._buffer = bytearray (6 )
321334 super ().__init__ (int1 = int1 , int2 = int2 )
@@ -338,7 +351,8 @@ class LIS3DH_SPI(LIS3DH):
338351 """Driver for the LIS3DH accelerometer connected over SPI."""
339352
340353 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+
342356 self ._spi = spi_device .SPIDevice (spi , cs , baudrate = baudrate )
343357 self ._buffer = bytearray (6 )
344358 super ().__init__ (int1 = int1 , int2 = int2 )
@@ -349,12 +363,12 @@ def _read_register(self, register, length):
349363 else :
350364 self ._buffer [0 ] = (register | 0xC0 ) & 0xFF # Read multiple, bit 6&7 high.
351365 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
354368 return self ._buffer
355369
356370 def _write_register_byte (self , register , value ):
357371 self ._buffer [0 ] = register & 0x7F # Write, bit 7 low.
358372 self ._buffer [1 ] = value & 0xFF
359373 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