6161__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LSM303_Accel.git"
6262
6363# 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)
6767
6868# Accelerometer registers
6969
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 )
8585_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 )
9090_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 )
9494_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 )
103103# note:: Tap related registers are called ``CLICK_`` in the datasheet
104104# 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
109109class Rate :
110110 """Options for `data_rate`"""
111+
111112 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+
121123
122124class Mode :
123125 """Options for `mode`"""
124- MODE_NORMAL = const (0 )
126+
127+ MODE_NORMAL = const (0 )
125128 MODE_HIGH_RESOLUTION = const (1 )
126- MODE_LOW_POWER = const (2 )
129+ MODE_LOW_POWER = const (2 )
130+
127131
128132class Range :
129133 """Options for `range`"""
134+
130135 RANGE_2G = const (0 )
131136 RANGE_4G = const (1 )
132137 RANGE_8G = const (2 )
133138 RANGE_16G = const (3 )
134139
140+
135141# pylint: enable=bad-whitespace,too-few-public-methods
136142
137- class LSM303_Accel : #pylint:disable=too-many-instance-attributes
143+
144+ class LSM303_Accel : # pylint:disable=too-many-instance-attributes
138145 """Driver for the LSM303's accelerometer."""
139146
140147 # Class-level buffer for reading and writing data with the sensor.
@@ -189,6 +196,7 @@ class LSM303_Accel: #pylint:disable=too-many-instance-attributes
189196 _tap_time_window = UnaryStruct (_REG_ACCEL_TIME_WINDOW_A , "B" )
190197
191198 _BUFFER = bytearray (6 )
199+
192200 def __init__ (self , i2c ):
193201 self ._accel_device = I2CDevice (i2c , _ADDRESS_ACCEL )
194202 self .i2c_device = self ._accel_device
@@ -203,8 +211,16 @@ def __init__(self, i2c):
203211 self ._cached_mode = 0
204212 self ._cached_range = 0
205213
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+ ):
208224 """
209225 The tap detection parameters.
210226
@@ -221,10 +237,11 @@ def set_tap(self, tap, threshold, *,
221237 """
222238
223239 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+ )
225243 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)" )
228245
229246 if tap == 0 and tap_cfg is None :
230247 # Disable click interrupt.
@@ -242,7 +259,7 @@ def set_tap(self, tap, threshold, *,
242259 # Or, if a custom tap configuration register value specified, use it.
243260 self ._tap_config = tap_cfg
244261
245- self ._tap_threshold = threshold # why and?
262+ self ._tap_threshold = threshold # why and?
246263 self ._tap_time_limit = time_limit
247264 self ._tap_time_latency = time_latency
248265 self ._tap_time_window = time_window
@@ -259,8 +276,10 @@ def tapped(self):
259276
260277 @property
261278 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 ])
264283
265284 @property
266285 def acceleration (self ):
@@ -279,14 +298,14 @@ def acceleration(self):
279298 def _scale_data (self , raw_measurement ):
280299 lsb , shift = self ._lsb_shift ()
281300
282- return (raw_measurement >> shift ) * lsb * _SMOLLER_GRAVITY
301+ return (raw_measurement >> shift ) * lsb * _SMOLLER_GRAVITY
283302
284- def _lsb_shift (self ): # pylint:disable=too-many-branches
303+ def _lsb_shift (self ): # pylint:disable=too-many-branches
285304 # the bit depth of the data depends on the mode, and the lsb value
286305 # depends on the mode and range
287- lsb = - 1 # the default, normal mode @ 2G
306+ lsb = - 1 # the default, normal mode @ 2G
288307
289- if self ._cached_mode is Mode .MODE_HIGH_RESOLUTION : # 12-bit
308+ if self ._cached_mode is Mode .MODE_HIGH_RESOLUTION : # 12-bit
290309 shift = 4
291310 if self ._cached_range is Range .RANGE_2G :
292311 lsb = 0.98
@@ -296,7 +315,7 @@ def _lsb_shift(self): #pylint:disable=too-many-branches
296315 lsb = 3.9
297316 elif self ._cached_range is Range .RANGE_16G :
298317 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
300319 shift = 6
301320 if self ._cached_range is Range .RANGE_2G :
302321 lsb = 3.9
@@ -307,8 +326,7 @@ def _lsb_shift(self): #pylint:disable=too-many-branches
307326 elif self ._cached_range is Range .RANGE_16G :
308327 lsb = 46.9
309328
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
312330 shift = 8
313331 if self ._cached_range is Range .RANGE_2G :
314332 lsb = 15.63
@@ -320,8 +338,10 @@ def _lsb_shift(self): #pylint:disable=too-many-branches
320338 lsb = 187.58
321339
322340 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+ )
325345 return (lsb , shift )
326346
327347 @property
@@ -360,14 +380,13 @@ def mode(self, value):
360380 if value < 0 or value > 2 :
361381 raise AttributeError ("mode must be a `Mode`" )
362382 self ._high_resolution = value & 0b01
363- self ._low_power = (value & 0b10 ) >> 1
383+ self ._low_power = (value & 0b10 ) >> 1
364384 self ._cached_mode = value
365385
366386 def _read_u8 (self , device , address ):
367387 with device as i2c :
368388 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 )
371390 return self ._BUFFER [0 ]
372391
373392 def _write_u8 (self , device , address , val ):
0 commit comments