File tree Expand file tree Collapse file tree
CircuitPython_NeXT_Mouse_RP2040 Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments