def __init__( self, name: str, i2c_lock: threading.Lock, bus: int, address: int, mux: Optional[int] = None, channel: Optional[int] = None, simulate: bool = False, mux_simulator: Optional[MuxSimulator] = None, Simulator: Optional[PeripheralSimulator] = None, ) -> None: """ Initializes atlas driver. """ # Initialize parameters self.simulate = simulate # Initialize logger self.logger = Logger(name="Driver({})".format(name), dunder_name=__name__) # Initialize I2C try: self.i2c = I2C( name=name, i2c_lock=i2c_lock, bus=bus, address=address, mux=mux, channel=channel, mux_simulator=mux_simulator, PeripheralSimulator=Simulator, ) except I2CError as e: raise InitError(logger=self.logger)
def test_init(): i2c = I2C( name="Test", i2c_lock=threading.RLock(), bus=2, address=0x40, mux=0x77, channel=4, mux_simulator=MuxSimulator(), PeripheralSimulator=PeripheralSimulator, )
def test_write_register(): i2c = I2C( name="Test", i2c_lock=threading.RLock(), bus=2, address=0x40, mux=0x77, channel=4, mux_simulator=MuxSimulator(), PeripheralSimulator=PeripheralSimulator, ) i2c.write_register(0x01, 0x02)
def test_read_empty_register(): i2c = I2C( name="Test", i2c_lock=threading.RLock(), bus=2, address=0x40, mux=0x77, channel=4, mux_simulator=MuxSimulator(), PeripheralSimulator=PeripheralSimulator, ) with pytest.raises(ReadError): byte = i2c.read_register(0x01)
def test_write_unknown(): i2c = I2C( name="Test", i2c_lock=threading.RLock(), bus=2, address=0x40, mux=0x77, channel=4, mux_simulator=MuxSimulator(), PeripheralSimulator=PeripheralSimulator, ) with pytest.raises(WriteError): i2c.write([0x01])
def test_read_empty(): i2c = I2C( name="Test", i2c_lock=threading.RLock(), bus=2, address=0x40, mux=0x77, channel=4, mux_simulator=MuxSimulator(), PeripheralSimulator=PeripheralSimulator, ) bytes_ = i2c.read(2) assert bytes_[0] == 0x00 assert bytes_[1] == 0x00
def test_read_custom_register(): class CustomPeripheralSimulator(PeripheralSimulator): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.registers = {0xE7: 0x00} i2c = I2C( name="Test", i2c_lock=threading.RLock(), bus=2, address=0x40, mux=0x77, channel=4, mux_simulator=MuxSimulator(), PeripheralSimulator=CustomPeripheralSimulator, ) assert i2c.read_register(0xE7) == 0x00
def test_write_read(): class CustomPeripheralSimulator(PeripheralSimulator): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.writes = {byte_str(bytes([0x01])): bytes([0x02])} i2c = I2C( name="Test", i2c_lock=threading.RLock(), bus=2, address=0x40, mux=0x77, channel=4, mux_simulator=MuxSimulator(), PeripheralSimulator=CustomPeripheralSimulator, ) i2c.write(bytes([0x01])) bytes_ = i2c.read(1) assert bytes_[0] == 0x02
def __init__( self, name: str, i2c_lock: threading.Lock, bus: int, address: int, mux: Optional[int] = None, channel: Optional[int] = None, simulate: bool = False, mux_simulator: Optional[MuxSimulator] = None, ) -> None: # Initialize simulation mode self.simulate = simulate # Initialize logger self.logger = Logger(name="Driver({})".format(name), dunder_name=__name__) self.logger.info("Initializing driver") # Check if simulating if simulate: self.logger.info("Simulating driver") Simulator = CCS811Simulator else: Simulator = None # Initialize I2C try: self.i2c = I2C( name=name, i2c_lock=i2c_lock, bus=bus, address=address, mux=mux, channel=channel, mux_simulator=mux_simulator, PeripheralSimulator=Simulator, ) except I2CError as e: raise InitError(logger=self.logger) from e
def __init__( self, name: str, i2c_lock: threading.Lock, bus: int, address: int, mux: Optional[int] = None, channel: Optional[int] = None, simulate: Optional[bool] = False, mux_simulator: Optional[MuxSimulator] = None, ) -> None: """Initializes driver.""" # Initialize logger self.logger = Logger(name="Driver({})".format(name), dunder_name=__name__) # Check if simulating if simulate: self.logger.info("Simulating driver") Simulator = SHT25Simulator else: Simulator = None # Initialize I2C try: self.i2c = I2C( name=name, i2c_lock=i2c_lock, bus=bus, address=address, mux=mux, channel=channel, mux_simulator=mux_simulator, PeripheralSimulator=Simulator, verify_device=False, # need to write before device responds to read ) self.read_user_register(retry=True) except I2CError as e: raise InitError(logger=self.logger) from e
# Get current working directory cwd = os.getcwd() print("Running from: {}".format(cwd)) # Set correct import path if cwd.endswith("i2c2"): print("Running locally") sys.path.append("../../../") elif cwd.endswith("openag-device-software"): print("Running globally") else: print("Running from invalid location") sys.exit(0) # Import i2c comms from device.communication.i2c.main import I2C from device.communication.i2c.exceptions import InitError # Enable logging output logging.basicConfig(level=logging.DEBUG) # def scan(address_range=None, mux_range=None, channel_range=None): # """Scan for devices at specified address, mux, and channel ranges.""" # ... if __name__ == "__main__": try: i2c = I2C("Test", 2, 0x40, 0x77, 2, None, None) except InitError: print("Unable to initialize I2C")