Increase neurosky test coverage

This commit is contained in:
Adrian Zankich 2014-07-22 18:00:54 -07:00
parent 95334434c9
commit fbb054c27e
5 changed files with 150 additions and 26 deletions

Binary file not shown.

View File

@ -1,15 +1,16 @@
package neurosky
import (
"io"
"github.com/hybridgroup/gobot"
"github.com/tarm/goserial"
"io"
)
type NeuroskyAdaptor struct {
gobot.Adaptor
sp io.ReadWriteCloser
connect func(string) io.ReadWriteCloser
connect func(*NeuroskyAdaptor)
}
func NewNeuroskyAdaptor(name string, port string) *NeuroskyAdaptor {
@ -19,18 +20,18 @@ func NewNeuroskyAdaptor(name string, port string) *NeuroskyAdaptor {
"NeuroskyAdaptor",
port,
),
connect: func(port string) io.ReadWriteCloser {
sp, err := serial.OpenPort(&serial.Config{Name: port, Baud: 57600})
connect: func(n *NeuroskyAdaptor) {
sp, err := serial.OpenPort(&serial.Config{Name: n.Port(), Baud: 57600})
if err != nil {
panic(err)
}
return sp
n.sp = sp
},
}
}
func (n *NeuroskyAdaptor) Connect() bool {
n.sp = n.connect(n.Adaptor.Port())
n.connect(n)
n.SetConnected(true)
return true
}

View File

@ -1,21 +1,26 @@
package neurosky
import (
"github.com/hybridgroup/gobot"
"testing"
"github.com/hybridgroup/gobot"
)
func initTestNeuroskyAdaptor() *NeuroskyAdaptor {
return NewNeuroskyAdaptor("bot", "/dev/null")
a := NewNeuroskyAdaptor("bot", "/dev/null")
a.connect = func(n *NeuroskyAdaptor) {
n.sp = gobot.NullReadWriteCloser{}
}
return a
}
func TestNeuroskyAdaptorFinalize(t *testing.T) {
t.SkipNow()
a := initTestNeuroskyAdaptor()
gobot.Assert(t, a.Finalize(), true)
}
func TestNeuroskyAdaptorConnect(t *testing.T) {
t.SkipNow()
a := initTestNeuroskyAdaptor()
gobot.Assert(t, a.Connect(), true)
}
func TestNeuroskyAdaptorFinalize(t *testing.T) {
a := initTestNeuroskyAdaptor()
a.Connect()
gobot.Assert(t, a.Finalize(), true)
}

View File

