1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-05-11 19:29:20 +08:00
hybridgroup.gobot/platforms/firmata/firmata_adaptor_test.go

126 lines
3.5 KiB
Go
Raw Normal View History

2014-04-27 19:56:18 -07:00
package firmata
import (
"fmt"
2014-06-12 16:22:34 -07:00
"testing"
2014-07-14 14:10:04 -07:00
"time"
2014-07-10 14:28:54 -07:00
"github.com/hybridgroup/gobot"
)
2014-06-13 16:01:39 -07:00
func initTestFirmataAdaptor() *FirmataAdaptor {
a := NewFirmataAdaptor("board", "/dev/null")
a.connect = func(f *FirmataAdaptor) {
2014-07-14 14:10:04 -07:00
f.board = newBoard(gobot.NullReadWriteCloser{})
f.board.initTimeInterval = 0 * time.Second
2014-07-15 14:52:11 -07:00
// 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})
2014-06-12 16:22:34 -07:00
}
2014-06-13 16:01:39 -07:00
a.Connect()
return a
2014-06-12 16:22:34 -07:00
}
2014-06-13 16:01:39 -07:00
func TestFirmataAdaptorFinalize(t *testing.T) {
a := initTestFirmataAdaptor()
gobot.Assert(t, a.Finalize(), nil)
2014-06-12 16:22:34 -07:00
}
2014-06-13 16:01:39 -07:00
func TestFirmataAdaptorConnect(t *testing.T) {
a := initTestFirmataAdaptor()
gobot.Assert(t, a.Connect(), nil)
2014-06-12 16:22:34 -07:00
}
2014-07-14 14:10:04 -07:00
2014-06-13 16:01:39 -07:00
func TestFirmataAdaptorInitServo(t *testing.T) {
a := initTestFirmataAdaptor()
a.InitServo()
2014-06-12 16:22:34 -07:00
}
2014-07-14 14:10:04 -07:00
2014-06-13 16:01:39 -07:00
func TestFirmataAdaptorServoWrite(t *testing.T) {
a := initTestFirmataAdaptor()
a.ServoWrite("1", 50)
2014-06-12 16:22:34 -07:00
}
2014-07-14 14:10:04 -07:00
2014-06-13 16:01:39 -07:00
func TestFirmataAdaptorPwmWrite(t *testing.T) {
a := initTestFirmataAdaptor()
a.PwmWrite("1", 50)
2014-06-12 16:22:34 -07:00
}
2014-07-14 14:10:04 -07:00
2014-06-13 16:01:39 -07:00
func TestFirmataAdaptorDigitalWrite(t *testing.T) {
a := initTestFirmataAdaptor()
a.DigitalWrite("1", 1)
2014-06-12 16:22:34 -07:00
}
2014-07-14 14:10:04 -07:00
2014-06-13 16:01:39 -07:00
func TestFirmataAdaptorDigitalRead(t *testing.T) {
a := initTestFirmataAdaptor()
2014-07-15 14:52:11 -07:00
pinNumber := "1"
2014-06-12 16:22:34 -07:00
// -1 on no data
gobot.Assert(t, a.DigitalRead(pinNumber), -1)
2014-06-12 16:22:34 -07:00
2014-07-14 20:32:53 -07:00
go func() {
<-time.After(5 * time.Millisecond)
gobot.Publish(a.board.events[fmt.Sprintf("digital_read_%v", pinNumber)],
[]byte{0x01})
}()
gobot.Assert(t, a.DigitalRead(pinNumber), 0x01)
2014-06-12 16:22:34 -07:00
}
2014-07-14 14:10:04 -07:00
2014-06-13 16:01:39 -07:00
func TestFirmataAdaptorAnalogRead(t *testing.T) {
a := initTestFirmataAdaptor()
2014-07-15 14:52:11 -07:00
pinNumber := "1"
2014-06-12 16:22:34 -07:00
// -1 on no data
gobot.Assert(t, a.AnalogRead(pinNumber), -1)
2014-06-12 16:22:34 -07:00
value := 133
2014-07-14 20:32:53 -07:00
go func() {
<-time.After(5 * time.Millisecond)
gobot.Publish(a.board.events[fmt.Sprintf("analog_read_%v", pinNumber)],
[]byte{
byte(value >> 24),
byte(value >> 16),
byte(value >> 8),
byte(value & 0xff),
},
)
}()
gobot.Assert(t, a.AnalogRead(pinNumber), 133)
2014-06-12 16:22:34 -07:00
}
2014-07-14 14:10:04 -07:00
func TestFirmataAdaptorAnalogWrite(t *testing.T) {
a := initTestFirmataAdaptor()
a.AnalogWrite("1", 50)
}
2014-06-13 16:01:39 -07:00
func TestFirmataAdaptorI2cStart(t *testing.T) {
a := initTestFirmataAdaptor()
a.I2cStart(0x00)
2014-06-12 16:22:34 -07:00
}
2014-06-13 16:01:39 -07:00
func TestFirmataAdaptorI2cRead(t *testing.T) {
a := initTestFirmataAdaptor()
2014-06-12 16:22:34 -07:00
// [] on no data
gobot.Assert(t, a.I2cRead(1), []byte{})
2014-06-12 16:22:34 -07:00
i := []byte{100}
i2cReply := map[string][]byte{}
i2cReply["data"] = i
2014-07-14 20:32:53 -07:00
go func() {
<-time.After(5 * time.Millisecond)
gobot.Publish(a.board.events["i2c_reply"], i2cReply)
}()
gobot.Assert(t, a.I2cRead(1), i)
2014-06-12 16:22:34 -07:00
}
2014-06-13 16:01:39 -07:00
func TestFirmataAdaptorI2cWrite(t *testing.T) {
a := initTestFirmataAdaptor()
a.I2cWrite([]byte{0x00, 0x01})
2014-06-12 16:22:34 -07:00
}