diff --git a/platforms/raspi/raspi_adaptor.go b/platforms/raspi/raspi_adaptor.go index 8bc355ab..cf0da619 100644 --- a/platforms/raspi/raspi_adaptor.go +++ b/platforms/raspi/raspi_adaptor.go @@ -14,7 +14,6 @@ import ( "gobot.io/x/gobot/drivers/i2c" "gobot.io/x/gobot/drivers/spi" "gobot.io/x/gobot/sysfs" - xspi "golang.org/x/exp/io/spi" ) var readFile = func() ([]byte, error) { @@ -209,46 +208,18 @@ func (r *Adaptor) GetDefaultBus() int { // GetSpiConnection returns an spi connection to a device on a specified bus. // Valid bus number is [0..1] which corresponds to /dev/spidev0.0 through /dev/spidev0.1. func (r *Adaptor) GetSpiConnection(busNum, mode int, maxSpeed int64) (connection spi.Connection, err error) { - if (busNum < 0) || (busNum > 1) { - return nil, fmt.Errorf("Bus number %d out of range", busNum) - } - device, err := r.getSpiBus(busNum, mode, maxSpeed) - return spi.NewConnection(device), err -} - -func (r *Adaptor) getSpiBus(busNum, mode int, maxSpeed int64) (_ spi.SPIDevice, err error) { r.mutex.Lock() defer r.mutex.Unlock() - if r.spiBuses[busNum] == nil { - var spiMode xspi.Mode - switch mode { - case 0: - spiMode = xspi.Mode0 - case 1: - spiMode = xspi.Mode1 - case 2: - spiMode = xspi.Mode2 - case 3: - spiMode = xspi.Mode3 - default: - spiMode = xspi.Mode0 - } - dev := fmt.Sprintf("/dev/spidev0.%d", busNum) - devfs := &xspi.Devfs{ - Dev: dev, - Mode: spiMode, - MaxSpeed: maxSpeed, - } - if r.spiBuses[busNum] == nil { - bus, err := xspi.Open(devfs) - if err != nil { - return nil, err - } - r.spiBuses[busNum] = spi.NewConnection(bus) - } + if (busNum < 0) || (busNum > 1) { + return nil, fmt.Errorf("Bus number %d out of range", busNum) } - return r.spiBuses[busNum], err + + if r.spiBuses[busNum] == nil { + r.spiBuses[busNum], err = spi.GetSpiBus(busNum, mode, maxSpeed) + } + + return spi.NewConnection(r.spiBuses[busNum]), err } // GetSpiDefaultBus returns the default spi bus for this platform. diff --git a/platforms/raspi/raspi_adaptor_test.go b/platforms/raspi/raspi_adaptor_test.go index 9f2d8ef7..5a93c80d 100644 --- a/platforms/raspi/raspi_adaptor_test.go +++ b/platforms/raspi/raspi_adaptor_test.go @@ -5,15 +5,16 @@ import ( "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" "gobot.io/x/gobot/sysfs" - "runtime" - "strconv" - "sync" ) // make sure that this Adaptor fullfills all the required interfaces @@ -189,18 +190,15 @@ func TestAdaptorSPI(t *testing.T) { }) sysfs.SetFilesystem(fs) sysfs.SetSyscall(&sysfs.MockSyscall{}) - // TODO: find a better way to test this - _, err := a.GetSpiConnection(1, 0, 500000) - gobottest.Assert(t, err, err) + gobottest.Assert(t, a.GetSpiDefaultBus(), 1) gobottest.Assert(t, a.GetSpiDefaultMode(), 0) gobottest.Assert(t, a.GetSpiDefaultMaxSpeed(), int64(500000)) - _, err = a.GetSpiConnection(1, 1, 500000) - _, err = a.GetSpiConnection(1, 2, 500000) - _, err = a.GetSpiConnection(1, 3, 500000) - _, err = a.GetSpiConnection(1, 5, 500000) - _, err = a.GetSpiConnection(4, 0, 500000) + _, err := a.GetSpiConnection(10, 0, 500000) + gobottest.Assert(t, err.Error(), "Bus number 10 out of range") + + // TODO: test tx/rx here... } func TestAdaptorDigitalPinConcurrency(t *testing.T) {