From 039f626e2a71486fd45a108d9900f31319478166 Mon Sep 17 00:00:00 2001 From: Tod Kurt Date: Sat, 14 Oct 2023 16:45:08 -0700 Subject: [PATCH] update qtpy_knob_scroller.py to work in CirPy 8 and RP2040 --- qtpy_knob_scroller.py | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/qtpy_knob_scroller.py b/qtpy_knob_scroller.py index 057cca3..47219e5 100644 --- a/qtpy_knob_scroller.py +++ b/qtpy_knob_scroller.py @@ -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) @@ -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): @@ -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) @@ -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)