Skip to content

Commit cf564ad

Browse files
authored
Merge pull request #68 from jposada202020/improving_docs
improving_docs
2 parents 837bf0c + dfdcb39 commit cf564ad

File tree

6 files changed

+156
-38
lines changed

6 files changed

+156
-38
lines changed

adafruit_lis3dh.py

Lines changed: 121 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
2828
**Software and Dependencies:**
2929
30-
* Adafruit CircuitPython firmware for the ESP8622 and M0-based boards:
31-
https://github.com/adafruit/circuitpython/releases
30+
* Adafruit CircuitPython firmware for the supported boards:
31+
https://circuitpython.org/downloads
3232
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
3333
"""
3434

@@ -85,7 +85,14 @@
8585

8686

8787
class LIS3DH:
88-
"""Driver base for the LIS3DH accelerometer."""
88+
"""Driver base for the LIS3DH accelerometer.
89+
90+
:param digitalio.DigitalInOut int1. pin on the sensor that would
91+
act as an in interrupt
92+
:param digitalio.DigitalInOut int2. pin on the sensor that would
93+
act as an in interrupt
94+
95+
"""
8996

9097
def __init__(self, int1=None, int2=None):
9198
# Check device ID.
@@ -115,10 +122,22 @@ def __init__(self, int1=None, int2=None):
115122

116123
@property
117124
def data_rate(self):
118-
"""The data rate of the accelerometer. Can be DATA_RATE_400_HZ, DATA_RATE_200_HZ,
119-
DATA_RATE_100_HZ, DATA_RATE_50_HZ, DATA_RATE_25_HZ, DATA_RATE_10_HZ,
120-
DATA_RATE_1_HZ, DATA_RATE_POWERDOWN, DATA_RATE_LOWPOWER_1K6HZ, or
121-
DATA_RATE_LOWPOWER_5KHZ."""
125+
"""The data rate of the accelerometer.
126+
127+
Could have the following values:
128+
129+
* DATA_RATE_400_HZ
130+
* DATA_RATE_200_HZ
131+
* DATA_RATE_100_HZ
132+
* DATA_RATE_50_HZ
133+
* DATA_RATE_25_HZ
134+
* DATA_RATE_10_HZ
135+
* DATA_RATE_1_HZ
136+
* DATA_RATE_POWERDOWN
137+
* DATA_RATE_LOWPOWER_1K6HZ
138+
* DATA_RATE_LOWPOWER_5KHZ.
139+
140+
"""
122141
ctl1 = self._read_register_byte(_REG_CTRL1)
123142
return (ctl1 >> 4) & 0x0F
124143

@@ -131,8 +150,16 @@ def data_rate(self, rate):
131150

132151
@property
133152
def range(self):
134-
"""The range of the accelerometer. Can be RANGE_2_G, RANGE_4_G, RANGE_8_G, or
135-
RANGE_16_G."""
153+
"""The range of the accelerometer.
154+
155+
Could have the following values:
156+
157+
* RANGE_2_G
158+
* RANGE_4_G
159+
* RANGE_8_G
160+
* RANGE_16_G.
161+
162+
"""
136163
ctl4 = self._read_register_byte(_REG_CTRL4)
137164
return (ctl4 >> 4) & 0x03
138165

@@ -145,7 +172,8 @@ def range(self, range_value):
145172

146173
@property
147174
def acceleration(self):
148-
"""The x, y, z acceleration values returned in a 3-tuple and are in m / s ^ 2."""
175+
"""The x, y, z acceleration values returned
176+
in a 3-tuple and are in :math:`m / s ^ 2`"""
149177
divider = 1
150178
accel_range = self.range
151179
if accel_range == RANGE_16_G:
@@ -167,21 +195,23 @@ def acceleration(self):
167195
return AccelerationTuple(x, y, z)
168196

169197
def shake(self, shake_threshold=30, avg_count=10, total_delay=0.1):
170-
"""
171-
Detect when the accelerometer is shaken. Optional parameters:
198+
"""Detect when the accelerometer is shaken. Optional parameters:
199+
200+
:param int shake_threshold: Increase or decrease to change shake sensitivity.
201+
This requires a minimum value of 10.
202+
10 is the total acceleration if the board is not
203+
moving, therefore anything less than
204+
10 will erroneously report a constant shake detected.
205+
Defaults to :const:`30`
172206
173-
:param shake_threshold: Increase or decrease to change shake sensitivity. This
174-
requires a minimum value of 10. 10 is the total
175-
acceleration if the board is not moving, therefore
176-
anything less than 10 will erroneously report a constant
177-
shake detected. (Default 30)
207+
:param int avg_count: The number of readings taken and used for the average
208+
acceleration. Default to :const:`10`
178209
179-
:param avg_count: The number of readings taken and used for the average
180-
acceleration. (Default 10)
210+
:param float total_delay: The total time in seconds it takes to obtain avg_count
211+
readings from acceleration. Defaults to :const:`0.1`
181212
182-
:param total_delay: The total time in seconds it takes to obtain avg_count
183-
readings from acceleration. (Default 0.1)
184213
"""
214+
185215
shake_accel = (0, 0, 0)
186216
for _ in range(avg_count):
187217
# shake_accel creates a list of tuples from acceleration data.
@@ -198,7 +228,7 @@ def shake(self, shake_threshold=30, avg_count=10, total_delay=0.1):
198228
return total_accel > shake_threshold
199229

