2014-04-27 18:54:41 -07:00
|
|
|
package i2c
|
2014-04-26 03:11:51 -07:00
|
|
|
|
2014-11-29 12:10:23 -08:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
2016-12-08 13:24:03 +01:00
|
|
|
"gobot.io/x/gobot"
|
2017-01-20 00:17:41 +01:00
|
|
|
"gobot.io/x/gobot/sysfs"
|
2014-11-29 12:10:23 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-07-10 20:39:31 +02:00
|
|
|
ErrEncryptedBytes = errors.New("Encrypted bytes")
|
|
|
|
ErrNotEnoughBytes = errors.New("Not enough bytes read")
|
|
|
|
ErrNotReady = errors.New("Device is not ready")
|
|
|
|
ErrInvalidPosition = errors.New("Invalid position value")
|
2014-11-29 12:10:23 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
Error = "error"
|
|
|
|
Joystick = "joystick"
|
|
|
|
C = "c"
|
|
|
|
Z = "z"
|
|
|
|
)
|
|
|
|
|
2015-07-03 18:57:29 -07:00
|
|
|
type I2cStarter interface {
|
2017-01-20 00:17:41 +01:00
|
|
|
// I2cStart initializes I2C communication to a device at a specified address
|
|
|
|
// on the default bus for further access using I2cRead() and I2cWrite().
|
2015-07-03 18:57:29 -07:00
|
|
|
I2cStart(address int) (err error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type I2cReader interface {
|
2017-01-20 00:17:41 +01:00
|
|
|
// I2cStart reads len bytes from a device at the specified address using
|
|
|
|
// block reads.
|
|
|
|
//
|
|
|
|
// Note: There is no way to specify command/register to read from using
|
|
|
|
// this interface, it always starts reading from register 0.
|
|
|
|
// To read register 42 you have to read 43 bytes (0 to 42)
|
|
|
|
// and throw away the first 42 bytes.
|
2015-07-03 18:57:29 -07:00
|
|
|
I2cRead(address int, len int) (data []byte, err error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type I2cWriter interface {
|
2017-01-20 00:17:41 +01:00
|
|
|
// I2cWrite writes a buffer of bytes to the device at the specified address
|
|
|
|
// using block writes.
|
|
|
|
// Note that the first byte in the buffer is interpreted as the target
|
|
|
|
// command/register.
|
2015-07-03 18:57:29 -07:00
|
|
|
I2cWrite(address int, buf []byte) (err error)
|
|
|
|
}
|
|
|
|
|
2014-11-29 12:10:23 -08:00
|
|
|
type I2c interface {
|
|
|
|
gobot.Adaptor
|
2015-07-03 18:57:29 -07:00
|
|
|
I2cStarter
|
|
|
|
I2cReader
|
|
|
|
I2cWriter
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
2017-01-20 00:17:41 +01:00
|
|
|
|
|
|
|
// I2cConnection is a connection to an I2C device with a specified address
|
|
|
|
// on a specific bus. Used as an alternative to the I2c interface.
|
|
|
|
// Implements sysfs.SMBusOperations 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 I2cConnection sysfs.SMBusOperations
|
|
|
|
|
|
|
|
type i2cConnection struct {
|
|
|
|
bus sysfs.I2cDevice
|
|
|
|
address int
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewI2cConnection(bus sysfs.I2cDevice, address int) (connection *i2cConnection) {
|
|
|
|
return &i2cConnection{bus: bus, address: address}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *i2cConnection) ReadByte() (val uint8, err error) {
|
|
|
|
if err := c.bus.SetAddress(c.address); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return c.bus.ReadByte()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *i2cConnection) ReadByteData(reg uint8) (val uint8, err error) {
|
|
|
|
if err := c.bus.SetAddress(c.address); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return c.bus.ReadByteData(reg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *i2cConnection) ReadWordData(reg uint8) (val uint16, err error) {
|
|
|
|
if err := c.bus.SetAddress(c.address); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return c.bus.ReadWordData(reg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *i2cConnection) ReadBlockData(b []byte) (n int, err error) {
|
|
|
|
if err := c.bus.SetAddress(c.address); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return c.bus.ReadBlockData(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *i2cConnection) WriteByte(val uint8) (err error) {
|
|
|
|
if err := c.bus.SetAddress(c.address); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.bus.WriteByte(val)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *i2cConnection) WriteByteData(reg uint8, val uint8) (err error) {
|
|
|
|
if err := c.bus.SetAddress(c.address); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.bus.WriteByteData(reg, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *i2cConnection) WriteBlockData(b []byte) (err error) {
|
|
|
|
if err := c.bus.SetAddress(c.address); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.bus.WriteBlockData(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// I2cConnector is a replacement to the I2c interface, providing
|
|
|
|
// access to more than one I2C bus per adaptor, and a more
|
|
|
|
// fine grained interface to the I2C bus.
|
|
|
|
type I2cConnector interface {
|
|
|
|
I2cGetConnection(address int, bus int) (device I2cConnection, err error)
|
|
|
|
}
|