usb-knob/code.py

61 lines
1.6 KiB
Python

import board
import digitalio
import time
import usb_hid
from adafruit_hid.consumer_control_code import ConsumerControlCode
from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.mouse import Mouse
clkPin = digitalio.DigitalInOut(board.GP2)
dtPin = digitalio.DigitalInOut(board.GP1)
dtPin.direction= digitalio.Direction.INPUT
clkPin.direction = digitalio.Direction.INPUT
clkPin.pull = digitalio.Pull.UP
dtPin.pull = digitalio.Pull.UP
global buttonPin
buttonPin = digitalio.DigitalInOut(board.GP15)
buttonPin.direction = digitalio.Direction.INPUT
buttonPin.pull = digitalio.Pull.UP
global consumer_control
consumer_control = ConsumerControl(usb_hid.devices)
global mode
mode = 0
global previousValue
previousValue = True
def control_volumen(pinStatus):
if pinStatus == False:
print('Volumen down')
consumer_control.send(ConsumerControlCode.VOLUME_DECREMENT)
else:
print('Volumen up')
consumer_control.send(ConsumerControlCode.VOLUME_INCREMENT)
def mute_volumen():
consumer_control.send(ConsumerControlCode.MUTE)
print('Mute')
while True:
if buttonPin.value == False:
if mode == 0:
mute_volumen()
else:
mode = 0
mute_volumen()
print('Button pressed')
time.sleep(0.5)
else:
if previousValue != clkPin.value:
if clkPin.value == False:
if mode == 0:
control_volumen(dtPin.value)
else:
mode = 0
control_volumen(dtPin.value)
previousValue = clkPin.value
print('Rotary Status: ' + str(clkPin.value))