/
joystick_driver.go
245 lines (216 loc) · 5.65 KB
/
joystick_driver.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package joystick
import (
"encoding/json"
"fmt"
"io/ioutil"
"time"
"github.com/veandco/go-sdl2/sdl"
"gobot.io/x/gobot"
)
const (
// Dualshock3 joystick configuration.
Dualshock3 = "dualshock3"
// Dualshock4 joystick configuration.
Dualshock4 = "dualshock4"
// TFlightHotasX flight stick configuration.
TFlightHotasX = "tflightHotasX"
// Xbox360 joystick configuration.
Xbox360 = "xbox360"
// Xbox360RockBandDrums controller configuration.
Xbox360RockBandDrums = "xbox360RockBandDrums"
// Nvidia Shield TV Controller
Shield = "shield"
)
// Driver represents a joystick
type Driver struct {
name string
interval time.Duration
connection gobot.Connection
configPath string
config joystickConfig
poll func() sdl.Event
halt chan bool
gobot.Eventer
}
// pair is a JSON representation of name and id
type pair struct {
Name string `json:"name"`
ID int `json:"id"`
}
// hat is a JSON representation of hat, name and id
type hat struct {
Hat int `json:"hat"`
Name string `json:"name"`
ID int `json:"id"`
}
// joystickConfig is a JSON representation of configuration values
type joystickConfig struct {
Name string `json:"name"`
GUID string `json:"guid"`
Axis []pair `json:"axis"`
Buttons []pair `json:"buttons"`
Hats []hat `json:"Hats"`
}
// NewDriver returns a new Driver with a polling interval of
// 10 Milliseconds given a Joystick Adaptor and json button configuration
// file location.
//
// Optionally accepts:
// time.Duration: Interval at which the Driver is polled for new information
func NewDriver(a *Adaptor, config string, v ...time.Duration) *Driver {
d := &Driver{
name: gobot.DefaultName("Joystick"),
connection: a,
Eventer: gobot.NewEventer(),
configPath: config,
poll: func() sdl.Event {
return sdl.PollEvent()
},
interval: 10 * time.Millisecond,
halt: make(chan bool, 0),
}
if len(v) > 0 {
d.interval = v[0]
}
d.AddEvent("error")
return d
}
// Name returns the Drivers name
func (j *Driver) Name() string { return j.name }
// SetName sets the Drivers name
func (j *Driver) SetName(n string) { j.name = n }
// Connection returns the Drivers connection
func (j *Driver) Connection() gobot.Connection { return j.connection }
// adaptor returns joystick adaptor
func (j *Driver) adaptor() *Adaptor {
return j.Connection().(*Adaptor)
}
// Start and polls the state of the joystick at the given interval.
//
// Emits the Events:
// Error error - On button error
// Events defined in the json button configuration file.
// They will have the format:
// [button]_press
// [button]_release
// [axis]
func (j *Driver) Start() (err error) {
switch j.configPath {
case "dualshock3":
j.config = dualshock3Config
case "dualshock4":
j.config = dualshock4Config
case "tflightHotasX":
j.config = tflightHotasXConfig
case "xbox360":
j.config = xbox360Config
case "xbox360RockBandDrums":
j.config = xbox360RockBandDrumsConfig
case "shield":
j.config = shieldConfig
default:
err := j.loadFile()
if err != nil {
return err
}
}
for _, value := range j.config.Buttons {
j.AddEvent(fmt.Sprintf("%s_press", value.Name))
j.AddEvent(fmt.Sprintf("%s_release", value.Name))
}
for _, value := range j.config.Axis {
j.AddEvent(value.Name)
}
for _, value := range j.config.Hats {
j.AddEvent(fmt.Sprintf("%s_press", value.Name))
j.AddEvent(fmt.Sprintf("%s_release", value.Name))
}
go func() {
for {
for event := j.poll(); event != nil; event = j.poll() {
if errs := j.handleEvent(event); errs != nil {
j.Publish(j.Event("error"), errs)
}
}
select {
case <-time.After(j.interval):
case <-j.halt:
return
}
}
}()
return
}
// Halt stops joystick driver
func (j *Driver) Halt() (err error) {
j.halt <- true
return
}
var previousHat = ""
// HandleEvent publishes an specific event according to data received
func (j *Driver) handleEvent(event sdl.Event) error {
switch data := event.(type) {
case *sdl.JoyAxisEvent:
if data.Which == j.adaptor().joystick.InstanceID() {
axis := j.findName(data.Axis, j.config.Axis)
if axis == "" {
return fmt.Errorf("Unknown Axis: %v", data.Axis)
}
j.Publish(j.Event(axis), data.Value)
}
case *sdl.JoyButtonEvent:
if data.Which == j.adaptor().joystick.InstanceID() {
button := j.findName(data.Button, j.config.Buttons)
if button == "" {
return fmt.Errorf("Unknown Button: %v", data.Button)
}
if data.State == 1 {
j.Publish(j.Event(fmt.Sprintf("%s_press", button)), nil)
} else {
j.Publish(j.Event(fmt.Sprintf("%s_release", button)), nil)
}
}
case *sdl.JoyHatEvent:
if data.Which == j.adaptor().joystick.InstanceID() {
hat := j.findHatName(data.Value, data.Hat, j.config.Hats)
if hat == "" {
return fmt.Errorf("Unknown Hat: %v %v", data.Hat, data.Value)
} else if hat == "released" {
hat = previousHat
j.Publish(j.Event(fmt.Sprintf("%s_release", hat)), true)
} else {
previousHat = hat
j.Publish(j.Event(fmt.Sprintf("%s_press", hat)), true)
}
}
}
return nil
}
func (j *Driver) findName(id uint8, list []pair) string {
for _, value := range list {
if int(id) == value.ID {
return value.Name
}
}
return ""
}
// findHatName returns name from hat found by id in provided list
func (j *Driver) findHatName(id uint8, hat uint8, list []hat) string {
for _, lHat := range list {
if int(id) == lHat.ID && int(hat) == lHat.Hat {
return lHat.Name
}
}
return ""
}
// loadFile load the joystick config from a .json file
func (j *Driver) loadFile() error {
file, e := ioutil.ReadFile(j.configPath)
if e != nil {
return e
}
var jsontype joystickConfig
json.Unmarshal(file, &jsontype)
j.config = jsontype
return nil
}