Skip to content

Commit

Permalink
i2c: refactor i2c interface definitions out of sysfs into i2c package
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Jul 12, 2017
1 parent a5b099b commit 4728563
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 54 deletions.
27 changes: 22 additions & 5 deletions drivers/i2c/i2c.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package i2c

import (
"errors"
"io"
"sync"

"gobot.io/x/gobot/sysfs"
//"gobot.io/x/gobot/sysfs"
)

const (
Expand All @@ -27,6 +27,23 @@ var (
ErrInvalidPosition = errors.New("Invalid position value")
)

type I2cOperations interface {
io.ReadWriteCloser
ReadByte() (val byte, err error)
ReadByteData(reg uint8) (val uint8, err error)
ReadWordData(reg uint8) (val uint16, err error)
WriteByte(val byte) (err error)
WriteByteData(reg uint8, val uint8) (err error)
WriteWordData(reg uint8, val uint16) (err error)
WriteBlockData(reg uint8, b []byte) (err error)
}

// I2cDevice is the interface to a specific i2c bus
type I2cDevice interface {
I2cOperations
SetAddress(int) error
}

// Connector lets Adaptors provide the interface for Drivers
// to get access to the I2C buses on platforms that support I2C.
type Connector interface {
Expand All @@ -44,17 +61,17 @@ type Connector interface {
// Implements sysfs.I2cOperations to talk to the device, wrapping the
// calls in SetAddress to always target the specified device.
// Provided by an Adaptor by implementing the I2cConnector interface.
type Connection sysfs.I2cOperations
type Connection I2cOperations

type i2cConnection struct {
bus sysfs.I2cDevice
bus I2cDevice
address int
mutex *sync.Mutex
}

// NewConnection creates and returns a new connection to a specific
// i2c device on a bus and address.
func NewConnection(bus sysfs.I2cDevice, address int) (connection *i2cConnection) {
func NewConnection(bus I2cDevice, address int) (connection *i2cConnection) {
return &i2cConnection{bus: bus, address: address, mutex: &sync.Mutex{}}
}

Expand Down
4 changes: 2 additions & 2 deletions drivers/i2c/i2c_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func syscallImplFail(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err syscall.Errn
return 0, 0, 1
}

func initI2CDevice() sysfs.I2cDevice {
func initI2CDevice() I2cDevice {
fs := sysfs.NewMockFilesystem([]string{
"/dev/i2c-1",
})
Expand All @@ -41,7 +41,7 @@ func initI2CDevice() sysfs.I2cDevice {
return i
}

func initI2CDeviceAddressError() sysfs.I2cDevice {
func initI2CDeviceAddressError() I2cDevice {
fs := sysfs.NewMockFilesystem([]string{
"/dev/i2c-1",
})
Expand Down
4 changes: 2 additions & 2 deletions platforms/beaglebone/beaglebone_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Adaptor struct {
name string
digitalPins []*sysfs.DigitalPin
pwmPins map[string]*sysfs.PWMPin
i2cBuses map[int]sysfs.I2cDevice
i2cBuses map[int]i2c.I2cDevice
usrLed string
analogPath string
slots string
Expand All @@ -35,7 +35,7 @@ func NewAdaptor() *Adaptor {
name: gobot.DefaultName("Beaglebone"),
digitalPins: make([]*sysfs.DigitalPin, 120),
pwmPins: make(map[string]*sysfs.PWMPin),
i2cBuses: make(map[int]sysfs.I2cDevice),
i2cBuses: make(map[int]i2c.I2cDevice),
mutex: &sync.Mutex{},
}

Expand Down
2 changes: 1 addition & 1 deletion platforms/chip/chip_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Adaptor struct {
pinmap map[string]sysfsPin
digitalPins map[int]*sysfs.DigitalPin
pwmPins map[int]*sysfs.PWMPin
i2cBuses [3]sysfs.I2cDevice
i2cBuses [3]i2c.I2cDevice
mutex *sync.Mutex
}

Expand Down
2 changes: 1 addition & 1 deletion platforms/dragonboard/dragonboard_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Adaptor struct {
name string
digitalPins map[int]*sysfs.DigitalPin
pinMap map[string]int
i2cBuses [3]sysfs.I2cDevice
i2cBuses [3]i2c.I2cDevice
mutex *sync.Mutex
}

Expand Down
2 changes: 1 addition & 1 deletion platforms/intel-iot/edison/edison_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Adaptor struct {
tristate *sysfs.DigitalPin
digitalPins map[int]*sysfs.DigitalPin
pwmPins map[int]*sysfs.PWMPin
i2cBus sysfs.I2cDevice
i2cBus i2c.I2cDevice
connect func(e *Adaptor) (err error)
writeFile func(path string, data []byte) (i int, err error)
readFile func(path string) ([]byte, error)
Expand Down
2 changes: 1 addition & 1 deletion platforms/intel-iot/joule/joule_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Adaptor struct {
name string
digitalPins map[int]*sysfs.DigitalPin
pwmPins map[int]*sysfs.PWMPin
i2cBuses [3]sysfs.I2cDevice
i2cBuses [3]i2c.I2cDevice
connect func(e *Adaptor) (err error)
mutex *sync.Mutex
}
Expand Down
7 changes: 4 additions & 3 deletions platforms/raspi/raspi_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (
"strconv"
"strings"

"sync"

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

var readFile = func() ([]byte, error) {
Expand All @@ -26,7 +27,7 @@ type Adaptor struct {
digitalPins map[int]*sysfs.DigitalPin
pwmPins map[int]*PWMPin
i2cDefaultBus int
i2cBuses [2]sysfs.I2cDevice
i2cBuses [2]i2c.I2cDevice
}

// NewAdaptor creates a Raspi Adaptor
Expand Down Expand Up @@ -173,7 +174,7 @@ func (r *Adaptor) GetConnection(address int, bus int) (connection i2c.Connection
return i2c.NewConnection(device, address), err
}

func (r *Adaptor) getI2cBus(bus int) (_ sysfs.I2cDevice, err error) {
func (r *Adaptor) getI2cBus(bus int) (_ i2c.I2cDevice, err error) {
r.mutex.Lock()
defer r.mutex.Unlock()

Expand Down
2 changes: 1 addition & 1 deletion platforms/tinkerboard/adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Adaptor struct {
pinmap map[string]sysfsPin
digitalPins map[int]*sysfs.DigitalPin
pwmPins map[int]*sysfs.PWMPin
i2cBuses [2]sysfs.I2cDevice
i2cBuses [2]i2c.I2cDevice
mutex *sync.Mutex
}

Expand Down
18 changes: 0 additions & 18 deletions sysfs/i2c_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package sysfs

import (
"fmt"
"io"
"os"
"syscall"
"unsafe"
Expand Down Expand Up @@ -46,23 +45,6 @@ type i2cSmbusIoctlData struct {
data uintptr
}

type I2cOperations interface {
io.ReadWriteCloser
ReadByte() (val byte, err error)
ReadByteData(reg uint8) (val uint8, err error)
ReadWordData(reg uint8) (val uint16, err error)
WriteByte(val byte) (err error)
WriteByteData(reg uint8, val uint8) (err error)
WriteWordData(reg uint8, val uint16) (err error)
WriteBlockData(reg uint8, b []byte) (err error)
}

// I2cDevice is the interface to a specific i2c bus
type I2cDevice interface {
I2cOperations
SetAddress(int) error
}

type i2cDevice struct {
file File
funcs uint64 // adapter functionality mask
Expand Down
39 changes: 20 additions & 19 deletions sysfs/i2c_device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"syscall"
"testing"

"gobot.io/x/gobot/drivers/i2c"
"gobot.io/x/gobot/gobottest"
)

Expand All @@ -18,7 +19,7 @@ func TestNewI2cDeviceClose(t *testing.T) {
SetSyscall(&MockSyscall{})

i, err := NewI2cDevice("/dev/i2c-1")
var _ I2cDevice = i
var _ i2c.I2cDevice = i

gobottest.Assert(t, err, nil)
gobottest.Assert(t, i.Close(), nil)
Expand All @@ -37,7 +38,7 @@ func TestNewI2cDeviceQueryFuncError(t *testing.T) {
})

i, err := NewI2cDevice("/dev/i2c-1")
var _ I2cDevice = i
var _ i2c.I2cDevice = i

gobottest.Assert(t, err, errors.New("Querying functionality failed with syscall.Errno operation not permitted"))
}
Expand All @@ -61,7 +62,7 @@ func TestNewI2cDevice(t *testing.T) {
SetSyscall(&MockSyscall{})

i, err = NewI2cDevice("/dev/i2c-1")
var _ I2cDevice = i
var _ i2c.I2cDevice = i

gobottest.Assert(t, err, nil)

Expand Down Expand Up @@ -89,7 +90,7 @@ func TestNewI2cDeviceReadByte(t *testing.T) {
SetFilesystem(fs)

i, err := NewI2cDevice("/dev/i2c-1")
var _ I2cDevice = i
var _ i2c.I2cDevice = i

gobottest.Assert(t, err, nil)

Expand All @@ -108,7 +109,7 @@ func TestNewI2cDeviceReadByteError(t *testing.T) {
SetFilesystem(fs)

i, err := NewI2cDevice("/dev/i2c-1")
var _ I2cDevice = i
var _ i2c.I2cDevice = i

gobottest.Assert(t, err, nil)

Expand All @@ -128,7 +129,7 @@ func TestNewI2cDeviceReadByteError(t *testing.T) {
func TestNewI2cDeviceReadByteNotSupported(t *testing.T) {
SetSyscall(&MockSyscall{})
i, err := NewI2cDevice("/dev/i2c-1")
var _ I2cDevice = i
var _ i2c.I2cDevice = i

gobottest.Assert(t, err, nil)

Expand All @@ -145,7 +146,7 @@ func TestNewI2cDeviceWriteByte(t *testing.T) {
SetFilesystem(fs)

i, err := NewI2cDevice("/dev/i2c-1")
var _ I2cDevice = i
var _ i2c.I2cDevice = i

gobottest.Assert(t, err, nil)

Expand All @@ -159,7 +160,7 @@ func TestNewI2cDeviceWriteByte(t *testing.T) {
func TestNewI2cDeviceWriteByteNotSupported(t *testing.T) {
SetSyscall(&MockSyscall{})
i, err := NewI2cDevice("/dev/i2c-1")
var _ I2cDevice = i
var _ i2c.I2cDevice = i

gobottest.Assert(t, err, nil)

Expand All @@ -176,7 +177,7 @@ func TestNewI2cDeviceReadByteData(t *testing.T) {
SetFilesystem(fs)

i, err := NewI2cDevice("/dev/i2c-1")
var _ I2cDevice = i
var _ i2c.I2cDevice = i

gobottest.Assert(t, err, nil)

Expand All @@ -191,7 +192,7 @@ func TestNewI2cDeviceReadByteData(t *testing.T) {
func TestNewI2cDeviceReadByteDataNotSupported(t *testing.T) {
SetSyscall(&MockSyscall{})
i, err := NewI2cDevice("/dev/i2c-1")
var _ I2cDevice = i
var _ i2c.I2cDevice = i

gobottest.Assert(t, err, nil)

Expand All @@ -208,7 +209,7 @@ func TestNewI2cDeviceWriteByteData(t *testing.T) {
SetFilesystem(fs)

i, err := NewI2cDevice("/dev/i2c-1")
var _ I2cDevice = i
var _ i2c.I2cDevice = i

gobottest.Assert(t, err, nil)

Expand All @@ -222,7 +223,7 @@ func TestNewI2cDeviceWriteByteData(t *testing.T) {
func TestNewI2cDeviceWriteByteDataNotSupported(t *testing.T) {
SetSyscall(&MockSyscall{})
i, err := NewI2cDevice("/dev/i2c-1")
var _ I2cDevice = i
var _ i2c.I2cDevice = i

gobottest.Assert(t, err, nil)

Expand All @@ -239,7 +240,7 @@ func TestNewI2cDeviceReadWordData(t *testing.T) {
SetFilesystem(fs)

i, err := NewI2cDevice("/dev/i2c-1")
var _ I2cDevice = i
var _ i2c.I2cDevice = i

gobottest.Assert(t, err, nil)

Expand All @@ -254,7 +255,7 @@ func TestNewI2cDeviceReadWordData(t *testing.T) {
func TestNewI2cDeviceReadWordDataNotSupported(t *testing.T) {
SetSyscall(&MockSyscall{})
i, err := NewI2cDevice("/dev/i2c-1")
var _ I2cDevice = i
var _ i2c.I2cDevice = i

gobottest.Assert(t, err, nil)

Expand All @@ -271,7 +272,7 @@ func TestNewI2cDeviceWriteWordData(t *testing.T) {
SetFilesystem(fs)

i, err := NewI2cDevice("/dev/i2c-1")
var _ I2cDevice = i
var _ i2c.I2cDevice = i

gobottest.Assert(t, err, nil)

Expand All @@ -285,7 +286,7 @@ func TestNewI2cDeviceWriteWordData(t *testing.T) {
func TestNewI2cDeviceWriteWordDataNotSupported(t *testing.T) {
SetSyscall(&MockSyscall{})
i, err := NewI2cDevice("/dev/i2c-1")
var _ I2cDevice = i
var _ i2c.I2cDevice = i

gobottest.Assert(t, err, nil)

Expand All @@ -302,7 +303,7 @@ func TestNewI2cDeviceWriteBlockData(t *testing.T) {
SetFilesystem(fs)

i, err := NewI2cDevice("/dev/i2c-1")
var _ I2cDevice = i
var _ i2c.I2cDevice = i

gobottest.Assert(t, err, nil)

Expand All @@ -319,7 +320,7 @@ func TestNewI2cDeviceWriteBlockDataTooMuch(t *testing.T) {
SetFilesystem(fs)

i, err := NewI2cDevice("/dev/i2c-1")
var _ I2cDevice = i
var _ i2c.I2cDevice = i

gobottest.Assert(t, err, nil)

Expand All @@ -334,7 +335,7 @@ func TestNewI2cDeviceWriteBlockDataTooMuch(t *testing.T) {
func TestNewI2cDeviceWrite(t *testing.T) {
SetSyscall(&MockSyscall{})
i, err := NewI2cDevice("/dev/i2c-1")
var _ I2cDevice = i
var _ i2c.I2cDevice = i

gobottest.Assert(t, err, nil)

Expand Down

0 comments on commit 4728563

Please sign in to comment.