200230
def read_adc_raw(self, adc):
201-
"""Retrieve the raw analog to digital converter value. ADC must be a
231+
"""Retrieve the raw analog to digital converter value. ADC must be a
202232
value 1, 2, or 3.
203233
"""
204234
if adc < 1 or adc > 3:
@@ -230,18 +260,19 @@ def read_adc_mV(self, adc): # pylint: disable=invalid-name
230260
def tapped(self):
231261
"""
232262
True if a tap was detected recently. Whether its a single tap or double tap is
233-
determined by the tap param on ``set_tap``. ``tapped`` may be True over
263+
determined by the tap param on :attr:`set_tap`. :attr:`tapped` may be True over
234264
multiple reads even if only a single tap or single double tap occurred if the
235265
interrupt (int) pin is not specified.
236266
237-
The following example uses ``i2c`` and specifies the interrupt pin:
267+
The following example uses `board.I2C` and specifies the interrupt pin:
238268
239269
.. code-block:: python
240270
241271
import adafruit_lis3dh
242272
import digitalio
273+
import board
243274
244-
i2c = busio.I2C(board.SCL, board.SDA)
275+
i2c = board.I2C() # uses board.SCL and board.SDA
245276
int1 = digitalio.DigitalInOut(board.D11) # pin connected to interrupt
246277
lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, int1=int1)
247278
lis3dh.range = adafruit_lis3dh.RANGE_8_G
@@ -275,10 +306,11 @@ def set_tap(
275306
the accelerometer range. Good values are 5-10 for 16G,
276307
10-20 for 8G, 20-40 for 4G, and 40-80 for 2G.
277308
278-
:param int time_limit: TIME_LIMIT register value (default 10).
279-
:param int time_latency: TIME_LATENCY register value (default 20).
280-
:param int time_window: TIME_WINDOW register value (default 255).
309+
:param int time_limit: TIME_LIMIT register value. Defaults to :const:`10`
310+
:param int time_latency: TIME_LATENCY register value. Defaults to :const:`20`
311+
:param int time_window: TIME_WINDOW register value. Defaults to :const:`255`
281312
:param int click_cfg: CLICK_CFG register value.
313+
282314
"""
283315
if (tap < 0 or tap > 2) and click_cfg is None:
284316
raise ValueError(
@@ -324,7 +356,37 @@ def _write_register_byte(self, register, value):
324356

325357

326358
class LIS3DH_I2C(LIS3DH):
327-
"""Driver for the LIS3DH accelerometer connected over I2C."""
359+
"""Driver for the LIS3DH accelerometer connected over I2C.
360+
361+
:param ~busio.I2C i2c: The I2C bus the LIS3DH is connected to.
362+
:param address: The I2C device address. Defaults to :const:`0x18`
363+
364+
365+
**Quickstart: Importing and using the device**
366+
367+
Here is an example of using the :class:`LIS3DH_I2C` class.
368+
First you will need to import the libraries to use the sensor
369+
370+
.. code-block:: python
371+
372+
import board
373+
import adafruit_lis3dh
374+
375+
Once this is done you can define your `board.I2C` object and define your sensor object
376+
377+
.. code-block:: python
378+
379+
i2c = board.I2C() # uses board.SCL and board.SDA
380+
lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c)
381+
382+
Now you have access to the :attr:`acceleration` attribute
383+
384+
.. code-block:: python
385+
386+
acc_x, acc_y, acc_z = lis3dh.acceleration
387+
388+
389+
"""
328390

329391
def __init__(self, i2c, *, address=0x18, int1=None, int2=None):
330392
import adafruit_bus_device.i2c_device as i2c_device # pylint: disable=import-outside-toplevel
@@ -348,7 +410,36 @@ def _write_register_byte(self, register, value):
348410

349411

350412
class LIS3DH_SPI(LIS3DH):
351-
"""Driver for the LIS3DH accelerometer connected over SPI."""
413+
"""Driver for the LIS3DH accelerometer connected over SPI.
414+
415+
:param ~busio.I2C i2c: The I2C bus the LIS3DH is connected to.
416+
:param address: The I2C device address. Defaults to :const:`0x18`
417+
418+
419+
**Quickstart: Importing and using the device**
420+
421+
Here is an example of using the :class:`LIS3DH_SPI` class.
422+
First you will need to import the libraries to use the sensor
423+
424+
.. code-block:: python
425+
426+
import board
427+
import adafruit_lis3dh
428+
429+
Once this is done you can define your `board.SPI` object and define your sensor object
430+
431+
.. code-block:: python
432+
433+
i2c = board.SPI()
434+
lis3dh = adafruit_lis3dh.LIS3DH_SPI(spi)
435+
436+
Now you have access to the :attr:`acceleration` attribute
437+
438+
.. code-block:: python
439+
440+
acc_x, acc_y, acc_z = lis3dh.acceleration
441+
442+
"""
352443

353444
def __init__(self, spi, cs, *, baudrate=100000, int1=None, int2=None):
354445
import adafruit_bus_device.spi_device as spi_device # pylint: disable=import-outside-toplevel

docs/examples.rst

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,41 @@ Ensure your device works with this simple test.
77
:caption: examples/lis3dh_simpletest.py
88
:linenos:
99

10-
Here are some additional examples:
10+
11+
Tap test
12+
------------
13+
14+
Illustrates tap different capabilities
1115

1216
.. literalinclude:: ../examples/lis3dh_tap.py
1317
:caption: examples/lis3dh_tap.py
1418
:linenos:
1519

20+
ADC test
21+
------------
22+
23+
Get the voltage readings
24+
1625
.. literalinclude:: ../examples/lis3dh_adc.py
1726
:caption: examples/lis3dh_adc.py
1827
:linenos:
1928

29+
30+
Spinner Example
31+
---------------
32+
33+
Creates a spinner
34+
35+
.. literalinclude:: ../examples/lis3dh_spinner.py
36+
:caption: examples/lis3dh_spinner.py
37+
:linenos:
38+
39+
40+
Advanced Spinner Example
41+
------------------------
42+
43+
Creates a spinner with colors and animations
44+
2045
.. literalinclude:: ../examples/lis3dh_spinner.py
2146
:caption: examples/lis3dh_spinner.py
2247
:linenos:

docs/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Table of Contents
2323
.. toctree::
2424
:caption: Tutorials
2525

26+
Adafruit LIS3DH Triple-Axis Accelerometer Breakout Learning Guide <https://learn.adafruit.com/adafruit-lis3dh-triple-axis-accelerometer-breakout>
27+
2628
.. toctree::
2729
:caption: Related Products
2830

examples/lis3dh_adc.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@
2020
i2c = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA)
2121
lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, address=0x19)
2222
else:
23-
i2c = busio.I2C(board.SCL, board.SDA)
23+
i2c = board.I2C() # uses board.SCL and board.SDA
2424
lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c)
2525

2626
# Hardware SPI setup:
27-
# spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
27+
# spi = board.SPI()
2828
# cs = digitalio.DigitalInOut(board.D5) # Set to correct CS pin!
2929
# lis3dh = adafruit_lis3dh.LIS3DH_SPI(spi, cs)
3030

3131
# PyGamer I2C Setup:
32-
# i2c = busio.I2C(board.SCL, board.SDA)
32+
# i2c = board.I2C(A)
3333
# lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, address=0x19)
3434

3535

examples/lis3dh_simpletest.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@
1212
i2c = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA)
1313
lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, address=0x19)
1414
else:
15-
i2c = busio.I2C(board.SCL, board.SDA)
15+
i2c = board.I2C() # uses board.SCL and board.SDA
1616
lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c)
1717

1818
# Hardware SPI setup:
19-
# spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
19+
# spi = board.SPI()
2020
# cs = digitalio.DigitalInOut(board.D5) # Set to correct CS pin!
2121
# lis3dh = adafruit_lis3dh.LIS3DH_SPI(spi, cs)
2222

2323
# PyGamer or MatrixPortal I2C Setup:
24-
# i2c = busio.I2C(board.SCL, board.SDA)
24+
# i2c = board.I2C() # uses board.SCL and board.SDA
2525
# lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, address=0x19)
2626

2727

examples/lis3dh_tap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
int1 = digitalio.DigitalInOut(board.ACCELEROMETER_INTERRUPT)
1515
lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, address=0x19, int1=int1)
1616
else:
17-
i2c = busio.I2C(board.SCL, board.SDA)
17+
i2c = board.I2C() # uses board.SCL and board.SDA
1818
int1 = digitalio.DigitalInOut(
1919
board.D9
2020
) # Set this to the correct pin for the interrupt!

0 commit comments

Comments
 (0)