try to support both controllers

This commit is contained in:
Trit0 2025-07-03 19:55:34 -04:00 committed by TristanBrault
parent 6ca7501e8b
commit 5c7c429c70
2 changed files with 29 additions and 55 deletions

View File

@ -1,4 +1,5 @@
export interface ControllerState { export interface ControllerState {
id?: number,
joystick: { joystick: {
x: number, x: number,
y: number, y: number,

View File

@ -47,6 +47,7 @@ type Vec2B struct {
} }
type ControllerState struct { type ControllerState struct {
Id int `json:"id"`
Joystick Vec2B `json:"joystick"` Joystick Vec2B `json:"joystick"`
Buttons byte `json:"buttons"` Buttons byte `json:"buttons"`
} }
@ -107,74 +108,46 @@ func Start(onStateChange func(ControllerState)) {
const vendorID = 0x0079 const vendorID = 0x0079
const productID = 0x0006 const productID = 0x0006
// Enumerate all matching devices
devices := hid.Enumerate(vendorID, productID) devices := hid.Enumerate(vendorID, productID)
if len(devices) == 0 { if len(devices) == 0 {
fmt.Printf("Device with VID:PID %04X:%04X not found", vendorID, productID) fmt.Printf("Device with VID:PID %04X:%04X not found\n", vendorID, productID)
return return
} }
for _, d := range devices { for i, d := range devices {
fmt.Printf("Found: %s - VID:%04X PID:%04X\n", d.Product, d.VendorID, d.ProductID) fmt.Printf("Found device %d: %s - VID:%04X PID:%04X\n", i, d.Product, d.VendorID, d.ProductID)
}
// Open the first matching device go func(i int, d hid.DeviceInfo) {
device, err := d.Open()
for i, deviceDetected := range devices {
fmt.Printf("device %d detected\n", i)
device, err := deviceDetected.Open()
if err != nil { if err != nil {
fmt.Printf("Failed to open device: %v", err) fmt.Printf("Failed to open device %d: %v\n", i, err)
return return
} }
defer device.Close() defer device.Close()
controller := Controller{Device: device} controller := Controller{Device: device}
buf := make([]byte, 32)
fmt.Println("Reading data... Press Ctrl+C to exit") fmt.Printf("Reading data from device %d... Press Ctrl+C to exit\n", i)
buf := make([]byte, 32) // Adjust size if needed
for { for {
state, err := controller.ReadState(buf) state, err := controller.ReadState(buf)
if err != nil { if err != nil {
fmt.Printf("Read error: %v", err) fmt.Printf("Read error on device %d: %v\n", i, err)
return return
} }
state.Id = i
if state.Buttons != 0 || state.Joystick.X != 127 || state.Joystick.Y != 127 { if state.Buttons != 0 || state.Joystick.X != 127 || state.Joystick.Y != 127 {
fmt.Printf("State changed on device %d\n", i) fmt.Printf("State changed on device %d\n", i)
onStateChange(*state) onStateChange(*state)
} }
} }
}(i, d)
} }
fmt.Println("Out") // Prevent the function from exiting
select {}
// Open the joystick device file
// file, err := os.Open("/dev/input/js0")
// if err != nil {
// fmt.Println("Error opening joystick:", err)
// return
// }
// defer file.Close()
// // Continuously read joystick events
// for {
// var e JoystickEvent
// err := binary.Read(file, binary.LittleEndian, &e)
// if err != nil {
// fmt.Println("Error reading joystick event:", err)
// return
// }
// // Handle the event
// handleJoystickEvent(e)
// // Sleep to avoid flooding output
// time.Sleep(10 * time.Millisecond)
// }
} }
// handleJoystickEvent processes joystick events. // handleJoystickEvent processes joystick events.