Skip to content

Commit

Permalink
Remove address from spi code
Browse files Browse the repository at this point in the history
  • Loading branch information
ulisesflynn committed Sep 12, 2017
1 parent 4bd3b67 commit d362e47
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 63 deletions.
15 changes: 5 additions & 10 deletions drivers/spi/spi.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ import (
const (
// BusNotInitialized is the initial value for a bus
BusNotInitialized = -1

// AddressNotInitialized is the initial value for an address
AddressNotInitialized = -1
)

type SPIOperations interface {
Expand All @@ -27,16 +24,15 @@ type SPIOperations interface {
// SPIDevice is the interface to a specific spi bus
type SPIDevice interface {
SPIOperations
// SetAddress(int) error
}

// Connector lets Adaptors provide the interface for Drivers
// to get access to the SPI buses on platforms that support SPI.
type Connector interface {
// GetConnection returns a connection to device at the specified address
// and bus. Bus numbering starts at index 0, the range of valid buses is
// GetConnection returns a connection to device at the specified bus.
// Bus numbering starts at index 0, the range of valid buses is
// platform specific.
GetSpiConnection(busNum, address, mode int, maxSpeed int64) (device Connection, err error)
GetSpiConnection(busNum, mode int, maxSpeed int64) (device Connection, err error)

// GetDefaultBus returns the default SPI bus index
GetSpiDefaultBus() int
Expand All @@ -59,13 +55,12 @@ type SpiConnection struct {
bus SPIDevice
mode int
maxSpeed int64
address int
}

// NewConnection creates and returns a new connection to a specific
// spi device on a bus and address
func NewConnection(bus SPIDevice, address int) (connection *SpiConnection) {
return &SpiConnection{bus: bus, address: address}
func NewConnection(bus SPIDevice) (connection *SpiConnection) {
return &SpiConnection{bus: bus}
}

func (c *SpiConnection) Close() error {
Expand Down
33 changes: 2 additions & 31 deletions drivers/spi/spi_config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package spi

type spiConfig struct {
bus int
address int
bus int
}

// Config is the interface which describes how a Driver can specify
Expand All @@ -13,17 +12,11 @@ type Config interface {

// GetBusOrDefault gets which bus to use
GetBusOrDefault(def int) int

// WithAddress sets which address to use
WithAddress(address int)

// GetAddressOrDefault gets which address to use
GetAddressOrDefault(def int) int
}

// NewConfig returns a new SPI Config.
func NewConfig() Config {
return &spiConfig{bus: BusNotInitialized, address: AddressNotInitialized}
return &spiConfig{bus: BusNotInitialized}
}

// WithBus sets preferred bus to use.
Expand All @@ -46,25 +39,3 @@ func WithBus(bus int) func(Config) {
s.WithBus(bus)
}
}

// WithAddress sets which address to use.
func (s *spiConfig) WithAddress(address int) {
s.address = address
}

// GetAddressOrDefault returns which address to use, either
// the one set using WithBus(), or the default value which
// is passed in as the param.
func (s *spiConfig) GetAddressOrDefault(a int) int {
if s.address == AddressNotInitialized {
return a
}
return s.address
}

// WithAddress sets which address to use as a optional param.
func WithAddress(address int) func(Config) {
return func(s Config) {
s.WithAddress(address)
}
}
11 changes: 0 additions & 11 deletions platforms/gopigo3/gopigo3_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,6 @@ func NewGoPiGo3Driver(a *Adaptor) *GoPiGo3Driver {
return g
}

// func (g *GoPiGo3Driver) initialization() (err error) {
// bus := g.GetBusOrDefault(g.connector.GetSpiDefaultBus())
// address := g.GetAddressOrDefault(goPiGo3Address)
// mode := g.connector.GetSpiDefaultMode()
// maxSpeed := g.connector.GetSpiDefaultMaxSpeed()
// g.connection, err = g.connector.GetSpiConnection(bus, address, mode, maxSpeed)
// g.sCon = g.connection.(*SpiConnection)
// return err
// }

// Name returns the name of the device.
func (g *GoPiGo3Driver) Name() string { return g.name }

Expand All @@ -187,7 +177,6 @@ func (g *GoPiGo3Driver) Halt() (err error) { return }

// Start initializes the GoPiGo3
func (g *GoPiGo3Driver) Start() (err error) {
// return g.initialization()
return nil
}

Expand Down
10 changes: 5 additions & 5 deletions platforms/raspi/raspi_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,15 @@ 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, address, mode int, maxSpeed int64) (connection spi.Connection, err error) {
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, address, mode, maxSpeed)
return spi.NewConnection(device, address), err
device, err := r.getSpiBus(busNum, mode, maxSpeed)
return spi.NewConnection(device), err
}

func (r *Adaptor) getSpiBus(busNum, address, mode int, maxSpeed int64) (_ spi.SPIDevice, err error) {
func (r *Adaptor) getSpiBus(busNum, mode int, maxSpeed int64) (_ spi.SPIDevice, err error) {
r.mutex.Lock()
defer r.mutex.Unlock()

Expand Down Expand Up @@ -245,7 +245,7 @@ func (r *Adaptor) getSpiBus(busNum, address, mode int, maxSpeed int64) (_ spi.SP
if err != nil {
return nil, err
}
r.spiBuses[busNum] = spi.NewConnection(bus, address)
r.spiBuses[busNum] = spi.NewConnection(bus)
}
}
return r.spiBuses[busNum], err
Expand Down
12 changes: 6 additions & 6 deletions platforms/raspi/raspi_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,17 +190,17 @@ func TestAdaptorSPI(t *testing.T) {
sysfs.SetFilesystem(fs)
sysfs.SetSyscall(&sysfs.MockSyscall{})
// TODO: find a better way to test this
_, err := a.GetSpiConnection(1, 0xC0, 0, 500000)
_, 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, 0xC0, 1, 500000)
_, err = a.GetSpiConnection(1, 0xC0, 2, 500000)
_, err = a.GetSpiConnection(1, 0xC0, 3, 500000)
_, err = a.GetSpiConnection(1, 0xC0, 5, 500000)
_, err = a.GetSpiConnection(4, 0xC0, 0, 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)
}

func TestAdaptorDigitalPinConcurrency(t *testing.T) {
Expand Down

0 comments on commit d362e47

Please sign in to comment.