mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-04-27 13:48:56 +08:00
Increase firmata test coverage
This commit is contained in:
parent
5a15fb3198
commit
e55744657c
@ -368,9 +368,8 @@ func (b *board) process(data []byte) {
|
||||
b.firmwareName = string(name[:])
|
||||
gobot.Publish(b.events["firmware_query"], b.firmwareName)
|
||||
case stringData:
|
||||
str := currentBuffer[2 : len(currentBuffer)-1]
|
||||
fmt.Println(string(str[:len(str)]))
|
||||
gobot.Publish(b.events["string_data"], str)
|
||||
str := currentBuffer[2:len(currentBuffer)]
|
||||
gobot.Publish(b.events["string_data"], string(str[:len(str)]))
|
||||
default:
|
||||
fmt.Println("bad byte", fmt.Sprintf("0x%x", command))
|
||||
}
|
||||
|
@ -13,12 +13,22 @@ func initTestFirmataAdaptor() *FirmataAdaptor {
|
||||
a.connect = func(f *FirmataAdaptor) {
|
||||
f.board = newBoard(gobot.NullReadWriteCloser{})
|
||||
f.board.initTimeInterval = 0 * time.Second
|
||||
f.board.pins = make([]pin, 100)
|
||||
f.board.events["digital_read_1"] = gobot.NewEvent()
|
||||
f.board.events["analog_read_1"] = gobot.NewEvent()
|
||||
gobot.Publish(f.board.events["firmware_query"], nil)
|
||||
gobot.Publish(f.board.events["capability_query"], nil)
|
||||
gobot.Publish(f.board.events["analog_mapping_query"], nil)
|
||||
// arduino uno r3 firmware response "StandardFirmata.ino"
|
||||
f.board.process([]byte{240, 121, 2, 3, 83, 0, 116, 0, 97, 0, 110, 0, 100,
|
||||
0, 97, 0, 114, 0, 100, 0, 70, 0, 105, 0, 114, 0, 109, 0, 97, 0, 116, 0,
|
||||
97, 0, 46, 0, 105, 0, 110, 0, 111, 0, 247})
|
||||
// arduino uno r3 capabilities response
|
||||
f.board.process([]byte{240, 108, 127, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1,
|
||||
1, 1, 3, 8, 4, 14, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 3, 8, 4, 14,
|
||||
127, 0, 1, 1, 1, 3, 8, 4, 14, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1,
|
||||
4, 14, 127, 0, 1, 1, 1, 3, 8, 4, 14, 127, 0, 1, 1, 1, 3, 8, 4, 14, 127,
|
||||
0, 1, 1, 1, 3, 8, 4, 14, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 4, 14,
|
||||
127, 0, 1, 1, 1, 2, 10, 127, 0, 1, 1, 1, 2, 10, 127, 0, 1, 1, 1, 2, 10,
|
||||
127, 0, 1, 1, 1, 2, 10, 127, 0, 1, 1, 1, 2, 10, 6, 1, 127, 0, 1, 1, 1,
|
||||
2, 10, 6, 1, 127, 247})
|
||||
// arduino uno r3 analog mapping response
|
||||
f.board.process([]byte{240, 106, 127, 127, 127, 127, 127, 127, 127, 127,
|
||||
127, 127, 127, 127, 127, 127, 0, 1, 2, 3, 4, 5, 247})
|
||||
}
|
||||
a.Connect()
|
||||
return a
|
||||
@ -55,10 +65,10 @@ func TestFirmataAdaptorDigitalWrite(t *testing.T) {
|
||||
|
||||
func TestFirmataAdaptorDigitalRead(t *testing.T) {
|
||||
a := initTestFirmataAdaptor()
|
||||
// -1 on no data
|
||||
gobot.Expect(t, a.DigitalRead("1"), -1)
|
||||
|
||||
pinNumber := "1"
|
||||
// -1 on no data
|
||||
gobot.Expect(t, a.DigitalRead(pinNumber), -1)
|
||||
|
||||
go func() {
|
||||
<-time.After(5 * time.Millisecond)
|
||||
gobot.Publish(a.board.events[fmt.Sprintf("digital_read_%v", pinNumber)],
|
||||
@ -69,10 +79,10 @@ func TestFirmataAdaptorDigitalRead(t *testing.T) {
|
||||
|
||||
func TestFirmataAdaptorAnalogRead(t *testing.T) {
|
||||
a := initTestFirmataAdaptor()
|
||||
// -1 on no data
|
||||
gobot.Expect(t, a.AnalogRead("1"), -1)
|
||||
|
||||
pinNumber := "1"
|
||||
// -1 on no data
|
||||
gobot.Expect(t, a.AnalogRead(pinNumber), -1)
|
||||
|
||||
value := 133
|
||||
go func() {
|
||||
<-time.After(5 * time.Millisecond)
|
||||
|
110
platforms/firmata/firmata_test.go
Normal file
110
platforms/firmata/firmata_test.go
Normal file
@ -0,0 +1,110 @@
|
||||
package firmata
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/hybridgroup/gobot"
|
||||
)
|
||||
|
||||
func initTestFirmata() *board {
|
||||
b := newBoard(gobot.NullReadWriteCloser{})
|
||||
b.initTimeInterval = 0 * time.Second
|
||||
// arduino uno r3 firmware response "StandardFirmata.ino"
|
||||
b.process([]byte{240, 121, 2, 3, 83, 0, 116, 0, 97, 0, 110, 0, 100, 0, 97,
|
||||
0, 114, 0, 100, 0, 70, 0, 105, 0, 114, 0, 109, 0, 97, 0, 116, 0, 97, 0, 46,
|
||||
0, 105, 0, 110, 0, 111, 0, 247})
|
||||
// arduino uno r3 capabilities response
|
||||
b.process([]byte{240, 108, 127, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 3,
|
||||
8, 4, 14, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 3, 8, 4, 14, 127, 0, 1,
|
||||
1, 1, 3, 8, 4, 14, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 4, 14, 127, 0,
|
||||
1, 1, 1, 3, 8, 4, 14, 127, 0, 1, 1, 1, 3, 8, 4, 14, 127, 0, 1, 1, 1, 3, 8,
|
||||
4, 14, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 2,
|
||||
10, 127, 0, 1, 1, 1, 2, 10, 127, 0, 1, 1, 1, 2, 10, 127, 0, 1, 1, 1, 2, 10,
|
||||
127, 0, 1, 1, 1, 2, 10, 6, 1, 127, 0, 1, 1, 1, 2, 10, 6, 1, 127, 247})
|
||||
// arduino uno r3 analog mapping response
|
||||
b.process([]byte{240, 106, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
|
||||
127, 127, 127, 127, 0, 1, 2, 3, 4, 5, 247})
|
||||
return b
|
||||
}
|
||||
|
||||
func TestProcess(t *testing.T) {
|
||||
b := initTestFirmata()
|
||||
sem := make(chan bool)
|
||||
//reportVersion
|
||||
gobot.Once(b.events["report_version"], func(data interface{}) {
|
||||
gobot.Expect(t, data.(string), "1.17")
|
||||
sem <- true
|
||||
})
|
||||
b.process([]byte{0xF9, 0x01, 0x11})
|
||||
<-sem
|
||||
//analogMessageRangeStart
|
||||
gobot.Once(b.events["analog_read_0"], func(data interface{}) {
|
||||
b := data.([]byte)
|
||||
gobot.Expect(t,
|
||||
int(uint(b[0])<<24|uint(b[1])<<16|uint(b[2])<<8|uint(b[3])),
|
||||
675)
|
||||
sem <- true
|
||||
})
|
||||
b.process([]byte{0xE0, 0x23, 0x05})
|
||||
<-sem
|
||||
gobot.Once(b.events["analog_read_1"], func(data interface{}) {
|
||||
b := data.([]byte)
|
||||
gobot.Expect(t,
|
||||
int(uint(b[0])<<24|uint(b[1])<<16|uint(b[2])<<8|uint(b[3])),
|
||||
803)
|
||||
sem <- true
|
||||
})
|
||||
b.process([]byte{0xE1, 0x23, 0x06})
|
||||
<-sem
|
||||
//digitalMessageRangeStart
|
||||
b.pins[2].mode = input
|
||||
gobot.Once(b.events["digital_read_2"], func(data interface{}) {
|
||||
gobot.Expect(t, int(data.([]byte)[0]), 1)
|
||||
sem <- true
|
||||
})
|
||||
b.process([]byte{0x90, 0x04, 0x00})
|
||||
<-sem
|
||||
b.pins[4].mode = input
|
||||
gobot.Once(b.events["digital_read_4"], func(data interface{}) {
|
||||
gobot.Expect(t, int(data.([]byte)[0]), 1)
|
||||
sem <- true
|
||||
})
|
||||
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
|
||||
//i2cReply
|
||||
gobot.Once(b.events["i2c_reply"], func(data interface{}) {
|
||||
i2c_reply := map[string][]byte{
|
||||
"slave_address": []byte{9},
|
||||
"register": []byte{0},
|
||||
"data": []byte{152, 1, 154}}
|
||||
gobot.Expect(t, data.(map[string][]byte), i2c_reply)
|
||||
sem <- true
|
||||
})
|
||||
b.process([]byte{240, 119, 9, 0, 0, 0, 24, 1, 1, 0, 26, 1, 247})
|
||||
<-sem
|
||||
//firmwareName
|
||||
gobot.Once(b.events["firmware_query"], func(data interface{}) {
|
||||
gobot.Expect(t, data.(string), "StandardFirmata.ino")
|
||||
sem <- true
|
||||
})
|
||||
b.process([]byte{240, 121, 2, 3, 83, 0, 116, 0, 97, 0, 110, 0, 100, 0, 97,
|
||||
0, 114, 0, 100, 0, 70, 0, 105, 0, 114, 0, 109, 0, 97, 0, 116, 0, 97, 0, 46,
|
||||
0, 105, 0, 110, 0, 111, 0, 247})
|
||||
<-sem
|
||||
//stringData
|
||||
gobot.Once(b.events["string_data"], func(data interface{}) {
|
||||
gobot.Expect(t, data.(string), "Hello Firmata!")
|
||||
sem <- true
|
||||
})
|
||||
b.process(append([]byte{240, 0x71}, []byte("Hello Firmata!")...))
|
||||
<-sem
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user