This repository has been archived by the owner on Sep 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvrpn.go
69 lines (62 loc) · 1.63 KB
/
vrpn.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
package main
import (
"github.com/Laremere/vrpn-webapp/vrpn"
"log"
"strconv"
"time"
)
//VrpnType is how the device should be prepresented in VRPN
type VrpnType uint8
const (
//VrpnButton represents a vrpn true or false value
VrpnButton = VrpnType(iota)
//VrpnAnalog represents a vrpn float value
VrpnAnalog
)
//VrpnServe handles starting a vrpn server and broadcasting events over it.
func VrpnServe(port uint16, devices []DeviceConfig) {
conn := vrpn.NewConnection(int(port))
vrpnButtonDevices := make(map[string]*vrpn.Button)
vrpnAnalogDevices := make(map[string]*vrpn.Analog)
for _, device := range devices {
name := device.GetName()
if device.VrpnType() == VrpnButton {
vrpnButtonDevices[name] = conn.NewButton(name, 1)
} else {
vrpnAnalogDevices[name] = conn.NewAnalog(name, 1)
}
}
events := make(chan *Event)
Subscribe <- events
ticker := time.Tick(time.Second)
for {
select {
case event := <-events:
if button, ok := vrpnButtonDevices[event.Name]; ok {
val, err := strconv.ParseBool(event.Value)
if err != nil {
log.Println("Error parsing button value,", err)
continue
}
button.Update([]bool{val})
} else if analog, ok := vrpnAnalogDevices[event.Name]; ok {
val, err := strconv.ParseFloat(event.Value, 64)
if err != nil {
log.Println("Error parsing analog value,", err)
continue
}
analog.Update([]float64{val})
} else {
log.Println("Unkown device identity,", event.Name)
}
case <-ticker:
}
for _, button := range vrpnButtonDevices {
button.Mainloop()
}
for _, analog := range vrpnAnalogDevices {
analog.Mainloop()
}
conn.Mainloop()
}
}