gobot.io/x/gobot/v2@v2.1.0/platforms/neurosky/neurosky_adaptor.go (about) 1 // Package neurosky is the Gobot platform for the Neurosky Mindwave EEG 2 package neurosky 3 4 import ( 5 "io" 6 7 "go.bug.st/serial" 8 ) 9 10 // Adaptor is the Gobot Adaptor for the Neurosky Mindwave 11 type Adaptor struct { 12 name string 13 port string 14 sp io.ReadWriteCloser 15 connect func(*Adaptor) (io.ReadWriteCloser, error) 16 } 17 18 // NewAdaptor creates a neurosky adaptor with specified port 19 func NewAdaptor(port string) *Adaptor { 20 return &Adaptor{ 21 name: "Neurosky", 22 port: port, 23 connect: func(n *Adaptor) (io.ReadWriteCloser, error) { 24 return serial.Open(n.Port(), &serial.Mode{BaudRate: 57600}) 25 }, 26 } 27 } 28 29 // Name returns the Adaptor Name 30 func (n *Adaptor) Name() string { return n.name } 31 32 // SetName sets the Adaptor Name 33 func (n *Adaptor) SetName(name string) { n.name = name } 34 35 // Port returns the Adaptor port 36 func (n *Adaptor) Port() string { return n.port } 37 38 // Connect returns true if connection to device is successful 39 func (n *Adaptor) Connect() error { 40 sp, err := n.connect(n) 41 if err != nil { 42 return err 43 } 44 45 n.sp = sp 46 return nil 47 } 48 49 // Finalize returns true if device finalization is successful 50 func (n *Adaptor) Finalize() (err error) { 51 err = n.sp.Close() 52 return 53 }