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