Skip to content

Commit

Permalink
Refactored platforms to new I2C interfaces
Browse files Browse the repository at this point in the history
Signed-off-by: Erik Agsjö <[email protected]>
  • Loading branch information
erkkah committed Feb 6, 2017
1 parent 543a246 commit 09142c5
Show file tree
Hide file tree
Showing 14 changed files with 391 additions and 321 deletions.
43 changes: 18 additions & 25 deletions platforms/beaglebone/beaglebone_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
multierror "github.com/hashicorp/go-multierror"
"gobot.io/x/gobot"
"gobot.io/x/gobot/sysfs"
"gobot.io/x/gobot/drivers/i2c"
)

var glob = func(pattern string) (matches []string, err error) {
Expand All @@ -25,7 +26,7 @@ type Adaptor struct {
kernel string
digitalPins []sysfs.DigitalPin
pwmPins map[string]*pwmPin
i2cDevice sysfs.I2cDevice
i2cBuses [2]sysfs.I2cDevice
usrLed string
ocp string
analogPath string
Expand Down Expand Up @@ -120,9 +121,11 @@ func (b *Adaptor) Finalize() (err error) {
}
}
}
if b.i2cDevice != nil {
if e := b.i2cDevice.Close(); e != nil {
err = multierror.Append(err, e)
for _, bus := range b.i2cBuses {
if bus != nil {
if e := bus.Close(); e != nil {
err = multierror.Append(err, e)
}
}
}
return
Expand Down Expand Up @@ -195,31 +198,21 @@ func (b *Adaptor) AnalogRead(pin string) (val int, err error) {
return
}

// I2cStart starts a i2c device in specified address on i2c bus /dev/i2c-1
func (b *Adaptor) I2cStart(address int) (err error) {
if b.i2cDevice == nil {
b.i2cDevice, err = sysfs.NewI2cDevice("/dev/i2c-1", address)
}
return
}

// I2cWrite writes data to i2c device
func (b *Adaptor) I2cWrite(address int, data []byte) (err error) {
if err = b.i2cDevice.SetAddress(address); err != nil {
return
// I2cGetConnection returns a connection to a device on a specified bus.
// Valid bus number is [0..1] which corresponds to /dev/i2c-0 through /dev/i2c-1.
func (c *Adaptor) I2cGetConnection(address int, bus int) (connection i2c.I2cConnection, err error) {
if (bus < 0) || (bus > 1) {
return nil, fmt.Errorf("Bus number %d out of range", bus)
}
_, err = b.i2cDevice.Write(data)
return
if c.i2cBuses[bus] == nil {
c.i2cBuses[bus], err = sysfs.NewI2cDevice(fmt.Sprintf("/dev/i2c-%d", bus))
}
return i2c.NewI2cConnection(c.i2cBuses[bus], address), err
}

// I2cRead returns size bytes from the i2c device
func (b *Adaptor) I2cRead(address int, size int) (data []byte, err error) {
if err = b.i2cDevice.SetAddress(address); err != nil {
return
}
data = make([]byte, size)
_, err = b.i2cDevice.Read(data)
return
func (c *Adaptor) I2cGetDefaultBus() int {
return 1
}

// translatePin converts digital pin name to pin position
Expand Down
11 changes: 6 additions & 5 deletions platforms/beaglebone/beaglebone_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var _ aio.AnalogReader = (*Adaptor)(nil)
var _ gpio.PwmWriter = (*Adaptor)(nil)
var _ gpio.ServoWriter = (*Adaptor)(nil)

var _ i2c.I2c = (*Adaptor)(nil)
var _ i2c.I2cConnector = (*Adaptor)(nil)

type NullReadWriteCloser struct {
contents []byte
Expand Down Expand Up @@ -139,12 +139,13 @@ func TestBeagleboneAdaptor(t *testing.T) {

// I2c
sysfs.SetSyscall(&sysfs.MockSyscall{})
a.I2cStart(0xff)

a.i2cDevice = &NullReadWriteCloser{}
con, err := a.I2cGetConnection(0xff, 1)
gobottest.Assert(t, err, nil)

a.I2cWrite(0xff, []byte{0x00, 0x01})
data, _ := a.I2cRead(0xff, 2)
con.Write([]byte{0x00, 0x01})
data := []byte{42, 42}
con.Read(data)
gobottest.Assert(t, data, []byte{0x00, 0x01})

gobottest.Assert(t, a.Finalize(), nil)
Expand Down
33 changes: 4 additions & 29 deletions platforms/chip/chip_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,35 +174,6 @@ func (c *Adaptor) DigitalWrite(pin string, val byte) (err error) {
return sysfsPin.Write(int(val))
}

// I2cStart starts an i2c device in specified address.
// This assumes that the bus used is /dev/i2c-1, which corresponds to
// pins labeled TWI1-SDA and TW1-SCK (pins 9 and 11 on header 13).
func (c *Adaptor) I2cStart(address int) (err error) {
if c.i2cBuses[1] == nil {
c.i2cBuses[1], err = sysfs.NewI2cDevice("/dev/i2c-1", address)
}
return err
}

// I2cWrite writes data to i2c device
func (c *Adaptor) I2cWrite(address int, data []byte) (err error) {
if err = c.i2cBuses[1].SetAddress(address); err != nil {
return
}
_, err = c.i2cBuses[1].Write(data)
return
}

// I2cRead returns value from i2c device using specified size
func (c *Adaptor) I2cRead(address int, size int) (data []byte, err error) {
if err = c.i2cBuses[1].SetAddress(address); err != nil {
return
}
data = make([]byte, size)
_, err = c.i2cBuses[1].Read(data)
return
}

// I2cGetConnection returns a connection to a device on a specified bus.
// Valid bus number is [0..2] which corresponds to /dev/i2c-0 through /dev/i2c-2.
func (c *Adaptor) I2cGetConnection(address int, bus int) (connection i2c.I2cConnection, err error) {
Expand All @@ -215,6 +186,10 @@ func (c *Adaptor) I2cGetConnection(address int, bus int) (connection i2c.I2cConn
return i2c.NewI2cConnection(c.i2cBuses[bus], address), err
}

func (c *Adaptor) I2cGetDefaultBus() int {
return 1
}

func getXIOBase() (baseAddr int, err error) {
// Default to original base from 4.3 kernel
baseAddr = 408
Expand Down
12 changes: 8 additions & 4 deletions platforms/chip/chip_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var _ gobot.Adaptor = (*Adaptor)(nil)
var _ gpio.DigitalReader = (*Adaptor)(nil)
var _ gpio.DigitalWriter = (*Adaptor)(nil)

var _ i2c.I2c = (*Adaptor)(nil)
var _ i2c.I2cConnector = (*Adaptor)(nil)

type NullReadWriteCloser struct {
contents []byte
Expand Down Expand Up @@ -80,9 +80,13 @@ func TestChipAdaptorI2c(t *testing.T) {
})
sysfs.SetFilesystem(fs)
sysfs.SetSyscall(&sysfs.MockSyscall{})
a.I2cStart(0xff)
a.I2cWrite(0xff, []byte{0x00, 0x01})
data, _ := a.I2cRead(0xff, 2)

con, err := a.I2cGetConnection(0xff, 1)
gobottest.Assert(t, err, nil)

con.Write([]byte{0x00, 0x01})
data := []byte{42, 42}
con.Read(data)
gobottest.Assert(t, data, []byte{0x00, 0x01})

gobottest.Assert(t, a.Finalize(), nil)
Expand Down
34 changes: 11 additions & 23 deletions platforms/firmata/firmata_adaptor.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package firmata

import (
"fmt"
"io"
"strconv"
"time"

"github.com/tarm/serial"
"gobot.io/x/gobot"
"gobot.io/x/gobot/drivers/i2c"
"gobot.io/x/gobot/platforms/firmata/client"
)

Expand Down Expand Up @@ -219,30 +221,16 @@ func (f *Adaptor) digitalPin(pin int) int {
return pin + 14
}

// I2cStart starts an i2c device at specified address
func (f *Adaptor) I2cStart(address int) (err error) {
return f.board.I2cConfig(0)
}

// I2cRead returns size bytes from the i2c device
// Returns an empty array if the response from the board has timed out
func (f *Adaptor) I2cRead(address int, size int) (data []byte, err error) {
ret := make(chan []byte)

if err = f.board.I2cRead(address, size); err != nil {
return
// I2cGetConnection returns a connection to a device on a specified bus.
// Only supports bus number 0
func (f *Adaptor) I2cGetConnection(address int, bus int) (connection i2c.I2cConnection, err error) {
if bus != 0 {
return nil, fmt.Errorf("Invalid bus number %d, only 0 is supported", bus)
}

f.Once(f.board.Event("I2cReply"), func(data interface{}) {
ret <- data.(client.I2cReply).Data
})

data = <-ret

return
err = f.board.I2cConfig(0)
return NewFirmataI2cConnection(f, address), err
}

// I2cWrite writes data to i2c device
func (f *Adaptor) I2cWrite(address int, data []byte) (err error) {
return f.board.I2cWrite(address, data)
func (c *Adaptor) I2cGetDefaultBus() int {
return 0
}
19 changes: 14 additions & 5 deletions platforms/firmata/firmata_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var _ aio.AnalogReader = (*Adaptor)(nil)
var _ gpio.PwmWriter = (*Adaptor)(nil)
var _ gpio.ServoWriter = (*Adaptor)(nil)

var _ i2c.I2c = (*Adaptor)(nil)
var _ i2c.I2cConnector = (*Adaptor)(nil)

type readWriteCloser struct{}

Expand Down Expand Up @@ -165,8 +165,9 @@ func TestAdaptorAnalogRead(t *testing.T) {

func TestAdaptorI2cStart(t *testing.T) {
a := initTestAdaptor()
a.I2cStart(0x00)
a.I2cGetConnection(0, 0)
}

func TestAdaptorI2cRead(t *testing.T) {
a := initTestAdaptor()
i := []byte{100}
Expand All @@ -175,13 +176,21 @@ func TestAdaptorI2cRead(t *testing.T) {
<-time.After(10 * time.Millisecond)
a.Publish(a.board.Event("I2cReply"), i2cReply)
}()
data, err := a.I2cRead(0x00, 1)

con, err := a.I2cGetConnection(0, 0)
gobottest.Assert(t, err, nil)

response := []byte{12}
_, err = con.Read(response)
gobottest.Assert(t, err, nil)
gobottest.Assert(t, data, i)
gobottest.Assert(t, response, i)
}

func TestAdaptorI2cWrite(t *testing.T) {
a := initTestAdaptor()
a.I2cWrite(0x00, []byte{0x00, 0x01})
con, err := a.I2cGetConnection(0, 0)
gobottest.Assert(t, err, nil)
con.Write([]byte{0x00, 0x01})
}

func TestServoConfig(t *testing.T) {
Expand Down
Loading

0 comments on commit 09142c5

Please sign in to comment.