-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathperipherals.py
executable file
·256 lines (209 loc) · 6.8 KB
/
peripherals.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
# SPDX-FileCopyrightText: Copyright (c) 2021 Melissa LeBlanc-Williams for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
`adafruit_funhouse.peripherals`
================================================================================
Helper library for the Adafruit FunHouse board.
* Author(s): Melissa LeBlanc-Williams
Implementation Notes
--------------------
**Hardware:**
* `Adafruit FunHouse <https://www.adafruit.com/product/4985>`_
**Software and Dependencies:**
* Adafruit CircuitPython firmware for the supported boards:
https://github.com/adafruit/circuitpython/releases
* Adafruit's PortalBase library: https://github.com/adafruit/Adafruit_CircuitPython_PortalBase
"""
import board
from digitalio import DigitalInOut, Direction, Pull
from analogio import AnalogIn
import touchio
import simpleio
import adafruit_dps310
import adafruit_ahtx0
import adafruit_dotstar
try:
from typing import Optional
except ImportError:
pass
__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FunHouse.git"
class Peripherals:
"""Peripherals Helper Class for the FunHouse Library
Attributes:
dotstars (DotStar): The DotStars on the FunHouse board.
See https://circuitpython.readthedocs.io/projects/dotstar/en/latest/api.html
"""
# pylint: disable=too-many-instance-attributes, too-many-locals, too-many-branches, too-many-statements
def __init__(self) -> None:
# Dotstars
self.dotstars = adafruit_dotstar.DotStar(
board.DOTSTAR_CLOCK, board.DOTSTAR_DATA, 5, brightness=0.3
)
# Light Sensor
self._light = AnalogIn(board.LIGHT)
# Buttons
self._buttons = []
for pin in (board.BUTTON_DOWN, board.BUTTON_SELECT, board.BUTTON_UP):
switch = DigitalInOut(pin)
switch.direction = Direction.INPUT
switch.pull = Pull.DOWN
self._buttons.append(switch)
# Cap Tocuh Pads
self._ctp = []
for pin in (
board.CAP6,
board.CAP7,
board.CAP8,
board.CAP13,
board.CAP12,
board.CAP11,
board.CAP10,
board.CAP9,
):
cap = touchio.TouchIn(pin)
cap.threshold = 20000
self._ctp.append(cap)
self.i2c = board.I2C()
self._dps310 = adafruit_dps310.DPS310(self.i2c)
self._aht20 = adafruit_ahtx0.AHTx0(self.i2c)
# LED
self._led = DigitalInOut(board.LED)
self._led.direction = Direction.OUTPUT
# PIR Sensor
self._pir = DigitalInOut(board.PIR_SENSE)
self._pir.direction = Direction.INPUT
@staticmethod
def play_tone(frequency: float, duration: float) -> None:
"""Automatically Enable/Disable the speaker and play
a tone at the specified frequency for the specified duration
It will attempt to play the sound up to 3 times in the case of
an error.
"""
if frequency < 0:
raise ValueError("Negative frequencies are not allowed.")
attempt = 0
# Try up to 3 times to play the sound
while attempt < 3:
try:
simpleio.tone(board.SPEAKER, frequency, duration)
break
except NameError:
pass
attempt += 1
def set_dotstars(self, *values: int) -> None:
"""Set the dotstar values to the provided values"""
for i, value in enumerate(values[: len(self.dotstars)]):
self.dotstars[i] = value
def deinit(self) -> None:
"""Call deinit on all resources to free them"""
self.dotstars.deinit()
for button in self._buttons:
button.deinit()
for ctp in self._ctp:
ctp.deinit()
self._light.deinit()
self._led.deinit()
self._pir.deinit()
@property
def button_down(self) -> bool:
"""
Return whether Down Button is pressed
"""
return self._buttons[0].value
@property
def button_sel(self) -> bool:
"""
Return whether Sel Button is pressed
"""
return self._buttons[1].value
@property
def button_up(self) -> bool:
"""
Return whether Up Button is pressed
"""
return self._buttons[2].value
@property
def any_button_pressed(self) -> bool:
"""
Return whether any button is pressed
"""
return True in [button.value for (i, button) in enumerate(self._buttons)]
@property
def captouch6(self) -> bool:
"""
Return whether CT6 Touch Pad is touched
"""
return self._ctp[0].value
@property
def captouch7(self) -> bool:
"""
Return whether CT7 Touch Pad is touched
"""
return self._ctp[1].value
@property
def captouch8(self) -> bool:
"""
Return whether CT8 Touch Pad is touched
"""
return self._ctp[2].value
@property
def slider(self) -> Optional[float]:
"""
Return the slider position value in the range of 0.0-1.0 or None if not touched
"""
val = 0
cap_map = (0x01, 0x03, 0x02, 0x06, 0x04, 0x0C, 0x08, 0x18, 0x10)
for cap in range(5):
if self._ctp[cap + 3].value:
val += 1 << (cap)
return cap_map.index(val) / 8 if val in cap_map else None
@property
def light(self) -> int:
"""
Return the value of the light sensor. The neopixel_disable property
must be false to get a value.
.. code-block:: python
import time
from adafruit_funhouse import FunHouse
funhouse = FunHouse()
while True:
print(funhouse.peripherals.light)
time.sleep(0.01)
"""
return self._light.value
@property
def temperature(self) -> float:
"""
Return the temperature in degrees Celsius
"""
return self._aht20.temperature
@property
def relative_humidity(self) -> float:
"""
Return the relative humidity as a percentage (0 - 100)
"""
return self._aht20.relative_humidity
@property
def pressure(self) -> float:
"""
Return the barometric pressure in hPa, or equivalently in mBar
"""
return self._dps310.pressure
@property
def led(self) -> bool:
"""
Return or set the value of the LED
"""
return self._led.value
@led.setter
def led(self, value: bool) -> None:
self._led.value = bool(value)
@property
def pir_sensor(self) -> bool:
"""
Return the value of the PIR Sensor
"""
return self._pir.value