Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
6525b316aThis file is pinned to the selected release. Download the original to open it in your design tool.
import rotaryio
import board
import digitalio
import usb_hid
from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.consumer_control_code import ConsumerControlCode
button = digitalio.DigitalInOut(board.D12)
button.direction = digitalio.Direction.INPUT
button.pull = digitalio.Pull.UP
cc = ConsumerControl(usb_hid.devices)
button_state = None
last_position = encoder.position
encoder = rotaryio.IncrementalEncoder(board.GP26, board.GP22)
while True:
current_position = encoder.position
position_change = current_position - last_position
if position_change > 0:
for _ in range(position_change):
cc.send(ConsumerControlCode.VOLUME_INCREMENT)
print(current_position)
elif position_change < 0:
for _ in range(-position_change):
cc.send(ConsumerControlCode.VOLUME_DECREMENT)
print(current_position)
last_position = current_position
if not button.value and button_state is None:
button_state = "pressed"
if button.value and button_state == "pressed":
print("Button pressed.")
cc.send(ConsumerControlCode.PLAY_PAUSE)
button_state = None