Which joystick is first?

Status
Not open for further replies.

Maziac

Member
Hi,

I would like to use the Teensy for a joystick project and I would like to know if there is a way to influence the order of the devices in the OS, e.g. Windows or Linux.

If I attach 2 same Teensy boards/with same SW on it to a PC and switch it on,
which of the devices will be the first controller and which one will be the 2nd?

Is there any way to influence it from the Teensy side? I mean: could I use 2 different descriptors for the 2 Teensy joysticks to make sure that the order is always the same?
 
In Linux, the joysticks get assigned in the order you plug them in.

If both joysticks are connected when the computer boots or restarts, I believe the order is random.

udev does create persistent aliases at /dev/input/by-id/, so if you have the Teensies provide different serial numbers, you can use the persistent aliases in application configuration, like MAME or Attract-Mode.
 
Thanks, this is of great help for me.
So at least in Linux I will not have any problems.

For the serial number: I guess if I have 2 different HWs they will automatically use different serial numbers.
Or do I have to set the serial number in SW?
 
I have not yet seen two Teensies with the same serial number, and I do believe they are assigned different serial numbers by the boot loader.

As an example, the two Teensy 3.2 that I have lying next to me, have serial numbers 318590 and 318656. I could check others, but I suppose that is enough? :)

I personally prefer to create udev/eudev rules for symlinks for the specific devices. See this thread here for other details, like managing which user/group (or all users) have access to the device.
In my case, adding a .rules file with
Code:
ACTION=="add|change", KERNEL=="js*", ATTRS{idVendor}=="16c0", ATTRS{idProduct}=="04[789AB]?", ENV{ID_SERIAL}=="318590", SYMLINK+="input/by-id/teensy32-joystick-1"
ACTION=="add|change", KERNEL=="js*", ATTRS{idVendor}=="16c0", ATTRS{idProduct}=="04[789AB]?", ENV{ID_SERIAL}=="318656", SYMLINK+="input/by-id/teensy32-joystick-2"
would create /dev/input/by-id/teensy32-joystick-1 and /dev/input/by-id/teensy32-joystick-2 symlinks whenever one of those Teensy 3.2 were plugged in as a USB HID joystick.
(If I reuse one for some other purpose, say USB serial, the rule won't trigger.)

Note that after adding udev/eudev rules files, you'll want to run
Code:
sudo udevadm control --reload-rules
sudo udevadm trigger
to tell udev/eudev to reload the new rules.

There are ways to find out the serial number in Linux. For example,
Code:
for D in /sys/bus/usb/devices/* ; do V="$(< "$D/idVendor")" || continue ; P="$(< "$D/idProduct")" ; S="$(< "$D/serial")" ; [ -n "$S" ] && echo "$V:$P $S" ; done 2>/dev/null
To ignore all but Teensies, use
Code:
for D in /sys/bus/usb/devices/* ; do V="$(< "$D/idVendor")" || continue ; P="$(< "$D/idProduct")" ; S="$(< "$D/serial")" ; [ -n "$S" ] && echo "$V:$P $S" ; done 2>/dev/null | sed -ne '/^16c0:04[7-9a-b]. /p'
(The difference between the rules and the sed expression is explained by udev rules and filename matching useing glob patterns, but sed using POSIX basic regular expressions.)
 
Status
Not open for further replies.
Back
Top