Skip to content

Commit 583969f

Browse files
authored
Merge pull request #2821 from adafruit/trrs_atmakers
analog mouse demo
2 parents 4c375a1 + b3d09e6 commit 583969f

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# SPDX-FileCopyrightText: 2024 Bill Binko
2+
# SPDX-License-Identifier: MIT
3+
4+
import time
5+
import board
6+
import analogio
7+
import digitalio
8+
import usb_hid
9+
from adafruit_hid.mouse import Mouse
10+
# pylint: disable=wildcard-import, line-too-long
11+
#import some settings that are specific to joystick/platform
12+
from settings import *
13+
14+
#Calculated value from imported settings
15+
centerVert = int((highVert - lowVert)/2.0)
16+
deadVert = abs((highVert - lowVert)*deadPct)
17+
18+
centerHor = int((highHor - lowHor)/2.0)
19+
deadHor = abs((highHor - lowHor)*deadPct)
20+
21+
#Create a HID Mouse device
22+
mouse = Mouse(usb_hid.devices)
23+
24+
#Setup the RING_2 as Ground
25+
ground = digitalio.DigitalInOut(board.RING_2)
26+
ground.direction=digitalio.Direction.OUTPUT
27+
ground.value = False
28+
#And SLEEVE as VCC (3.3V)
29+
vcc = digitalio.DigitalInOut(board.SLEEVE)
30+
vcc.direction=digitalio.Direction.OUTPUT
31+
vcc.value = True
32+
33+
#setup the switch on the tip to detect a plug being inserted
34+
switch = digitalio.DigitalInOut(board.TIP_SWITCH)
35+
switch.direction=digitalio.Direction.OUTPUT
36+
switch.value=False
37+
#These values shouldn't need changing w/Joystick changes
38+
switchMin = 500
39+
switchMax = 5000
40+
41+
#Two analog inputs for TIP and RING_1
42+
hor = analogio.AnalogIn(board.TIP)
43+
vert = analogio.AnalogIn(board.RING_1)
44+
45+
#A convenience cunction similar to Arduino's mapping function
46+
def range_map(value, in_min, in_max, out_min, out_max):
47+
return int(max(out_min,min(out_max,(value - in_min) * (out_max - out_min) // (in_max - in_min) + out_min)))
48+
49+
#Start Main Loop
50+
while True:
51+
#Check to be sure cord is plugged in
52+
switch.value=False #Start with TIP_SWITCH pulled Low
53+
if hor.value < switchMin: #TIP is pulled Low
54+
switch.value = True #change TIP_SWITCH pin to high
55+
if hor.value > switchMax: #TIP is now pulled High
56+
print("no plug")
57+
time.sleep(.5) #sleep when there's no plug
58+
continue
59+
60+
#Ok, the switch is inserted, start reading/processing joystick motions
61+
horVal = hor.value
62+
vertVal = vert.value
63+
# print((horVal, vertVal,))
64+
65+
#ignore any motions inside the center dead zone (default 10% of full throw)
66+
if abs(centerHor - horVal) > deadHor or abs(centerVert - vertVal) > deadVert:
67+
#map X and Y to the analog inputs (settings.py sets these values)
68+
mouse_x = range_map(horVal, lowHor, highHor, -maxMouseMove, maxMouseMove)
69+
mouse_y = range_map(vertVal, lowVert, highVert, -maxMouseMove, maxMouseMove)
70+
71+
if mouse_x != 0 or mouse_y != 0: #don't bother moving if both 0
72+
mouse.move(invertHor * mouse_x, invertVert * mouse_y)
73+
74+
#wait a bit to not flood the USB port
75+
time.sleep(0.025)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# SPDX-FileCopyrightText: 2024 Bill Binko
2+
# SPDX-License-Identifier: MIT
3+
4+
#Customizable per Analog Joystick - these are good for Adafruit Thumbsticks to start
5+
#How big the "dead zone" is in the center of the joystick
6+
deadPct = .10
7+
8+
#Vertical limits
9+
#Reading at "down"
10+
lowVert = 0
11+
#"up"
12+
highVert = 65000
13+
#set to -1 to invert the vertical axis
14+
invertVert = -1
15+
16+
#Horizontal limits
17+
lowHor= 0
18+
highHor = 65000
19+
#set to -1 to invert the horizontal axis
20+
invertHor = 1
21+
22+
#How much to move a mouse a full-throw
23+
#(10 works well on a PC, and 2 is good for iOS AssitiveTouch)
24+
maxMouseMove=8

0 commit comments

Comments
 (0)