/
phidget.go
133 lines (106 loc) · 2.4 KB
/
phidget.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
package phidgets
import (
"github.com/samuelkadolph/go/phidgets/raw"
"time"
)
const (
channelSize = 50
)
type Phidget struct {
Attached <-chan bool
Connected <-chan bool
Detached <-chan bool
Disconnected <-chan bool
Error <-chan error
attached func()
detached func()
rawPhidget *raw.Phidget
}
func (p *Phidget) Class() (raw.Class, error) {
return p.rawPhidget.GetDeviceClass()
}
func (p *Phidget) Close() error {
return p.rawPhidget.Close()
}
func (p *Phidget) ID() (raw.ID, error) {
return p.rawPhidget.GetDeviceID()
}
func (p *Phidget) Label() (string, error) {
return p.rawPhidget.GetDeviceLabel()
}
func (p *Phidget) Name() (string, error) {
return p.rawPhidget.GetDeviceName()
}
func (p *Phidget) Open(c Connector) error {
return c.Open(p)
}
func (p *Phidget) Serial() (int, error) {
return p.rawPhidget.GetSerialNumber()
}
func (p *Phidget) ServerAddress() (string, int, error) {
return p.rawPhidget.GetServerAddress()
}
func (p *Phidget) ServerID() (string, error) {
return p.rawPhidget.GetServerID()
}
func (p *Phidget) ServerStatus() (int, error) {
return p.rawPhidget.GetServerStatus()
}
func (p *Phidget) SetLabel(label string) error {
return p.rawPhidget.SetDeviceLabel(label)
}
func (p *Phidget) Status() (int, error) {
return p.rawPhidget.GetDeviceStatus()
}
func (p *Phidget) Type() (string, error) {
return p.rawPhidget.GetDeviceType()
}
func (p *Phidget) Version() (int, error) {
return p.rawPhidget.GetDeviceVersion()
}
func (p *Phidget) WaitForAttachment(timeout time.Duration) error {
err := p.rawPhidget.WaitForAttachment(timeout)
if err == nil && p.attached != nil {
p.attached()
}
return err
}
func (p *Phidget) initPhidget(r *raw.Phidget) error {
p.rawPhidget = r
attached := make(chan bool)
connected := make(chan bool)
detached := make(chan bool)
disconnected := make(chan bool)
p.Attached = attached
p.Connected = connected
p.Detached = detached
p.Disconnected = disconnected
p.Error = p.rawPhidget.Error
go func() {
for v := range p.rawPhidget.Attached {
if p.attached != nil {
p.attached()
}
attached <- v
}
}()
go func() {
for v := range p.rawPhidget.Connected {
if p.detached != nil {
p.detached()
}
connected <- v
}
}()
go func() {
for v := range p.rawPhidget.Detached {
detached <- v
}
}()
go func() {
for v := range p.rawPhidget.Disconnected {
disconnected <- v
}
}()
return nil
}