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"
|
|
|
|
)
|
|
|
|
|
|
|
|
type NeuroskyAdaptor struct {
|
|
|
|
gobot.Adaptor
|
2014-04-27 17:17:05 -07:00
|
|
|
sp io.ReadWriteCloser
|
2014-07-22 18:00:54 -07:00
|
|
|
connect func(*NeuroskyAdaptor)
|
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-07-07 17:27:10 -07:00
|
|
|
Adaptor: *gobot.NewAdaptor(
|
|
|
|
name,
|
|
|
|
"NeuroskyAdaptor",
|
|
|
|
port,
|
|
|
|
),
|
2014-07-22 18:00:54 -07:00
|
|
|
connect: func(n *NeuroskyAdaptor) {
|
|
|
|
sp, err := serial.OpenPort(&serial.Config{Name: n.Port(), Baud: 57600})
|
2014-04-27 17:17:05 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-07-22 18:00:54 -07:00
|
|
|
n.sp = sp
|
2014-04-27 17:17:05 -07:00
|
|
|
},
|
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-04-27 17:17:05 -07:00
|
|
|
func (n *NeuroskyAdaptor) Connect() bool {
|
2014-07-22 18:00:54 -07:00
|
|
|
n.connect(n)
|
2014-07-07 17:27:10 -07:00
|
|
|
n.SetConnected(true)
|
2014-04-26 03:11:51 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-10-20 11:00:00 -05:00
|
|
|
// Finalize returns true if device finalization is successful
|
2014-04-27 17:17:05 -07:00
|
|
|
func (n *NeuroskyAdaptor) Finalize() bool {
|
|
|
|
n.sp.Close()
|
2014-07-07 17:27:10 -07:00
|
|
|
n.SetConnected(false)
|
2014-04-26 03:11:51 -07:00
|
|
|
return true
|
|
|
|
}
|