try to support both controllers
This commit is contained in:
parent
6ca7501e8b
commit
5c7c429c70
@ -1,4 +1,5 @@
|
||||
export interface ControllerState {
|
||||
id?: number,
|
||||
joystick: {
|
||||
x: number,
|
||||
y: number,
|
||||
|
||||
@ -47,6 +47,7 @@ type Vec2B struct {
|
||||
}
|
||||
|
||||
type ControllerState struct {
|
||||
Id int `json:"id"`
|
||||
Joystick Vec2B `json:"joystick"`
|
||||
Buttons byte `json:"buttons"`
|
||||
}
|
||||
@ -107,74 +108,46 @@ func Start(onStateChange func(ControllerState)) {
|
||||
const vendorID = 0x0079
|
||||
const productID = 0x0006
|
||||
|
||||
// Enumerate all matching devices
|
||||
devices := hid.Enumerate(vendorID, productID)
|
||||
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
|
||||
}
|
||||
|
||||
for _, d := range devices {
|
||||
fmt.Printf("Found: %s - VID:%04X PID:%04X\n", d.Product, d.VendorID, d.ProductID)
|
||||
}
|
||||
|
||||
// Open the first matching device
|
||||
|
||||
for i, deviceDetected := range devices {
|
||||
|
||||
fmt.Printf("device %d detected\n", i)
|
||||
device, err := deviceDetected.Open()
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to open device: %v", err)
|
||||
return
|
||||
}
|
||||
defer device.Close()
|
||||
|
||||
controller := Controller{Device: device}
|
||||
|
||||
fmt.Println("Reading data... Press Ctrl+C to exit")
|
||||
|
||||
buf := make([]byte, 32) // Adjust size if needed
|
||||
for {
|
||||
state, err := controller.ReadState(buf)
|
||||
for i, d := range devices {
|
||||
fmt.Printf("Found device %d: %s - VID:%04X PID:%04X\n", i, d.Product, d.VendorID, d.ProductID)
|
||||
|
||||
go func(i int, d hid.DeviceInfo) {
|
||||
device, err := d.Open()
|
||||
if err != nil {
|
||||
fmt.Printf("Read error: %v", err)
|
||||
fmt.Printf("Failed to open device %d: %v\n", i, err)
|
||||
return
|
||||
}
|
||||
defer device.Close()
|
||||
|
||||
if state.Buttons != 0 || state.Joystick.X != 127 || state.Joystick.Y != 127 {
|
||||
fmt.Printf("State changed on device %d\n", i)
|
||||
onStateChange(*state)
|
||||
controller := Controller{Device: device}
|
||||
buf := make([]byte, 32)
|
||||
|
||||
fmt.Printf("Reading data from device %d... Press Ctrl+C to exit\n", i)
|
||||
for {
|
||||
state, err := controller.ReadState(buf)
|
||||
if err != nil {
|
||||
fmt.Printf("Read error on device %d: %v\n", i, err)
|
||||
return
|
||||
}
|
||||
|
||||
state.Id = i
|
||||
|
||||
if state.Buttons != 0 || state.Joystick.X != 127 || state.Joystick.Y != 127 {
|
||||
fmt.Printf("State changed on device %d\n", i)
|
||||
onStateChange(*state)
|
||||
}
|
||||
}
|
||||
}
|
||||
}(i, d)
|
||||
}
|
||||
|
||||
fmt.Println("Out")
|
||||
|
||||
// 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)
|
||||
// }
|
||||
// Prevent the function from exiting
|
||||
select {}
|
||||
}
|
||||
|
||||
// handleJoystickEvent processes joystick events.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user