Skip to content

Commit

Permalink
core: Refactor Neurosky platform for new Adaptor/Driver creation sign…
Browse files Browse the repository at this point in the history
…atures

Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Oct 1, 2016
1 parent 9140a5d commit d210367
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 45 deletions.
6 changes: 3 additions & 3 deletions platforms/neurosky/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ import (
func main() {
gbot := gobot.NewGobot()

adaptor := neurosky.NewNeuroskyAdaptor("neurosky", "/dev/rfcomm0")
neuro := neurosky.NewNeuroskyDriver(adaptor, "neuro")
adaptor := neurosky.NewAdaptor("/dev/rfcomm0")
neuro := neurosky.NewDriver(adaptor)

work := func() {
gobot.On(neuro.Event("extended"), func(data interface{}) {
Expand Down Expand Up @@ -104,4 +104,4 @@ func main() {
gbot.AddRobot(robot)
gbot.Start()
}
```
```
4 changes: 2 additions & 2 deletions platforms/neurosky/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ Example:
func main() {
gbot := gobot.NewGobot()
adaptor := neurosky.NewNeuroskyAdaptor("neurosky", "/dev/rfcomm0")
neuro := neurosky.NewNeuroskyDriver(adaptor, "neuro")
adaptor := neurosky.NewAdaptor("/dev/rfcomm0")
neuro := neurosky.NewDriver(adaptor)
work := func() {
gobot.On(neuro.Event("extended"), func(data interface{}) {
Expand Down
23 changes: 12 additions & 11 deletions platforms/neurosky/neurosky_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,29 @@ import (
"github.com/tarm/goserial"
)

type NeuroskyAdaptor struct {
type Adaptor struct {
name string
port string
sp io.ReadWriteCloser
connect func(*NeuroskyAdaptor) (io.ReadWriteCloser, error)
connect func(*Adaptor) (io.ReadWriteCloser, error)
}

// NewNeuroskyAdaptor creates a neurosky adaptor with specified name
func NewNeuroskyAdaptor(name string, port string) *NeuroskyAdaptor {
return &NeuroskyAdaptor{
name: name,
// NewAdaptor creates a neurosky adaptor with specified port
func NewAdaptor(port string) *Adaptor {
return &Adaptor{
port: port,
connect: func(n *NeuroskyAdaptor) (io.ReadWriteCloser, error) {
connect: func(n *Adaptor) (io.ReadWriteCloser, error) {
return serial.OpenPort(&serial.Config{Name: n.Port(), Baud: 57600})
},
}
}
func (n *NeuroskyAdaptor) Name() string { return n.name }
func (n *NeuroskyAdaptor) Port() string { return n.port }

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 *NeuroskyAdaptor) Connect() (errs []error) {
func (n *Adaptor) Connect() (errs []error) {
if sp, err := n.connect(n); err != nil {
return []error{err}
} else {
Expand All @@ -37,7 +38,7 @@ func (n *NeuroskyAdaptor) Connect() (errs []error) {
}

// Finalize returns true if device finalization is successful
func (n *NeuroskyAdaptor) Finalize() (errs []error) {
func (n *Adaptor) Finalize() (errs []error) {
if err := n.sp.Close(); err != nil {
return []error{err}
}
Expand Down
14 changes: 7 additions & 7 deletions platforms/neurosky/neurosky_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/hybridgroup/gobot/gobottest"
)

var _ gobot.Adaptor = (*NeuroskyAdaptor)(nil)
var _ gobot.Adaptor = (*Adaptor)(nil)

type NullReadWriteCloser struct{}

Expand All @@ -29,24 +29,24 @@ func (NullReadWriteCloser) Close() error {
return closeError
}

func initTestNeuroskyAdaptor() *NeuroskyAdaptor {
a := NewNeuroskyAdaptor("bot", "/dev/null")
a.connect = func(n *NeuroskyAdaptor) (io.ReadWriteCloser, error) {
func initTestNeuroskyAdaptor() *Adaptor {
a := NewAdaptor("/dev/null")
a.connect = func(n *Adaptor) (io.ReadWriteCloser, error) {
return &NullReadWriteCloser{}, nil
}
return a
}

func TestNeuroskyAdaptor(t *testing.T) {
a := NewNeuroskyAdaptor("bot", "/dev/null")
gobottest.Assert(t, a.Name(), "bot")
a := NewAdaptor("/dev/null")
gobottest.Assert(t, a.Port(), "/dev/null")
}

func TestNeuroskyAdaptorConnect(t *testing.T) {
a := initTestNeuroskyAdaptor()
gobottest.Assert(t, len(a.Connect()), 0)

a.connect = func(n *NeuroskyAdaptor) (io.ReadWriteCloser, error) {
a.connect = func(n *Adaptor) (io.ReadWriteCloser, error) {
return nil, errors.New("connection error")
}
gobottest.Assert(t, a.Connect()[0], errors.New("connection error"))
Expand Down
30 changes: 15 additions & 15 deletions platforms/neurosky/neurosky_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const CodeWave byte = 0x80
// ASIC EEG POWER 8 3-byte big-endian integers
const CodeAsicEEG byte = 0x83

type NeuroskyDriver struct {
type Driver struct {
name string
connection gobot.Connection
gobot.Eventer
Expand All @@ -46,7 +46,7 @@ type EEG struct {
MidGamma int
}

// NewNeuroskyDriver creates a NeuroskyDriver by name
// NewDriver creates a Neurosky Driver
// and adds the following events:
//
// extended - user's current extended level
Expand All @@ -56,9 +56,8 @@ type EEG struct {
// blink - user's current blink level
// wave - shows wave data
// eeg - showing eeg data
func NewNeuroskyDriver(a *NeuroskyAdaptor, name string) *NeuroskyDriver {
n := &NeuroskyDriver{
name: name,
func NewDriver(a *Adaptor) *Driver {
n := &Driver{
connection: a,
Eventer: gobot.NewEventer(),
}
Expand All @@ -74,17 +73,18 @@ func NewNeuroskyDriver(a *NeuroskyAdaptor, name string) *NeuroskyDriver {

return n
}
func (n *NeuroskyDriver) Connection() gobot.Connection { return n.connection }
func (n *NeuroskyDriver) Name() string { return n.name }
func (n *Driver) Connection() gobot.Connection { return n.connection }
func (n *Driver) Name() string { return n.name }
func (n *Driver) SetName(name string) { n.name = name }

// adaptor returns neurosky adaptor
func (n *NeuroskyDriver) adaptor() *NeuroskyAdaptor {
return n.Connection().(*NeuroskyAdaptor)
func (n *Driver) adaptor() *Adaptor {
return n.Connection().(*Adaptor)
}

// Start creates a go routine to listen from serial port
// and parse buffer readings
func (n *NeuroskyDriver) Start() (errs []error) {
func (n *Driver) Start() (errs []error) {
go func() {
for {
buff := make([]byte, 1024)
Expand All @@ -100,10 +100,10 @@ func (n *NeuroskyDriver) Start() (errs []error) {
}

// Halt stops neurosky driver (void)
func (n *NeuroskyDriver) Halt() (errs []error) { return }
func (n *Driver) Halt() (errs []error) { return }

// parse converts bytes buffer into packets until no more data is present
func (n *NeuroskyDriver) parse(buf *bytes.Buffer) {
func (n *Driver) parse(buf *bytes.Buffer) {
for buf.Len() > 2 {
b1, _ := buf.ReadByte()
b2, _ := buf.ReadByte()
Expand All @@ -119,7 +119,7 @@ func (n *NeuroskyDriver) parse(buf *bytes.Buffer) {
}

// parsePacket publishes event according to data parsed
func (n *NeuroskyDriver) parsePacket(buf *bytes.Buffer) {
func (n *Driver) parsePacket(buf *bytes.Buffer) {
for buf.Len() > 0 {
b, _ := buf.ReadByte()
switch b {
Expand Down Expand Up @@ -153,7 +153,7 @@ func (n *NeuroskyDriver) parsePacket(buf *bytes.Buffer) {
}

// parseEEG returns data converted into EEG map
func (n *NeuroskyDriver) parseEEG(data []byte) EEG {
func (n *Driver) parseEEG(data []byte) EEG {
return EEG{
Delta: n.parse3ByteInteger(data[0:3]),
Theta: n.parse3ByteInteger(data[3:6]),
Expand All @@ -166,7 +166,7 @@ func (n *NeuroskyDriver) parseEEG(data []byte) EEG {
}
}

func (n *NeuroskyDriver) parse3ByteInteger(data []byte) int {
func (n *Driver) parse3ByteInteger(data []byte) int {
return ((int(data[0]) << 16) |
(((1 << 16) - 1) & (int(data[1]) << 8)) |
(((1 << 8) - 1) & int(data[2])))
Expand Down
13 changes: 6 additions & 7 deletions platforms/neurosky/neurosky_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,20 @@ import (
"github.com/hybridgroup/gobot/gobottest"
)

var _ gobot.Driver = (*NeuroskyDriver)(nil)
var _ gobot.Driver = (*Driver)(nil)

func initTestNeuroskyDriver() *NeuroskyDriver {
a := NewNeuroskyAdaptor("bot", "/dev/null")
a.connect = func(n *NeuroskyAdaptor) (io.ReadWriteCloser, error) {
func initTestNeuroskyDriver() *Driver {
a := NewAdaptor("/dev/null")
a.connect = func(n *Adaptor) (io.ReadWriteCloser, error) {
return &NullReadWriteCloser{}, nil
}
a.Connect()
return NewNeuroskyDriver(a, "bot")
return NewDriver(a)
}

func TestNeuroskyDriver(t *testing.T) {
d := initTestNeuroskyDriver()
gobottest.Assert(t, d.Name(), "bot")
gobottest.Assert(t, d.Connection().Name(), "bot")
gobottest.Refute(t, d.Connection(), nil)
}
func TestNeuroskyDriverStart(t *testing.T) {
sem := make(chan bool, 0)
Expand Down

0 comments on commit d210367

Please sign in to comment.