Skip to content

Instantly share code, notes, and snippets.

@FoamyGuy
Created December 2, 2021 21:15
Show Gist options
  • Save FoamyGuy/e345295f3eeea27ab34c2e5fd2830bb6 to your computer and use it in GitHub Desktop.
Save FoamyGuy/e345295f3eeea27ab34c2e5fd2830bb6 to your computer and use it in GitHub Desktop.
import time
import board
from digitalio import DigitalInOut, Direction, Pull
LONG_PRESS_DURATION = 0.65 # seconds
btn_select = DigitalInOut(board.BUTTON_SELECT)
btn_select.direction = Direction.INPUT
btn_select.pull = Pull.DOWN
btn_pressed_time = -1
prev_btn_select_val = False
prev_btn_select_longpress_val = False
while True:
# current time
now = time.monotonic()
# current btn value
cur_btn_select_val = btn_select.value
# button was just pressed
if not prev_btn_select_val and cur_btn_select_val:
btn_pressed_time = now
# Button was released
if not cur_btn_select_val and prev_btn_select_val:
if not prev_btn_select_longpress_val:
print("Button was short pressed / released!")
# button is currently pressed
if cur_btn_select_val:
if now - btn_pressed_time >= LONG_PRESS_DURATION:
if not prev_btn_select_longpress_val:
print("Button is longpressed!")
prev_btn_select_longpress_val = True
else:
prev_btn_select_longpress_val = False
prev_btn_select_val = cur_btn_select_val
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment