2017-04-15 23:07:53 +08:00
|
|
|
package microbit
|
|
|
|
|
|
|
|
import (
|
2017-04-15 23:37:33 +08:00
|
|
|
"errors"
|
2017-04-15 23:07:53 +08:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"gobot.io/x/gobot"
|
|
|
|
"gobot.io/x/gobot/drivers/aio"
|
|
|
|
"gobot.io/x/gobot/drivers/gpio"
|
|
|
|
"gobot.io/x/gobot/gobottest"
|
|
|
|
)
|
|
|
|
|
|
|
|
// the IOPinDriver is a Driver
|
|
|
|
var _ gobot.Driver = (*IOPinDriver)(nil)
|
|
|
|
|
|
|
|
// that supports the DigitalReader, DigitalWriter, & AnalogReader interfaces
|
|
|
|
var _ gpio.DigitalReader = (*IOPinDriver)(nil)
|
|
|
|
var _ gpio.DigitalWriter = (*IOPinDriver)(nil)
|
|
|
|
var _ aio.AnalogReader = (*IOPinDriver)(nil)
|
|
|
|
|
|
|
|
func initTestIOPinDriver() *IOPinDriver {
|
|
|
|
d := NewIOPinDriver(NewBleTestAdaptor())
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIOPinDriver(t *testing.T) {
|
|
|
|
d := initTestIOPinDriver()
|
|
|
|
gobottest.Assert(t, strings.HasPrefix(d.Name(), "Microbit IO Pin"), true)
|
|
|
|
d.SetName("NewName")
|
|
|
|
gobottest.Assert(t, d.Name(), "NewName")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIOPinDriverStartAndHalt(t *testing.T) {
|
2017-04-16 00:42:21 +08:00
|
|
|
a := NewBleTestAdaptor()
|
|
|
|
d := NewIOPinDriver(a)
|
|
|
|
a.TestReadCharacteristic(func(cUUID string) ([]byte, error) {
|
|
|
|
return []byte{0, 1, 1, 0}, nil
|
|
|
|
})
|
2017-04-15 23:07:53 +08:00
|
|
|
gobottest.Assert(t, d.Start(), nil)
|
|
|
|
gobottest.Assert(t, d.Halt(), nil)
|
|
|
|
}
|
|
|
|
|
2017-04-16 07:06:31 +08:00
|
|
|
func TestIOPinDriverStartError(t *testing.T) {
|
|
|
|
a := NewBleTestAdaptor()
|
|
|
|
d := NewIOPinDriver(a)
|
|
|
|
a.TestReadCharacteristic(func(cUUID string) ([]byte, error) {
|
|
|
|
return nil, errors.New("read error")
|
|
|
|
})
|
|
|
|
gobottest.Assert(t, d.Start(), errors.New("read error"))
|
|
|
|
}
|
|
|
|
|
2017-04-15 23:07:53 +08:00
|
|
|
func TestIOPinDriverDigitalRead(t *testing.T) {
|
|
|
|
a := NewBleTestAdaptor()
|
|
|
|
d := NewIOPinDriver(a)
|
|
|
|
a.TestReadCharacteristic(func(cUUID string) ([]byte, error) {
|
|
|
|
return []byte{0, 1, 1, 0, 2, 1}, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
val, _ := d.DigitalRead("0")
|
|
|
|
gobottest.Assert(t, val, 1)
|
|
|
|
|
|
|
|
val, _ = d.DigitalRead("1")
|
|
|
|
gobottest.Assert(t, val, 0)
|
|
|
|
}
|
|
|
|
|
2017-04-15 23:37:33 +08:00
|
|
|
func TestIOPinDriverDigitalReadInvalidPin(t *testing.T) {
|
|
|
|
a := NewBleTestAdaptor()
|
|
|
|
d := NewIOPinDriver(a)
|
|
|
|
|
|
|
|
_, err := d.DigitalRead("A3")
|
|
|
|
gobottest.Refute(t, err, nil)
|
|
|
|
|
|
|
|
_, err = d.DigitalRead("6")
|
|
|
|
gobottest.Assert(t, err, errors.New("Invalid pin."))
|
|
|
|
}
|
|
|
|
|
2017-04-15 23:07:53 +08:00
|
|
|
func TestIOPinDriverDigitalWrite(t *testing.T) {
|
|
|
|
a := NewBleTestAdaptor()
|
|
|
|
d := NewIOPinDriver(a)
|
|
|
|
|
|
|
|
// TODO: a better test
|
|
|
|
gobottest.Assert(t, d.DigitalWrite("0", 1), nil)
|
|
|
|
}
|
|
|
|
|
2017-04-15 23:37:33 +08:00
|
|
|
func TestIOPinDriverDigitalWriteInvalidPin(t *testing.T) {
|
|
|
|
a := NewBleTestAdaptor()
|
|
|
|
d := NewIOPinDriver(a)
|
|
|
|
|
|
|
|
gobottest.Refute(t, d.DigitalWrite("A3", 1), nil)
|
|
|
|
gobottest.Assert(t, d.DigitalWrite("6", 1), errors.New("Invalid pin."))
|
|
|
|
}
|
|
|
|
|
2017-04-15 23:07:53 +08:00
|
|
|
func TestIOPinDriverAnalogRead(t *testing.T) {
|
|
|
|
a := NewBleTestAdaptor()
|
|
|
|
d := NewIOPinDriver(a)
|
|
|
|
a.TestReadCharacteristic(func(cUUID string) ([]byte, error) {
|
|
|
|
return []byte{0, 0, 1, 128, 2, 1}, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
val, _ := d.AnalogRead("0")
|
|
|
|
gobottest.Assert(t, val, 0)
|
|
|
|
|
|
|
|
val, _ = d.AnalogRead("1")
|
|
|
|
gobottest.Assert(t, val, 128)
|
|
|
|
}
|
2017-04-15 23:19:19 +08:00
|
|
|
|
2017-04-15 23:37:33 +08:00
|
|
|
func TestIOPinDriverAnalogReadInvalidPin(t *testing.T) {
|
|
|
|
a := NewBleTestAdaptor()
|
|
|
|
d := NewIOPinDriver(a)
|
|
|
|
|
|
|
|
_, err := d.AnalogRead("A3")
|
|
|
|
gobottest.Refute(t, err, nil)
|
|
|
|
|
|
|
|
_, err = d.AnalogRead("6")
|
|
|
|
gobottest.Assert(t, err, errors.New("Invalid pin."))
|
|
|
|
}
|
|
|
|
|
2017-04-15 23:19:19 +08:00
|
|
|
func TestIOPinDriverDigitalAnalogRead(t *testing.T) {
|
|
|
|
a := NewBleTestAdaptor()
|
|
|
|
d := NewIOPinDriver(a)
|
|
|
|
a.TestReadCharacteristic(func(cUUID string) ([]byte, error) {
|
|
|
|
return []byte{0, 0, 1, 128, 2, 1}, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
val, _ := d.DigitalRead("0")
|
|
|
|
gobottest.Assert(t, val, 0)
|
|
|
|
|
|
|
|
val, _ = d.AnalogRead("0")
|
|
|
|
gobottest.Assert(t, val, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIOPinDriverDigitalWriteAnalogRead(t *testing.T) {
|
|
|
|
a := NewBleTestAdaptor()
|
|
|
|
d := NewIOPinDriver(a)
|
|
|
|
a.TestReadCharacteristic(func(cUUID string) ([]byte, error) {
|
|
|
|
return []byte{0, 0, 1, 128, 2, 1}, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
gobottest.Assert(t, d.DigitalWrite("1", 0), nil)
|
|
|
|
|
|
|
|
val, _ := d.AnalogRead("1")
|
|
|
|
gobottest.Assert(t, val, 128)
|
|
|
|
}
|
2017-04-15 23:37:33 +08:00
|
|
|
|
|
|
|
func TestIOPinDriverAnalogReadDigitalWrite(t *testing.T) {
|
|
|
|
a := NewBleTestAdaptor()
|
|
|
|
d := NewIOPinDriver(a)
|
|
|
|
a.TestReadCharacteristic(func(cUUID string) ([]byte, error) {
|
|
|
|
return []byte{0, 0, 1, 128, 2, 1}, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
val, _ := d.AnalogRead("1")
|
|
|
|
gobottest.Assert(t, val, 128)
|
|
|
|
|
|
|
|
gobottest.Assert(t, d.DigitalWrite("1", 0), nil)
|
|
|
|
}
|