-
Notifications
You must be signed in to change notification settings - Fork 7
/
joysticks.go
401 lines (356 loc) · 10 KB
/
joysticks.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package joysticks
import (
"math"
"time"
//"fmt"
)
var LongPressDelay = time.Second / 2
var DoublePressDelay = time.Second / 10
type hatAxis struct {
number uint8
axis uint8
reversed bool
time time.Duration
value float32
}
type button struct {
number uint8
time time.Duration
value bool
}
type eventType uint8
const (
buttonChange eventType = iota
buttonClose
buttonOpen
buttonLongPress
buttonDoublePress
hatChange
hatPanX
hatPanY
hatPosition
hatAngle
hatRadius
hatCentered
hatEdge
hatVelocityX
hatVelocityY
)
// signature of an event
type eventSignature struct {
eventType
number uint8
}
// HID holds the in-coming event channel, available button and hat indexes, and registered events, for a human interface device.
// It has methods to control and adjust behaviour.
type HID struct {
OSEvents chan osEventRecord
Buttons map[uint8]button
HatAxes map[uint8]hatAxis
Events map[eventSignature]chan Event
}
// Events always have the time they occurred.
type Event interface {
Moment() time.Duration
}
type when struct {
Time time.Duration
}
func (b when) Moment() time.Duration {
return b.Time
}
// button changed
type ButtonEvent struct {
when
number uint8
value bool
}
// hat changed
type HatEvent struct {
when
number uint8
axis uint8
value float32
}
// Hat position event type. X,Y{-1...1}
type CoordsEvent struct {
when
X, Y float32
}
// Hat Axis event type. V{-1...1}
type AxisEvent struct {
when
V float32
}
// Hat angle event type. Angle{-Pi...Pi}
type AngleEvent struct {
when
Angle float32
}
// Hat radius event type. Radius{0...√2}
type RadiusEvent struct {
when
Radius float32
}
// ParcelOutEvents waits on the HID.OSEvents channel (so is blocking), then puts any events matching onto any registered channel(s).
func (d HID) ParcelOutEvents() {
for evt := range d.OSEvents {
switch evt.Type {
case 1:
b := d.Buttons[evt.Index]
if c, ok := d.Events[eventSignature{buttonChange, b.number}]; ok {
c <- ButtonEvent{when{toDuration(evt.Time)}, b.number, evt.Value == 1}
}
if evt.Value == 0 {
if c, ok := d.Events[eventSignature{buttonOpen, b.number}]; ok {
c <- when{toDuration(evt.Time)}
}
if c, ok := d.Events[eventSignature{buttonLongPress, b.number}]; ok {
if toDuration(evt.Time) > b.time+LongPressDelay {
c <- when{toDuration(evt.Time)}
}
}
}
if evt.Value == 1 {
if c, ok := d.Events[eventSignature{buttonClose, b.number}]; ok {
c <- when{toDuration(evt.Time)}
}
if c, ok := d.Events[eventSignature{buttonDoublePress, b.number}]; ok {
if toDuration(evt.Time) < b.time+DoublePressDelay {
c <- when{toDuration(evt.Time)}
}
}
}
d.Buttons[evt.Index] = button{b.number, toDuration(evt.Time), evt.Value != 0}
case 2:
h := d.HatAxes[evt.Index]
v := float32(evt.Value) / maxValue
if h.reversed {
v = -v
}
if c, ok := d.Events[eventSignature{hatChange, h.number}]; ok {
c <- HatEvent{when{toDuration(evt.Time)}, h.number, h.axis, v}
}
switch h.axis {
case 1:
if c, ok := d.Events[eventSignature{hatPanY, h.number}]; ok {
c <- AxisEvent{when{toDuration(evt.Time)}, v}
}
if c, ok := d.Events[eventSignature{hatVelocityY, h.number}]; ok {
c <- AxisEvent{when{toDuration(evt.Time)}, (v-d.HatAxes[evt.Index].value)/float32((toDuration(evt.Time)-d.HatAxes[evt.Index].time).Seconds())}
}
case 2:
if c, ok := d.Events[eventSignature{hatPanX, h.number}]; ok {
c <- AxisEvent{when{toDuration(evt.Time)}, v}
}
if c, ok := d.Events[eventSignature{hatVelocityX, h.number}]; ok {
c <- AxisEvent{when{toDuration(evt.Time)}, (v-d.HatAxes[evt.Index].value)/float32((toDuration(evt.Time)-d.HatAxes[evt.Index].time).Seconds())}
}
}
if c, ok := d.Events[eventSignature{hatPosition, h.number}]; ok {
switch h.axis {
case 1:
c <- CoordsEvent{when{toDuration(evt.Time)}, v ,d.HatAxes[evt.Index+1].value}
case 2:
c <- CoordsEvent{when{toDuration(evt.Time)}, d.HatAxes[evt.Index-1].value,v}
}
}
if c, ok := d.Events[eventSignature{hatAngle, h.number}]; ok {
switch h.axis {
case 1:
c <- AngleEvent{when{toDuration(evt.Time)}, float32(math.Atan2(float64(d.HatAxes[evt.Index+1].value), float64(v)))}
case 2:
c <- AngleEvent{when{toDuration(evt.Time)}, float32(math.Atan2(float64(v), float64(d.HatAxes[evt.Index-1].value)))}
}
}
if c, ok := d.Events[eventSignature{hatRadius, h.number}]; ok {
switch h.axis {
case 1:
c <- RadiusEvent{when{toDuration(evt.Time)}, float32(math.Sqrt(float64(d.HatAxes[evt.Index+1].value)*float64(d.HatAxes[evt.Index+1].value) + float64(v)*float64(v)))}
case 2:
c <- RadiusEvent{when{toDuration(evt.Time)}, float32(math.Sqrt(float64(v)*float64(v) + float64(d.HatAxes[evt.Index-1].value)*float64(d.HatAxes[evt.Index-1].value)))}
}
}
if c, ok := d.Events[eventSignature{hatEdge, h.number}]; ok {
// fmt.Println(v,h)
if (v == 1 || v == -1) && h.value != 1 && h.value != -1 {
switch h.axis {
case 1:
c <- AngleEvent{when{toDuration(evt.Time)}, float32(math.Atan2(float64(d.HatAxes[evt.Index+1].value), float64(v)))}
case 2:
c <- AngleEvent{when{toDuration(evt.Time)}, float32(math.Atan2(float64(v), float64(d.HatAxes[evt.Index-1].value)))}
}
}
}
if c, ok := d.Events[eventSignature{hatCentered, h.number}]; ok {
if v == 0 && h.value != 0 {
switch h.axis {
case 2:
if d.HatAxes[evt.Index-1].value == 0 {
c <- when{toDuration(evt.Time)}
}
case 1:
if d.HatAxes[evt.Index+1].value == 0 {
c <- when{toDuration(evt.Time)}
}
}
}
}
d.HatAxes[evt.Index] = hatAxis{h.number, h.axis, h.reversed, toDuration(evt.Time), v}
default:
// log.Println("unknown input type. ",evt.Type & 0x7f)
}
}
}
// Type of register-able methods and the index they are called with. (Note: the event type is indicated by the method.)
type Channel struct {
Number uint8
Method func(HID, uint8) chan Event
}
// Capture is highlevel automation of the setup of event channels.
// Returned is a slice of chan's, matching each registree, which then receive events of the type and index the registree indicated.
// It uses the first available joystick, from a max of 4.
// Since it doesn't return a HID object, channels are immutable.
func Capture(registrees ...Channel) []chan Event {
d := Connect(1)
for i := 2; d == nil && i < 5; i++ {
d = Connect(i)
}
if d == nil {
return nil
}
go d.ParcelOutEvents()
chans := make([]chan Event, len(registrees))
for i, fns := range registrees {
chans[i] = fns.Method(*d, fns.Number)
}
return chans
}
// button changes event channel.
func (d HID) OnButton(index uint8) chan Event {
c := make(chan Event)
d.Events[eventSignature{buttonChange, index}] = c
return c
}
// button goes open event channel.
func (d HID) OnOpen(index uint8) chan Event {
c := make(chan Event)
d.Events[eventSignature{buttonOpen, index}] = c
return c
}
// button goes closed event channel.
func (d HID) OnClose(index uint8) chan Event {
c := make(chan Event)
d.Events[eventSignature{buttonClose, index}] = c
return c
}
// button goes open and the previous event, closed, was more than LongPressDelay ago, event channel.
func (d HID) OnLong(index uint8) chan Event {
c := make(chan Event)
d.Events[eventSignature{buttonLongPress, index}] = c
return c
}
// button goes closed and the previous event, open, was less than DoublePressDelay ago, event channel.
func (d HID) OnDouble(index uint8) chan Event {
c := make(chan Event)
d.Events[eventSignature{buttonDoublePress, index}] = c
return c
}
// hat moved event channel.
func (d HID) OnHat(index uint8) chan Event {
c := make(chan Event)
d.Events[eventSignature{hatChange, index}] = c
return c
}
// hat position changed event channel.
func (d HID) OnMove(index uint8) chan Event {
c := make(chan Event)
d.Events[eventSignature{hatPosition, index}] = c
return c
}
// hat axis-X moved event channel.
func (d HID) OnPanX(index uint8) chan Event {
c := make(chan Event)
d.Events[eventSignature{hatPanX, index}] = c
return c
}
// hat axis-Y moved event channel.
func (d HID) OnPanY(index uint8) chan Event {
c := make(chan Event)
d.Events[eventSignature{hatPanY, index}] = c
return c
}
// hat axis-X speed changed event channel.
func (d HID) OnSpeedX(index uint8) chan Event {
c := make(chan Event)
d.Events[eventSignature{hatVelocityX, index}] = c
return c
}
// hat axis-Y speed changed event channel.
func (d HID) OnSpeedY(index uint8) chan Event {
c := make(chan Event)
d.Events[eventSignature{hatVelocityY, index}] = c
return c
}
// hat angle changed event channel.
func (d HID) OnRotate(index uint8) chan Event {
c := make(chan Event)
d.Events[eventSignature{hatAngle, index}] = c
return c
}
// hat moved event channel.
func (d HID) OnCenter(index uint8) chan Event {
c := make(chan Event)
d.Events[eventSignature{hatCentered, index}] = c
return c
}
// hat moved to edge
func (d HID) OnEdge(index uint8) chan Event {
c := make(chan Event)
d.Events[eventSignature{hatEdge, index}] = c
return c
}
// hat integrate
//func (d HID) OnIntegrate(c Channel) chan Event {
// var e,le Event
// e:=Event{}
// c := make(chan Event)
// d.Events[eventSignature{hatEdge, hat}] = c
// return c
//}
// see if Button exists.
func (d HID) ButtonExists(index uint8) (ok bool) {
for _, v := range d.Buttons {
if v.number == index {
return true
}
}
return
}
// see if Hat exists.
func (d HID) HatExists(index uint8) (ok bool) {
for _, v := range d.HatAxes {
if v.number == index {
return true
}
}
return
}
// Button current state.
func (d HID) ButtonClosed(index uint8) bool {
return d.Buttons[index].value
}
// Hat latest position.
// provided coords slice needs to be long enough to hold all the hat's axis.
func (d HID) HatCoords(index uint8, coords []float32) {
for _, h := range d.HatAxes {
if h.number == index {
coords[h.axis-1] = h.value
}
}
return
}
// insert events as if from hardware.
func (d HID) InsertSyntheticEvent(v int16, t uint8, i uint8) {
d.OSEvents <- osEventRecord{Value: v, Type: t, Index: i}
}