mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-04-27 13:48:56 +08:00
60 lines
1.3 KiB
Go
60 lines
1.3 KiB
Go
package raspi
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/hybridgroup/gobot"
|
|
"github.com/hybridgroup/gobot/sysfs"
|
|
)
|
|
|
|
func initTestRaspiAdaptor() *RaspiAdaptor {
|
|
boardRevision = func() (string, string) {
|
|
return "3", "/dev/i2c-1"
|
|
}
|
|
a := NewRaspiAdaptor("myAdaptor")
|
|
a.Connect()
|
|
return a
|
|
}
|
|
|
|
func TestRaspiAdaptorFinalize(t *testing.T) {
|
|
a := initTestRaspiAdaptor()
|
|
a.DigitalWrite("3", 1)
|
|
a.i2cDevice = new(gobot.NullReadWriteCloser)
|
|
gobot.Assert(t, a.Finalize(), nil)
|
|
}
|
|
|
|
func TestRaspiAdaptorDigitalIO(t *testing.T) {
|
|
a := initTestRaspiAdaptor()
|
|
fs := sysfs.NewMockFilesystem([]string{
|
|
"/sys/class/gpio/export",
|
|
"/sys/class/gpio/unexport",
|
|
"/sys/class/gpio/gpio4/value",
|
|
"/sys/class/gpio/gpio4/direction",
|
|
"/sys/class/gpio/gpio27/value",
|
|
"/sys/class/gpio/gpio27/direction",
|
|
})
|
|
|
|
sysfs.SetFilesystem(fs)
|
|
|
|
a.DigitalWrite("7", 1)
|
|
gobot.Assert(t, fs.Files["/sys/class/gpio/gpio4/value"].Contents, "1")
|
|
|
|
a.DigitalWrite("13", 1)
|
|
i, _ := a.DigitalRead("13")
|
|
gobot.Assert(t, i, 1)
|
|
}
|
|
|
|
func TestRaspiAdaptorI2c(t *testing.T) {
|
|
a := initTestRaspiAdaptor()
|
|
fs := sysfs.NewMockFilesystem([]string{
|
|
"/dev/i2c-1",
|
|
})
|
|
sysfs.SetFilesystem(fs)
|
|
sysfs.SetSyscall(&sysfs.MockSyscall{})
|
|
a.I2cStart(0xff)
|
|
|
|
a.I2cWrite([]byte{0x00, 0x01})
|
|
data, _ := a.I2cRead(2)
|
|
gobot.Assert(t, data, []byte{0x00, 0x01})
|
|
}
|