# I2C

## Code Example

    from periphery import I2C

    # Open i2c-0 controller
    i2c = I2C("/dev/i2c-0")

    # Read byte at address 0x100 of EEPROM at 0x50
    msgs = [I2C.Message([0x01, 0x00]), I2C.Message([0x00], read=True)]
    i2c.transfer(0x50, msgs)
    print("0x100: 0x{:02x}".format(msgs[1].data[0]))

    i2c.close()

## API

class `periphery.``I2C`(devpath)

  Bases: `object`

  Instantiate an I2C object and open the i2c-dev device at the specified path.

  Parameters: | devpath (str) – i2c-dev device path.
  Returns: | I2C object.
  Return type: | I2C
  Raises: | `I2CError` – if an I/O or OS error occurs.

  `transfer`(address, messages)

    Transfer messages to the specified I2C address. Modifies the messages array with the results of any read transactions.

    Parameters:

    - address (int) – I2C address.
    - messages (list) – list of I2C.Message messages.

    Raises:

    - `I2CError` – if an I/O or OS error occurs.
    - `TypeError` – if messages type is not list.
    - `ValueError` – if messages length is zero, or if message data is not valid bytes.

  `close`()

    Close the i2c-dev I2C device.

    Raises: | `I2CError` – if an I/O or OS error occurs.

  `fd`

    Get the file descriptor of the underlying i2c-dev device.

    Type: | int

  `devpath`

    Get the device path of the underlying i2c-dev device.

    Type: | str

  class `Message`(data, read=False, flags=0)

    Bases: `object`

    Instantiate an I2C Message object.

    Parameters:

    - data (bytes, bytearray, list) – a byte array or list of 8-bit integers to write.
    - read (bool) – specify this as a read message, where data serves as placeholder bytes for the read.
    - flags (int) – additional i2c-dev flags for this message.

    Returns: | Message object.
    Return type: | Message
    Raises: | `TypeError` – if data, read, or flags types are invalid.

class `periphery.``I2CError`

  Bases: `OSError`

  Base class for I2C errors.