Skip to content

Commit

Permalink
Fix USB enumeration for Raspberry Pi 4
Browse files Browse the repository at this point in the history
When testing on Raspberry Pi 4, only
one USB device was showing whe using two sticks of the same
brand, even though they had different names.

This was becaue the GUID being reported by underlying OS was the same
for both sticks.

This change modifies the logic such that the name is also included
when computing the stick hash/ID. Even then, there is still a chance
that you can connect two identical sticks. In that case, there
is additional logic where the enumeration index is appended to the hash/ID
to distinguish between the two.
  • Loading branch information
kaack committed Aug 5, 2023
1 parent 75a8686 commit 29c9aff
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pkg/devices/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ package devices
import (
"crypto/md5"
"encoding/hex"
"fmt"
"github.com/veandco/go-sdl2/sdl"
)

func GetJoyStickId(joystick *sdl.Joystick) string {
guid := sdl.JoystickGetGUIDString(joystick.GUID())
hash := md5.Sum([]byte(guid))
name := joystick.Name()
// on linux, the SDL GUID is not unique, use the name as well
combined := fmt.Sprintf("guid: %s, name: %s", guid, name)
hash := md5.Sum([]byte(combined))
return hex.EncodeToString(hash[:])[1:7]
}

Expand All @@ -25,6 +29,11 @@ func EnumerateDevices() map[string]*InputGamepad {
continue
}
dev := NewDevice(stick)

if _, ok := devices[dev.Id]; ok {
//device is already on the map, re-assign the ID to include index
dev.Id = fmt.Sprintf("%s_%d", dev.Id, i)
}
devices[dev.Id] = &dev
}

Expand Down

0 comments on commit 29c9aff

Please sign in to comment.