fossasia / pslab-python

Python Library for PSLab Desktop: https://pslab.io
GNU General Public License v3.0
1.62k stars 227 forks source link

Add static read and write methods for `pslab.bus.i2c.I2CSlave` #179

Closed nkpro2000sr closed 3 years ago

nkpro2000sr commented 3 years ago

I would like to add two static methods in pslab.bus.i2c.I2CSlave for the implementation of pslab.bus.busio.

  1. I2CSlave.read_from(device: SerialHandler, address: int, bytes_to_read: int) -> bytearray
  2. I2CSlave.write_to(device: SerialHandler, address: int, bytes_to_write: bytearray)

OR changing address as optional parameter in I2CSlave.__init__

  1. I2CSlave.read_from(address: int, bytes_to_read: int) -> bytearray
  2. I2CSlave.write_to(address: int, bytes_to_write: bytearray)

OR moving these methods to I2CMaster.

otherwise busio's read and write methods have to create I2CSlave object for each call.

bessman commented 3 years ago

Perhaps pslab.bus.busio.I2C could keep track of connected slaves with a dictionary? Every time a read or write method is called, check if the address is already in the dictionary. If it is, use the associated slave instance. If not, create a new one and add it to the dictionary.

If that doesn't work, option 1 is preferable, i.e. adding static methods to I2CSlave.

nkpro2000sr commented 3 years ago

OK, I will do with dictionary.