From fbb054c27e7ec850476aa26cf2c01515511e28e7 Mon Sep 17 00:00:00 2001 From: Adrian Zankich Date: Tue, 22 Jul 2014 18:00:54 -0700 Subject: [PATCH] Increase neurosky test coverage --- platforms/neurosky/.neurosky_adaptor.go.swp | Bin 0 -> 12288 bytes platforms/neurosky/neurosky_adaptor.go | 13 +- platforms/neurosky/neurosky_adaptor_test.go | 21 ++-- platforms/neurosky/neurosky_driver.go | 17 +-- platforms/neurosky/neurosky_driver_test.go | 125 +++++++++++++++++++- 5 files changed, 150 insertions(+), 26 deletions(-) create mode 100644 platforms/neurosky/.neurosky_adaptor.go.swp diff --git a/platforms/neurosky/.neurosky_adaptor.go.swp b/platforms/neurosky/.neurosky_adaptor.go.swp new file mode 100644 index 0000000000000000000000000000000000000000..72b8b062b428a2f30d3b07efda2c9db37c320405 GIT binary patch literal 12288 zcmeI2KWG#|6o)5*#ez}MM#W-sLfDJv-f3bY2LUw@NZ~07#!6&vckZ&}Zf2RCRg&Wr zOD$|I4ffh;X=$mAjUuMeHX?$xzx`t{7dgRB@)o|^*_pZb-h8voanr0VFI?d>-W0=i zn6Ve%-z>ddm}jqVGA05O3Y88FznW47(hQ%Ij^51!Rs(rkc68L0%2wMt5_Y8;8^78P z?Ml-0eBG_CtTjy-w2e+8%xG#`jXJ`%wCToGB@?6L)wS)$EfI*wYU8!_-)o>5WMEGk zNNnrOg;6$n?rbex|Eba#KYHZ)o>W1LCIe)E43GgbKnBPF86X3D)qssg*gY(MUm@Uh zaUU7D7msu!17v^<2%P^BedKHo-^m z0lWv#!3KB;`k)8aK?7U_WpEOtbsq6U%bi9Y@|=N5|4FvsR*f?kYafC=MxK%8w?O`FyBECtSxQ z-qgCod-z#1JVBZ@i|^+-T{SFzQP|G#$l&xV_{gf{vpkN{hqT*qBB4UxmBt{6aY_+^moC_(Tf$-dFtnndeVgDtkMYavqovaVJTk-Ql*VTY@JVJ=fk~*yi;_!+gis5BJ}e8 z!PJ2PS0O5otqE1T!qW>!I1!)gFYO%Lb!fiCFd@_>7Iw2Wxr{}^Ulnc1Rq=kZ EpYb(AEC2ui literal 0 HcmV?d00001 diff --git a/platforms/neurosky/neurosky_adaptor.go b/platforms/neurosky/neurosky_adaptor.go index e48eeec2..d62bd8e9 100644 --- a/platforms/neurosky/neurosky_adaptor.go +++ b/platforms/neurosky/neurosky_adaptor.go @@ -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 } diff --git a/platforms/neurosky/neurosky_adaptor_test.go b/platforms/neurosky/neurosky_adaptor_test.go index 588725b3..24d8770b 100644 --- a/platforms/neurosky/neurosky_adaptor_test.go +++ b/platforms/neurosky/neurosky_adaptor_test.go @@ -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) +} diff --git a/platforms/neurosky/neurosky_driver.go b/platforms/neurosky/neurosky_driver.go index be7ae9e1..5c8b8e81 100644 --- a/platforms/neurosky/neurosky_driver.go +++ b/platforms/neurosky/neurosky_driver.go @@ -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]))) } diff --git a/platforms/neurosky/neurosky_driver_test.go b/platforms/neurosky/neurosky_driver_test.go index e1afe846..ff44dbe3 100644 --- a/platforms/neurosky/neurosky_driver_test.go +++ b/platforms/neurosky/neurosky_driver_test.go @@ -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 +}