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

138 lines
3.2 KiB
Go
Raw Normal View History

2014-04-28 04:39:51 -07:00
package beaglebone
import (
2014-11-19 18:40:06 -08:00
"errors"
2014-11-09 14:35:36 -08:00
"strings"
2014-06-12 16:12:38 -07:00
"testing"
2014-11-03 19:41:34 -08:00
"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
}
2014-11-09 14:35:36 -08:00
func TestBeagleboneAdaptor(t *testing.T) {
glob = func(pattern string) (matches []string, err error) {
return make([]string, 2), nil
}
2014-11-07 18:45:11 -08:00
fs := sysfs.NewMockFilesystem([]string{
2014-11-09 14:35:36 -08:00
"/dev/i2c-1",
"/sys/devices/bone_capemgr.4",
"/sys/devices/ocp.3",
"/sys/devices/ocp.3/gpio-leds.8/leds/beaglebone:green:usr1/brightness",
"/sys/devices/ocp.3/helper.5",
"/sys/devices/ocp.3/helper.5/AIN1",
"/sys/devices/ocp.3/pwm_test_P9_14.5",
"/sys/devices/ocp.3/pwm_test_P9_14.5/run",
"/sys/devices/ocp.3/pwm_test_P9_14.5/period",
2014-12-23 18:35:45 -08:00
"/sys/devices/ocp.3/pwm_test_P9_14.5/polarity",
2014-11-09 14:35:36 -08:00
"/sys/devices/ocp.3/pwm_test_P9_14.5/duty",
2014-11-07 18:45:11 -08:00
"/sys/class/gpio/export",
"/sys/class/gpio/unexport",
"/sys/class/gpio/gpio60/value",
"/sys/class/gpio/gpio60/direction",
"/sys/class/gpio/gpio10/value",
"/sys/class/gpio/gpio10/direction",
})
sysfs.SetFilesystem(fs)
2014-11-09 14:35:36 -08:00
a := NewBeagleboneAdaptor("myAdaptor")
a.slots = "/sys/devices/bone_capemgr.4"
a.ocp = "/sys/devices/ocp.3"
a.Connect()
a.helper = "/sys/devices/ocp.3/helper.5"
2014-11-09 14:35:36 -08:00
// PWM
glob = func(pattern string) (matches []string, err error) {
pattern = strings.TrimSuffix(pattern, "*")
return []string{pattern + "5"}, nil
}
2014-11-19 18:40:06 -08:00
gobot.Assert(t, a.PwmWrite("P9_99", 175), errors.New("Not a valid pin"))
2014-11-09 14:35:36 -08:00
a.PwmWrite("P9_14", 175)
gobot.Assert(
t,
fs.Files["/sys/devices/ocp.3/pwm_test_P9_14.5/period"].Contents,
"500000",
)
gobot.Assert(
t,
fs.Files["/sys/devices/ocp.3/pwm_test_P9_14.5/duty"].Contents,
2014-12-23 18:35:45 -08:00
"343137",
2014-11-09 14:35:36 -08:00
)
a.ServoWrite("P9_14", 100)
gobot.Assert(
t,
fs.Files["/sys/devices/ocp.3/pwm_test_P9_14.5/period"].Contents,
2014-12-23 18:35:45 -08:00
"16666666",
2014-11-09 14:35:36 -08:00
)
gobot.Assert(
t,
fs.Files["/sys/devices/ocp.3/pwm_test_P9_14.5/duty"].Contents,
2014-12-23 18:35:45 -08:00
"1898148",
2014-11-09 14:35:36 -08:00
)
// Analog
fs.Files["/sys/devices/ocp.3/helper.5/AIN1"].Contents = "567\n"
i, _ := a.AnalogRead("P9_40")
2014-11-09 14:35:36 -08:00
gobot.Assert(t, i, 567)
2014-11-19 18:40:06 -08:00
i, err := a.AnalogRead("P9_99")
gobot.Assert(t, err, errors.New("Not a valid pin"))
2014-11-09 14:35:36 -08:00
// DigitalIO
a.DigitalWrite("usr1", 1)
gobot.Assert(t,
fs.Files["/sys/devices/ocp.3/gpio-leds.8/leds/beaglebone:green:usr1/brightness"].Contents,
"1",
)
2014-11-03 19:41:34 -08:00
a.DigitalWrite("P9_12", 1)
2014-11-07 18:45:11 -08:00
gobot.Assert(t, fs.Files["/sys/class/gpio/gpio60/value"].Contents, "1")
2014-11-03 19:41:34 -08:00
2014-11-19 18:40:06 -08:00
gobot.Assert(t, a.DigitalWrite("P9_99", 1), errors.New("Not a valid pin"))
2014-11-07 18:45:11 -08:00
fs.Files["/sys/class/gpio/gpio10/value"].Contents = "1"
i, _ = a.DigitalRead("P8_31")
2014-11-03 19:41:34 -08:00
gobot.Assert(t, i, 1)
2014-11-09 14:35:36 -08:00
// I2c
2014-11-07 18:45:11 -08:00
sysfs.SetSyscall(&sysfs.MockSyscall{})
2014-11-03 19:41:34 -08:00
a.I2cStart(0xff)
a.i2cDevice = &NullReadWriteCloser{}
a.I2cWrite(0xff, []byte{0x00, 0x01})
data, _ := a.I2cRead(0xff, 2)
gobot.Assert(t, data, []byte{0x00, 0x01})
2014-11-09 14:35:36 -08:00
gobot.Assert(t, len(a.Finalize()), 0)
2014-06-12 16:12:38 -07:00
}