hybridgroup.gobot/platforms/neurosky/neurosky_adaptor.go

46 lines
993 B
Go
Raw Normal View History

2014-04-28 08:17:05 +08:00
package neurosky
import (
2014-07-23 09:00:54 +08:00
"io"
"github.com/tarm/serial"
)
type Adaptor struct {
name string
port string
2014-04-28 08:17:05 +08:00
sp io.ReadWriteCloser
connect func(*Adaptor) (io.ReadWriteCloser, error)
}
// NewAdaptor creates a neurosky adaptor with specified port
func NewAdaptor(port string) *Adaptor {
return &Adaptor{
name: "Neurosky",
port: port,
connect: func(n *Adaptor) (io.ReadWriteCloser, error) {
2014-12-19 06:42:59 +08:00
return serial.OpenPort(&serial.Config{Name: n.Port(), Baud: 57600})
2014-04-28 08:17:05 +08:00
},
}
}
func (n *Adaptor) Name() string { return n.name }
func (n *Adaptor) SetName(name string) { n.name = name }
func (n *Adaptor) Port() string { return n.port }
// Connect returns true if connection to device is successful
func (n *Adaptor) Connect() error {
2014-12-19 06:42:59 +08:00
if sp, err := n.connect(n); err != nil {
return err
2014-12-19 06:42:59 +08:00
} else {
n.sp = sp
}
return nil
}
// Finalize returns true if device finalization is successful
func (n *Adaptor) Finalize() (err error) {
err = n.sp.Close()
return
}