Skip to content

Add NeXT standalone mouse code #2374

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions CircuitPython_NeXT_Mouse_RP2040/code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# SPDX-FileCopyrightText: 2023 Jeff Epler for Adafruit Industries
# SPDX-License-Identifier: MIT
import board
import digitalio
import rotaryio
from adafruit_hid.mouse import Mouse
from usb_hid import devices

SCALE = 4

class RelativeEncoder:
def __init__(self, pin_a, pin_b, divisor=1):
self._encoder = rotaryio.IncrementalEncoder(pin_a, pin_b, divisor)
self._old = self._encoder.position

@property
def delta(self):
old = self._old
new = self._old = self._encoder.position
return new - old

xpos = RelativeEncoder(board.A0, board.A1)
ypos = RelativeEncoder(board.A2, board.A3)
lmb = digitalio.DigitalInOut(board.SCL)
lmb.pull = digitalio.Pull.UP
rmb = digitalio.DigitalInOut(board.SDA)
rmb.pull = digitalio.Pull.UP

mouse = Mouse(devices)

while True:
dx = xpos.delta * SCALE
dy = ypos.delta * SCALE
l = not lmb.value
r = not rmb.value
mouse.report[0] = (
mouse.MIDDLE_BUTTON if (l and r) else
mouse.LEFT_BUTTON if l else
mouse.RIGHT_BUTTON if r else
0)

if dx or dy:
mouse.move(dx, dy)
else:
mouse._send_no_move() # pylint: disable=protected-access