|
| 1 | +import adafruit_lis3dh |
1 | 2 | import adafruit_thermistor
|
2 | 3 | import analogio
|
3 | 4 | import array
|
4 | 5 | import audioio
|
5 | 6 | import board
|
| 7 | +import busio |
6 | 8 | import digitalio
|
7 | 9 | import math
|
8 | 10 | import neopixel
|
@@ -60,6 +62,80 @@ def __init__(self):
|
60 | 62 | self._touch_A6 = None
|
61 | 63 | self._touch_A7 = None
|
62 | 64 |
|
| 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 | + |
63 | 139 | @property
|
64 | 140 | def touch_A1(self):
|
65 | 141 | """Detect touch on capacitive touch pad A1.
|
|
0 commit comments