piripherals.bus module

Wrapper classes to abstract bus access.

class piripherals.bus.Bus(bus=1)[source]

Bases: object

Abstraction for a data bus, i.e. I2C.

Parameters:bus – something with read and write methods. It does not have such methods, bus is passed to SMBus(bus).

The bus need to have at least the following methods

  • read_byte_data(addr,reg) -> byte
  • write_byte_data(addr,reg,byte)

and additionally

  • read_word_data(addr,reg) -> word
  • write_word_data(addr,reg,work)
  • read_i2c_block_data(addr,reg,size) -> [byte,...]
  • write_i2c_block_data(addr,reg,[byte,...])

If these are not present, it will read/write words and blocks using read_byte_data and write_byte_data.

The bus usually is an smbus.SMBus or smbus2.SMBus instance.

device(addr)[source]

Get a Device.

Parameters:addr (int) – device address
Returns:device at given address
Return type:Device
read_block(addr, reg, n)[source]

read a block of bytes.

Parameters:
  • addr (int) – address of device to read the block from
  • reg (int) – base register, first byte of the block
  • n (int) – # of bytes to read n<=32
Returns:

bytes, that were read

Return type:

list of int

read_byte(addr, reg)[source]

read a byte.

Parameters:
  • addr (int) – address of device to read the byte from
  • reg (int) – register to be read
Returns:

the byte, that was read

Return type:

int

read_word(addr, reg)[source]

read a word (2 bytes).

Parameters:
  • addr (int) – address of device to read the word from
  • reg (int) – base register, low byte of the word is there, high byte is at reg+1
Returns:

the word, that was read

Return type:

int

write_block(addr, reg, block)[source]

write a block of bytes.

Parameters:
  • addr (int) – address of device to write the block to
  • reg (int) – base register, first byte of the block
  • block (list of int) – bytes to be written, len(block)<=32
write_byte(addr, reg, byte)[source]

write a byte.

Parameters:
  • addr (int) – address of device to write the byte to
  • reg (int) – register to write to
  • byte (int) – byte to be written
write_word(addr, reg, word)[source]

write a word (2 bytes).

Parameters:
  • addr (int) – address of device to write the word to
  • reg (int) – base register, low byte of the word is there, high byte is at reg+1
  • word (int) – word to be written
class piripherals.bus.Device(bus, addr)[source]

Bases: object

Abstraction of a device on a bus.

It has the same methods as Bus, but with the addr already set.

Parameters:
  • bus (Bus) – the bus the Device is attached to
  • addr (int) – address of the device