circuitpython/ports/stm/boards/swan_r5/tests/button.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

23 lines
560 B
Python
Raw Normal View History

2021-09-28 12:14:38 -04:00
import board
import digitalio
import time
2021-09-28 12:14:38 -04:00
def monitor_button(pin, callback):
with digitalio.DigitalInOut(pin) as button:
newstate = not button.value # state is inverted
state = not newstate # ensure change reported to start with
while callback(newstate, newstate != state):
2021-09-28 12:14:38 -04:00
state = newstate
newstate = button.value
time.sleep(0.01)
2021-09-28 12:14:38 -04:00
def print_changes(state, changed):
if changed:
print(f"button pressed {state}")
return True
monitor_button(board.BUTTON_USR, print_changes)