Skip to content

Instantly share code, notes, and snippets.

@benevpi
Created July 28, 2022 10:55
Show Gist options
  • Save benevpi/bba4b8afcc8686f5e04528e4926b8273 to your computer and use it in GitHub Desktop.
Save benevpi/bba4b8afcc8686f5e04528e4926b8273 to your computer and use it in GitHub Desktop.
Piezo bash keyboard
# Write your code here :-)
import time
import board
from analogio import AnalogIn
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode
bash_key = Keycode.SPACE
PAUSE = 0.01
THRESHOLD = 5000
PRESS_PAUSE = 0.2
debounce = 0.2
keyboard = Keyboard(usb_hid.devices)
keyboard_layout = KeyboardLayoutUS(keyboard)
analog_in = AnalogIn(board.A1)
prev_ten = []
for i in range(10):
prev_ten.insert(0,analog_in.value)
time.sleep(PAUSE)
last_press = time.monotonic()
while True:
current_value = analog_in.value
total = 0
for value in prev_ten:
total = total + value
difference = abs((current_value * 10) - total)
if difference > THRESHOLD:
if (time.monotonic() - last_press) > debounce:
print("Press")
keyboard.press(bash_key)
time.sleep(PRESS_PAUSE)
keyboard.release_all()
last_press = time.monotonic()
'''
else:
print("debouce")
print(last_press)
print(time.monotonic())
'''
prev_ten = prev_ten[:-1]
prev_ten.insert(0,current_value)
time.sleep(PAUSE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment