tristan-wip #2
@ -1,4 +1,5 @@
|
|||||||
export interface ControllerState {
|
export interface ControllerState {
|
||||||
|
id?: number,
|
||||||
joystick: {
|
joystick: {
|
||||||
x: number,
|
x: number,
|
||||||
y: number,
|
y: number,
|
||||||
|
|||||||
@ -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
|
|
||||||
|
|
||||||
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)
|
|
||||||
|
|
||||||
|
go func(i int, d hid.DeviceInfo) {
|
||||||
|
device, err := d.Open()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Read error: %v", err)
|
fmt.Printf("Failed to open device %d: %v\n", i, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
defer device.Close()
|
||||||
|
|
||||||
if state.Buttons != 0 || state.Joystick.X != 127 || state.Joystick.Y != 127 {
|
controller := Controller{Device: device}
|
||||||
fmt.Printf("State changed on device %d\n", i)
|
buf := make([]byte, 32)
|
||||||
onStateChange(*state)
|
|
||||||
|
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")
|
// 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.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user