1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-27 13:48:56 +08:00
hybridgroup.gobot/platforms/jetson/jetson_adaptor_test.go

149 lines
3.6 KiB
Go
Raw Normal View History

2021-08-01 15:27:57 +09:00
package jetson
import (
"errors"
"strings"
"testing"
"runtime"
"strconv"
"sync"
"gobot.io/x/gobot"
"gobot.io/x/gobot/drivers/gpio"
"gobot.io/x/gobot/drivers/i2c"
"gobot.io/x/gobot/drivers/spi"
"gobot.io/x/gobot/gobottest"
2022-11-20 19:22:26 +01:00
"gobot.io/x/gobot/system"
2021-08-01 15:27:57 +09:00
)
2022-11-20 19:22:26 +01:00
// make sure that this Adaptor fulfills all the required interfaces
2021-08-01 15:27:57 +09:00
var _ gobot.Adaptor = (*Adaptor)(nil)
2022-11-20 19:22:26 +01:00
var _ gobot.DigitalPinnerProvider = (*Adaptor)(nil)
var _ gobot.PWMPinnerProvider = (*Adaptor)(nil)
2021-08-01 15:27:57 +09:00
var _ gpio.DigitalReader = (*Adaptor)(nil)
var _ gpio.DigitalWriter = (*Adaptor)(nil)
var _ i2c.Connector = (*Adaptor)(nil)
var _ spi.Connector = (*Adaptor)(nil)
2022-11-20 19:22:26 +01:00
func initTestAdaptorWithMockedFilesystem(mockPaths []string) (*Adaptor, *system.MockFilesystem) {
2021-08-01 15:27:57 +09:00
a := NewAdaptor()
2022-11-20 19:22:26 +01:00
fs := a.sys.UseMockFilesystem(mockPaths)
if err := a.Connect(); err != nil {
panic(err)
}
2022-11-05 07:42:28 +01:00
return a, fs
2021-08-01 15:27:57 +09:00
}
2022-11-05 07:42:28 +01:00
func TestNewAdaptor(t *testing.T) {
2021-08-01 15:27:57 +09:00
a := NewAdaptor()
2022-11-05 07:42:28 +01:00
2021-08-01 15:27:57 +09:00
gobottest.Assert(t, strings.HasPrefix(a.Name(), "JetsonNano"), true)
2022-11-05 07:42:28 +01:00
gobottest.Assert(t, a.GetDefaultBus(), 1)
2021-08-01 15:27:57 +09:00
2022-11-05 07:42:28 +01:00
a.SetName("NewName")
gobottest.Assert(t, a.Name(), "NewName")
2021-08-01 15:27:57 +09:00
}
2022-11-05 07:42:28 +01:00
func TestFinalize(t *testing.T) {
mockPaths := []string{
2021-08-01 15:27:57 +09:00
"/sys/class/gpio/export",
"/sys/class/gpio/unexport",
"/dev/i2c-1",
"/dev/i2c-0",
"/dev/spidev0.0",
"/dev/spidev0.1",
2022-11-05 07:42:28 +01:00
}
a, _ := initTestAdaptorWithMockedFilesystem(mockPaths)
2021-08-01 15:27:57 +09:00
a.DigitalWrite("3", 1)
a.GetConnection(0xff, 0)
gobottest.Assert(t, a.Finalize(), nil)
}
2022-11-05 07:42:28 +01:00
func TestDigitalIO(t *testing.T) {
mockPaths := []string{
2021-08-01 15:27:57 +09:00
"/sys/class/gpio/export",
"/sys/class/gpio/unexport",
2022-10-22 20:08:55 +09:00
"/sys/class/gpio/gpio216/value",
"/sys/class/gpio/gpio216/direction",
"/sys/class/gpio/gpio14/value",
"/sys/class/gpio/gpio14/direction",
2022-11-05 07:42:28 +01:00
}
a, fs := initTestAdaptorWithMockedFilesystem(mockPaths)
2021-08-01 15:27:57 +09:00
err := a.DigitalWrite("7", 1)
gobottest.Assert(t, err, nil)
2021-08-01 15:27:57 +09:00
gobottest.Assert(t, fs.Files["/sys/class/gpio/gpio216/value"].Contents, "1")
err = a.DigitalWrite("13", 1)
gobottest.Assert(t, err, nil)
i, err := a.DigitalRead("13")
gobottest.Assert(t, err, nil)
2021-08-01 15:27:57 +09:00
gobottest.Assert(t, i, 1)
gobottest.Assert(t, a.DigitalWrite("notexist", 1), errors.New("'notexist' is not a valid id for a digital pin"))
gobottest.Assert(t, a.Finalize(), nil)
2021-08-01 15:27:57 +09:00
}
2022-11-05 07:42:28 +01:00
func TestI2c(t *testing.T) {
a, _ := initTestAdaptorWithMockedFilesystem([]string{"/dev/i2c-1"})
2022-11-20 19:22:26 +01:00
a.sys.UseMockSyscall()
2021-08-01 15:27:57 +09:00
con, err := a.GetConnection(0xff, 1)
gobottest.Assert(t, err, nil)
2022-11-05 07:42:28 +01:00
_, err = con.Write([]byte{0x00, 0x01})
gobottest.Assert(t, err, nil)
2021-08-01 15:27:57 +09:00
data := []byte{42, 42}
2022-11-05 07:42:28 +01:00
_, err = con.Read(data)
gobottest.Assert(t, err, nil)
2021-08-01 15:27:57 +09:00
gobottest.Assert(t, data, []byte{0x00, 0x01})
_, err = a.GetConnection(0xff, 51)
gobottest.Assert(t, err, errors.New("Bus number 51 out of range"))
gobottest.Assert(t, a.GetDefaultBus(), 1)
}
2022-11-05 07:42:28 +01:00
func TestSPI(t *testing.T) {
a := NewAdaptor()
2021-08-01 15:27:57 +09:00
gobottest.Assert(t, a.GetSpiDefaultBus(), 0)
gobottest.Assert(t, a.GetSpiDefaultChip(), 0)
gobottest.Assert(t, a.GetSpiDefaultMode(), 0)
gobottest.Assert(t, a.GetSpiDefaultMaxSpeed(), int64(10000000))
_, err := a.GetSpiConnection(10, 0, 0, 8, 10000000)
gobottest.Assert(t, err.Error(), "Bus number 10 out of range")
2022-11-20 19:22:26 +01:00
// TODO: tests for real connection currently not possible, because not using system.Accessor using
2021-08-01 15:27:57 +09:00
// TODO: test tx/rx here...
}
2022-11-05 07:42:28 +01:00
func TestDigitalPinConcurrency(t *testing.T) {
2021-08-01 15:27:57 +09:00
oldProcs := runtime.GOMAXPROCS(0)
runtime.GOMAXPROCS(8)
2022-10-26 18:21:34 +02:00
defer runtime.GOMAXPROCS(oldProcs)
2021-08-01 15:27:57 +09:00
for retry := 0; retry < 20; retry++ {
2022-11-05 07:42:28 +01:00
a := NewAdaptor()
2021-08-01 15:27:57 +09:00
var wg sync.WaitGroup
for i := 0; i < 20; i++ {
wg.Add(1)
pinAsString := strconv.Itoa(i)
go func(pin string) {
defer wg.Done()
a.DigitalPin(pin)
2021-08-01 15:27:57 +09:00
}(pinAsString)
}
wg.Wait()
}
}