joystick: add CLI utilty to scan display events to make it easier to add new joyticks

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram 2017-09-12 21:07:53 +02:00
parent 6b51c9da85
commit cf35f0e692
2 changed files with 90 additions and 0 deletions

View File

@ -235,3 +235,25 @@ func main() {
robot.Start()
}
```
## How to Add A New Joystick
In the `bin` directory for this package is a CLI utility program that scans for SDL joystick events, and displays the ID and value:
```
$ go run ./platforms/joystick/bin/scanner.go
Joystick 0 connected
[6625 ms] Axis: 1 value:-22686
[6641 ms] Axis: 1 value:-32768
[6836 ms] Axis: 1 value:-18317
[6852 ms] Axis: 1 value:0
[8663 ms] Axis: 3 value:-32768
[8873 ms] Axis: 3 value:0
[10183 ms] Axis: 0 value:-24703
[10183 ms] Axis: 0 value:-32768
[10313 ms] Axis: 1 value:-3193
[10329 ms] Axis: 1 value:0
[10345 ms] Axis: 0 value:0
```
You can use the output from this program to create a JSON file for the various buttons and axes on your joystick/gamepad.

View File

@ -0,0 +1,68 @@
// Joystick scanner
// Based on original code from Jacky Boen
// https://github.com/veandco/go-sdl2/blob/master/examples/events/events.go
package main
import (
"fmt"
"os"
"github.com/veandco/go-sdl2/sdl"
)
var joysticks [16]*sdl.Joystick
func run() int {
var event sdl.Event
var running bool
sdl.Init(sdl.INIT_JOYSTICK)
defer sdl.Quit()
sdl.JoystickEventState(sdl.ENABLE)
running = true
for running {
for event = sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch t := event.(type) {
case *sdl.QuitEvent:
running = false
case *sdl.JoyAxisEvent:
fmt.Printf("[%d ms] Axis: %d\tvalue:%d\n",
t.Timestamp, t.Axis, t.Value)
case *sdl.JoyBallEvent:
fmt.Printf("[%d ms] Ball:%d\txrel:%d\tyrel:%d\n",
t.Timestamp, t.Ball, t.XRel, t.YRel)
case *sdl.JoyButtonEvent:
fmt.Printf("[%d ms] Button:%d\tstate:%d\n",
t.Timestamp, t.Button, t.State)
case *sdl.JoyHatEvent:
fmt.Printf("[%d ms] Hat:%d\tvalue:%d\n",
t.Timestamp, t.Hat, t.Value)
case *sdl.JoyDeviceEvent:
if t.Type == sdl.JOYDEVICEADDED {
joysticks[int(t.Which)] = sdl.JoystickOpen(t.Which)
if joysticks[int(t.Which)] != nil {
fmt.Printf("Joystick %d connected\n", t.Which)
}
} else if t.Type == sdl.JOYDEVICEREMOVED {
if joystick := joysticks[int(t.Which)]; joystick != nil {
joystick.Close()
}
fmt.Printf("Joystick %d disconnected\n", t.Which)
}
default:
fmt.Printf("Unknown event\n")
}
}
sdl.Delay(16)
}
return 0
}
func main() {
os.Exit(run())
}