1

i am writing something which should let me use a joystick as a Spacemouse that i want to use in fusion 360, im doing this by making the mouse move and pressing the middle mouse button and shift, and releasing it when the joystick stops moving, but my problem is that my mouse won't move and klick at the same time and everytime i start the script my pc will go crazy and i'll have to reset it.

This is the code:

import analogio
import board
import digitalio
import usb_hid
from adafruit_hid.mouse import Mouse
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode

kbd = Keyboard(usb_hid.devices)
mouse = Mouse(usb_hid.devices)
 
x_axis = analogio.AnalogIn(board.A3)
y_axis = analogio.AnalogIn(board.A2)
 
select = digitalio.DigitalInOut(board.A1)
select.direction = digitalio.Direction.INPUT
select.pull = digitalio.Pull.UP

pot_min = 0.00
pot_max = 3.29
step = (pot_max - pot_min) / 20.0
 
 
def get_voltage(pin):
    return (pin.value * 5) / 65536
 
 
def steps(axis):
    """ Maps the potentiometer voltage range to 0-20 """
    return round((axis - pot_min) / step)
 
 
while True:
    #print(str(digitalio.DigitalInOut.value))
    x = get_voltage(x_axis)
    y = get_voltage(y_axis)
    
 
    if select.value is False:
        print("test")
        

        

    if steps(x) > 20.0:
        # print(steps(x))
        mouse.move(x=1)
    if steps(x) < 9.0:
        # print(steps(x))
        mouse.move(x=-1)
 
    if steps(x) > 25.0:
        # print(steps(x))
        mouse.move(x=8)
    if steps(x) < 1.0:
        # print(steps(x))
        mouse.move(x=-8)
 
    if steps(y) > 20.0:
        # print(steps(y))
        mouse.move(y=1)
    if steps(y) < 9.0:
        # print(steps(y))
        mouse.move(y=-1)
 
    if steps(y) > 25.0:
        # print(steps(y))
        mouse.move(y=8)
    if steps(y) < 1.0:
        # print(steps(y))
        mouse.move(y=-8)

    while steps(x) > 20.0 or steps(x) < 9.0 or steps(x) > 25.0 or steps(x) < 1.0 or steps(y) > 20.0 or steps(y) < 9.0 or steps(y) > 25.0 or steps(y) < 1.0:
        kbd.press(Keycode.SHIFT)
        mouse.press(Mouse.MIDDLE_BUTTON)

    else:
        kbd.release(Keycode.SHIFT)
        mouse.release(Mouse.MIDDLE_BUTTON)```

I hope anyone can help me with this.

        


Jonas
  • 121,568
  • 97
  • 310
  • 388

1 Answers1

0

Found the answer cause someone on reddit helped me, i had te make some functions and had to recheck the position off the joystick:

import time
import analogio
import board
import digitalio
import usb_hid
from adafruit_hid.mouse import Mouse
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode

kbd = Keyboard(usb_hid.devices)
mouse = Mouse(usb_hid.devices)
x_axis = analogio.AnalogIn(board.A3)
y_axis = analogio.AnalogIn(board.A2)
select = digitalio.DigitalInOut(board.A1)
select.direction = digitalio.Direction.INPUT
select.pull = digitalio.Pull.UP

pot_min = 0.00
pot_max = 3.29
step = (pot_max - pot_min) / 20.0

def get_voltage(pin):
    return (pin.value * 5) / 65536

def steps(axis):
    """ Maps the potentiometer voltage range to 0-20 """
    return round((axis - pot_min) / step)

def is_joystick_active( x, y ):
    """Checks whether the joystick is away from the center position"""
    return steps(x) > 21.0 or steps(x) < 9.0 or steps(x) > 25.0 or steps(x) < 1.0 or steps(y) > 21.0 or steps(y) < 9.0 or steps(y) > 25.0 or steps(y) < 1.0

def get_mouse_move( x, y ):
    """Translates x,y voltages into how far to move the mouse on each step"""
    mouse_x = 0;
    mouse_y = 0;
    if steps(x) > 21.0:
        # print(steps(x))
        mouse_x+=1

    if steps(x) < 9.0:
        # print(steps(x))
        mouse_x-=1

    if steps(x) > 25.0:
        # print(steps(x))
        mouse_x+=8

    if steps(x) < 1.0:
        # print(steps(x))
        mouse_x-=8

    if steps(y) > 21.0:
        # print(steps(y))
        mouse_y+=1

    if steps(y) < 9.0:
        # print(steps(y))
        mouse_y-=1

    if steps(y) > 25.0:
        # print(steps(y))
        mouse_y+=8

    if steps(y) < 1.0:
        # print(steps(y))
        mouse_y-=8
    return mouse_x, mouse_y

while True:
    #print(str(digitalio.DigitalInOut.value))
    x = get_voltage(x_axis)
    y = get_voltage(y_axis)

    print("x=" + str(steps(x)))
    print("y=" + str(steps(y)))

    time.sleep(0.5)

    if is_joystick_active( x, y ):
        kbd.press(Keycode.SHIFT)
        mouse.press(Mouse.MIDDLE_BUTTON)

        while is_joystick_active( x, y ):
            mouse_x, mouse_y = get_mouse_move( x, y )
            mouse.move(x=mouse_x, y=mouse_y)
            # Wait for a bit
            time.sleep(0.1)
            # Check the joystick position again
            x = get_voltage(x_axis)
            y = get_voltage(y_axis)
        else:
            kbd.release(Keycode.SHIFT)
            mouse.release(Mouse.MIDDLE_BUTTON)```