Created
November 14, 2023 08:28
-
-
Save suzumura-ss/fb01d2046de7c518ea3bfd7969fe7b13 to your computer and use it in GitHub Desktop.
rotary encoder and led example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import digitalio | |
import board | |
import usb_hid | |
import rotaryio | |
from adafruit_hid.consumer_control import ConsumerControl | |
from adafruit_hid.consumer_control_code import ConsumerControlCode | |
consumer_control = ConsumerControl(usb_hid.devices) | |
class Encoder: | |
def __init__(self): | |
self.enc = rotaryio.IncrementalEncoder(board.GP14, board.GP15) | |
self.lastValue = None | |
def processEncoder(self): | |
value = self.enc.position | |
if self.lastValue == value: | |
return | |
print("Encoder: {}".format(value)) | |
if self.lastValue is not None: | |
if self.lastValue < value: | |
consumer_control.send(ConsumerControlCode.VOLUME_INCREMENT) | |
else: | |
consumer_control.send(ConsumerControlCode.VOLUME_DECREMENT) | |
self.lastValue = value | |
theEncoder = Encoder() | |
led = digitalio.DigitalInOut(board.GP16) | |
led.direction = digitalio.Direction.OUTPUT | |
led.value = True | |
while True: | |
theEncoder.processEncoder() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment