Last active
May 12, 2021 14:10
-
-
Save jfurcean/be1c34366dbaf44d0d5904c8a54501fb to your computer and use it in GitHub Desktop.
CircuitPythyon Simple i2C Seesaw Rotary Enocder
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 board | |
import busio | |
from adafruit_seesaw.seesaw import Seesaw | |
from adafruit_seesaw.digitalio import DigitalIO | |
from adafruit_seesaw.neopixel import NeoPixel | |
import struct | |
_ENCODER_BASE = 0x11 | |
_ENCODER_POSITION = 0x30 | |
_ENCODER_STATUS = 0x00 | |
_ENCODER_DELTA = 0x40 | |
_SWITCH = 24 | |
_NEOPIX = 6 | |
def get_encoder_pos(ss,encoder=0): | |
"""Read the current position of the encoder""" | |
buf = bytearray(4) | |
ss.read(_ENCODER_BASE, _ENCODER_POSITION + encoder, buf) | |
return struct.unpack(">i", buf)[0] | |
def set_encoder_pos(ss, pos, encoder=0): | |
"""Set the current position of the encoder""" | |
cmd = struct.pack(">i", pos) | |
ss.write(_ENCODER_BASE, _ENCODER_POSITION + encoder, cmd) | |
def get_encoder_delta(ss,encoder=0): | |
"""Read the change in encoder position since it was last read""" | |
buf = bytearray(4) | |
ss.read(_ENCODER_BASE, _ENCODER_DELTA + encoder, buf) | |
return struct.unpack(">i", buf)[0] | |
# initialize i2c_bus (QTPY RP2040) | |
i2c_bus = busio.I2C(board.SCL1, board.SDA1) | |
# initialize seesaw | |
ss = Seesaw(i2c_bus,addr=0x36) | |
button = DigitalIO(ss,_SWITCH) | |
button_held = False | |
neopixel = NeoPixel(ss,_NEOPIX,1) | |
neopixel[0] = (0,255,0) | |
while True: | |
delta = get_encoder_delta(ss) | |
if delta != 0: | |
print(delta) | |
if not button.value and not button_held: | |
button_held = True | |
neopixel[0] = (255,0,0) | |
print("pressed") | |
if button.value and button_held: | |
neopixel[0] = (0,255,0) | |
button_held = False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment