Closed mglowinski93 closed 2 years ago
SMBus
class accessing the same physical bus. See herewith
clause, you are responsible for closing the bus yourself. See the difference between Example 1a and 1b in the readme.I've crated below class to manage access to I2C Bus:
class SMBusWrapper(metaclass=Singleton):
def __init__(self):
self._smbus = SMBus(1)
atexit.register(self.close_smbus)
@property
def smbus(self):
return self._smbus
@smbus.setter
def smbus(self, value):
self._smbus = value
def close_smbus(self):
self._smbus.close()
And getting access to I2CBus:
def get_smbus() -> SMBus:
"""
Returns
-------
SMBus
Return a new SMBus object that is connected to the specified I2C device interface.
"""
return SMBusWrapper().smbus
Hi, I have a question about creating several instances of an
SMBus
object. I have a system that uses several sensors that communicate over I2C.I noticed that the library for each of them creates its own instance of
SMBus
in__init__
.Example:
Can I have two
SMBus
objects? They will not impact each other?Additionally, I noticed that often the connection in such libraries is not closed (method
close
in not invoked). How might this affect the application?Wouldn't it be a better idea to pass the
SMBus
instance to the__init__
and manage it outside of a class?Thanks for answers :) !