Skip to content

Commit

Permalink
Introduce I2cBusAdaptor for composition in platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
gen2thomas authored Dec 8, 2022
1 parent e3f2ece commit 3559e78
Show file tree
Hide file tree
Showing 51 changed files with 1,184 additions and 665 deletions.
2 changes: 1 addition & 1 deletion drivers/i2c/adafruit_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func (a *AdafruitMotorHatDriver) startDriver(connection Connection) (err error)

// Start initializes both I2C-addressable Adafruit Motor HAT drivers
func (a *AdafruitMotorHatDriver) Start() (err error) {
bus := a.GetBusOrDefault(a.connector.GetDefaultBus())
bus := a.GetBusOrDefault(a.connector.DefaultBus())

err = a.startServoHat(bus)
if adafruitDebug && err != nil {
Expand Down
6 changes: 3 additions & 3 deletions drivers/i2c/bh1750_driver.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package i2c

import (
"time"
"errors"
"time"

"gobot.io/x/gobot"
)
Expand Down Expand Up @@ -44,7 +44,7 @@ func NewBH1750Driver(a Connector, options ...func(Config)) *BH1750Driver {
name: gobot.DefaultName("BH1750"),
connector: a,
Config: NewConfig(),
mode: BH1750_CONTINUOUS_HIGH_RES_MODE,
mode: BH1750_CONTINUOUS_HIGH_RES_MODE,
}

for _, option := range options {
Expand All @@ -66,7 +66,7 @@ func (h *BH1750Driver) Connection() gobot.Connection { return h.connector.(gobot

// Start initialized the bh1750
func (h *BH1750Driver) Start() (err error) {
bus := h.GetBusOrDefault(h.connector.GetDefaultBus())
bus := h.GetBusOrDefault(h.connector.DefaultBus())
address := h.GetAddressOrDefault(bh1750Address)

h.connection, err = h.connector.GetConnection(address, bus)
Expand Down
2 changes: 1 addition & 1 deletion drivers/i2c/blinkm_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (b *BlinkMDriver) Connection() gobot.Connection { return b.connection.(gobo

// Start starts the Driver up, and writes start command
func (b *BlinkMDriver) Start() (err error) {
bus := b.GetBusOrDefault(b.connector.GetDefaultBus())
bus := b.GetBusOrDefault(b.connector.DefaultBus())
address := b.GetAddressOrDefault(blinkmAddress)

b.connection, err = b.connector.GetConnection(address, bus)
Expand Down
2 changes: 1 addition & 1 deletion drivers/i2c/drv2605l_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (d *DRV2605LDriver) writeByteRegisters(regValPairs []struct{ reg, val uint8
}

func (d *DRV2605LDriver) initialize() (err error) {
bus := d.GetBusOrDefault(d.connector.GetDefaultBus())
bus := d.GetBusOrDefault(d.connector.DefaultBus())
address := d.GetAddressOrDefault(drv2605Address)

d.connection, err = d.connector.GetConnection(address, bus)
Expand Down
2 changes: 1 addition & 1 deletion drivers/i2c/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (t *i2cTestAdaptor) GetConnection(address int, bus int) (connection Connect
return t, nil
}

func (t *i2cTestAdaptor) GetDefaultBus() int {
func (t *i2cTestAdaptor) DefaultBus() int {
return 0
}

Expand Down
2 changes: 1 addition & 1 deletion drivers/i2c/hmc6352_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (h *HMC6352Driver) Connection() gobot.Connection { return h.connector.(gobo

// Start initializes the hmc6352
func (h *HMC6352Driver) Start() (err error) {
bus := h.GetBusOrDefault(h.connector.GetDefaultBus())
bus := h.GetBusOrDefault(h.connector.DefaultBus())
address := h.GetAddressOrDefault(hmc6352Address)

h.connection, err = h.connector.GetConnection(address, bus)
Expand Down
8 changes: 4 additions & 4 deletions drivers/i2c/i2c_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ type I2cDevice interface {
// Connector lets Adaptors provide the interface for Drivers
// to get access to the I2C buses on platforms that support I2C.
type Connector interface {
// GetConnection returns a connection to device at the specified address
// GetConnection creates and returns a connection to device at the specified address
// and bus. Bus numbering starts at index 0, the range of valid buses is
// platform specific.
GetConnection(address int, bus int) (device Connection, err error)
GetConnection(address int, busNr int) (device Connection, err error)

// GetDefaultBus returns the default I2C bus index
GetDefaultBus() int
// DefaultBus returns the default I2C bus index
DefaultBus() int
}

// Connection is a connection to an I2C device with a specified address
Expand Down
2 changes: 1 addition & 1 deletion drivers/i2c/i2c_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (d *Driver) Start() error {
defer d.mutex.Unlock()

var err error
bus := d.GetBusOrDefault(d.connector.GetDefaultBus())
bus := d.GetBusOrDefault(d.connector.DefaultBus())
address := d.GetAddressOrDefault(int(d.defaultAddress))

if d.connection, err = d.connector.GetConnection(address, bus); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion drivers/i2c/ina3221_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (i *INA3221Driver) Connection() gobot.Connection {
// Start initializes the INA3221
func (i *INA3221Driver) Start() error {
var err error
bus := i.GetBusOrDefault(i.connector.GetDefaultBus())
bus := i.GetBusOrDefault(i.connector.DefaultBus())
address := i.GetAddressOrDefault(int(ina3221Address))

if i.connection, err = i.connector.GetConnection(address, bus); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion drivers/i2c/jhd1313m1_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (h *JHD1313M1Driver) Connection() gobot.Connection {

// Start starts the backlit and the screen and initializes the states.
func (h *JHD1313M1Driver) Start() (err error) {
bus := h.GetBusOrDefault(h.connector.GetDefaultBus())
bus := h.GetBusOrDefault(h.connector.DefaultBus())

if h.lcdConnection, err = h.connector.GetConnection(h.lcdAddress, bus); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion drivers/i2c/lidarlite_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (h *LIDARLiteDriver) Connection() gobot.Connection { return h.connector.(go

// Start initialized the LIDAR
func (h *LIDARLiteDriver) Start() (err error) {
bus := h.GetBusOrDefault(h.connector.GetDefaultBus())
bus := h.GetBusOrDefault(h.connector.DefaultBus())
address := h.GetAddressOrDefault(lidarliteAddress)

h.connection, err = h.connector.GetConnection(address, bus)
Expand Down
2 changes: 1 addition & 1 deletion drivers/i2c/mma7660_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (h *MMA7660Driver) Connection() gobot.Connection { return h.connector.(gobo

// Start initialized the mma7660
func (h *MMA7660Driver) Start() (err error) {
bus := h.GetBusOrDefault(h.connector.GetDefaultBus())
bus := h.GetBusOrDefault(h.connector.DefaultBus())
address := h.GetAddressOrDefault(mma7660Address)

h.connection, err = h.connector.GetConnection(address, bus)
Expand Down
2 changes: 1 addition & 1 deletion drivers/i2c/pca9501_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (p *PCA9501Driver) WriteEEPROM(address uint8, val uint8) error {

func (p *PCA9501Driver) initialize() (err error) {
// initialize the EEPROM connection
bus := p.GetBusOrDefault(p.connector.GetDefaultBus())
bus := p.GetBusOrDefault(p.connector.DefaultBus())
addressMem := p.GetAddressOrDefault(pca9501DefaultAddress) | 0x40
p.connectionMem, err = p.connector.GetConnection(addressMem, bus)
return
Expand Down
2 changes: 1 addition & 1 deletion drivers/i2c/pca9685_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (p *PCA9685Driver) Connection() gobot.Connection { return p.connector.(gobo

// Start initializes the pca9685
func (p *PCA9685Driver) Start() (err error) {
bus := p.GetBusOrDefault(p.connector.GetDefaultBus())
bus := p.GetBusOrDefault(p.connector.DefaultBus())
address := p.GetAddressOrDefault(pca9685Address)

p.connection, err = p.connector.GetConnection(address, bus)
Expand Down
2 changes: 1 addition & 1 deletion drivers/i2c/pcf8591_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func (p *PCF8591Driver) Connection() gobot.Connection { return p.connector.(gobo

// Start initializes the PCF8591
func (p *PCF8591Driver) Start() (err error) {
bus := p.GetBusOrDefault(p.connector.GetDefaultBus())
bus := p.GetBusOrDefault(p.connector.DefaultBus())
address := p.GetAddressOrDefault(pcf8591DefaultAddress)

p.connection, err = p.connector.GetConnection(address, bus)
Expand Down
2 changes: 1 addition & 1 deletion drivers/i2c/sht2x_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (d *SHT2xDriver) Connection() gobot.Connection { return d.connector.(gobot.

// Start initializes the SHT2x
func (d *SHT2xDriver) Start() (err error) {
bus := d.GetBusOrDefault(d.connector.GetDefaultBus())
bus := d.GetBusOrDefault(d.connector.DefaultBus())
address := d.GetAddressOrDefault(SHT2xDefaultAddress)

if d.connection, err = d.connector.GetConnection(address, bus); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion drivers/i2c/sht3x_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (s *SHT3xDriver) Connection() gobot.Connection { return s.connector.(gobot.

// Start initializes the SHT3x
func (s *SHT3xDriver) Start() (err error) {
bus := s.GetBusOrDefault(s.connector.GetDefaultBus())
bus := s.GetBusOrDefault(s.connector.DefaultBus())
address := s.GetAddressOrDefault(s.sht3xAddress)

s.connection, err = s.connector.GetConnection(address, bus)
Expand Down
2 changes: 1 addition & 1 deletion drivers/i2c/ssd1306_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ func (s *SSD1306Driver) Start() (err error) {
s.initSequence.contrast = 0x9F
s.initSequence.prechargePeriod = 0x22
}
bus := s.GetBusOrDefault(s.connector.GetDefaultBus())
bus := s.GetBusOrDefault(s.connector.DefaultBus())
address := s.GetAddressOrDefault(ssd1306I2CAddress)
s.connection, err = s.connector.GetConnection(address, bus)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion drivers/i2c/tsl2561_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func (d *TSL2561Driver) Connection() gobot.Connection {

// Start initializes the device.
func (d *TSL2561Driver) Start() (err error) {
bus := d.GetBusOrDefault(d.connector.GetDefaultBus())
bus := d.GetBusOrDefault(d.connector.DefaultBus())
address := d.GetAddressOrDefault(TSL2561AddressFloat)

if d.connection, err = d.connector.GetConnection(address, bus); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion drivers/i2c/wiichuck_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (w *WiichuckDriver) Connection() gobot.Connection { return w.connector.(gob
// Start initilizes i2c and reads from adaptor
// using specified interval to update with new value
func (w *WiichuckDriver) Start() (err error) {
bus := w.GetBusOrDefault(w.connector.GetDefaultBus())
bus := w.GetBusOrDefault(w.connector.DefaultBus())
address := w.GetAddressOrDefault(wiichuckAddress)

w.connection, err = w.connector.GetConnection(address, bus)
Expand Down
2 changes: 2 additions & 0 deletions drivers/spi/spi.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ type Operations interface {
Tx(w, r []byte) error
}

// TODO: rename to golang getter spec (no prefix "Get" for simple getters)

// Connector lets Adaptors provide the interface for Drivers
// to get access to the SPI buses on platforms that support SPI.
type Connector interface {
Expand Down
8 changes: 3 additions & 5 deletions examples/edison_blink.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ import (
"gobot.io/x/gobot/platforms/intel-iot/edison"
)

const boardType = "arduino" // "sparkfun" for a Sparkfun Edison board with the GPIO block

func main() {
e := edison.NewAdaptor()
e := edison.NewAdaptor(boardType)
led := gpio.NewLedDriver(e, "13")

// Uncomment the line below if you are using a Sparkfun
// Edison board with the GPIO block
// e.SetBoard("sparkfun")

work := func() {
gobot.Every(1*time.Second, func() {
led.Toggle()
Expand Down
3 changes: 1 addition & 2 deletions examples/edison_miniboard_grove_accelerometer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import (
)

func main() {
board := edison.NewAdaptor()
board.SetBoard("miniboard")
board := edison.NewAdaptor("miniboard")

accel := i2c.NewGroveAccelerometerDriver(board)

Expand Down
1 change: 1 addition & 0 deletions platforms/adaptors/digitalpinsadaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func TestDigitalPinsReConnect(t *testing.T) {
err := a.Connect()
// assert
gobottest.Assert(t, err, nil)
gobottest.Refute(t, a.pins, nil)
gobottest.Assert(t, len(a.pins), 0)
}

Expand Down
86 changes: 86 additions & 0 deletions platforms/adaptors/i2cbusadaptor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package adaptors

import (
"fmt"
"sync"

multierror "github.com/hashicorp/go-multierror"
"gobot.io/x/gobot/drivers/i2c"
"gobot.io/x/gobot/system"
)

type i2cBusNumberValidator func(busNumber int) error

// I2cBusAdaptor is a adaptor for i2c bus, normally used for composition in platforms.
type I2cBusAdaptor struct {
sys *system.Accesser
validateNumber i2cBusNumberValidator
defaultBusNumber int
mutex sync.Mutex
buses map[int]i2c.I2cDevice
}

// NewI2cBusAdaptor provides the access to i2c buses of the board. The validator is used to check the bus number,
// which is given by user, to the abilities of the board.
func NewI2cBusAdaptor(sys *system.Accesser, v i2cBusNumberValidator, defaultBusNr int) *I2cBusAdaptor {
a := &I2cBusAdaptor{
sys: sys,
validateNumber: v,
defaultBusNumber: defaultBusNr,
}
return a
}

// Connect prepares the connection to i2c buses.
func (a *I2cBusAdaptor) Connect() error {
a.mutex.Lock()
defer a.mutex.Unlock()

a.buses = make(map[int]i2c.I2cDevice)
return nil
}

// Finalize closes all i2c connections.
func (a *I2cBusAdaptor) Finalize() error {
a.mutex.Lock()
defer a.mutex.Unlock()

var err error
for _, bus := range a.buses {
if bus != nil {
if e := bus.Close(); e != nil {
err = multierror.Append(err, e)
}
}
}
a.buses = nil
return err
}

// GetConnection returns a connection to a device on a specified i2c bus
func (a *I2cBusAdaptor) GetConnection(address int, busNr int) (connection i2c.Connection, err error) {
a.mutex.Lock()
defer a.mutex.Unlock()

if a.buses == nil {
return nil, fmt.Errorf("not connected")
}

bus := a.buses[busNr]
if bus == nil {
if err := a.validateNumber(busNr); err != nil {
return nil, err
}
bus, err = a.sys.NewI2cDevice(fmt.Sprintf("/dev/i2c-%d", busNr))
if err != nil {
return nil, err
}
a.buses[busNr] = bus
}
return i2c.NewConnection(bus, address), err
}

// GetDefaultBus returns the default i2c bus number for this platform.
func (a *I2cBusAdaptor) DefaultBus() int {
return a.defaultBusNumber
}
Loading

0 comments on commit 3559e78

Please sign in to comment.