mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-04-29 13:49:14 +08:00
Refactored platforms to new I2C interfaces
Signed-off-by: Erik Agsjö <erik.agsjo@gmail.com>
This commit is contained in:
parent
543a246916
commit
09142c55ee
@ -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) {
|
||||
@ -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
|
||||
@ -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
|
||||
@ -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)
|
||||
|
||||
// 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)
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
_, err = b.i2cDevice.Write(data)
|
||||
return
|
||||
}
|
||||
|
||||
// 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
|
||||
|
@ -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
|
||||
@ -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)
|
||||
|
@ -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) {
|
||||
@ -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
|
||||
|
@ -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
|
||||
@ -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)
|
||||
|
@ -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"
|
||||
)
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
@ -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{}
|
||||
|
||||
@ -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}
|
||||
@ -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)
|
||||
gobottest.Assert(t, data, i)
|
||||
|
||||
response := []byte{12}
|
||||
_, err = con.Read(response)
|
||||
gobottest.Assert(t, err, nil)
|
||||
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) {
|
||||
|
126
platforms/firmata/firmata_i2c.go
Normal file
126
platforms/firmata/firmata_i2c.go
Normal file
@ -0,0 +1,126 @@
|
||||
package firmata
|
||||
|
||||
import (
|
||||
// "gobot.io/x/gobot/drivers/i2c"
|
||||
"gobot.io/x/gobot/platforms/firmata/client"
|
||||
)
|
||||
|
||||
type firmataI2cConnection struct {
|
||||
address int
|
||||
adaptor *Adaptor
|
||||
}
|
||||
|
||||
// NewFirmataI2cConnection creates an I2C connection to an I2C device at
|
||||
// the specified address
|
||||
func NewFirmataI2cConnection(adaptor *Adaptor, address int) (connection *firmataI2cConnection) {
|
||||
return &firmataI2cConnection{adaptor: adaptor, address: address}
|
||||
}
|
||||
|
||||
// Read tries to read a full buffer from the i2c device.
|
||||
// Returns an empty array if the response from the board has timed out.
|
||||
func (c *firmataI2cConnection) Read(b []byte) (read int, err error) {
|
||||
ret := make(chan []byte)
|
||||
|
||||
if err = c.adaptor.board.I2cRead(c.address, len(b)); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
c.adaptor.Once(c.adaptor.board.Event("I2cReply"), func(data interface{}) {
|
||||
ret <- data.(client.I2cReply).Data
|
||||
})
|
||||
|
||||
result := <-ret
|
||||
copy(b, result)
|
||||
|
||||
read = len(result)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (c *firmataI2cConnection) Write(data []byte) (written int, err error) {
|
||||
err = c.adaptor.board.I2cWrite(c.address, data)
|
||||
// Assume successful write of whole buffer
|
||||
written = len(data)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *firmataI2cConnection) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *firmataI2cConnection) ReadByte() (val uint8, err error) {
|
||||
buf := []byte{0}
|
||||
if _, err = c.Read(buf); err != nil {
|
||||
return
|
||||
}
|
||||
val = buf[0]
|
||||
return
|
||||
}
|
||||
|
||||
func (c *firmataI2cConnection) ReadByteData(reg uint8) (val uint8, err error) {
|
||||
if err = c.WriteByte(reg); err != nil {
|
||||
return
|
||||
}
|
||||
return c.ReadByte()
|
||||
}
|
||||
|
||||
func (c *firmataI2cConnection) ReadWordData(reg uint8) (val uint16, err error) {
|
||||
if err = c.WriteByte(reg); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
buf := []byte{0, 0}
|
||||
if _, err = c.Read(buf); err != nil {
|
||||
return
|
||||
}
|
||||
low, high := buf[0], buf[1]
|
||||
|
||||
val = (uint16(high) << 8) | uint16(low)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *firmataI2cConnection) ReadBlockData(reg uint8, b []byte) (n int, err error) {
|
||||
// Assume target device handles this well, will not behave exactly as on SMBus
|
||||
if err = c.WriteByte(reg); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Try to read full 32 bytes
|
||||
buf := make([]byte, 32)
|
||||
bytesRead, err := c.Read(buf)
|
||||
copy(b, buf)
|
||||
|
||||
return bytesRead, err
|
||||
}
|
||||
|
||||
func (c *firmataI2cConnection) WriteByte(val uint8) (err error) {
|
||||
buf := []byte{val}
|
||||
_, err = c.Write(buf)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *firmataI2cConnection) WriteByteData(reg uint8, val uint8) (err error) {
|
||||
buf := []byte{reg, val}
|
||||
_, err = c.Write(buf)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *firmataI2cConnection) WriteWordData(reg uint8, val uint16) (err error) {
|
||||
low := uint8(val & 0xff)
|
||||
high := uint8((val >> 8) & 0xff)
|
||||
buf := []byte{reg, low, high}
|
||||
_, err = c.Write(buf)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *firmataI2cConnection) WriteBlockData(reg uint8, data []byte) (err error) {
|
||||
if len(data) > 32 {
|
||||
data = data[:32]
|
||||
}
|
||||
|
||||
buf := make([]byte, len(data)+1)
|
||||
copy(buf[:1], data)
|
||||
buf[0] = reg
|
||||
_, err = c.Write(buf)
|
||||
return
|
||||
}
|
@ -2,11 +2,13 @@ package edison
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
multierror "github.com/hashicorp/go-multierror"
|
||||
"gobot.io/x/gobot"
|
||||
"gobot.io/x/gobot/drivers/i2c"
|
||||
"gobot.io/x/gobot/sysfs"
|
||||
)
|
||||
|
||||
@ -56,7 +58,7 @@ type Adaptor struct {
|
||||
tristate sysfs.DigitalPin
|
||||
digitalPins map[int]sysfs.DigitalPin
|
||||
pwmPins map[int]*pwmPin
|
||||
i2cDevice sysfs.I2cDevice
|
||||
i2cBus sysfs.I2cDevice
|
||||
connect func(e *Adaptor) (err error)
|
||||
}
|
||||
|
||||
@ -140,8 +142,8 @@ func (e *Adaptor) Finalize() (err error) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if e.i2cDevice != nil {
|
||||
if errs := e.i2cDevice.Close(); errs != nil {
|
||||
if e.i2cBus != nil {
|
||||
if errs := e.i2cBus.Close(); errs != nil {
|
||||
err = multierror.Append(err, errs)
|
||||
}
|
||||
}
|
||||
@ -421,40 +423,27 @@ func (e *Adaptor) AnalogRead(pin string) (val int, err error) {
|
||||
return val / 4, err
|
||||
}
|
||||
|
||||
// I2cStart initializes i2c device for addresss
|
||||
func (e *Adaptor) I2cStart(address int) (err error) {
|
||||
if e.i2cDevice != nil {
|
||||
return
|
||||
// I2cGetConnection returns a connection to a device on a specified bus.
|
||||
// Valid bus numbers are 1 and 6 (arduino).
|
||||
func (e *Adaptor) I2cGetConnection(address int, bus int) (connection i2c.I2cConnection, err error) {
|
||||
if !(bus == e.I2cGetDefaultBus()) {
|
||||
return nil, errors.New("Unsupported I2C bus")
|
||||
}
|
||||
if e.i2cBus == nil {
|
||||
if bus == 6 && e.board == "arduino" {
|
||||
e.arduinoI2CSetup()
|
||||
}
|
||||
e.i2cBus, err = sysfs.NewI2cDevice(fmt.Sprintf("/dev/i2c-%d", bus))
|
||||
}
|
||||
return i2c.NewI2cConnection(e.i2cBus, address), err
|
||||
}
|
||||
|
||||
// most board use I2C bus 1
|
||||
bus := "/dev/i2c-1"
|
||||
|
||||
// except for Arduino which uses bus 6
|
||||
func (e *Adaptor) I2cGetDefaultBus() int {
|
||||
// Arduino uses bus 6
|
||||
if e.board == "arduino" {
|
||||
bus = "/dev/i2c-6"
|
||||
e.arduinoI2CSetup()
|
||||
return 6
|
||||
} else {
|
||||
// all other default to 1
|
||||
return 1
|
||||
}
|
||||
|
||||
e.i2cDevice, err = sysfs.NewI2cDevice(bus, address)
|
||||
return
|
||||
}
|
||||
|
||||
// I2cWrite writes data to i2c device
|
||||
func (e *Adaptor) I2cWrite(address int, data []byte) (err error) {
|
||||
if err = e.i2cDevice.SetAddress(address); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = e.i2cDevice.Write(data)
|
||||
return
|
||||
}
|
||||
|
||||
// I2cRead returns size bytes from the i2c device
|
||||
func (e *Adaptor) I2cRead(address int, size int) (data []byte, err error) {
|
||||
data = make([]byte, size)
|
||||
if err = e.i2cDevice.SetAddress(address); err != nil {
|
||||
return
|
||||
}
|
||||
_, err = e.i2cDevice.Read(data)
|
||||
return
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ var _ gpio.DigitalWriter = (*Adaptor)(nil)
|
||||
var _ aio.AnalogReader = (*Adaptor)(nil)
|
||||
var _ gpio.PwmWriter = (*Adaptor)(nil)
|
||||
|
||||
var _ i2c.I2c = (*Adaptor)(nil)
|
||||
var _ i2c.I2cConnector = (*Adaptor)(nil)
|
||||
|
||||
type NullReadWriteCloser struct {
|
||||
contents []byte
|
||||
@ -139,7 +139,7 @@ func TestAdaptorFinalize(t *testing.T) {
|
||||
a.PwmWrite("5", 100)
|
||||
|
||||
sysfs.SetSyscall(&sysfs.MockSyscall{})
|
||||
a.I2cStart(0xff)
|
||||
a.I2cGetConnection(0xff, 6)
|
||||
|
||||
gobottest.Assert(t, a.Finalize(), nil)
|
||||
|
||||
@ -164,12 +164,12 @@ func TestAdaptorI2c(t *testing.T) {
|
||||
a, _ := initTestAdaptor()
|
||||
|
||||
sysfs.SetSyscall(&sysfs.MockSyscall{})
|
||||
a.I2cStart(0xff)
|
||||
con, err := a.I2cGetConnection(0xff, 6)
|
||||
gobottest.Assert(t, err, nil)
|
||||
|
||||
a.i2cDevice = &NullReadWriteCloser{}
|
||||
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})
|
||||
}
|
||||
|
||||
|
@ -1,116 +1,116 @@
|
||||
package edison
|
||||
|
||||
var sparkfunPinMap = map[string]sysfsPin{
|
||||
"12": sysfsPin{
|
||||
pin: 12,
|
||||
resistor: -1,
|
||||
levelShifter: -1,
|
||||
pwmPin: -1,
|
||||
mux: []mux{},
|
||||
},
|
||||
"13": sysfsPin{
|
||||
pin: 13,
|
||||
resistor: -1,
|
||||
levelShifter: -1,
|
||||
pwmPin: -1,
|
||||
mux: []mux{},
|
||||
},
|
||||
"14": sysfsPin{
|
||||
pin: 14,
|
||||
resistor: -1,
|
||||
levelShifter: -1,
|
||||
pwmPin: -1,
|
||||
mux: []mux{},
|
||||
},
|
||||
"15": sysfsPin{
|
||||
pin: 15,
|
||||
resistor: -1,
|
||||
levelShifter: -1,
|
||||
pwmPin: -1,
|
||||
mux: []mux{},
|
||||
},
|
||||
"44": sysfsPin{
|
||||
pin: 44,
|
||||
resistor: -1,
|
||||
levelShifter: -1,
|
||||
pwmPin: -1,
|
||||
mux: []mux{},
|
||||
},
|
||||
"45": sysfsPin{
|
||||
pin: 45,
|
||||
resistor: -1,
|
||||
levelShifter: -1,
|
||||
pwmPin: -1,
|
||||
mux: []mux{},
|
||||
},
|
||||
"46": sysfsPin{
|
||||
pin: 46,
|
||||
resistor: -1,
|
||||
levelShifter: -1,
|
||||
pwmPin: -1,
|
||||
mux: []mux{},
|
||||
},
|
||||
"47": sysfsPin{
|
||||
pin: 47,
|
||||
resistor: -1,
|
||||
levelShifter: -1,
|
||||
pwmPin: -1,
|
||||
mux: []mux{},
|
||||
},
|
||||
"48": sysfsPin{
|
||||
pin: 48,
|
||||
resistor: -1,
|
||||
levelShifter: -1,
|
||||
pwmPin: -1,
|
||||
mux: []mux{},
|
||||
},
|
||||
"49": sysfsPin{
|
||||
pin: 49,
|
||||
resistor: -1,
|
||||
levelShifter: -1,
|
||||
pwmPin: -1,
|
||||
mux: []mux{},
|
||||
},
|
||||
"128": sysfsPin{
|
||||
pin: 128,
|
||||
resistor: -1,
|
||||
levelShifter: -1,
|
||||
pwmPin: -1,
|
||||
mux: []mux{},
|
||||
},
|
||||
"129": sysfsPin{
|
||||
pin: 129,
|
||||
resistor: -1,
|
||||
levelShifter: -1,
|
||||
pwmPin: -1,
|
||||
mux: []mux{},
|
||||
},
|
||||
"130": sysfsPin{
|
||||
pin: 130,
|
||||
resistor: -1,
|
||||
levelShifter: -1,
|
||||
pwmPin: -1,
|
||||
mux: []mux{},
|
||||
},
|
||||
"131": sysfsPin{
|
||||
pin: 131,
|
||||
resistor: -1,
|
||||
levelShifter: -1,
|
||||
pwmPin: -1,
|
||||
mux: []mux{},
|
||||
},
|
||||
"182": sysfsPin{
|
||||
pin: 182,
|
||||
resistor: -1,
|
||||
levelShifter: -1,
|
||||
pwmPin: -1,
|
||||
mux: []mux{},
|
||||
},
|
||||
"183": sysfsPin{
|
||||
pin: 183,
|
||||
resistor: -1,
|
||||
levelShifter: -1,
|
||||
pwmPin: -1,
|
||||
mux: []mux{},
|
||||
},
|
||||
}
|
||||
"12": sysfsPin{
|
||||
pin: 12,
|
||||
resistor: -1,
|
||||
levelShifter: -1,
|
||||
pwmPin: -1,
|
||||
mux: []mux{},
|
||||
},
|
||||
"13": sysfsPin{
|
||||
pin: 13,
|
||||
resistor: -1,
|
||||
levelShifter: -1,
|
||||
pwmPin: -1,
|
||||
mux: []mux{},
|
||||
},
|
||||
"14": sysfsPin{
|
||||
pin: 14,
|
||||
resistor: -1,
|
||||
levelShifter: -1,
|
||||
pwmPin: -1,
|
||||
mux: []mux{},
|
||||
},
|
||||
"15": sysfsPin{
|
||||
pin: 15,
|
||||
resistor: -1,
|
||||
levelShifter: -1,
|
||||
pwmPin: -1,
|
||||
mux: []mux{},
|
||||
},
|
||||
"44": sysfsPin{
|
||||
pin: 44,
|
||||
resistor: -1,
|
||||
levelShifter: -1,
|
||||
pwmPin: -1,
|
||||
mux: []mux{},
|
||||
},
|
||||
"45": sysfsPin{
|
||||
pin: 45,
|
||||
resistor: -1,
|
||||
levelShifter: -1,
|
||||
pwmPin: -1,
|
||||
mux: []mux{},
|
||||
},
|
||||
"46": sysfsPin{
|
||||
pin: 46,
|
||||
resistor: -1,
|
||||
levelShifter: -1,
|
||||
pwmPin: -1,
|
||||
mux: []mux{},
|
||||
},
|
||||
"47": sysfsPin{
|
||||
pin: 47,
|
||||
resistor: -1,
|
||||
levelShifter: -1,
|
||||
pwmPin: -1,
|
||||
mux: []mux{},
|
||||
},
|
||||
"48": sysfsPin{
|
||||
pin: 48,
|
||||
resistor: -1,
|
||||
levelShifter: -1,
|
||||
pwmPin: -1,
|
||||
mux: []mux{},
|
||||
},
|
||||
"49": sysfsPin{
|
||||
pin: 49,
|
||||
resistor: -1,
|
||||
levelShifter: -1,
|
||||
pwmPin: -1,
|
||||
mux: []mux{},
|
||||
},
|
||||
"128": sysfsPin{
|
||||
pin: 128,
|
||||
resistor: -1,
|
||||
levelShifter: -1,
|
||||
pwmPin: -1,
|
||||
mux: []mux{},
|
||||
},
|
||||
"129": sysfsPin{
|
||||
pin: 129,
|
||||
resistor: -1,
|
||||
levelShifter: -1,
|
||||
pwmPin: -1,
|
||||
mux: []mux{},
|
||||
},
|
||||
"130": sysfsPin{
|
||||
pin: 130,
|
||||
resistor: -1,
|
||||
levelShifter: -1,
|
||||
pwmPin: -1,
|
||||
mux: []mux{},
|
||||
},
|
||||
"131": sysfsPin{
|
||||
pin: 131,
|
||||
resistor: -1,
|
||||
levelShifter: -1,
|
||||
pwmPin: -1,
|
||||
mux: []mux{},
|
||||
},
|
||||
"182": sysfsPin{
|
||||
pin: 182,
|
||||
resistor: -1,
|
||||
levelShifter: -1,
|
||||
pwmPin: -1,
|
||||
mux: []mux{},
|
||||
},
|
||||
"183": sysfsPin{
|
||||
pin: 183,
|
||||
resistor: -1,
|
||||
levelShifter: -1,
|
||||
pwmPin: -1,
|
||||
mux: []mux{},
|
||||
},
|
||||
}
|
||||
|
@ -4,10 +4,12 @@ import (
|
||||
"errors"
|
||||
"os"
|
||||
"strconv"
|
||||
"fmt"
|
||||
|
||||
multierror "github.com/hashicorp/go-multierror"
|
||||
"gobot.io/x/gobot"
|
||||
"gobot.io/x/gobot/sysfs"
|
||||
"gobot.io/x/gobot/drivers/i2c"
|
||||
)
|
||||
|
||||
func writeFile(path string, data []byte) (i int, err error) {
|
||||
@ -46,7 +48,7 @@ type Adaptor struct {
|
||||
name string
|
||||
digitalPins map[int]sysfs.DigitalPin
|
||||
pwmPins map[int]*pwmPin
|
||||
i2cDevice sysfs.I2cDevice
|
||||
i2cBuses [3]sysfs.I2cDevice
|
||||
connect func(e *Adaptor) (err error)
|
||||
}
|
||||
|
||||
@ -93,9 +95,11 @@ func (e *Adaptor) Finalize() (err error) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if e.i2cDevice != nil {
|
||||
if errs := e.i2cDevice.Close(); errs != nil {
|
||||
err = multierror.Append(err, errs)
|
||||
for _, bus := range e.i2cBuses {
|
||||
if bus != nil {
|
||||
if errs := bus.Close(); errs != nil {
|
||||
err = multierror.Append(err, errs)
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
@ -172,32 +176,19 @@ func (e *Adaptor) PwmWrite(pin string, val byte) (err error) {
|
||||
return errors.New("Not a PWM pin")
|
||||
}
|
||||
|
||||
// I2cStart initializes i2c device for addresss
|
||||
func (e *Adaptor) I2cStart(address int) (err error) {
|
||||
if e.i2cDevice != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// TODO: handle the additional I2C buses
|
||||
e.i2cDevice, err = sysfs.NewI2cDevice("/dev/i2c-0", address)
|
||||
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) {
|
||||
if (bus < 0) || (bus > 2) {
|
||||
return nil, fmt.Errorf("Bus number %d out of range", bus)
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
// I2cWrite writes data to i2c device
|
||||
func (e *Adaptor) I2cWrite(address int, data []byte) (err error) {
|
||||
if err = e.i2cDevice.SetAddress(address); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = e.i2cDevice.Write(data)
|
||||
return
|
||||
}
|
||||
|
||||
// I2cRead returns size bytes from the i2c device
|
||||
func (e *Adaptor) I2cRead(address int, size int) (data []byte, err error) {
|
||||
data = make([]byte, size)
|
||||
if err = e.i2cDevice.SetAddress(address); err != nil {
|
||||
return
|
||||
}
|
||||
_, err = e.i2cDevice.Read(data)
|
||||
return
|
||||
func (c *Adaptor) I2cGetDefaultBus() int {
|
||||
return 0
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ var _ gpio.DigitalReader = (*Adaptor)(nil)
|
||||
var _ gpio.DigitalWriter = (*Adaptor)(nil)
|
||||
var _ gpio.PwmWriter = (*Adaptor)(nil)
|
||||
|
||||
var _ i2c.I2c = (*Adaptor)(nil)
|
||||
var _ i2c.I2cConnector = (*Adaptor)(nil)
|
||||
|
||||
type NullReadWriteCloser struct {
|
||||
contents []byte
|
||||
@ -126,7 +126,7 @@ func TestAdaptorFinalize(t *testing.T) {
|
||||
a.PwmWrite("25", 100)
|
||||
|
||||
sysfs.SetSyscall(&sysfs.MockSyscall{})
|
||||
a.I2cStart(0xff)
|
||||
a.I2cGetConnection(0xff, 0)
|
||||
|
||||
gobottest.Assert(t, a.Finalize(), nil)
|
||||
|
||||
@ -151,12 +151,12 @@ func TestAdaptorI2c(t *testing.T) {
|
||||
a, _ := initTestAdaptor()
|
||||
|
||||
sysfs.SetSyscall(&sysfs.MockSyscall{})
|
||||
a.I2cStart(0xff)
|
||||
con, err := a.I2cGetConnection(0xff, 0)
|
||||
gobottest.Assert(t, err, nil)
|
||||
|
||||
a.i2cDevice = &NullReadWriteCloser{}
|
||||
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})
|
||||
}
|
||||
|
||||
|
@ -10,6 +10,7 @@ import (
|
||||
|
||||
multierror "github.com/hashicorp/go-multierror"
|
||||
"gobot.io/x/gobot"
|
||||
"gobot.io/x/gobot/drivers/i2c"
|
||||
"gobot.io/x/gobot/sysfs"
|
||||
)
|
||||
|
||||
@ -19,12 +20,12 @@ var readFile = func() ([]byte, error) {
|
||||
|
||||
// Adaptor is the Gobot Adaptor for the Raspberry Pi
|
||||
type Adaptor struct {
|
||||
name string
|
||||
revision string
|
||||
i2cLocation string
|
||||
digitalPins map[int]sysfs.DigitalPin
|
||||
pwmPins []int
|
||||
i2cDevice sysfs.I2cDevice
|
||||
name string
|
||||
revision string
|
||||
digitalPins map[int]sysfs.DigitalPin
|
||||
pwmPins []int
|
||||
i2cDefaultBus int
|
||||
i2cBuses [2]sysfs.I2cDevice
|
||||
}
|
||||
|
||||
// NewAdaptor creates a Raspi Adaptor
|
||||
@ -39,10 +40,10 @@ func NewAdaptor() *Adaptor {
|
||||
if strings.Contains(v, "Revision") {
|
||||
s := strings.Split(string(v), " ")
|
||||
version, _ := strconv.ParseInt("0x"+s[len(s)-1], 0, 64)
|
||||
r.i2cLocation = "/dev/i2c-1"
|
||||
r.i2cDefaultBus = 1
|
||||
if version <= 3 {
|
||||
r.revision = "1"
|
||||
r.i2cLocation = "/dev/i2c-0"
|
||||
r.i2cDefaultBus = 0
|
||||
} else if version <= 15 {
|
||||
r.revision = "2"
|
||||
} else {
|
||||
@ -80,9 +81,11 @@ func (r *Adaptor) Finalize() (err error) {
|
||||
err = multierror.Append(err, perr)
|
||||
}
|
||||
}
|
||||
if r.i2cDevice != nil {
|
||||
if perr := r.i2cDevice.Close(); err != nil {
|
||||
err = multierror.Append(err, perr)
|
||||
for _, bus := range r.i2cBuses {
|
||||
if bus != nil {
|
||||
if e := bus.Close(); e != nil {
|
||||
err = multierror.Append(err, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
@ -161,31 +164,20 @@ func (r *Adaptor) DigitalWrite(pin string, val byte) (err error) {
|
||||
return sysfsPin.Write(int(val))
|
||||
}
|
||||
|
||||
// I2cStart starts a i2c device in specified address
|
||||
func (r *Adaptor) I2cStart(address int) (err error) {
|
||||
if r.i2cDevice == nil {
|
||||
r.i2cDevice, err = sysfs.NewI2cDevice(r.i2cLocation, address)
|
||||
// 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)
|
||||
}
|
||||
return err
|
||||
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
|
||||
}
|
||||
|
||||
// I2CWrite writes data to i2c device
|
||||
func (r *Adaptor) I2cWrite(address int, data []byte) (err error) {
|
||||
if err = r.i2cDevice.SetAddress(address); err != nil {
|
||||
return
|
||||
}
|
||||
_, err = r.i2cDevice.Write(data)
|
||||
return
|
||||
}
|
||||
|
||||
// I2cRead returns value from i2c device using specified size
|
||||
func (r *Adaptor) I2cRead(address int, size int) (data []byte, err error) {
|
||||
if err = r.i2cDevice.SetAddress(address); err != nil {
|
||||
return
|
||||
}
|
||||
data = make([]byte, size)
|
||||
_, err = r.i2cDevice.Read(data)
|
||||
return
|
||||
func (c *Adaptor) I2cGetDefaultBus() int {
|
||||
return c.i2cDefaultBus
|
||||
}
|
||||
|
||||
// PwmWrite writes a PWM signal to the specified pin
|
||||
|
@ -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
|
||||
@ -67,7 +67,7 @@ Serial : 000000003bc748ea
|
||||
}
|
||||
a := NewAdaptor()
|
||||
gobottest.Assert(t, a.Name(), "RaspberryPi")
|
||||
gobottest.Assert(t, a.i2cLocation, "/dev/i2c-1")
|
||||
gobottest.Assert(t, a.i2cDefaultBus, 1)
|
||||
gobottest.Assert(t, a.revision, "3")
|
||||
|
||||
readFile = func() ([]byte, error) {
|
||||
@ -78,7 +78,7 @@ Serial : 000000003bc748ea
|
||||
`), nil
|
||||
}
|
||||
a = NewAdaptor()
|
||||
gobottest.Assert(t, a.i2cLocation, "/dev/i2c-1")
|
||||
gobottest.Assert(t, a.i2cDefaultBus, 1)
|
||||
gobottest.Assert(t, a.revision, "2")
|
||||
|
||||
readFile = func() ([]byte, error) {
|
||||
@ -89,7 +89,7 @@ Serial : 000000003bc748ea
|
||||
`), nil
|
||||
}
|
||||
a = NewAdaptor()
|
||||
gobottest.Assert(t, a.i2cLocation, "/dev/i2c-0")
|
||||
gobottest.Assert(t, a.i2cDefaultBus, 0)
|
||||
gobottest.Assert(t, a.revision, "1")
|
||||
|
||||
}
|
||||
@ -110,7 +110,7 @@ func TestAdaptorFinalize(t *testing.T) {
|
||||
a.DigitalWrite("3", 1)
|
||||
a.PwmWrite("7", 255)
|
||||
|
||||
a.I2cStart(0xff)
|
||||
a.I2cGetConnection(0xff, 0)
|
||||
gobottest.Assert(t, a.Finalize(), nil)
|
||||
}
|
||||
|
||||
@ -161,10 +161,12 @@ func TestAdaptorI2c(t *testing.T) {
|
||||
})
|
||||
sysfs.SetFilesystem(fs)
|
||||
sysfs.SetSyscall(&sysfs.MockSyscall{})
|
||||
a.I2cStart(0xff)
|
||||
a.i2cDevice = &NullReadWriteCloser{}
|
||||
|
||||
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})
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user