Skip to content

Commit 54e8fca

Browse files
committed
linting, travising, doxing
1 parent 16aaf9b commit 54e8fca

File tree

7 files changed

+54
-54
lines changed

7 files changed

+54
-54
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ bundles
99
*.DS_Store
1010
.eggs
1111
dist
12-
**/*.egg-info
12+
**/*.egg-info
13+
.vscode/
14+

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ This driver depends on:
2222

2323
* `Adafruit CircuitPython <https://github.com/adafruit/circuitpython>`_
2424
* `Bus Device <https://github.com/adafruit/Adafruit_CircuitPython_BusDevice>`_
25+
* `Register <https://github.com/adafruit/Adafruit_CircuitPython_Register>`_
2526

2627
Please ensure all dependencies are available on the CircuitPython filesystem.
2728
This is easily achieved by downloading

adafruit_lsm303_accel.py

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,14 @@
100100
_REG_ACCEL_TIME_WINDOW_A = const(0x3D)
101101
_REG_ACCEL_ACT_THS_A = const(0x3E)
102102
_REG_ACCEL_ACT_DUR_A = const(0x3F)
103-
103+
# note:: Tap related registers are called ``CLICK_`` in the datasheet
104104
# Conversion constants
105105
_LSM303ACCEL_MG_LSB = 16704.0 # magic!
106106
_GRAVITY_STANDARD = 9.80665 # Earth's gravity in m/s^2
107107
_SMOLLER_GRAVITY = 0.00980665
108-
108+
#pylint:disable=too-few-public-methods
109109
class Rate:
110+
"""Options for `data_rate`"""
110111
RATE_SHUTDOWN = const(0)
111112
RATE_1_HZ = const(1)
112113
RATE_10_HZ = const(2)
@@ -131,9 +132,9 @@ class Range:
131132
RANGE_8G = const(2)
132133
RANGE_16G = const(3)
133134

134-
# pylint: enable=bad-whitespace
135+
# pylint: enable=bad-whitespace,too-few-public-methods
135136

136-
class LSM303_Accel:
137+
class LSM303_Accel: #pylint:disable=too-many-instance-attributes
137138
"""Driver for the LSM303's accelerometer."""
138139

139140
# Class-level buffer for reading and writing data with the sensor.
@@ -151,7 +152,22 @@ class LSM303_Accel:
151152

152153
_act_threshold = UnaryStruct(_REG_ACCEL_ACT_THS_A, "B")
153154
_act_duration = UnaryStruct(_REG_ACCEL_ACT_DUR_A, "B")
155+
"""
156+
.. code-block:: python
157+
158+
import board
159+
i2c = board.I2C()
154160
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+
"""
155171
_data_rate = RWBits(4, _REG_ACCEL_CTRL_REG1_A, 4)
156172
_enable_xyz = RWBits(3, _REG_ACCEL_CTRL_REG1_A, 0)
157173
_raw_accel_data = StructArray(_REG_ACCEL_OUT_X_L_A, "<h", 3)
@@ -161,8 +177,6 @@ class LSM303_Accel:
161177

162178
_range = RWBits(2, _REG_ACCEL_CTRL_REG4_A, 4)
163179

164-
# filter _REG_ACCEL_CTRL_REG2_A
165-
# CTRL_REG5_A boot (7)
166180
_int1_src = UnaryStruct(_REG_ACCEL_INT1_SOURCE_A, "B")
167181
_tap_src = UnaryStruct(_REG_ACCEL_CLICK_SRC_A, "B")
168182

@@ -174,13 +188,10 @@ class LSM303_Accel:
174188
_tap_time_latency = UnaryStruct(_REG_ACCEL_TIME_LATENCY_A, "B")
175189
_tap_time_window = UnaryStruct(_REG_ACCEL_TIME_WINDOW_A, "B")
176190

