Skip to content

Commit 58649ca

Browse files
authored
Merge pull request #21 from relic-se/optional-buttons
Optional hardware peripherals initialization
2 parents 57fc5e8 + 7319b50 commit 58649ca

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

adafruit_fruitjam/peripherals.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,16 @@ def __init__( # noqa: PLR0913, PLR0912
155155
bit_depth: int = 16,
156156
i2c: busio.I2C = None,
157157
):
158-
self.neopixels = NeoPixel(board.NEOPIXEL, 5)
158+
self.neopixels = NeoPixel(board.NEOPIXEL, 5) if "NEOPIXEL" in dir(board) else None
159159

160-
self._buttons = []
161-
for pin in (board.BUTTON1, board.BUTTON2, board.BUTTON3):
162-
switch = DigitalInOut(pin)
163-
switch.direction = Direction.INPUT
164-
switch.pull = Pull.UP
165-
self._buttons.append(switch)
160+
self._buttons = None
161+
if "BUTTON1" in dir(board) and "BUTTON2" in dir(board) and "BUTTON3" in dir(board):
162+
self._buttons = [
163+
DigitalInOut(pin) for pin in (board.BUTTON1, board.BUTTON2, board.BUTTON3)
164+
]
165+
for switch in self._buttons:
166+
switch.direction = Direction.INPUT
167+
switch.pull = Pull.UP
166168

167169
if i2c is None:
168170
i2c = board.I2C()
@@ -229,28 +231,28 @@ def button1(self) -> bool:
229231
"""
230232
Return whether Button 1 is pressed
231233
"""
232-
return not self._buttons[0].value
234+
return self._buttons is not None and not self._buttons[0].value
233235

234236
@property
235237
def button2(self) -> bool:
236238
"""
237239
Return whether Button 2 is pressed
238240
"""
239-
return not self._buttons[1].value
241+
return self._buttons is not None and not self._buttons[1].value
240242

241243
@property
242244
def button3(self) -> bool:
243245
"""
244246
Return whether Button 3 is pressed
245247
"""
246-
return not self._buttons[2].value
248+
return self._buttons is not None and not self._buttons[2].value
247249

248250
@property
249251
def any_button_pressed(self) -> bool:
250252
"""
251253
Return whether any button is pressed
252254
"""
253-
return True in [not button.value for (i, button) in enumerate(self._buttons)]
255+
return self._buttons is not None and True in [not button.value for button in self._buttons]
254256

255257
@property
256258
def dac(self) -> adafruit_tlv320.TLV320DAC3100:

0 commit comments

Comments
 (0)