Added rotary encoder example with debouncer

This commit is contained in:
David Thurstenson 2022-10-19 23:46:01 -05:00
parent 2ec2951415
commit 32d339c0fa
1 changed files with 35 additions and 0 deletions

View File

@ -0,0 +1,35 @@
# Ref: https://learn.adafruit.com/adafruit-i2c-qt-rotary-encoder/python-circuitpython
import board
from adafruit_seesaw import seesaw, rotaryio, digitalio
import busio
from adafruit_debouncer import Button # Also requires adafruit_ticks to be present in /lib/
SDA = board.GP0
SCL = board.GP1
i2c = busio.I2C(SCL, SDA)
seesaw = seesaw.Seesaw(i2c, 0x36)
seesaw.pin_mode(24, seesaw.INPUT_PULLUP)
pin = digitalio.DigitalIO(seesaw, 24)
button = Button(pin)
encoder = rotaryio.IncrementalEncoder(seesaw)
last_position = None
while True:
# Update the debouncer.
# Needs to happen very frequently
button.update()
# negate the position to make clockwise rotation positive
position = -encoder.position
if position != last_position:
last_position = position
print("Position: {}".format(position))
if button.short_count == 1:
print("Button was short pressed once")
if button.long_press:
print("Button was long pressed")