177-
178-
179191
_BUFFER = bytearray(6)
180192
def __init__(self, i2c):
181193
self._accel_device = I2CDevice(i2c, _ADDRESS_ACCEL)
182194
self.i2c_device = self._accel_device
183-
#self._write_u8(self._accel_device, _REG_ACCEL_CTRL_REG1_A, 0x27) # Enable the accelerometer
184195
self._data_rate = 2
185196
self._enable_xyz = 0b111
186197
self._int1_latching = True
@@ -196,18 +207,19 @@ def set_tap(self, tap, threshold, *,
196207
time_limit=10, time_latency=20, time_window=255, tap_cfg=None):
197208
"""
198209
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.
206216
:param int time_limit: TIME_LIMIT register value (default 10).
207217
:param int time_latency: TIME_LATENCY register value (default 20).
208218
:param int time_window: TIME_WINDOW register value (default 255).
209219
:param int click_cfg: CLICK_CFG register value.
220+
210221
"""
222+
211223
if (tap < 0 or tap > 2) and tap_cfg is None:
212224
raise ValueError('Tap must be 0 (disabled), 1 (single tap), or 2 (double tap)!')
213225
if threshold > 127 or threshold < 0:
@@ -240,39 +252,23 @@ def tapped(self):
240252
"""
241253
True if a tap was detected recently. Whether its a single tap or double tap is
242254
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.
253256
"""
254257
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!")
259258
return tap_src & 0b1000000 > 0
260259

261260
@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):
266262
self._read_bytes(self._accel_device, _REG_ACCEL_OUT_X_L_A | 0x80, 6, self._BUFFER)
267263
return struct.unpack_from('<hhh', self._BUFFER[0:6])
268264

269265
@property
270266
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.
273269
"""
274270

275-
raw_accel_data = self.raw_acceleration
271+
raw_accel_data = self._raw_acceleration
276272

277273
x = self._scale_data(raw_accel_data[0])
278274
y = self._scale_data(raw_accel_data[1])
@@ -285,7 +281,7 @@ def _scale_data(self, raw_measurement):
285281

286282
return(raw_measurement >> shift) * lsb * _SMOLLER_GRAVITY
287283

288-
def _lsb_shift(self):
284+
def _lsb_shift(self): #pylint:disable=too-many-branches
289285
# the bit depth of the data depends on the mode, and the lsb value
290286
# depends on the mode and range
291287
lsb = -1 # the default, normal mode @ 2G
@@ -325,7 +321,7 @@ def _lsb_shift(self):
325321

326322
if lsb is -1:
327323
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))
329325
return (lsb, shift)
330326

331327
@property
@@ -348,7 +344,7 @@ def range(self):
348344

349345
@range.setter
350346
def range(self, value):
351-
if value < 0 or value >3:
347+
if value < 0 or value > 3:
352348
raise AttributeError("range must be a `Range`")
353349
self._range = value
354350
self._cached_range = value

examples/activity_interrupt.py

Lines changed: 0 additions & 12 deletions
This file was deleted.

examples/tap_detection.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import board
2+
import busio
3+
import adafruit_lsm303_accel
4+
5+
i2c = busio.I2C(board.SCL, board.SDA)
6+
accel = adafruit_lsm303_accel.LSM303_Accel(i2c)
7+
accel.range = adafruit_lsm303_accel.Range.RANGE_8G
8+
accel.set_tap(1, 30)
9+
10+
while True:
11+
if accel.tapped:
12+
print("Tapped!\n")

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
Adafruit-Blinka
22
adafruit-circuitpython-busdevice
3+
adafruit-circuitpython-register

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
author='Adafruit Industries',
3535
author_email='[email protected]',
3636

37-
install_requires=['Adafruit-Blinka', 'adafruit-circuitpython-busdevice'],
37+
install_requires=['Adafruit-Blinka', 'adafruit-circuitpython-busdevice', 'adafruit-circuitpython-register'],
3838

3939
# Choose your license
4040
license='MIT',

0 commit comments

Comments
 (0)