timer-bar/examples/rotary-encoder-stemma-debou...

35 lines
984 B
Python

# 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")