roboglia.i2c.MockSMBus

class MockSMBus(robot, err=0.1)[source]

Bases: smbus2.smbus2.SMBus

Class for testing. Overides the SMBus methods in order to simulate the data exchange. Intended for use in the CI testing.

Parameters
  • robot (BaseRobot) – The robot (we need it to access the registers)

  • err (float) – A small number that will be used for generating random communication errors so that we can perform testing of the code handling those.

__init__(robot, err=0.1)[source]

Initialize and (optionally) open an i2c bus connection.

Parameters
  • bus (int or str) – i2c bus number (e.g. 0 or 1) or an absolute file path (e.g. /dev/i2c-42). If not given, a subsequent call to open() is required.

  • force (boolean) – force using the slave address even when driver is already using it.

open(port)[source]

mock opens the bus.

close()[source]

Mock closes the bus. It raises a OSError at the end so that the code can be checked for this behavior too.

read_byte_data(dev_id, address)[source]

Simulates the read of 1 Byte.

read_word_data(dev_id, address)[source]

Simulates the read of 1 Word.

write_byte_data(dev_id, address, value)[source]

Simulates the write of one byte.

write_word_data(dev_id, address, value)[source]

Simulates the write of one word.

read_i2c_block_data(dev_id, address, length, force=None)[source]

Simulates the read of one block of data.

__enter__()

Enter handler.

__exit__(exc_type, exc_val, exc_tb)

Exit handler.

block_process_call(i2c_addr, register, data, force=None)

Executes a SMBus Block Process Call, sending a variable-size data block and receiving another variable-size response

Parameters
  • i2c_addr (int) – i2c address

  • register (int) – Register to read/write to

  • data (list) – List of bytes

  • force (Boolean) –

Returns

List of bytes

Return type

list

i2c_rdwr(*i2c_msgs)

Combine a series of i2c read and write operations in a single transaction (with repeated start bits but no stop bits in between).

This method takes i2c_msg instances as input, which must be created first with i2c_msg.read() or i2c_msg.write().

Parameters

i2c_msgs (i2c_msg) – One or more i2c_msg class instances.

Return type

None

process_call(i2c_addr, register, value, force=None)

Executes a SMBus Process Call, sending a 16-bit value and receiving a 16-bit response

Parameters
  • i2c_addr (int) – i2c address

  • register (int) – Register to read/write to

  • value (int) – Word value to transmit

  • force (Boolean) –

Return type

int

read_block_data(i2c_addr, register, force=None)

Read a block of up to 32-bytes from a given register.

Parameters
  • i2c_addr (int) – i2c address

  • register (int) – Start register

  • force (Boolean) –

Returns

List of bytes

Return type

list

read_byte(i2c_addr, force=None)

Read a single byte from a device.

Return type

int

Parameters
  • i2c_addr (int) – i2c address

  • force (Boolean) –

Returns

Read byte value

write_block_data(i2c_addr, register, data, force=None)

Write a block of byte data to a given register.

Parameters
  • i2c_addr (int) – i2c address

  • register (int) – Start register

  • data (list) – List of bytes

  • force (Boolean) –

Return type

None

write_byte(i2c_addr, value, force=None)

Write a single byte to a device.

Parameters
  • i2c_addr (int) – i2c address

  • value (int) – value to write

  • force (Boolean) –

write_i2c_block_data(dev_id, address, data)[source]

Simulates the write of one block of data.

write_quick(i2c_addr, force=None)

Perform quick transaction. Throws IOError if unsuccessful. :param i2c_addr: i2c address :type i2c_addr: int :param force: :type force: Boolean