2016-01-30 02:00:27 -08:00
|
|
|
package chip
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
2016-12-08 13:24:03 +01:00
|
|
|
"gobot.io/x/gobot"
|
|
|
|
"gobot.io/x/gobot/drivers/gpio"
|
|
|
|
"gobot.io/x/gobot/drivers/i2c"
|
|
|
|
"gobot.io/x/gobot/gobottest"
|
|
|
|
"gobot.io/x/gobot/sysfs"
|
2016-01-30 02:00:27 -08:00
|
|
|
)
|
|
|
|
|
2017-02-10 11:08:32 +01:00
|
|
|
// make sure that this Adaptor fullfills all the required interfaces
|
2016-09-25 20:19:19 +02:00
|
|
|
var _ gobot.Adaptor = (*Adaptor)(nil)
|
|
|
|
var _ gpio.DigitalReader = (*Adaptor)(nil)
|
|
|
|
var _ gpio.DigitalWriter = (*Adaptor)(nil)
|
2017-02-10 11:08:32 +01:00
|
|
|
var _ i2c.Connector = (*Adaptor)(nil)
|
2016-08-26 14:23:03 +02:00
|
|
|
|
2016-01-30 02:00:27 -08:00
|
|
|
type NullReadWriteCloser struct {
|
|
|
|
contents []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NullReadWriteCloser) SetAddress(int) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NullReadWriteCloser) Write(b []byte) (int, error) {
|
|
|
|
n.contents = make([]byte, len(b))
|
|
|
|
copy(n.contents[:], b[:])
|
|
|
|
|
|
|
|
return len(b), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NullReadWriteCloser) Read(b []byte) (int, error) {
|
|
|
|
copy(b, n.contents)
|
|
|
|
return len(b), nil
|
|
|
|
}
|
|
|
|
|
2016-11-27 21:56:09 +01:00
|
|
|
var closeErr error
|
2016-01-30 02:00:27 -08:00
|
|
|
|
|
|
|
func (n *NullReadWriteCloser) Close() error {
|
|
|
|
return closeErr
|
|
|
|
}
|
|
|
|
|
2016-09-25 20:19:19 +02:00
|
|
|
func initTestChipAdaptor() *Adaptor {
|
|
|
|
a := NewAdaptor()
|
2016-01-30 02:00:27 -08:00
|
|
|
a.Connect()
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChipAdaptorDigitalIO(t *testing.T) {
|
|
|
|
a := initTestChipAdaptor()
|
|
|
|
fs := sysfs.NewMockFilesystem([]string{
|
|
|
|
"/sys/class/gpio/export",
|
|
|
|
"/sys/class/gpio/unexport",
|
2017-01-11 22:21:07 +01:00
|
|
|
"/sys/class/gpio/gpio50/value",
|
|
|
|
"/sys/class/gpio/gpio50/direction",
|
|
|
|
"/sys/class/gpio/gpio139/value",
|
|
|
|
"/sys/class/gpio/gpio139/direction",
|
2016-01-30 02:00:27 -08:00
|
|
|
})
|
|
|
|
|
|
|
|
sysfs.SetFilesystem(fs)
|
|
|
|
|
2017-01-11 22:21:07 +01:00
|
|
|
a.DigitalWrite("CSID7", 1)
|
|
|
|
gobottest.Assert(t, fs.Files["/sys/class/gpio/gpio139/value"].Contents, "1")
|
2016-01-30 02:00:27 -08:00
|
|
|
|
2017-01-11 22:21:07 +01:00
|
|
|
fs.Files["/sys/class/gpio/gpio50/value"].Contents = "1"
|
|
|
|
i, _ := a.DigitalRead("TWI2-SDA")
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, i, 1)
|
2016-01-30 02:00:27 -08:00
|
|
|
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, a.DigitalWrite("XIO-P10", 1), errors.New("Not a valid pin"))
|
2016-01-30 02:00:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestChipAdaptorI2c(t *testing.T) {
|
|
|
|
a := initTestChipAdaptor()
|
|
|
|
fs := sysfs.NewMockFilesystem([]string{
|
|
|
|
"/dev/i2c-1",
|
|
|
|
})
|
|
|
|
sysfs.SetFilesystem(fs)
|
|
|
|
sysfs.SetSyscall(&sysfs.MockSyscall{})
|
2017-02-06 14:50:14 +01:00
|
|
|
|
2017-02-10 11:08:32 +01:00
|
|
|
con, err := a.GetConnection(0xff, 1)
|
2017-02-06 14:50:14 +01:00
|
|
|
gobottest.Assert(t, err, nil)
|
|
|
|
|
|
|
|
con.Write([]byte{0x00, 0x01})
|
|
|
|
data := []byte{42, 42}
|
|
|
|
con.Read(data)
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, data, []byte{0x00, 0x01})
|
2016-01-30 02:00:27 -08:00
|
|
|
|
2016-11-07 17:43:55 +01:00
|
|
|
gobottest.Assert(t, a.Finalize(), nil)
|
2016-01-30 02:00:27 -08:00
|
|
|
}
|