2021-09-28 12:14:38 -04:00
|
|
|
import board
|
|
|
|
import digitalio
|
|
|
|
import time
|
|
|
|
|
2021-09-28 17:13:04 -04:00
|
|
|
|
2021-09-28 12:14:38 -04:00
|
|
|
def monitor_button(pin, callback):
|
|
|
|
with digitalio.DigitalInOut(pin) as button:
|
2021-09-28 17:13:04 -04:00
|
|
|
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 17:13:04 -04:00
|
|
|
|
2021-09-28 12:14:38 -04:00
|
|
|
def print_changes(state, changed):
|
|
|
|
if changed:
|
|
|
|
print(f"button pressed {state}")
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2021-09-28 17:13:04 -04:00
|
|
|
monitor_button(board.BUTTON_USR, print_changes)
|