Skip to content

Commit

Permalink
update qtpy_knob_scroller.py to work in CirPy 8 and RP2040
Browse files Browse the repository at this point in the history
  • Loading branch information
todbot committed Oct 14, 2023
1 parent 100f686 commit 039f626
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions qtpy_knob_scroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,24 @@
# Turning knob scrolls vertically,
# pressing & turning scrolls horizontally
# https://github.com/todbot/qtpy-knob
# 2021 @todbot / Tod Kurt
# 2021-2023 @todbot / Tod Kurt
#

import os
import time
import board
from digitalio import DigitalInOut, Direction, Pull
import neopixel
import rotaryio
import usb_hid
from adafruit_hid.mouse import Mouse
from adafruit_hid.keyboard import Keyboard,Keycode
import gc
gc.collect() # not much space on SAMD21 devices any more
import neopixel
import usb_hid

if os.uname().machine.find("rp2040") > 0: # RP2040
from fakerotaryio import IncrementalEncoder
else:
from rotaryio import IncrementalEncoder

# 16 position neopixel ring
ring = neopixel.NeoPixel(board.MISO, 16, brightness=0.2, auto_write=False)
Expand All @@ -24,16 +31,14 @@

# Use pin A2 as a fake ground for the rotary encoder
fakegnd = DigitalInOut(board.A2)
fakegnd.direction = Direction.OUTPUT
fakegnd.value = False
fakegnd.switch_to_output(value=False)

encoder = rotaryio.IncrementalEncoder( board.A3, board.A1 )
encoder = IncrementalEncoder( board.A3, board.A1 )

#cc = ConsumerControl(usb_hid.devices)
mouse = Mouse(usb_hid.devices)
keyboard = Keyboard(usb_hid.devices)

print("hello from qtpy-knob-scroller!")
print("hello from qtpy-knob-scroller!!!")

# standard colorwheel
def colorwheel(pos):
Expand Down Expand Up @@ -63,13 +68,13 @@ def scroll(diff):
for i in range(-diff):
mouse.move(0,0, -1)
time.sleep(0.01)
while True:

while True:
diff = last_encoder_val - encoder.position # encoder clicks since last read
last_encoder_val = encoder.position
ring_pos = (ring_pos + diff) % len(ring) # position on LED ring
hue = colorwheel( encoder.position*4 % 255 ) # fun hue change based on pos

if not button.value: # button pressed
keyboard.press(Keycode.LEFT_SHIFT)
scroll(diff)
Expand All @@ -81,12 +86,12 @@ def scroll(diff):
rainbow_pos = (rainbow_pos + 1) % 256
else:
scroll(diff)
ring.fill( [int(i/4) for i in hue] ) # make it 1/4 dimmer

ring.fill( [int(i/4) for i in hue] ) # make it 1/4 dimmer
ring[ring_pos] = (255,255,255)
ring[(ring_pos-1)%len(ring)] = (67,67,67)
ring[(ring_pos+1)%len(ring)] = (67,67,67)
ring.show()

print(encoder.position,diff,button.value,ring_pos)
time.sleep(0.05)

0 comments on commit 039f626

Please sign in to comment.