Skip to content

Instantly share code, notes, and snippets.

@NanoPi
Last active March 17, 2025 08:34
Show Gist options
  • Select an option

  • Save NanoPi/930b08ccb8c4f5ee7c3b to your computer and use it in GitHub Desktop.

Select an option

Save NanoPi/930b08ccb8c4f5ee7c3b to your computer and use it in GitHub Desktop.
FreePIE joystick mouse script
# https://github.com/AndersMalmgren/FreePIE/wiki/Sign
def sign(val): #Returns -1 for negatives, 0 for zero and 1 for positives
return (1 * (val > 0)) + (-1 * (val < 0))
# https://github.com/AndersMalmgren/FreePIE/wiki/ScaledDeadzone
# (modified)
def ScaledDeadzone(val, mini, maxi, dz): #Paremeters are Value, Minimum, Maximum and Deadzone Size.
scaled = filters.mapRange(val,mini,maxi, -1.0,1.0)
output = 0.0
if abs(scaled) > dz:
output = filters.mapRange(abs(scaled), dz, 1.0, 0.0, 1.0) * sign(val)
return filters.mapRange(output,-1.0,1.0, mini,maxi)
def dz(a):
return ScaledDeadzone(a, -jsrange,jsrange,0.03)
def dzR(a):
return ScaledDeadzone(a, -jsrange,jsrange,0.07)
if starting:
j = -1
gpa = 0
gpb = 0
mw = 0.0
jsrange = 1000
#diagnostics.debug("Press the second button on your gamepad.")
# choose which joystick to use by pressing the second button
if j == -1:
for i in range(0, len(joystick)):
# using getPressed here prevents unintended double click
# (getPressed can only be used once per frame, using it a second time always returns False)
if joystick[i].getPressed(1):
j = i
joystick[i].setRange(-jsrange,jsrange)
if j >= 0:
# slow down with RT analogue
slow = 0.0 + float(joystick[j].sliders[1])/jsrange + 2
#diagnostics.watch(slow)
# movement
mouse.deltaX = x = dz(joystick[j].x)/(slow*slow) * 0.003
mouse.deltaY = y = dz(joystick[j].y)/(slow*slow) * 0.003
diagnostics.watch(x)
diagnostics.watch(y)
"""
if joystick[j].getPressed(0):
gpa += 1
if joystick[j].getPressed(0):
gpb += 1
diagnostics.watch(gpa)
diagnostics.watch(gpb)
"""
# buttons
mouse.leftButton = joystick[j].getDown(0)
mouse.rightButton = joystick[j].getDown(2)
mouse.middleButton = joystick[j].getDown(3)
# double click
if joystick[j].getPressed(1):
mouse.setButton(0,True)
mouse.setButton(0,False)
mouse.setButton(0,True)
mouse.setButton(0,False)
#smooth scrolling in web browsers. chaos everywhere else
if dzR(joystick[j].zRotation) != 0.0:
# use gamecube's RT-click to dramatically boost slowness
if joystick[j].getDown(5):
slow*=slow
mw += -math.tan(dzR(joystick[j].zRotation)/jsrange) * mouse.wheelMax/(slow*slow*30)
if abs(mw) >= 1.0:
mouse.wheel = mw
mw -= int(mw)
diagnostics.watch(mw)
diagnostics.watch(mouse.wheel)
#diagnostics.watch(math.tan(dzR(joystick[j].zRotation)/jsrange))
@Badal3850

Copy link
Copy Markdown

i got an error from freepie "29 len() of unsized object"

same

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment