1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-29 13:49:14 +08:00
hybridgroup.gobot/platforms/intel-iot/edison/edison_adaptor_test.go

102 lines
2.3 KiB
Go
Raw Normal View History

package edison
import (
2014-11-03 19:02:33 -08:00
"io"
"os"
"testing"
"github.com/hybridgroup/gobot"
2014-11-03 19:02:33 -08:00
"github.com/hybridgroup/gobot/sysfs"
)
func initTestEdisonAdaptor() *EdisonAdaptor {
2014-11-03 19:02:33 -08:00
i2cLocation = os.DevNull
sysfs.WriteFile = func(path string, data []byte) (i int, err error) {
return
}
writeFile = func(name, data string) error {
return nil
}
2014-11-04 12:21:18 -08:00
readFile = func(name string) ([]byte, error) {
return []byte("11"), nil
}
2014-11-03 19:02:33 -08:00
a := NewEdisonAdaptor("myAdaptor")
a.Connect()
return a
}
2014-11-03 19:02:33 -08:00
func TestEdisonAdaptorFinalize(t *testing.T) {
2014-11-04 12:21:18 -08:00
a := initTestEdisonAdaptor()
a.DigitalWrite("3", 1)
a.PwmWrite("5", 100)
a.i2cDevice = new(gobot.NullReadWriteCloser)
gobot.Assert(t, a.Finalize(), true)
2014-11-03 19:02:33 -08:00
}
func TestEdisonAdaptorDigitalIO(t *testing.T) {
a := initTestEdisonAdaptor()
2014-11-03 19:02:33 -08:00
lastWritePath := ""
lastReadPath := ""
lastWriteData := []byte{}
sysfs.WriteFile = func(path string, data []byte) (i int, err error) {
lastWritePath = path
lastWriteData = data
return
}
sysfs.ReadFile = func(path string) (b []byte, err error) {
lastReadPath = path
return []byte("1"), nil
}
a.DigitalWrite("13", 1)
gobot.Assert(t, lastWritePath, "/sys/class/gpio/gpio40/value")
gobot.Assert(t, lastWriteData, []byte{49})
i := a.DigitalRead("2")
gobot.Assert(t, lastReadPath, "/sys/class/gpio/gpio128/value")
gobot.Assert(t, i, 1)
}
2014-11-03 19:02:33 -08:00
func TestEdisonAdaptorI2c(t *testing.T) {
a := initTestEdisonAdaptor()
2014-11-03 19:02:33 -08:00
a.I2cStart(0xff)
var _ io.ReadWriteCloser = a.i2cDevice
a.i2cDevice = new(gobot.NullReadWriteCloser)
a.I2cWrite([]byte{0x00, 0x01})
gobot.Assert(t, a.I2cRead(2), make([]byte, 2))
}
2014-11-04 12:21:18 -08:00
func TestEdisonAdaptorPwm(t *testing.T) {
a := initTestEdisonAdaptor()
lastWritePath := ""
lastReadPath := ""
lastWriteData := ""
writeFile = func(path, data string) (err error) {
lastWritePath = path
lastWriteData = data
return
}
readFile = func(path string) (b []byte, err error) {
lastReadPath = path
return []byte("100\n"), nil
}
a.PwmWrite("5", 100)
gobot.Assert(t, lastWritePath, "/sys/class/pwm/pwmchip0/pwm1/duty_cycle")
}
func TestEdisonAdaptorAnalog(t *testing.T) {
a := initTestEdisonAdaptor()
lastReadPath := ""
readFile = func(path string) (b []byte, err error) {
lastReadPath = path
return []byte("100\n"), nil
}
i := a.AnalogRead("0")
gobot.Assert(t, lastReadPath, "/sys/bus/iio/devices/iio:device1/in_voltage0_raw")
gobot.Assert(t, i, 100)
}