fusion_hat._i2c module

I2C bus read/write functions

A simple wrap for smbus2 library.

Example

import I2C

>>> from fusion_hat.i2c import I2C

Init I2C bus with address 0x17

>>> i2c = I2C(address=0x17)

Write a byte 0x00

>>> i2c.write_byte(0x00)

Write a byte 0x01 to register 0x01

>>> i2c.write_byte_data(0x01, 0x01)

Write a word 0x0001 to register 0x01

>>> i2c.write_word_data(0x01, 0x0001)

Write a word 0x0001 to register 0x01 in LSB first

>>> i2c.write_word_data(0x01, 0x0001, lsb=True)

Write a block of bytes to register 0x01

>>> i2c.write_block_data(0x01, [0x00, 0x01, 0x02])

Read a byte

>>> print(i2c.read_byte())
0

Read a byte from register 0x01

>>> print(i2c.read_byte_data(0x01))
1

Read a word from register 0x01

>>> print(i2c.read_word_data(0x01))
256

Read a word from register 0x01 in LSB first

>>> print(i2c.read_word_data(0x01, lsb=True))
1

Read a block of bytes from register 0x01

>>> print(i2c.read_block_data(0x01))
[0, 1, 2]
class fusion_hat._i2c.I2C(*args, address: int = None, bus: int = 1, **kwargs)[source]

Bases: _Base

I2C bus read/write functions

Parameters:
RETRY = 5
DEFAULT_BUS = 1
write_byte(**kwargs)
write_byte_data(**kwargs)
write_word_data(**kwargs)
write_i2c_block_data(**kwargs)
read_byte(**kwargs)
read_byte_data(**kwargs)
read_word_data(**kwargs)
read_i2c_block_data(**kwargs)
is_ready(**kwargs)
static scan(bus: int = 1, force: bool = False) list[source]

Scan the I2C bus for devices

Parameters:
  • bus (int, optional) – I2C bus number, default is 1

  • force (bool, optional) – True if force to access the I2C bus, False otherwise, default is False

Returns:

List of I2C addresses of devices found

Return type:

list

write(data: int | list | bytearray) None[source]

Write data to the I2C device

Parameters:

data (int | list | bytearray) – Data to write

Raises:

ValueError – if write is not an int, list or bytearray

read(length: int = 1) list[source]

Read data from I2C device

Parameters:

length (int) – Number of bytes to receive

Returns:

Received data

Return type:

list

mem_write(data: int | list | bytearray, memaddr: int) None[source]

Write data to specific register address

Parameters:
  • data (int | list | bytearray) – Data to send

  • memaddr (int) – Register address

Raises:

ValueError – If data is not int, list, or bytearray

mem_read(length: int, memaddr: int) list[source]

Read data from specific register address

Parameters:
  • length (int) – Number of bytes to receive

  • memaddr (int) – Register address

Returns:

Received bytearray data or False if error

Return type:

list

is_avaliable() bool[source]

Check if the I2C device is avaliable

Returns:

True if the I2C device is avaliable, False otherwise

Return type:

bool

_write_byte(data: int) None[source]

[DEPRECATED] Write a byte to I2C register

Parameters:

data (int) – Data to write

_write_byte_data(reg: int, data: int) None[source]

[DEPRECATED] Write a byte data to I2C register

Parameters:
  • reg (int) – Register address

  • data (int) – Data to write

_write_word_data(reg: int, data: int) None[source]

[DEPRECATED] Write word data to I2C register

Parameters:
  • reg (int) – Register address

  • data (int) – Data to write

_write_i2c_block_data(reg: int, data: list) None[source]

[DEPRECATED] Write block data to I2C register

Parameters:
  • reg (int) – Register address

  • data (list) – Data to write

_read_byte() int[source]

[DEPRECATED] Read a byte from the I2C bus

Returns:

byte read from the I2C bus

Return type:

int

_read_byte_data(reg: int) int[source]

[DEPRECATED] Read a byte from the I2C bus

Parameters:

reg (int) – register address

Returns:

byte read from the I2C bus

Return type:

int

_read_word_data(reg: int) list[source]

[DEPRECATED] Read a word from the I2C bus

Parameters:

reg (int) – register address

Returns:

word read from the I2C bus

Return type:

list

_read_i2c_block_data(reg: int, length: int) list[source]

[DEPRECATED] Read block data from I2C register

Parameters:
  • reg (int) – Register address

  • length (int) – Number of bytes to receive

Returns:

Received bytearray data or False if error

Return type:

list