-
Notifications
You must be signed in to change notification settings - Fork 17
/
joysticktest.go
102 lines (86 loc) · 1.87 KB
/
joysticktest.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Simple program that displays the state of the specified joystick
//
// go run joysticktest.go 2
// displays state of joystick id 2
package main
import (
"fmt"
"github.com/nsf/termbox-go"
"github.com/simulatedsimian/joystick"
"os"
"strconv"
"time"
)
func printAt(x, y int, s string) {
for _, r := range s {
termbox.SetCell(x, y, r, termbox.ColorDefault, termbox.ColorDefault)
x++
}
}
func readJoystick(js joystick.Joystick) {
jinfo, err := js.Read()
if err != nil {
printAt(1, 5, "Error: "+err.Error())
return
}
printAt(1, 5, "Buttons:")
for button := 0; button < js.ButtonCount(); button++ {
if jinfo.Buttons&(1<<uint32(button)) != 0 {
printAt(10+button, 5, "X")
} else {
printAt(10+button, 5, ".")
}
}
for axis := 0; axis < js.AxisCount(); axis++ {
printAt(1, axis+7, fmt.Sprintf("Axis %2d Value: %7d", axis, jinfo.AxisData[axis]))
}
return
}
func main() {
jsid := 0
if len(os.Args) > 1 {
i, err := strconv.Atoi(os.Args[1])
if err != nil {
fmt.Println(err)
return
}
jsid = i
}
js, jserr := joystick.Open(jsid)
if jserr != nil {
fmt.Println(jserr)
return
}
err := termbox.Init()
if err != nil {
panic(err)
}
defer termbox.Close()
eventQueue := make(chan termbox.Event)
go func() {
for {
eventQueue <- termbox.PollEvent()
}
}()
ticker := time.NewTicker(time.Millisecond * 40)
for doQuit := false; !doQuit; {
select {
case ev := <-eventQueue:
if ev.Type == termbox.EventKey {
if ev.Ch == 'q' {
doQuit = true
}
}
if ev.Type == termbox.EventResize {
termbox.Flush()
}
case <-ticker.C:
printAt(1, 0, "-- Press 'q' to Exit --")
printAt(1, 1, fmt.Sprintf("Joystick Name: %s", js.Name()))
printAt(1, 2, fmt.Sprintf(" Axis Count: %d", js.AxisCount()))
printAt(1, 3, fmt.Sprintf(" Button Count: %d", js.ButtonCount()))
readJoystick(js)
termbox.Flush()
}
}
}