Skip to content

Commit d67dd66

Browse files
authored
Merge pull request #2374 from jepler/next-mouse
Add NeXT standalone mouse code
2 parents 3b4378c + 0955ff2 commit d67dd66

File tree

1 file changed

+45
-0
lines changed
  • CircuitPython_NeXT_Mouse_RP2040

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# SPDX-FileCopyrightText: 2023 Jeff Epler for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
import board
4+
import digitalio
5+
import rotaryio
6+
from adafruit_hid.mouse import Mouse
7+
from usb_hid import devices
8+
9+
SCALE = 4
10+
11+
class RelativeEncoder:
12+
def __init__(self, pin_a, pin_b, divisor=1):
13+
self._encoder = rotaryio.IncrementalEncoder(pin_a, pin_b, divisor)
14+
self._old = self._encoder.position
15+
16+
@property
17+
def delta(self):
18+
old = self._old
19+
new = self._old = self._encoder.position
20+
return new - old
21+
22+
xpos = RelativeEncoder(board.A0, board.A1)
23+
ypos = RelativeEncoder(board.A2, board.A3)
24+
lmb = digitalio.DigitalInOut(board.SCL)
25+
lmb.pull = digitalio.Pull.UP
26+
rmb = digitalio.DigitalInOut(board.SDA)
27+
rmb.pull = digitalio.Pull.UP
28+
29+
mouse = Mouse(devices)
30+
31+
while True:
32+
dx = xpos.delta * SCALE
33+
dy = ypos.delta * SCALE
34+
l = not lmb.value
35+
r = not rmb.value
36+
mouse.report[0] = (
37+
mouse.MIDDLE_BUTTON if (l and r) else
38+
mouse.LEFT_BUTTON if l else
39+
mouse.RIGHT_BUTTON if r else
40+
0)
41+
42+
if dx or dy:
43+
mouse.move(dx, dy)
44+
else:
45+
mouse._send_no_move() # pylint: disable=protected-access

0 commit comments

Comments
 (0)