i2c: refactor i2c interface definitions out of sysfs into i2c package

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram 2017-07-12 09:00:35 -06:00
parent a5b099b800
commit 472856310a
11 changed files with 55 additions and 54 deletions

View File

@ -2,9 +2,9 @@ package i2c
import (
"errors"
"io"
"sync"
"gobot.io/x/gobot/sysfs"
//"gobot.io/x/gobot/sysfs"
)
const (
@ -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 {
@ -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{}}
}

View File

@ -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",
})
@ -41,7 +41,7 @@ func initI2CDevice() sysfs.I2cDevice {
return i
}
func initI2CDeviceAddressError() sysfs.I2cDevice {
func initI2CDeviceAddressError() I2cDevice {
fs := sysfs.NewMockFilesystem([]string{
"/dev/i2c-1",
})

View File

@ -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
@ -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{},
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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)

View File

@ -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
}

View File

@ -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) {
@ -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
@ -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()

View File

@ -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
}

View File

@ -2,7 +2,6 @@ package sysfs
import (
"fmt"
"io"
"os"
"syscall"
"unsafe"
@ -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

View File

@ -6,6 +6,7 @@ import (
"syscall"
"testing"
"gobot.io/x/gobot/drivers/i2c"
"gobot.io/x/gobot/gobottest"
)
@ -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)
@ -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"))
}
@ -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)
@ -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)
@ -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)
@ -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)
@ -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)
@ -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)
@ -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)
@ -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)
@ -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)
@ -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)
@ -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)
@ -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)
@ -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)
@ -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)
@ -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)
@ -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)
@ -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)