mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-04-29 13:49:14 +08:00
82 lines
1.7 KiB
Go
82 lines
1.7 KiB
Go
![]() |
package chip
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/hybridgroup/gobot"
|
||
|
"github.com/hybridgroup/gobot/sysfs"
|
||
|
)
|
||
|
|
||
|
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
|
||
|
}
|
||
|
|
||
|
var closeErr error = nil
|
||
|
|
||
|
func (n *NullReadWriteCloser) Close() error {
|
||
|
return closeErr
|
||
|
}
|
||
|
|
||
|
func initTestChipAdaptor() *ChipAdaptor {
|
||
|
a := NewChipAdaptor("myAdaptor")
|
||
|
a.Connect()
|
||
|
return a
|
||
|
}
|
||
|
|
||
|
func TestChipAdaptorDigitalIO(t *testing.T) {
|
||
|
a := initTestChipAdaptor()
|
||
|
fs := sysfs.NewMockFilesystem([]string{
|
||
|
"/sys/class/gpio/export",
|
||
|
"/sys/class/gpio/unexport",
|
||
|
"/sys/class/gpio/gpio408/value",
|
||
|
"/sys/class/gpio/gpio408/direction",
|
||
|
"/sys/class/gpio/gpio415/value",
|
||
|
"/sys/class/gpio/gpio415/direction",
|
||
|
})
|
||
|
|
||
|
sysfs.SetFilesystem(fs)
|
||
|
|
||
|
a.DigitalWrite("U14_13", 1)
|
||
|
gobot.Assert(t, fs.Files["/sys/class/gpio/gpio408/value"].Contents, "1")
|
||
|
|
||
|
fs.Files["/sys/class/gpio/gpio415/value"].Contents = "1"
|
||
|
i, _ := a.DigitalRead("U14_20")
|
||
|
gobot.Assert(t, i, 1)
|
||
|
|
||
|
gobot.Assert(t, a.DigitalWrite("U13_99", 1), errors.New("Not a valid pin"))
|
||
|
}
|
||
|
|
||
|
func TestChipAdaptorI2c(t *testing.T) {
|
||
|
a := initTestChipAdaptor()
|
||
|
fs := sysfs.NewMockFilesystem([]string{
|
||
|
"/dev/i2c-1",
|
||
|
})
|
||
|
sysfs.SetFilesystem(fs)
|
||
|
sysfs.SetSyscall(&sysfs.MockSyscall{})
|
||
|
a.I2cStart(0xff)
|
||
|
a.i2cDevice = &NullReadWriteCloser{}
|
||
|
|
||
|
a.I2cWrite(0xff, []byte{0x00, 0x01})
|
||
|
data, _ := a.I2cRead(0xff, 2)
|
||
|
gobot.Assert(t, data, []byte{0x00, 0x01})
|
||
|
|
||
|
gobot.Assert(t, len(a.Finalize()), 0)
|
||
|
}
|