2014-04-27 17:17:05 -07:00
|
|
|
package neurosky
|
2014-04-26 03:11:51 -07:00
|
|
|
|
|
|
|
import (
|
2014-07-22 18:00:54 -07:00
|
|
|
"io"
|
|
|
|
|
2014-04-26 03:11:51 -07:00
|
|
|
"github.com/hybridgroup/gobot"
|
|
|
|
"github.com/tarm/goserial"
|
|
|
|
)
|
|
|
|
|
2014-11-28 18:01:31 -08:00
|
|
|
var _ gobot.Adaptor = (*NeuroskyAdaptor)(nil)
|
2014-11-16 12:25:48 -08:00
|
|
|
|
2014-04-26 03:11:51 -07:00
|
|
|
type NeuroskyAdaptor struct {
|
2014-11-28 18:01:31 -08:00
|
|
|
name string
|
|
|
|
port string
|
2014-04-27 17:17:05 -07:00
|
|
|
sp io.ReadWriteCloser
|
2014-12-18 14:42:59 -08:00
|
|
|
connect func(*NeuroskyAdaptor) (io.ReadWriteCloser, error)
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
|
2014-10-20 11:00:00 -05:00
|
|
|
// NewNeuroskyAdaptor creates a neurosky adaptor with specified name
|
2014-05-22 20:43:00 -07:00
|
|
|
func NewNeuroskyAdaptor(name string, port string) *NeuroskyAdaptor {
|
2014-04-27 17:17:05 -07:00
|
|
|
return &NeuroskyAdaptor{
|
2014-11-28 18:01:31 -08:00
|
|
|
name: name,
|
|
|
|
port: port,
|
2014-12-18 14:42:59 -08:00
|
|
|
connect: func(n *NeuroskyAdaptor) (io.ReadWriteCloser, error) {
|
|
|
|
return serial.OpenPort(&serial.Config{Name: n.Port(), Baud: 57600})
|
2014-04-27 17:17:05 -07:00
|
|
|
},
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
}
|
2014-11-28 18:01:31 -08:00
|
|
|
func (n *NeuroskyAdaptor) Name() string { return n.name }
|
|
|
|
func (n *NeuroskyAdaptor) Port() string { return n.port }
|
2014-04-26 03:11:51 -07:00
|
|
|
|
2014-10-20 11:00:00 -05:00
|
|
|
// Connect returns true if connection to device is successful
|
2014-11-19 23:21:19 -08:00
|
|
|
func (n *NeuroskyAdaptor) Connect() (errs []error) {
|
2014-12-18 14:42:59 -08:00
|
|
|
if sp, err := n.connect(n); err != nil {
|
2014-11-19 23:21:19 -08:00
|
|
|
return []error{err}
|
2014-12-18 14:42:59 -08:00
|
|
|
} else {
|
|
|
|
n.sp = sp
|
2014-11-19 23:21:19 -08:00
|
|
|
}
|
|
|
|
return
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
|
2014-10-20 11:00:00 -05:00
|
|
|
// Finalize returns true if device finalization is successful
|
2014-11-19 23:21:19 -08:00
|
|
|
func (n *NeuroskyAdaptor) Finalize() (errs []error) {
|
|
|
|
if err := n.sp.Close(); err != nil {
|
|
|
|
return []error{err}
|
|
|
|
}
|
|
|
|
return
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|