forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
joystick: add CLI utilty to scan display events to make it easier to …
…add new joyticks Signed-off-by: deadprogram <[email protected]>
- Loading branch information
1 parent
6b51c9d
commit cf35f0e
Showing
2 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) | ||
} |