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 (
|
2023-02-04 13:22:22 +01:00
|
|
|
"fmt"
|
2022-12-17 11:56:11 +01:00
|
|
|
|
2023-05-20 14:25:21 +02:00
|
|
|
"gobot.io/x/gobot/v2"
|
2014-11-29 12:10:23 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-02-10 11:54:45 +01:00
|
|
|
// Error event
|
2017-02-09 16:47:11 +01:00
|
|
|
Error = "error"
|
2014-11-29 12:10:23 -08:00
|
|
|
)
|
|
|
|
|
2017-02-09 09:41:12 +01:00
|
|
|
const (
|
2017-02-10 11:54:45 +01:00
|
|
|
// BusNotInitialized is the initial value for a bus
|
|
|
|
BusNotInitialized = -1
|
|
|
|
|
|
|
|
// AddressNotInitialized is the initial value for an address
|
2017-02-09 14:35:48 +01:00
|
|
|
AddressNotInitialized = -1
|
2017-02-09 09:41:12 +01:00
|
|
|
)
|
|
|
|
|
2017-02-10 11:54:45 +01:00
|
|
|
var (
|
2023-02-04 13:22:22 +01:00
|
|
|
// ErrNotEnoughBytes is used when the count of read bytes was too small
|
|
|
|
ErrNotEnoughBytes = fmt.Errorf("Not enough bytes read")
|
|
|
|
// ErrNotReady is used when the device is not ready
|
|
|
|
ErrNotReady = fmt.Errorf("Device is not ready")
|
2017-02-10 11:54:45 +01:00
|
|
|
)
|
|
|
|
|
2022-03-19 21:16:31 +01:00
|
|
|
type bitState uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
clear bitState = 0x00
|
2023-05-19 14:16:22 +02:00
|
|
|
set bitState = 0x01
|
2022-03-19 21:16:31 +01:00
|
|
|
)
|
|
|
|
|
2017-02-13 08:31:49 +01:00
|
|
|
// Connection is a connection to an I2C device with a specified address
|
|
|
|
// on a specific bus. Used as an alternative to the I2c interface.
|
2017-07-12 09:47:44 -06:00
|
|
|
// Implements I2cOperations to talk to the device, wrapping the
|
2017-02-13 08:31:49 +01:00
|
|
|
// calls in SetAddress to always target the specified device.
|
|
|
|
// Provided by an Adaptor by implementing the I2cConnector interface.
|
2022-12-17 11:56:11 +01:00
|
|
|
type Connection gobot.I2cOperations
|
2017-02-10 11:54:45 +01:00
|
|
|
|
2017-02-13 08:31:49 +01:00
|
|
|
type i2cConnection struct {
|
2023-01-05 19:04:32 +01:00
|
|
|
bus gobot.I2cSystemDevicer
|
2017-02-13 08:31:49 +01:00
|
|
|
address int
|
2017-02-10 11:54:45 +01:00
|
|
|
}
|
|
|
|
|
2023-02-04 13:22:22 +01:00
|
|
|
// NewConnection creates and returns a new connection to a specific i2c device on a bus and address.
|
2023-01-05 19:04:32 +01:00
|
|
|
func NewConnection(bus gobot.I2cSystemDevicer, address int) (connection *i2cConnection) {
|
|
|
|
return &i2cConnection{bus: bus, address: address}
|
2017-01-20 00:17:41 +01:00
|
|
|
}
|
|
|
|
|
2017-02-10 11:54:45 +01:00
|
|
|
// Read data from an i2c device.
|
2017-02-06 00:19:42 +01:00
|
|
|
func (c *i2cConnection) Read(data []byte) (read int, err error) {
|
2023-02-04 13:22:22 +01:00
|
|
|
return c.bus.Read(c.address, data)
|
2017-02-06 00:19:42 +01:00
|
|
|
}
|
|
|
|
|
2017-02-13 08:31:49 +01:00
|
|
|
// Write data to an i2c device.
|
2017-02-06 00:19:42 +01:00
|
|
|
func (c *i2cConnection) Write(data []byte) (written int, err error) {
|
2023-02-04 13:22:22 +01:00
|
|
|
return c.bus.Write(c.address, data)
|
2017-02-06 00:19:42 +01:00
|
|
|
}
|
|
|
|
|
2023-02-04 13:22:22 +01:00
|
|
|
// Close connection to i2c device. The bus was created by adaptor and will be closed there.
|
2017-02-06 00:19:42 +01:00
|
|
|
func (c *i2cConnection) Close() error {
|
2023-02-04 13:22:22 +01:00
|
|
|
return nil
|
2017-02-06 00:19:42 +01:00
|
|
|
}
|
|
|
|
|
2017-02-13 08:31:49 +01:00
|
|
|
// ReadByte reads a single byte from the i2c device.
|
2023-02-04 13:22:22 +01:00
|
|
|
func (c *i2cConnection) ReadByte() (byte, error) {
|
|
|
|
return c.bus.ReadByte(c.address)
|
2017-01-20 00:17:41 +01:00
|
|
|
}
|
|
|
|
|
2017-02-13 08:31:49 +01:00
|
|
|
// ReadByteData reads a byte value for a register on the i2c device.
|
2023-02-04 13:22:22 +01:00
|
|
|
func (c *i2cConnection) ReadByteData(reg uint8) (uint8, error) {
|
|
|
|
return c.bus.ReadByteData(c.address, reg)
|
2017-01-20 00:17:41 +01:00
|
|
|
}
|
|
|
|
|
2017-02-13 08:31:49 +01:00
|
|
|
// ReadWordData reads a word value for a register on the i2c device.
|
2023-02-04 13:22:22 +01:00
|
|
|
func (c *i2cConnection) ReadWordData(reg uint8) (uint16, error) {
|
|
|
|
return c.bus.ReadWordData(c.address, reg)
|
2017-01-20 00:17:41 +01:00
|
|
|
}
|
|
|
|
|
2022-09-06 19:55:05 +02:00
|
|
|
// ReadBlockData reads a block of bytes from a register on the i2c device.
|
2023-02-04 13:22:22 +01:00
|
|
|
func (c *i2cConnection) ReadBlockData(reg uint8, b []byte) error {
|
|
|
|
return c.bus.ReadBlockData(c.address, reg, b)
|
2022-09-06 19:55:05 +02:00
|
|
|
}
|
|
|
|
|
2017-02-13 08:31:49 +01:00
|
|
|
// WriteByte writes a single byte to the i2c device.
|
2023-02-04 13:22:22 +01:00
|
|
|
func (c *i2cConnection) WriteByte(val byte) error {
|
|
|
|
return c.bus.WriteByte(c.address, val)
|
2017-01-20 00:17:41 +01:00
|
|
|
}
|
|
|
|
|
2017-02-13 08:31:49 +01:00
|
|
|
// WriteByteData writes a byte value to a register on the i2c device.
|
2023-02-04 13:22:22 +01:00
|
|
|
func (c *i2cConnection) WriteByteData(reg uint8, val uint8) error {
|
|
|
|
return c.bus.WriteByteData(c.address, reg, val)
|
2017-01-20 00:17:41 +01:00
|
|
|
}
|
|
|
|
|
2017-02-13 08:31:49 +01:00
|
|
|
// WriteWordData writes a word value to a register on the i2c device.
|
2017-02-06 00:19:42 +01:00
|
|
|
func (c *i2cConnection) WriteWordData(reg uint8, val uint16) (err error) {
|
2023-02-04 13:22:22 +01:00
|
|
|
return c.bus.WriteWordData(c.address, reg, val)
|
2017-02-06 00:19:42 +01:00
|
|
|
}
|
|
|
|
|
2017-02-13 08:31:49 +01:00
|
|
|
// WriteBlockData writes a block of bytes to a register on the i2c device.
|
2017-02-06 00:19:42 +01:00
|
|
|
func (c *i2cConnection) WriteBlockData(reg uint8, b []byte) (err error) {
|
2023-02-04 13:22:22 +01:00
|
|
|
return c.bus.WriteBlockData(c.address, reg, b)
|
2017-01-20 00:17:41 +01:00
|
|
|
}
|
2022-03-19 21:16:31 +01:00
|
|
|
|
2022-12-17 11:56:11 +01:00
|
|
|
// WriteBytes writes a block of bytes to the current register on the i2c device.
|
|
|
|
func (c *i2cConnection) WriteBytes(b []byte) (err error) {
|
2023-02-04 13:22:22 +01:00
|
|
|
return c.bus.WriteBytes(c.address, b)
|
2022-12-17 11:56:11 +01:00
|
|
|
}
|
|
|
|
|
2022-03-19 21:16:31 +01:00
|
|
|
// setBit is used to set a bit at a given position to 1.
|
|
|
|
func setBit(n uint8, pos uint8) uint8 {
|
|
|
|
n |= (1 << pos)
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
// clearBit is used to set a bit at a given position to 0.
|
|
|
|
func clearBit(n uint8, pos uint8) uint8 {
|
|
|
|
mask := ^uint8(1 << pos)
|
|
|
|
n &= mask
|
|
|
|
return n
|
|
|
|
}
|
2022-08-31 19:40:31 +02:00
|
|
|
|
|
|
|
func twosComplement16Bit(uValue uint16) int16 {
|
|
|
|
result := int32(uValue)
|
|
|
|
if result&0x8000 != 0 {
|
|
|
|
result -= 1 << 16
|
|
|
|
}
|
|
|
|
return int16(result)
|
|
|
|
}
|
2022-08-27 19:45:01 +02:00
|
|
|
|
|
|
|
func swapBytes(value uint16) uint16 {
|
|
|
|
return (value << 8) | (value >> 8)
|
|
|
|
}
|