forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
neurosky_adaptor.go
45 lines (39 loc) · 1.05 KB
/
neurosky_adaptor.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
package neurosky
import (
"io"
"github.com/tarm/goserial"
)
type NeuroskyAdaptor struct {
name string
port string
sp io.ReadWriteCloser
connect func(*NeuroskyAdaptor) (io.ReadWriteCloser, error)
}
// NewNeuroskyAdaptor creates a neurosky adaptor with specified name
func NewNeuroskyAdaptor(name string, port string) *NeuroskyAdaptor {
return &NeuroskyAdaptor{
name: name,
port: port,
connect: func(n *NeuroskyAdaptor) (io.ReadWriteCloser, error) {
return serial.OpenPort(&serial.Config{Name: n.Port(), Baud: 57600})
},
}
}
func (n *NeuroskyAdaptor) Name() string { return n.name }
func (n *NeuroskyAdaptor) Port() string { return n.port }
// Connect returns true if connection to device is successful
func (n *NeuroskyAdaptor) Connect() (errs []error) {
if sp, err := n.connect(n); err != nil {
return []error{err}
} else {
n.sp = sp
}
return
}
// Finalize returns true if device finalization is successful
func (n *NeuroskyAdaptor) Finalize() (errs []error) {
if err := n.sp.Close(); err != nil {
return []error{err}
}
return
}