I am using a Raspberry Pi4 with a USB Logitech Extreme 3D joystick. I have a Python 3 application using the evdev library to read the joystick which works fantastic, however, when I first boot the pi, the program cannot access the joystick until I disconnect and reconnect it. I have a udev rule which creates a symlink to the joystick.
I have verified that the joystick is being recognized and the symlink is being created upon bootup.
I also installed PyUSB to do some testing. If I boot the pi and go into a terminal, run python 3 and try to configure the joystick using PyUSB, it gives me the following error.
import usb.core
import usb.util
dev = usb.core.find(idVendor=0x046d, idProduct=0xc215)
print(dev) #This outputs the correct information, indicating I am finding it
dev.set_configuration() #This throws the following error
usb.core.USBError: [Error 16] Resource busy
This makes me think something else is binding to the joystick during boot before my application can grab it. I'm not sure how to troubleshoot this. Can anyone offer suggestions?
I have been searching for answers but can't find a solution to this exact problem.
Any help is much appreciated
**UPDATE 1 ** with info from a comment:
I have a bash script that is called from /etc/X11/xinit/xinitrc
when the X system is started and I run startx
from systemd at startup. The systemd waits for the device to be present and sets the environment XAUTHORITY=/home/pi/.Xauthority
which is the user I am running the script under.
**UPDATE 2 ** After trying suggested answer from @Ingo, this is my systemd file:
[Unit]
Description=Start-Program
After=graphical.target
[Service]
User=pi
Environment=DISPLAY=:0.0
Environment=XAUTHORITY=/home/pi/.Xauthority
ExecStart=/home/pi/myprog/program-start.sh
[Install]
WantedBy=graphical.target