Skip to content

Commit 597cada

Browse files
committed
Acceleration implemented.
Sphinx docs added, conf.py updated, image included.
1 parent 4090101 commit 597cada

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

_static/accelerometer.jpg

692 KB
Loading

adafruit_circuitplayground/express.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import adafruit_lis3dh
12
import adafruit_thermistor
23
import analogio
34
import array
45
import audioio
56
import board
7+
import busio
68
import digitalio
79
import math
810
import neopixel
@@ -60,6 +62,80 @@ def __init__(self):
6062
self._touch_A6 = None
6163
self._touch_A7 = None
6264

65+
# Define acceleration:
66+
self._i2c = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA)
67+
self._lis3dh = adafruit_lis3dh.LIS3DH_I2C(self._i2c, address=0x19)
68+
self._lis3dh.range = adafruit_lis3dh.RANGE_8_G
69+
self._lis3dh._write_register_byte(adafruit_lis3dh.REG_CTRL5, 0b01001000)
70+
self._lis3dh._write_register_byte(0x2E, 0b10000000)
71+
72+
def acceleration_update(self):
73+
"""``acceleration_update`` updates the acceleration data from the x, y
74+
and z axes. It is only needed once to update all three.
75+
76+
.. image :: /_static/accelerometer.jpg
77+
78+
.. code-block:: python
79+
80+
from adafruit_circuitplayground.express import cpx
81+
82+
while True:
83+
cpx.acceleration_update()
84+
print(cpx.acceleration_x, cpx.acceleration_y, cpx.acceleration_z, sep=", ")
85+
"""
86+
self._x, self._y, self._z = self._lis3dh.acceleration
87+
88+
@property
89+
def acceleration_x(self):
90+
"""Utilise the acceleration data from the x axis. ``acceleration_update``
91+
must be included before ``acceleration_x`` to get this data.
92+
93+
.. image :: /_static/accelerometer.jpg
94+
95+
.. code-block:: python
96+
97+
from adafruit_circuitplayground.express import cpx
98+
99+
while True:
100+
cpx.acceleration_update()
101+
print(cpx.acceleration_x)
102+
"""
103+
return self._x
104+
105+
@property
106+
def acceleration_y(self):
107+
"""Utilise the acceleration data from the y axis. ``acceleration_update``
108+
must be included before ``acceleration_y`` to get this data.
109+
110+
.. image :: /_static/accelerometer.jpg
111+
112+
.. code-block:: python
113+
114+
from adafruit_circuitplayground.express import cpx
115+
116+
while True:
117+
cpx.acceleration_update()
118+
print(cpx.acceleration_y)
119+
"""
120+
return self._y
121+
122+
@property
123+
def acceleration_z(self):
124+
"""Utilise the acceleration data from the z axis. ``acceleration_update``
125+
must be included before ``acceleration_z`` to get this data.
126+
127+
.. image :: /_static/accelerometer.jpg
128+
129+
.. code-block:: python
130+
131+
from adafruit_circuitplayground.express import cpx
132+
133+
while True:
134+
cpx.acceleration_update()
135+
print(cpx.acceleration_z)
136+
"""
137+
return self._z
138+
63139
@property
64140
def touch_A1(self):
65141
"""Detect touch on capacitive touch pad A1.

conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
'NeoPixel': ('https://circuitpython.readthedocs.io/projects/neopixel/en/latest/', None)}
2121

2222
# Libraries we depend on but don't need for generating docs.
23-
autodoc_mock_imports = ["board", "analogio", "digitalio", "neopixel", "adafruit_thermistor", "audioio", "touchio"]
23+
autodoc_mock_imports = ["board", "analogio", "digitalio", "neopixel", "adafruit_thermistor", "audioio", "touchio", "adafruit_lis3dh", "busio"]
2424

2525
# Add any paths that contain templates here, relative to this directory.
2626
templates_path = ['_templates']

0 commit comments

Comments
 (0)