@ -56,7 +56,7 @@ func (n *NeuroskyDriver) adaptor() *NeuroskyAdaptor {
func (n *NeuroskyDriver) Start() bool {
go func() {
for {
var buff = make([]byte, int(2048))
buff := make([]byte, 1024)
_, err := n.adaptor().sp.Read(buff[:])
if err != nil {
panic(err)
@ -75,17 +75,16 @@ func (n *NeuroskyDriver) parse(buf *bytes.Buffer) {
b2, _ := buf.ReadByte()
if b1 == BTSync && b2 == BTSync {
length, _ := buf.ReadByte()
var payload = make([]byte, int(length))
payload := make([]byte, length)
buf.Read(payload)
//checksum, _ := buf.ReadByte()
buf.Next(1)
n.parsePacket(payload)
n.parsePacket(bytes.NewBuffer(payload))
}
}
}
func (n *NeuroskyDriver) parsePacket(data []byte) {
buf := bytes.NewBuffer(data)
func (n *NeuroskyDriver) parsePacket(buf *bytes.Buffer) {
for buf.Len() > 0 {
b, _ := buf.ReadByte()
switch b {
@ -107,9 +106,9 @@ func (n *NeuroskyDriver) parsePacket(data []byte) {
buf.Next(1)
var ret = make([]byte, 2)
buf.Read(ret)
gobot.Publish(n.Event("wave"), ret)
gobot.Publish(n.Event("wave"), int16(ret[0])<<8|int16(ret[1]))
case CodeAsicEEG:
var ret = make([]byte, 25)
ret := make([]byte, 25)
i, _ := buf.Read(ret)
if i == 25 {
gobot.Publish(n.Event("eeg"), n.parseEEG(ret))
@ -132,5 +131,7 @@ func (n *NeuroskyDriver) parseEEG(data []byte) EEG {
}
func (n *NeuroskyDriver) parse3ByteInteger(data []byte) int {
return ((int(data[0]) << 16) | (((1 << 16) - 1) & (int(data[1]) << 8)) | (((1 << 8) - 1) & int(data[2])))
return ((int(data[0]) << 16) |
(((1 << 16) - 1) & (int(data[1]) << 8)) |
(((1 << 8) - 1) & int(data[2])))
}

View File

@ -1,22 +1,139 @@
package neurosky
import (
"github.com/hybridgroup/gobot"
"bytes"
"testing"
"time"
"github.com/hybridgroup/gobot"
)
func initTestNeuroskyDriver() *NeuroskyDriver {
return NewNeuroskyDriver(NewNeuroskyAdaptor("bot", "/dev/null"), "bot")
a := NewNeuroskyAdaptor("bot", "/dev/null")
a.connect = func(n *NeuroskyAdaptor) {
n.sp = gobot.NullReadWriteCloser{}
}
a.connect(a)
return NewNeuroskyDriver(a, "bot")
}
func TestNeuroskyDriverStart(t *testing.T) {
t.SkipNow()
d := initTestNeuroskyDriver()
gobot.Assert(t, d.Start(), true)
}
func TestNeuroskyDriverHalt(t *testing.T) {
t.SkipNow()
d := initTestNeuroskyDriver()
gobot.Assert(t, d.Halt(), true)
}
func TestNeuroskyDriverParse(t *testing.T) {
sem := make(chan bool)
d := initTestNeuroskyDriver()
// CodeEx
go func() {
<-time.After(5 * time.Millisecond)
d.parse(bytes.NewBuffer([]byte{0xAA, 0xAA, 1, 0x55, 0x00}))
}()
gobot.On(d.Event("extended"), func(data interface{}) {
sem <- true
})
select {
case <-sem:
case <-time.After(100 * time.Millisecond):
t.Errorf("Event \"extended\" was not published")
}
// CodeSignalQuality
go func() {
<-time.After(5 * time.Millisecond)
d.parse(bytes.NewBuffer([]byte{0xAA, 0xAA, 2, 0x02, 100, 0x00}))
}()
gobot.On(d.Event("signal"), func(data interface{}) {
gobot.Assert(t, data.(byte), byte(100))
sem <- true
})
<-sem
// CodeAttention
go func() {
<-time.After(5 * time.Millisecond)
d.parse(bytes.NewBuffer([]byte{0xAA, 0xAA, 2, 0x04, 40, 0x00}))
}()
gobot.On(d.Event("attention"), func(data interface{}) {
gobot.Assert(t, data.(byte), byte(40))
sem <- true
})
<-sem
// CodeMeditation
go func() {
<-time.After(5 * time.Millisecond)
d.parse(bytes.NewBuffer([]byte{0xAA, 0xAA, 2, 0x05, 60, 0x00}))
}()
gobot.On(d.Event("meditation"), func(data interface{}) {
gobot.Assert(t, data.(byte), byte(60))
sem <- true
})
<-sem
// CodeBlink
go func() {
<-time.After(5 * time.Millisecond)
d.parse(bytes.NewBuffer([]byte{0xAA, 0xAA, 2, 0x16, 150, 0x00}))
}()
gobot.On(d.Event("blink"), func(data interface{}) {
gobot.Assert(t, data.(byte), byte(150))
sem <- true
})
<-sem
// CodeWave
go func() {
<-time.After(5 * time.Millisecond)
d.parse(bytes.NewBuffer([]byte{0xAA, 0xAA, 4, 0x80, 0x00, 0x40, 0x11, 0x00}))
}()
gobot.On(d.Event("wave"), func(data interface{}) {
gobot.Assert(t, data.(int16), int16(16401))
sem <- true
})
<-sem
// CodeAsicEEG
go func() {
<-time.After(5 * time.Millisecond)
d.parse(bytes.NewBuffer([]byte{0xAA, 0xAA, 30, 0x83, 24, 1, 121, 89, 0,
97, 26, 0, 30, 189, 0, 57, 1, 0, 62, 160, 0, 31, 127, 0, 18, 207, 0, 13,
108, 0x00}))
}()
gobot.On(d.Event("eeg"), func(data interface{}) {
gobot.Assert(t,
data.(EEG),
EEG{
Delta: 1573241,
Theta: 5832801,
LoAlpha: 1703966,
HiAlpha: 12386361,
LoBeta: 65598,
HiBeta: 10485791,
LoGamma: 8323090,
MidGamma: 13565965,
})
sem <- true
})
<-sem
}