def test_empty(): cmd = SensirionI2cCommand(None, None, None, 0.1, 0.2, CrcCalculator(8, 0x31, 0xFF)) assert cmd.tx_data is None assert cmd.rx_length is None assert cmd.read_delay == 0.1 assert cmd.timeout == 0.2
def test_command_and_tx_data(): cmd = SensirionI2cCommand(0x1337, b"\xDE\xAD\xBE\xEF", 5, 0.1, 0.2, CrcCalculator(8, 0x31, 0xFF)) assert type(cmd.tx_data) is bytes assert cmd.tx_data == b"\x13\x37\xDE\xAD\x98\xBE\xEF\x92" assert cmd.rx_length is 5 assert cmd.read_delay == 0.1 assert cmd.timeout == 0.2
def test_only_command(): cmd = SensirionI2cCommand(0x1337, None, None, 0.1, 0.2, CrcCalculator(8, 0x31, 0xFF)) assert type(cmd.tx_data) is bytes assert cmd.tx_data == b"\x13\x37" assert cmd.rx_length is None assert cmd.read_delay == 0.1 assert cmd.timeout == 0.2
def test_only_tx_data_4_bytes(): cmd = SensirionI2cCommand(None, b"\xDE\xAD\xBE\xEF", None, 0.1, 0.2, CrcCalculator(8, 0x31, 0xFF)) assert type(cmd.tx_data) is bytes assert cmd.tx_data == b"\xDE\xAD\x98\xBE\xEF\x92" assert cmd.rx_length is None assert cmd.read_delay == 0.1 assert cmd.timeout == 0.2
def test_single_byte_command(): cmd = SensirionI2cCommand(0x42, b"\xDE\xAD\xBE\xEF", 5, 0.1, 0.2, CrcCalculator(8, 0x31, 0xFF), 1) assert type(cmd.tx_data) is bytes assert cmd.tx_data == b"\x42\xDE\xAD\x98\xBE\xEF\x92" assert cmd.rx_length is 5 assert cmd.read_delay == 0.1 assert cmd.timeout == 0.2
def __init__(self, command, tx_data, rx_length, read_delay, timeout, post_processing_time=0.0): """ Constructs a new shtc3 I²C command. :param int/None command: The command ID to be sent to the device. None means that no command will be sent, i.e. only ``tx_data`` (if not None) will be sent. No CRC is added to these bytes since the command ID usually already contains a CRC. :param bytes-like/list/None tx_data: Bytes to be extended with CRCs and then sent to the I²C device. None means that no write header will be sent at all (if ``command`` is None too). An empty list means to send the write header (even if ``command`` is None), but without data following it. :param int/None rx_length: Number of bytes to be read from the I²C device, including CRC bytes. None means that no read header is sent at all. Zero means to send the read header, but without reading any data. :param float read_delay: Delay (in Seconds) to be inserted between the end of the write operation and the beginning of the read operation. This is needed if the device needs some time to prepare the RX data, e.g. if it has to perform a measurement. Set to 0.0 to indicate that no delay is needed, i.e. the device does not need any processing time. :param float timeout: Timeout (in Seconds) to be used in case of clock stretching. If the device stretches the clock longer than this value, the transceive operation will be aborted with a timeout error. Set to 0.0 to indicate that the device will not stretch the clock for this command. :param float post_processing_time: Maximum time in seconds the device needs for post processing of this command until it is ready to receive the next command. For example after a device reset command, the device might need some time until it is ready again. Usually this is 0.0s, i.e. no post processing is needed. """ super(Shtc3I2cCmdBase, self).__init__( command=command, tx_data=tx_data, rx_length=rx_length, read_delay=read_delay, timeout=timeout, crc=CrcCalculator(8, 0x31, 0xFF, 0x00), command_bytes=2, post_processing_time=post_processing_time, )
def __init__(self, address, number_of_bytes_to_read): """ Constructs a new command. :param byte address: Address of OTP to read from. :param int number_of_bytes_to_read: Number of bytes to read. """ super(Sht2xI2cCmdReadOtp, self).__init__( tx_data=[0xFA, address], rx_length=number_of_bytes_to_read * 2, read_delay=0., timeout=0. ) self._crc = CrcCalculator(8, 0x31, 0x00)
def test_interpret_response_empty(): cmd = SensirionI2cCommand(None, None, 0, 0.1, 0.2, CrcCalculator(8, 0x31, 0xFF)) response = cmd.interpret_response(b"") assert response is None
def test_post_processing_time_float(): cmd = SensirionI2cCommand(0x1337, b"\xDE\xAD\xBE\xEF", 5, 0.1, 0.2, CrcCalculator(8, 0x31, 0xFF), 2, 2.3) assert type(cmd.post_processing_time) is float assert cmd.post_processing_time == 2.3
def test_interpret_response_crc_error(): cmd = SensirionI2cCommand(None, None, 6, 0.1, 0.2, CrcCalculator(8, 0x31, 0xFF)) with pytest.raises(I2cChecksumError): cmd.interpret_response(b"\xDE\xAD\x98\xBE\xEF\x93") # wrong crc
def test_interpret_response_6_bytes(): cmd = SensirionI2cCommand(None, None, 6, 0.1, 0.2, CrcCalculator(8, 0x31, 0xFF)) response = cmd.interpret_response(b"\xDE\xAD\x98\xBE\xEF\x92") assert type(response) is bytes assert response == b"\xDE\xAD\xBE\xEF"
def test_interpret_response_1_byte(): cmd = SensirionI2cCommand(None, None, 2, 0.1, 0.2, CrcCalculator(8, 0x31, 0xFF)) response = cmd.interpret_response(b"\xDE") assert type(response) is bytes assert response == b"\xDE"