Blackberry Trackball (ICSH044A) driver for Adafruit QtPY/Raspberry Pico. See https://developer.run/63 for context
| # Need to install adafruit_debouncer an adafruit_hid | |
| import digitalio | |
| import board | |
| import time | |
| import usb_hid | |
| from adafruit_debouncer import Debouncer | |
| from adafruit_hid.mouse import Mouse | |
| import time | |
| def setup(pin): | |
| inp = digitalio.DigitalInOut(pin) | |
| inp.direction = digitalio.Direction.INPUT | |
| inp.pull = digitalio.Pull.UP | |
| return Debouncer(inp, interval=0.0001) | |
| step = 4 | |
| smooth = 8 | |
| accel = 2 | |
| max_accel = 5 | |
| def move(last): | |
| now = time.time() | |
| multiplier = max_accel+1+max(last-now,-max_accel) | |
| return multiplier*accel*step, now | |
| mouse = Mouse(usb_hid.devices) | |
| # Raspberry Pico | |
| # up = setup(board.GP3) | |
| # down = setup(board.GP2) | |
| # left = setup(board.GP4) | |
| # right = setup(board.GP5) | |
| # Adafruit QtPY | |
| up = setup(board.A0) | |
| down = setup(board.A1) | |
| left = setup(board.A2) | |
| right = setup(board.A3) | |
| up_upd = time.time() | |
| down_upd = time.time() | |
| left_upd = time.time() | |
| right_upd = time.time() | |
| x=0 | |
| y=0 | |
| while True: | |
| up.update() | |
| if up.fell: | |
| mv,up_upd=move(up_upd) | |
| if y>0: | |
| y=-mv | |
| else: | |
| y-=mv | |
| else: | |
| down.update() | |
| if down.fell: | |
| mv,down_upd=move(down_upd) | |
| if y<0: | |
| y=mv | |
| else: | |
| y+=mv | |
| left.update() | |
| if left.fell: | |
| mv,left_upd=move(left_upd) | |
| if x<0: | |
| x=mv | |
| else: | |
| x+=mv | |
| else: | |
| right.update() | |
| if right.fell: | |
| mv,right_upd=move(right_upd) | |
| if x>0: | |
| x=-mv | |
| else: | |
| x-=mv | |
| mvx = 0 | |
| mvy = 0 | |
| if (x>0): | |
| x=max(0, x-smooth) | |
| mvx = smooth | |
| elif (x<0): | |
| x=min(0, x+smooth) | |
| mvx = -smooth | |
| if (y>0): | |
| y=max(0, y-smooth) | |
| mvy = smooth | |
| elif (y<0): | |
| y=min(0,y+smooth) | |
| mvy = -smooth | |
| if not mvx == 0 or not mvy == 0: | |
| mouse.move(x=mvx, y=mvy) | |