Пример #1
0
def test_v1_multi_channel_and_always_multi_is_multi_channel():
    transceiver = MagicMock()
    transceiver.API_VERSION = 1
    transceiver.channel_count = 3  # multi channel
    connection = I2cConnection(transceiver)
    connection.always_multi_channel_response = True
    assert connection.is_multi_channel is True
Пример #2
0
def test_v1_single_channel_error():
    transceiver = MagicMock()
    transceiver.API_VERSION = 1
    transceiver.channel_count = None
    transceiver.transceive.return_value = (3, Exception("timeout"), b"")
    connection = I2cConnection(transceiver)
    with pytest.raises(I2cTimeoutError):
        connection.execute(0x42, I2cCommand(b"\x55", 3, 0.1, 0.2))
Пример #3
0
 def __init__(self, logger: logging.Logger) -> None:
     threading.Thread.__init__(self)
     con = I2cConnection(LinuxI2cTransceiver('/dev/i2c-1'))
     self.sht_85 = Sht3xI2cDevice(con)
     self.running = True
     self.humidity = None
     self.temperature = None
     self.logger = logger
Пример #4
0
def test_v1_single_channel_execute_wait_post_process_false():
    transceiver = MagicMock()
    transceiver.API_VERSION = 1
    transceiver.channel_count = None
    transceiver.transceive.return_value = (0, None, b"\x11\x22\x33")
    connection = I2cConnection(transceiver)
    response = connection.execute(0x42,
                                  I2cCommand(b"\x55", 3, 0.1, 0.2, 0.1),
                                  wait_post_process=False)
    assert response == b"\x11\x22\x33"
Пример #5
0
def test_single_channel_with_connection():
    transceiver = MagicMock()
    transceiver.API_VERSION = 1
    transceiver.channel_count = None
    transceiver.transceive.return_value = (0, None, b"\x11\x22")
    connection = I2cConnection(transceiver)
    device = I2cDevice(connection, 0x42)
    assert device.connection == connection
    assert device.slave_address == 0x42
    assert device.execute(I2cCommand(b"\x55", 3, 0.1, 0.2)) == b"\x11\x22"
Пример #6
0
def stc3x(bridge):
    # Configure SensorBridge port 1 for STC3x
    bridge.set_i2c_frequency(SensorBridgePort.ONE, frequency=400e3)
    bridge.set_supply_voltage(SensorBridgePort.ONE, voltage=3.3)
    bridge.switch_supply_on(SensorBridgePort.ONE)

    # Create STC3x device
    i2c_transceiver = SensorBridgeI2cProxy(bridge, port=SensorBridgePort.ONE)
    stc3x = Stc3xI2cDevice(I2cConnection(i2c_transceiver))

    yield stc3x

    # make sure the channel is powered off after executing tests
    bridge.switch_supply_off(SensorBridgePort.ONE)
Пример #7
0
def device(bridge):
    # Configure SensorBridge port 1 for SVM40
    bridge.set_i2c_frequency(SensorBridgePort.ONE, frequency=100e3)
    bridge.set_supply_voltage(SensorBridgePort.ONE, voltage=3.3)
    bridge.switch_supply_on(SensorBridgePort.ONE)

    # Create SVM40 device
    i2c_transceiver = SensorBridgeI2cProxy(bridge, port=SensorBridgePort.ONE)
    device = Svm40I2cDevice(I2cConnection(i2c_transceiver))
    device.device_reset()

    yield device

    # make sure the channel is powered off after executing tests
    bridge.switch_supply_off(SensorBridgePort.ONE)
Пример #8
0
def test_v1_single_channel_execute():
    transceiver = MagicMock()
    transceiver.API_VERSION = 1
    transceiver.channel_count = None
    transceiver.transceive.return_value = (0, None, b"\x11\x22\x33")
    connection = I2cConnection(transceiver)
    response = connection.execute(0x42, I2cCommand(b"\x55", 3, 0.1, 0.2))
    args = [kwargs for args, kwargs in transceiver.transceive.call_args_list]
    assert args == [
        {
            "slave_address": 0x42,
            "tx_data": b"\x55",
            "rx_length": 3,
            "read_delay": 0.1,
            "timeout": 0.2,
        },
    ]
    assert response == b"\x11\x22\x33"
Пример #9
0
def test_v1_multi_channel_execute():
    transceiver = MagicMock()
    transceiver.API_VERSION = 1
    transceiver.channel_count = 2
    transceiver.transceive.return_value = [
        (0, None, b"\x11\x22\x33"),
        (2, Exception("NACK"), b""),
    ]
    connection = I2cConnection(transceiver)
    response = connection.execute(0x42, I2cCommand(b"\x55", 3, 0.1, 0.2))
    args = [kwargs for args, kwargs in transceiver.transceive.call_args_list]
    assert args == [
        {
            "slave_address": 0x42,
            "tx_data": b"\x55",
            "rx_length": 3,
            "read_delay": 0.1,
            "timeout": 0.2,
        },
    ]
    assert len(response) == 2
    assert response[0] == b"\x11\x22\x33"
    assert type(response[1]) is I2cNackError
Пример #10
0
def test_is_multi_channel_with_unsupported_api_version():
    transceiver = MagicMock()
    transceiver.API_VERSION = 99  # unsupported
    connection = I2cConnection(transceiver)
    with pytest.raises(Exception, match=r".*not supported.*"):
        connection.is_multi_channel
Пример #11
0
def test_v1_multi_channel_is_multi_channel():
    transceiver = MagicMock()
    transceiver.API_VERSION = 1
    transceiver.channel_count = 1  # multi channel
    connection = I2cConnection(transceiver)
    assert connection.is_multi_channel is True
Пример #12
0
def test_v1_single_channel_is_multi_channel():
    transceiver = MagicMock()
    transceiver.API_VERSION = 1
    transceiver.channel_count = None  # single channel
    connection = I2cConnection(transceiver)
    assert connection.is_multi_channel is False
Пример #13
0
def test_always_multi_channel_response_get_set():
    transceiver = MagicMock()
    connection = I2cConnection(transceiver)
    connection.always_multi_channel_response = True
    assert connection.always_multi_channel_response is True
Пример #14
0
def test_always_multi_channel_response_default_false():
    transceiver = MagicMock()
    connection = I2cConnection(transceiver)
    assert connection.always_multi_channel_response is False