Source code for sensirion_i2c_sdp.sdp.commands

# -*- coding: utf-8 -*-
# (c) Copyright 2021 Sensirion AG, Switzerland

##############################################################################
##############################################################################
#                 _____         _    _ _______ _____ ____  _   _
#                / ____|   /\  | |  | |__   __|_   _/ __ \| \ | |
#               | |       /  \ | |  | |  | |    | || |  | |  \| |
#               | |      / /\ \| |  | |  | |    | || |  | | . ` |
#               | |____ / ____ \ |__| |  | |   _| || |__| | |\  |
#                \_____/_/    \_\____/   |_|  |_____\____/|_| \_|
#
#     THIS FILE IS AUTOMATICALLY GENERATED AND MUST NOT BE EDITED MANUALLY!
#
# Generator:    sensirion-i2c-interface-generator 0.3.0
# Product:      SDP
# Version:      0.1.0
#
##############################################################################
##############################################################################

# flake8: noqa

from __future__ import absolute_import, division, print_function

import logging
from struct import unpack

from sensirion_i2c_driver import SensirionI2cCommand, CrcCalculator

from sensirion_i2c_sdp.sdp.response_types import SdpDifferentialPressure, SdpTemperature

log = logging.getLogger(__name__)


[docs]class SdpI2cCmdBase(SensirionI2cCommand): """ SDP I²C base command. """
[docs] def __init__(self, command, tx_data, rx_length, read_delay, timeout, post_processing_time=0.0): """ Constructs a new SDP 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(SdpI2cCmdBase, 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, )
[docs]class SdpI2cCmdStartContinuousMeasurementWithMassFlowTCompAndAveraging(SdpI2cCmdBase): """ Start Continuous Measurement With Mass Flow T Comp And Averaging I²C Command This command starts continuous measurements with mass flow temperature compensation and the average till read feature. .. note:: The measurement command must only be sent once, if acknowledged. The command must not be resent or other commands must not be sent until the stop measurement command has been issued. After the start measurement command is sent, the first measurement result is available after 8ms. Small accuracy deviations (few % of reading) can occur during the next 12ms. The measured values are updated every 0.5ms and can be read using the read measurement interface. """
[docs] def __init__(self): """ Constructor. """ super(SdpI2cCmdStartContinuousMeasurementWithMassFlowTCompAndAveraging, self).__init__( command=0x3603, tx_data=None, rx_length=None, read_delay=0.0, timeout=0, post_processing_time=0.01, )
[docs]class SdpI2cCmdStartContinuousMeasurementWithMassFlowTComp(SdpI2cCmdBase): """ Start Continuous Measurement With Mass Flow T Comp I²C Command This command starts continuous measurements with mass flow temperature compensation. .. note:: The measurement command must only be sent once, if acknowledged. The command must not be resent or other commands must not be sent until the stop measurement command has been issued. After the start measurement command is sent, the first measurement result is available after 8ms. Small accuracy deviations (few % of reading) can occur during the next 12ms. The measured values are updated every 0.5ms and can be read using the read measurement interface. """
[docs] def __init__(self): """ Constructor. """ super(SdpI2cCmdStartContinuousMeasurementWithMassFlowTComp, self).__init__( command=0x3608, tx_data=None, rx_length=None, read_delay=0.0, timeout=0, post_processing_time=0.01, )
[docs]class SdpI2cCmdStartContinuousMeasurementWithDiffPressureTCompAndAveraging(SdpI2cCmdBase): """ Start Continuous Measurement With Diff Pressure T Comp And Averaging I²C Command This command starts continuous measurements with differential pressure temperature compensation and the average till read feature. .. note:: The measurement command must only be sent once, if acknowledged. The command must not be resent or other commands must not be sent until the stop measurement command has been issued. After the start measurement command is sent, the first measurement result is available after 8ms. Small accuracy deviations (few % of reading) can occur during the next 12ms. The measured values are updated every 0.5ms and can be read using the read measurement interface. """
[docs] def __init__(self): """ Constructor. """ super(SdpI2cCmdStartContinuousMeasurementWithDiffPressureTCompAndAveraging, self).__init__( command=0x3615, tx_data=None, rx_length=None, read_delay=0.0, timeout=0, post_processing_time=0.01, )
[docs]class SdpI2cCmdStartContinuousMeasurementWithDiffPressureTComp(SdpI2cCmdBase): """ Start Continuous Measurement With Diff Pressure T Comp I²C Command This command starts continuous measurements with differential pressure temperature compensation. .. note:: The measurement command must only be sent once, if acknowledged. The command must not be resent or other commands must not be sent until the stop measurement command has been issued. After the start measurement command is sent, the first measurement result is available after 8ms. Small accuracy deviations (few % of reading) can occur during the next 12ms. The measured values are updated every 0.5ms and can be read using the read measurement interface. """
[docs] def __init__(self): """ Constructor. """ super(SdpI2cCmdStartContinuousMeasurementWithDiffPressureTComp, self).__init__( command=0x361E, tx_data=None, rx_length=None, read_delay=0.0, timeout=0, post_processing_time=0.01, )
[docs]class SdpI2cCmdStopContinuousMeasurement(SdpI2cCmdBase): """ Stop Continuous Measurement I²C Command This command stops the continuous measurement and puts the sensor in idle mode. It powers off the heater and makes the sensor receptive to another command after 500us. The Stop command is also required when switching between different continuous measurement commands. """
[docs] def __init__(self): """ Constructor. """ super(SdpI2cCmdStopContinuousMeasurement, self).__init__( command=0x3FF9, tx_data=None, rx_length=None, read_delay=0.0, timeout=0, post_processing_time=0.0005, )
[docs]class SdpI2cCmdTriggerMeasurementWithMassFlowTCompAndAveraging(SdpI2cCmdBase): """ Trigger Measurement With Mass Flow T Comp And Averaging I²C Command This command triggers a single shot measurement with mass flow temperature compensation. .. note:: During a triggered measurement the sensor measures both differential pressure and temperature. The measurement starts directly after the command has been sent. The command needs to be repeated with every measurement. During the 45ms that the sensor is measuring, no command can be sent to the sensor. After the 45ms the result can be read out and any command can be sent to the sensor. """
[docs] def __init__(self): """ Constructor. """ super(SdpI2cCmdTriggerMeasurementWithMassFlowTCompAndAveraging, self).__init__( command=0x3624, tx_data=None, rx_length=None, read_delay=0.0, timeout=0, post_processing_time=0.045, )
[docs]class SdpI2cCmdTriggerMeasurementWithDiffPressureTComp(SdpI2cCmdBase): """ Trigger Measurement With Diff Pressure T Comp I²C Command This command triggers a single shot measurement with differential pressure temperature compensation. .. note:: During a triggered measurement the sensor measures both differential pressure and temperature. The measurement starts directly after the command has been sent. The command needs to be repeated with every measurement. During the 45ms that the sensor is measuring, no command can be sent to the sensor. After the 45ms the result can be read out and any command can be sent to the sensor. """
[docs] def __init__(self): """ Constructor. """ super(SdpI2cCmdTriggerMeasurementWithDiffPressureTComp, self).__init__( command=0x362F, tx_data=None, rx_length=None, read_delay=0.0, timeout=0, post_processing_time=0.045, )
[docs]class SdpI2cCmdReadMeasurement(SdpI2cCmdBase): """ Read Measurement I²C Command After a start continuous measurement commands, the measurement results can be read out at most every 0.5ms. After a triggered measurement command, the results can be read out when the sensor is finished with the measurement. The temperature and scale factor don’t need to be read out (every time). The read sequence can be aborted by a NACK and a STOP condition. The scale factor is for differential pressure in Pascal. """
[docs] def __init__(self): """ Constructor. """ super(SdpI2cCmdReadMeasurement, self).__init__( command=None, tx_data=None, rx_length=9, read_delay=0.001, timeout=0, post_processing_time=0.0, )
[docs] def interpret_response(self, data): """ Validates the CRCs of the received data from the device and returns the interpreted data. :param bytes data: Received raw bytes from the read operation. :return: - differential_pressure (:py:class:sensirion_i2c_sdp.sdp.response_types.SdpDifferentialPressure) - The digital calibrated differential pressure signal read from the sensor is a signed integer number (two's complement number). The integer value is converted to the physical value by dividing it by the scale factor, which is returned with every measurement. - temperature (:py:class:sensirion_i2c_sdp.sdp.reasponse_types.SdpTemperature) - The digital calibrated temperature signal read from the sensor is a signed integer number (two's complement number). The integer value is converted to the physical value by dividing it by a scale factor of 200. :rtype: tuple :raise ~sensirion_i2c_driver.errors.I2cChecksumError: If a received CRC was wrong. """ # check and remove CRCs checked_data = SdpI2cCmdBase.interpret_response(self, data) # convert raw received data into proper data types differential_pressure_ticks = int(unpack(">h", checked_data[0:2])[0]) # int16 temperature_ticks = int(unpack(">h", checked_data[2:4])[0]) # int16 scaling_factor = int(unpack(">h", checked_data[4:6])[0]) # int16 return SdpDifferentialPressure(differential_pressure_ticks, scaling_factor), SdpTemperature(temperature_ticks)
[docs]class SdpI2cCmdEnterSleepMode(SdpI2cCmdBase): """ Enter Sleep Mode I²C Command In sleep mode the sensor uses the minimum amount of current. The mode can only be entered from idle mode, i.e. when the sensor is not measuring. This mode is particularly useful for battery operated devices. To minimize the current in this mode, the complexity of the sleep mode circuit has been reduced as much as possible, which is mainly reflected by the way the sensor exits the sleep mode. In sleep mode the sensor cannot be soft reset. .. note:: Triggered mode: the sleep command can be sent after the result have been read out and the sensor is in idle mode. Continuous mode: the sleep command can be sent after a stop continuous measurement command has been issued and the sensor is in idle mode. """
[docs] def __init__(self): """ Constructor. """ super(SdpI2cCmdEnterSleepMode, self).__init__( command=0x3677, tx_data=None, rx_length=None, read_delay=0.0, timeout=0, post_processing_time=0.0, )
[docs]class SdpI2cCmdExitSleepMode(SdpI2cCmdBase): """ Exit Sleep Mode I²C Command Exit sleep mode. See the data sheet for more detailed information """
[docs] def __init__(self): """ Constructor. """ super(SdpI2cCmdExitSleepMode, self).__init__( command=0x0002, tx_data=None, rx_length=None, read_delay=0.0, timeout=0, post_processing_time=0.002, )
[docs]class SdpI2cCmdPrepareProductIdentifier(SdpI2cCmdBase): """ Prepare Product Identifier I²C Command Prepare for reading the product identifier and sensor serial number. """
[docs] def __init__(self): """ Constructor. """ super(SdpI2cCmdPrepareProductIdentifier, self).__init__( command=0x367C, tx_data=None, rx_length=None, read_delay=0.0, timeout=0, post_processing_time=0.0, )
[docs]class SdpI2cCmdReadProductIdentifier(SdpI2cCmdBase): """ Read Product Identifier I²C Command Read the product identifier and serial number of the sensor. .. note:: Make sure to call 'prepare product identifier' immediately before. """
[docs] def __init__(self): """ Constructor. """ super(SdpI2cCmdReadProductIdentifier, self).__init__( command=0xE102, tx_data=None, rx_length=18, read_delay=0.001, timeout=0, post_processing_time=0.0, )
[docs] def interpret_response(self, data): """ Validates the CRCs of the received data from the device and returns the interpreted data. :param bytes data: Received raw bytes from the read operation. :return: - product_number (int) - 32 bit unique product and revision number. The number is listed in the datasheet. Note that the last 8 bits are the revision number and can be subject to change. - serial_number (unsigned long long) 64-bit unique serial number :rtype: tuple :raise ~sensirion_i2c_driver.errors.I2cChecksumError: If a received CRC was wrong. """ # check and remove CRCs checked_data = SdpI2cCmdBase.interpret_response(self, data) # convert raw received data into proper data types product_number = int(unpack(">I", checked_data[0:4])[0]) # uint32 serial_number = int(unpack(">Q", checked_data[4:12])[0]) # uint64 return product_number, serial_number