100
100
_REG_ACCEL_TIME_WINDOW_A = const (0x3D )
101
101
_REG_ACCEL_ACT_THS_A = const (0x3E )
102
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
105
_LSM303ACCEL_MG_LSB = 16704.0 # magic!
106
106
_GRAVITY_STANDARD = 9.80665 # Earth's gravity in m/s^2
107
107
_SMOLLER_GRAVITY = 0.00980665
108
-
108
+ #pylint:disable=too-few-public-methods
109
109
class Rate :
110
+ """Options for `data_rate`"""
110
111
RATE_SHUTDOWN = const (0 )
111
112
RATE_1_HZ = const (1 )
112
113
RATE_10_HZ = const (2 )
@@ -131,9 +132,9 @@ class Range:
131
132
RANGE_8G = const (2 )
132
133
RANGE_16G = const (3 )
133
134
134
- # pylint: enable=bad-whitespace
135
+ # pylint: enable=bad-whitespace,too-few-public-methods
135
136
136
- class LSM303_Accel :
137
+ class LSM303_Accel : #pylint:disable=too-many-instance-attributes
137
138
"""Driver for the LSM303's accelerometer."""
138
139
139
140
# Class-level buffer for reading and writing data with the sensor.
@@ -151,7 +152,22 @@ class LSM303_Accel:
151
152
152
153
_act_threshold = UnaryStruct (_REG_ACCEL_ACT_THS_A , "B" )
153
154
_act_duration = UnaryStruct (_REG_ACCEL_ACT_DUR_A , "B" )
155
+ """
156
+ .. code-block:: python
157
+
158
+ import board
159
+ i2c = board.I2C()
154
160
161
+ import adafruit_lsm303_accel
162
+ accel = adafruit_lsm303_accel.LSM303_Accel(i2c)
163
+
164
+ accel._act_threshold = 20
165
+ accel._act_duration = 1
166
+ accel._int2_activity_enable = True
167
+
168
+ # toggle pins, defaults to False
169
+ accel._int_pin_active_low = True
170
+ """
155
171
_data_rate = RWBits (4 , _REG_ACCEL_CTRL_REG1_A , 4 )
156
172
_enable_xyz = RWBits (3 , _REG_ACCEL_CTRL_REG1_A , 0 )
157
173
_raw_accel_data = StructArray (_REG_ACCEL_OUT_X_L_A , "<h" , 3 )
@@ -161,8 +177,6 @@ class LSM303_Accel:
161
177
162
178
_range = RWBits (2 , _REG_ACCEL_CTRL_REG4_A , 4 )
163
179
164
- # filter _REG_ACCEL_CTRL_REG2_A
165
- # CTRL_REG5_A boot (7)
166
180
_int1_src = UnaryStruct (_REG_ACCEL_INT1_SOURCE_A , "B" )
167
181
_tap_src = UnaryStruct (_REG_ACCEL_CLICK_SRC_A , "B" )
168
182
@@ -174,13 +188,10 @@ class LSM303_Accel:
174
188
_tap_time_latency = UnaryStruct (_REG_ACCEL_TIME_LATENCY_A , "B" )
175
189
_tap_time_window = UnaryStruct (_REG_ACCEL_TIME_WINDOW_A , "B" )
176
190
177
-
178
-
179
191
_BUFFER = bytearray (6 )
180
192
def __init__ (self , i2c ):
181
193
self ._accel_device = I2CDevice (i2c , _ADDRESS_ACCEL )
182
194
self .i2c_device = self ._accel_device
183
- #self._write_u8(self._accel_device, _REG_ACCEL_CTRL_REG1_A, 0x27) # Enable the accelerometer
184
195
self ._data_rate = 2
185
196
self ._enable_xyz = 0b111
186
197
self ._int1_latching = True
@@ -196,18 +207,19 @@ def set_tap(self, tap, threshold, *,
196
207
time_limit = 10 , time_latency = 20 , time_window = 255 , tap_cfg = None ):
197
208
"""
198
209
The tap detection parameters.
199
- .. note:: Tap related registers are called ``CLICK_`` in the datasheet.
200
- :param int tap: 0 to disable tap detection, 1 to detect only single
201
- taps, and 2 to detect only double taps.
202
- :param int threshold: A threshold for the tap detection. The higher the value
203
- the less sensitive the detection. This changes based on
204
- the accelerometer range. Good values are 5-10 for 16G,
205
- 10-20 for 8G, 20-40 for 4G, and 40-80 for 2G.
210
+
211
+ :param int tap: 0 to disable tap detection, 1 to detect only single taps, and 2 to detect \
212
+ only double taps.
213
+ :param int threshold: A threshold for the tap detection. The higher the value the less\
214
+ sensitive the detection. This changes based on the accelerometer range. Good values\
215
+ are 5-10 for 16G, 10-20 for 8G, 20-40 for 4G, and 40-80 for 2G.
206
216
:param int time_limit: TIME_LIMIT register value (default 10).
207
217
:param int time_latency: TIME_LATENCY register value (default 20).
208
218
:param int time_window: TIME_WINDOW register value (default 255).
209
219
:param int click_cfg: CLICK_CFG register value.
220
+
210
221
"""
222
+
211
223
if (tap < 0 or tap > 2 ) and tap_cfg is None :
212
224
raise ValueError ('Tap must be 0 (disabled), 1 (single tap), or 2 (double tap)!' )
213
225
if threshold > 127 or threshold < 0 :
@@ -240,39 +252,23 @@ def tapped(self):
240
252
"""
241
253
True if a tap was detected recently. Whether its a single tap or double tap is
242
254
determined by the tap param on ``set_tap``. ``tapped`` may be True over
243
- multiple reads even if only a single tap or single double tap occurred if the
244
- interrupt (int) pin is not specified.
245
- The following example uses ``i2c`` and specifies the interrupt pin:
246
- .. code-block:: python
247
- import adafruit_lis3dh
248
- import digitalio
249
- i2c = busio.I2C(board.SCL, board.SDA)
250
- int1 = digitalio.DigitalInOut(board.D11) # pin connected to interrupt
251
- lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, int1=int1)
252
- lis3dh.range = adafruit_lis3dh.RANGE_8_G
255
+ multiple reads even if only a single tap or single double tap occurred.
253
256
"""
254
257
tap_src = self ._tap_src
255
- # print("tap_src: %s"%bin(tap_src))
256
- # print("int1_src: %s"%bin(self._int1_src))
257
- # if tap_src & 0b1000000 > 0:
258
- # print("TAPPED!")
259
258
return tap_src & 0b1000000 > 0
260
259
261
260
@property
262
- def raw_acceleration (self ):
263
- """The raw accelerometer sensor values.
264
- A 3-tuple of X, Y, Z axis values that are 16-bit signed integers.
265
- """
261
+ def _raw_acceleration (self ):
266
262
self ._read_bytes (self ._accel_device , _REG_ACCEL_OUT_X_L_A | 0x80 , 6 , self ._BUFFER )
267
263
return struct .unpack_from ('<hhh' , self ._BUFFER [0 :6 ])
268
264
269
265
@property
270
266
def acceleration (self ):
271
- """The processed accelerometer sensor values.
272
- A 3-tuple of X, Y, Z axis values in meters per second squared that are signed floats.
267
+ """The measured accelerometer sensor values.
268
+ A 3-tuple of X, Y, Z axis values in m/s^2 squared that are signed floats.
273
269
"""
274
270
275
- raw_accel_data = self .raw_acceleration
271
+ raw_accel_data = self ._raw_acceleration
276
272
277
273
x = self ._scale_data (raw_accel_data [0 ])
278
274
y = self ._scale_data (raw_accel_data [1 ])
@@ -285,7 +281,7 @@ def _scale_data(self, raw_measurement):
285
281
286
282
return (raw_measurement >> shift ) * lsb * _SMOLLER_GRAVITY
287
283
288
- def _lsb_shift (self ):
284
+ def _lsb_shift (self ): #pylint:disable=too-many-branches
289
285
# the bit depth of the data depends on the mode, and the lsb value
290
286
# depends on the mode and range
291
287
lsb = - 1 # the default, normal mode @ 2G
@@ -325,7 +321,7 @@ def _lsb_shift(self):
325
321
326
322
if lsb is - 1 :
327
323
raise AttributeError ("'impossible' range or mode detected: range: %d mode: %d" %
328
- (self ._cached_range , self ._cached_mode ))
324
+ (self ._cached_range , self ._cached_mode ))
329
325
return (lsb , shift )
330
326
331
327
@property
@@ -348,7 +344,7 @@ def range(self):
348
344
349
345
@range .setter
350
346
def range (self , value ):
351
- if value < 0 or value > 3 :
347
+ if value < 0 or value > 3 :
352
348
raise AttributeError ("range must be a `Range`" )
353
349
self ._range = value
354
350
self ._cached_range = value
0 commit comments