Joystick module as mouse rpi5

April 21, 2024, 10:10

catsermin

so im trying to make a joystick module work as a mouse any help appreciated

catsermin

so this is the code

catsermin

from gpiozero import Button from evdev import UInput, ecodes as e from time import sleep VR_X_PIN = 17 # GPIO pin for VR-X (X-axis) VR_Y_PIN = 22 # GPIO pin for VR-Y (Y-axis) SW_PIN = 27 # GPIO pin for SW (switch/button) mouse = UInput() button_x = Button(VR_X_PIN) button_y = Button(VR_Y_PIN) button_sw = Button(SW_PIN) prev_x = False prev_y = False prev_sw = False while True: # Read button states x = button_x.is_pressed y = button_y.is_pressed sw = button_sw.is_pressed if x != prev_x: mouse.write(e.EV_REL, e.REL_X, -1 if x else 0) # Move mouse left prev_x = x # Check if VR-Y (Y-axis) state has changed if y != prev_y: mouse.write(e.EV_REL, e.REL_Y, -1 if y else 0) # Move mouse up prev_y = y # Check if SW (switch/button) state has changed if sw != prev_sw: if sw: # Button pressed mouse.write(e.EV_KEY, e.BTN_LEFT, 1) # Left mouse button press else: # Button released mouse.write(e.EV_KEY, e.BTN_LEFT, 0) # Left mouse button release prev_sw = sw mouse.syn() sleep(0.1) # Adjust sleep time as needed

osf0

chat gpt ah code