forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.go
68 lines (57 loc) · 1.53 KB
/
scanner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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())
}