Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 4 of 4

Thread: Which joystick is first?

  1. #1
    Junior Member
    Join Date
    Sep 2019
    Posts
    19

    Which joystick is first?

    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?

  2. #2
    Senior Member
    Join Date
    Feb 2015
    Location
    Finland
    Posts
    133
    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.

  3. #3
    Junior Member
    Join Date
    Sep 2019
    Posts
    19
    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?

  4. #4
    Senior Member
    Join Date
    Feb 2015
    Location
    Finland
    Posts
    133
    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.)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •