roboglia.i2c.I2CBus¶
-
class
I2CBus
(name='I2CBUS', robot=None, port='', auto=True, mock=False, err=0.1)[source]¶ Bases:
roboglia.base.bus.BaseBus
Implements a communication bus for I2C devices.
I2CBus
has the same paramters asBaseBus
. Please refer to this class for the details of the parameters.In addition there is an extra parameter mock.
At this moment the
I2CBus
supports devices with byte and word registers and permits defining composed regsiters withsize
> 1 that are treated as a single register.Note
A gyroscope sensor might have registers for the z, y and z axes reading that are stored as pairs of registers like this:
gyro_x_l #0x28 gyro_x_h #0x29 gyro_y_l #0x2A gyro_y_h #0x2B gyro_z_l #0x2C gyro_z_h #0x2D
For simplicity it is possible to define these registers like this in the device template:
registers: gyro_x: address: 0x28 size: 2 gyro_y: address: 0x2A size: 2 gyro_z: address: 0x2C size: 2
By default the registers are
Byte
and the order of data is low-high as described in the :py:class:roboglia.base.`BaseRegister`. The bus will handle this by reading the two registers sequentially and computing the register’s value using the size of the register and the order.- Parameters
name (str) – The name of the bus
robot (BaseRobot) – A reference to the robot using the bus
port (any) – An identification for the physical bus access. Some busses have string description like
/dev/ttySC0
while others could be just integers (like in the case of I2C or SPI buses)auto (Bool) – If
True
the bus will be opened when the robot is started by callingBaseRobot.start()
. IfFalse
the bus will be left closed during robot initialization and needs to be opened by the programmer.mock (bool) – Indicates if the I2C bus will use mock communication. It is provided for testing of functionality in CI environment. If
True
the bus will use theMockSMBus
class for performing read and write operations.err (float) – For mock buses this parameter controls the probability of generating a random mock communication error.
-
__init__
(name='I2CBUS', robot=None, port='', auto=True, mock=False, err=0.1)[source]¶ Initialize self. See help(type(self)) for accurate signature.
-
close
()[source]¶ Closes the communication port, if the
super().close()
allows it. If the bus is used in any sync loops, the close request might fail.
-
property
is_open
¶ Returns True or False if the bus is open.
-
read
(reg)[source]¶ Depending on the size of the register is calls the corresponding function from the
SMBus
.
-
write
(reg, value)[source]¶ Depending on the size of the register it calls the corresponding write function from
SMBus
.
-
read_block
(device, start_address, length)[source]¶ Reads a block of registers of given length.
- Parameters
device (I2CDevice or subclass) – The device on the I2C bus
start_addr (int) – The start address to read from
length (int) – Number of bytes to read from the device
- Returns
A list of bytes of length
length
with the values from the device. It intercepts any exceptions and logs them, in that case the return will beNone
.- Return type
list of int
-
write_block
(device, start_address, data)[source]¶ Writes a block of registers of given length.
- Parameters
device (I2CDevice or subclass) – The device on the I2C bus
start_addr (int) – The start address to read from
data (list of int) – The bytes to write to the device
- Returns
It intercepts any exceptions and logs them.
- Return type
None
-
__repr__
()¶ Returns a representation of a BaseBus that includes the name of the class, the port and the status (open or closed).
-
property
auto_open
¶ Indicates if the bus should be opened by the robot when initializing.
-
property
name
¶ (read-only) the bus name.
-
property
port
¶ (read-only) the bus port.
-
property
robot
¶ The robot that owns the bus.