From 419193db49450b35c39fc3a847121de6b2d03e56 Mon Sep 17 00:00:00 2001 From: Adrian Zankich Date: Wed, 16 Jul 2014 11:49:06 -0700 Subject: [PATCH] Add more firmata test coverage --- platforms/firmata/firmata.go | 16 +++++++++++--- platforms/firmata/firmata_test.go | 35 ++++++++++++++++++++++++------- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/platforms/firmata/firmata.go b/platforms/firmata/firmata.go index a65ff7eb..1cdf71cf 100644 --- a/platforms/firmata/firmata.go +++ b/platforms/firmata/firmata.go @@ -100,6 +100,7 @@ func newBoard(sp io.ReadWriteCloser) *board { func (b *board) connect() { if b.connected == false { + b.reset() b.initBoard() for { @@ -240,7 +241,9 @@ func (b *board) process(data []byte) { b.majorVersion, _ = buf.ReadByte() b.minorVersion, _ = buf.ReadByte() gobot.Publish(b.events["report_version"], b.version()) - case analogMessageRangeStart <= messageType && analogMessageRangeEnd >= messageType: + case analogMessageRangeStart <= messageType && + analogMessageRangeEnd >= messageType: + leastSignificantByte, _ := buf.ReadByte() mostSignificantByte, _ := buf.ReadByte() @@ -256,7 +259,9 @@ func (b *board) process(data []byte) { byte(value & 0xff), }, ) - case digitalMessageRangeStart <= messageType && digitalMessageRangeEnd >= messageType: + case digitalMessageRangeStart <= messageType && + digitalMessageRangeEnd >= messageType: + port := messageType & 0x0F firstBitmask, _ := buf.ReadByte() secondBitmask, _ := buf.ReadByte() @@ -299,6 +304,7 @@ func (b *board) process(data []byte) { } b.pins = append(b.pins, pin{modes, output, 0, 0}) b.events[fmt.Sprintf("digital_read_%v", len(b.pins)-1)] = gobot.NewEvent() + b.events[fmt.Sprintf("pin_%v_state", len(b.pins)-1)] = gobot.NewEvent() supportedModes = 0 n = 0 continue @@ -338,7 +344,11 @@ func (b *board) process(data []byte) { } gobot.Publish(b.events[fmt.Sprintf("pin_%v_state", currentBuffer[2])], - []byte{byte(pin.value & 0xff)}, + map[string]int{ + "pin": int(currentBuffer[2]), + "mode": int(pin.mode), + "value": int(pin.value), + }, ) case i2CReply: i2cReply := map[string][]byte{ diff --git a/platforms/firmata/firmata_test.go b/platforms/firmata/firmata_test.go index 8210fa8e..53faaefb 100644 --- a/platforms/firmata/firmata_test.go +++ b/platforms/firmata/firmata_test.go @@ -28,6 +28,24 @@ func initTestFirmata() *board { return b } +func TestReportVersion(t *testing.T) { + b := initTestFirmata() + //test if functions executes + b.reportVersion() +} + +func TestQueryFirmware(t *testing.T) { + b := initTestFirmata() + //test if functions executes + b.queryFirmware() +} + +func TestQueryPinState(t *testing.T) { + b := initTestFirmata() + //test if functions executes + b.queryPinState(byte(1)) +} + func TestProcess(t *testing.T) { b := initTestFirmata() sem := make(chan bool) @@ -72,14 +90,17 @@ func TestProcess(t *testing.T) { }) b.process([]byte{0x90, 0x16, 0x00}) <-sem - //TODO we need a test here //pinStateResponse - //gobot.Once(b.events["pin_0_state"], func(data interface{}) { - // gobot.Expect(t, data, "") - // sem <- true - //}) - //b.process([]byte{240, 0x6E}) - //<-sem + gobot.Once(b.events["pin_13_state"], func(data interface{}) { + gobot.Expect(t, data, map[string]int{ + "pin": 13, + "mode": 1, + "value": 1, + }) + sem <- true + }) + b.process([]byte{240, 110, 13, 1, 1, 247}) + <-sem //i2cReply gobot.Once(b.events["i2c_reply"], func(data interface{}) { i2c_reply := map[string][]byte{