gobot.io/x/gobot/v2@v2.1.0/drivers/i2c/i2c_connection.go (about)

     1  package i2c
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"gobot.io/x/gobot/v2"
     7  )
     8  
     9  const (
    10  	// Error event
    11  	Error = "error"
    12  )
    13  
    14  const (
    15  	// BusNotInitialized is the initial value for a bus
    16  	BusNotInitialized = -1
    17  
    18  	// AddressNotInitialized is the initial value for an address
    19  	AddressNotInitialized = -1
    20  )
    21  
    22  var (
    23  	// ErrNotEnoughBytes is used when the count of read bytes was too small
    24  	ErrNotEnoughBytes = fmt.Errorf("Not enough bytes read")
    25  	// ErrNotReady is used when the device is not ready
    26  	ErrNotReady = fmt.Errorf("Device is not ready")
    27  )
    28  
    29  type bitState uint8
    30  
    31  const (
    32  	clear bitState = 0x00
    33  	set   bitState = 0x01
    34  )
    35  
    36  // Connection is a connection to an I2C device with a specified address
    37  // on a specific bus. Used as an alternative to the I2c interface.
    38  // Implements I2cOperations to talk to the device, wrapping the
    39  // calls in SetAddress to always target the specified device.
    40  // Provided by an Adaptor by implementing the I2cConnector interface.
    41  type Connection gobot.I2cOperations
    42  
    43  type i2cConnection struct {
    44  	bus     gobot.I2cSystemDevicer
    45  	address int
    46  }
    47  
    48  // NewConnection creates and returns a new connection to a specific i2c device on a bus and address.
    49  func NewConnection(bus gobot.I2cSystemDevicer, address int) (connection *i2cConnection) {
    50  	return &i2cConnection{bus: bus, address: address}
    51  }
    52  
    53  // Read data from an i2c device.
    54  func (c *i2cConnection) Read(data []byte) (read int, err error) {
    55  	return c.bus.Read(c.address, data)
    56  }
    57  
    58  // Write data to an i2c device.
    59  func (c *i2cConnection) Write(data []byte) (written int, err error) {
    60  	return c.bus.Write(c.address, data)
    61  }
    62  
    63  // Close connection to i2c device. The bus was created by adaptor and will be closed there.
    64  func (c *i2cConnection) Close() error {
    65  	return nil
    66  }
    67  
    68  // ReadByte reads a single byte from the i2c device.
    69  func (c *i2cConnection) ReadByte() (byte, error) {
    70  	return c.bus.ReadByte(c.address)
    71  }
    72  
    73  // ReadByteData reads a byte value for a register on the i2c device.
    74  func (c *i2cConnection) ReadByteData(reg uint8) (uint8, error) {
    75  	return c.bus.ReadByteData(c.address, reg)
    76  }
    77  
    78  // ReadWordData reads a word value for a register on the i2c device.
    79  func (c *i2cConnection) ReadWordData(reg uint8) (uint16, error) {
    80  	return c.bus.ReadWordData(c.address, reg)
    81  }
    82  
    83  // ReadBlockData reads a block of bytes from a register on the i2c device.
    84  func (c *i2cConnection) ReadBlockData(reg uint8, b []byte) error {
    85  	return c.bus.ReadBlockData(c.address, reg, b)
    86  }
    87  
    88  // WriteByte writes a single byte to the i2c device.
    89  func (c *i2cConnection) WriteByte(val byte) error {
    90  	return c.bus.WriteByte(c.address, val)
    91  }
    92  
    93  // WriteByteData writes a byte value to a register on the i2c device.
    94  func (c *i2cConnection) WriteByteData(reg uint8, val uint8) error {
    95  	return c.bus.WriteByteData(c.address, reg, val)
    96  }
    97  
    98  // WriteWordData writes a word value to a register on the i2c device.
    99  func (c *i2cConnection) WriteWordData(reg uint8, val uint16) (err error) {
   100  	return c.bus.WriteWordData(c.address, reg, val)
   101  }
   102  
   103  // WriteBlockData writes a block of bytes to a register on the i2c device.
   104  func (c *i2cConnection) WriteBlockData(reg uint8, b []byte) (err error) {
   105  	return c.bus.WriteBlockData(c.address, reg, b)
   106  }
   107  
   108  // WriteBytes writes a block of bytes to the current register on the i2c device.
   109  func (c *i2cConnection) WriteBytes(b []byte) (err error) {
   110  	return c.bus.WriteBytes(c.address, b)
   111  }
   112  
   113  // setBit is used to set a bit at a given position to 1.
   114  func setBit(n uint8, pos uint8) uint8 {
   115  	n |= (1 << pos)
   116  	return n
   117  }
   118  
   119  // clearBit is used to set a bit at a given position to 0.
   120  func clearBit(n uint8, pos uint8) uint8 {
   121  	mask := ^uint8(1 << pos)
   122  	n &= mask
   123  	return n
   124  }
   125  
   126  func twosComplement16Bit(uValue uint16) int16 {
   127  	result := int32(uValue)
   128  	if result&0x8000 != 0 {
   129  		result -= 1 << 16
   130  	}
   131  	return int16(result)
   132  }
   133  
   134  func swapBytes(value uint16) uint16 {
   135  	return (value << 8) | (value >> 8)
   136  }