Source code for sensirion_i2c_sht3x.device

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# (c) Copyright 2023 Sensirion AG, Switzerland
#
#     THIS FILE IS AUTOMATICALLY GENERATED!
#
# Generator:     sensirion-driver-generator 0.33.0
# Product:       sht3x
# Model-Version: 1.0.0
#
"""
The class Sht3xDeviceBase implements the low level interface of the sensor.
The class Sht3xDevice extends the Sht3xDeviceBase. It provides additional functions to ease the use of the
sensor.
"""

import time
from sensirion_driver_adapters.transfer import execute_transfer
from sensirion_driver_support_types.mixin_access import MixinAccess
from sensirion_i2c_sht3x.commands import (ClearStatusRegister, DisableHeater, EnableHeater,
                                          MeasureSingleShotHighRepeatability,
                                          MeasureSingleShotHighRepeatabilityClockStretching,
                                          MeasureSingleShotLowRepeatability,
                                          MeasureSingleShotLowRepeatabilityClockStretching,
                                          MeasureSingleShotMediumRepeatability,
                                          MeasureSingleShotMediumRepeatabilityClockStretching, Mps, ReadMeasurement,
                                          ReadStatusRegister, Repeatability, SoftReset, StartArtMeasurement,
                                          StartMeasurement05MpsHighRepeatability,
                                          StartMeasurement05MpsLowRepeatability,
                                          StartMeasurement05MpsMediumRepeatability,
                                          StartMeasurement10MpsHighRepeatability,
                                          StartMeasurement10MpsLowRepeatability,
                                          StartMeasurement10MpsMediumRepeatability,
                                          StartMeasurement1MpsHighRepeatability, StartMeasurement1MpsLowRepeatability,
                                          StartMeasurement1MpsMediumRepeatability,
                                          StartMeasurement2MpsHighRepeatability, StartMeasurement2MpsLowRepeatability,
                                          StartMeasurement2MpsMediumRepeatability,
                                          StartMeasurement4MpsHighRepeatability, StartMeasurement4MpsLowRepeatability,
                                          StartMeasurement4MpsMediumRepeatability, StatusRegisterT, StopMeasurement)

from sensirion_i2c_sht3x.result_types import (SignalHumidity, SignalTemperature)


[docs]class Sht3xDeviceBase: """Low level API implementation of SHT3X"""
[docs] def __init__(self, channel): self._channel = channel
@property def channel(self): return self._channel
[docs] def measure_single_shot_high_repeatability(self): """ Single shot measurement with high repeatability :return temperature_ticks: Temperature ticks. Convert to degrees celsius by -45 + 175 * value / 65535 :return humidity_ticks: Humidity ticks. Convert to relative humidity by 100 * value / 65535 """ transfer = MeasureSingleShotHighRepeatability() return execute_transfer(self._channel, transfer)
[docs] def measure_single_shot_high_repeatability_clock_stretching(self): """ Single shot measurement with high repeatability and clock stretching enabled :return temperature_ticks: Temperature ticks. Convert to degrees celsius by -45 + 175 * value / 65535 :return humidity_ticks: Humidity ticks. Convert to relative humidity by 100 * value / 65535 """ transfer = MeasureSingleShotHighRepeatabilityClockStretching() return execute_transfer(self._channel, transfer)
[docs] def measure_single_shot_medium_repeatability(self): """ Single shot measurement with medium repeatability :return temperature_ticks: Temperature ticks. Convert to degrees celsius by -45 + 175 * value / 65535 :return humidity_ticks: Humidity ticks. Convert to relative humidity by 100 * value / 65535 """ transfer = MeasureSingleShotMediumRepeatability() return execute_transfer(self._channel, transfer)
[docs] def measure_single_shot_medium_repeatability_clock_stretching(self): """ Single shot measurement with medium repeatability and clock stretching enabled :return temperature_ticks: Temperature ticks. Convert to degrees celsius by -45 + 175 * value / 65535 :return humidity_ticks: Humidity ticks. Convert to relative humidity by 100 * value / 65535 """ transfer = MeasureSingleShotMediumRepeatabilityClockStretching() return execute_transfer(self._channel, transfer)
[docs] def measure_single_shot_low_repeatability(self): """ Single shot measurement with low repeatability :return temperature_ticks: Temperature ticks. Convert to degrees celsius by -45 + 175 * value / 65535 :return humidity_ticks: Humidity ticks. Convert to relative humidity by 100 * value / 65535 """ transfer = MeasureSingleShotLowRepeatability() return execute_transfer(self._channel, transfer)
[docs] def measure_single_shot_low_repeatability_clock_stretching(self): """ Single shot measurement with low repeatability and clock stretching enabled :return temperature_ticks: Temperature ticks. Convert to degrees celsius by -45 + 175 * value / 65535 :return humidity_ticks: Humidity ticks. Convert to relative humidity by 100 * value / 65535 """ transfer = MeasureSingleShotLowRepeatabilityClockStretching() return execute_transfer(self._channel, transfer)
[docs] def start_measurement_0_5_mps_high_repeatability(self): """Start periodic measurement mode with 0.5 mps and high repeatability.""" transfer = StartMeasurement05MpsHighRepeatability() return execute_transfer(self._channel, transfer)
[docs] def start_measurement_0_5_mps_medium_repeatability(self): """Start periodic measurement mode with 0.5 mps and medium repeatability.""" transfer = StartMeasurement05MpsMediumRepeatability() return execute_transfer(self._channel, transfer)
[docs] def start_measurement_0_5_mps_low_repeatability(self): """Start periodic measurement mode with 0.5 mps and low repeatability.""" transfer = StartMeasurement05MpsLowRepeatability() return execute_transfer(self._channel, transfer)
[docs] def start_measurement_1_mps_high_repeatability(self): """Start periodic measurement mode with 1 mps and high repeatability.""" transfer = StartMeasurement1MpsHighRepeatability() return execute_transfer(self._channel, transfer)
[docs] def start_measurement_1_mps_medium_repeatability(self): """Start periodic measurement mode with 1 mps and medium repeatability.""" transfer = StartMeasurement1MpsMediumRepeatability() return execute_transfer(self._channel, transfer)
[docs] def start_measurement_1_mps_low_repeatability(self): """Start periodic measurement mode with 1 mps and low repeatability.""" transfer = StartMeasurement1MpsLowRepeatability() return execute_transfer(self._channel, transfer)
[docs] def start_measurement_2_mps_high_repeatability(self): """Start periodic measurement mode with 2 mps and high repeatability.""" transfer = StartMeasurement2MpsHighRepeatability() return execute_transfer(self._channel, transfer)
[docs] def start_measurement_2_mps_medium_repeatability(self): """Start periodic measurement mode with 2 mps and medium repeatability.""" transfer = StartMeasurement2MpsMediumRepeatability() return execute_transfer(self._channel, transfer)
[docs] def start_measurement_2_mps_low_repeatability(self): """Start periodic measurement mode with 2 mps and low repeatability.""" transfer = StartMeasurement2MpsLowRepeatability() return execute_transfer(self._channel, transfer)
[docs] def start_measurement_4_mps_high_repeatability(self): """Start periodic measurement mode with 4 mps and high repeatability.""" transfer = StartMeasurement4MpsHighRepeatability() return execute_transfer(self._channel, transfer)
[docs] def start_measurement_4_mps_medium_repeatability(self): """Start periodic measurement mode with 4 mps and medium repeatability.""" transfer = StartMeasurement4MpsMediumRepeatability() return execute_transfer(self._channel, transfer)
[docs] def start_measurement_4_mps_low_repeatability(self): """Start periodic measurement mode with 4 mps and low repeatability.""" transfer = StartMeasurement4MpsLowRepeatability() return execute_transfer(self._channel, transfer)
[docs] def start_measurement_10_mps_high_repeatability(self): """Start periodic measurement mode with 10 mps and high repeatability.""" transfer = StartMeasurement10MpsHighRepeatability() return execute_transfer(self._channel, transfer)
[docs] def start_measurement_10_mps_medium_repeatability(self): """Start periodic measurement mode with 10 mps and medium repeatability.""" transfer = StartMeasurement10MpsMediumRepeatability() return execute_transfer(self._channel, transfer)
[docs] def start_measurement_10_mps_low_repeatability(self): """Start periodic measurement mode with 10 mps and low repeatability.""" transfer = StartMeasurement10MpsLowRepeatability() return execute_transfer(self._channel, transfer)
[docs] def start_art_measurement(self): """ Start ART (accelerated response time) measurement .. note:: After issuing the ART command the sensor will start acquiring data with a frequency of 4Hz. """ transfer = StartArtMeasurement() return execute_transfer(self._channel, transfer)
[docs] def read_measurement(self): """ Read out data after a "start measurement" or "start art measurement" command has been issued. :return temperature_ticks: Temperature ticks. Convert to degrees celsius by -45 + 175 * value / 65535 :return humidity_ticks: Humidity ticks. Convert to relative humidity by 100 * value / 65535 .. note:: After the read out command fetch data has been issued, the data memory is cleared, i.e. no measurement data is present. """ transfer = ReadMeasurement() return execute_transfer(self._channel, transfer)
[docs] def stop_measurement(self): """ Stop the periodic measurement mode. .. note:: Upon reception of this command the sensor will abort the ongoing measurement and enter the single shot mode. """ transfer = StopMeasurement() return execute_transfer(self._channel, transfer)
[docs] def enable_heater(self): """ Enable the heater .. note:: The SHT3x is equipped with an internal heater, which is meant for plausibility checking only. The temperature increase achieved by the heater depends on various parameters and lies in the range of a few degrees centigrade. After a reset the heater is disabled (default condition). """ transfer = EnableHeater() return execute_transfer(self._channel, transfer)
[docs] def disable_heater(self): """Disable the heater""" transfer = DisableHeater() return execute_transfer(self._channel, transfer)
[docs] def read_status_register(self): """ Read out the status register :return status_register: The contents of the status register .. note:: The status register contains information on the operational status of the heater, the alert mode and on the execution status of the last command and the last write sequence. """ transfer = ReadStatusRegister() return execute_transfer(self._channel, transfer)[0]
[docs] def clear_status_register(self): """Clear (set to zero) all flags (Bit 4) in the status register.""" transfer = ClearStatusRegister() return execute_transfer(self._channel, transfer)
[docs] def soft_reset(self): """ Perform a soft reset. .. note:: A system reset of the SHT3x can be achieved in multiple ways: - Soft reset: use this command - I2C general call: all devices on the I2C bus are reset by sending the command 0x06 to the I2C address 0x00 - Reset pin: send a pulse to the dedicated nReset pin. The nReset pin has to be pulled low for a minimum of 1 µs to generate a reset of the sensor. - Hard reset: Power down (incl. pulling SDA, SCL and ADDR low) During the reset procedure the sensor will not process commands. """ transfer = SoftReset() return execute_transfer(self._channel, transfer)
[docs]class Sht3xDevice(Sht3xDeviceBase): """Driver class implementation of SHT3X""" #: Access to base class sht3x = MixinAccess()
[docs] def __init__(self, channel): super().__init__(channel)
[docs] def measure_single_shot(self, measurement_repeatability, is_clock_stretching): """ Single shot measurement with the specified properties :param measurement_repeatability: The repeatability of the periodic measurement :param is_clock_stretching: Toggle clock stretching :return a_temperature: Converted from ticks to degrees celsius by -45 + (175 * value / 65535) :return a_humidity: Converted from ticks to relative humidity by 100 * value / 65535 """ if is_clock_stretching: if measurement_repeatability == Repeatability.HIGH: (raw_temp, raw_humi ) = self.sht3x.measure_single_shot_high_repeatability_clock_stretching() elif measurement_repeatability == Repeatability.MEDIUM: (raw_temp, raw_humi ) = self.sht3x.measure_single_shot_medium_repeatability_clock_stretching() elif measurement_repeatability == Repeatability.LOW: (raw_temp, raw_humi ) = self.sht3x.measure_single_shot_low_repeatability_clock_stretching() elif measurement_repeatability == Repeatability.HIGH: (raw_temp, raw_humi ) = self.sht3x.measure_single_shot_high_repeatability() elif measurement_repeatability == Repeatability.MEDIUM: (raw_temp, raw_humi ) = self.sht3x.measure_single_shot_medium_repeatability() elif measurement_repeatability == Repeatability.LOW: (raw_temp, raw_humi ) = self.sht3x.measure_single_shot_low_repeatability() return (SignalTemperature(raw_temp), SignalHumidity(raw_humi))
[docs] def start_periodic_measurement(self, measurement_repeatability, messages_per_second): """ Start the periodic measurement measurement mode. This is a convenience method that selects the correct measurement command based on the provided arguments. :param measurement_repeatability: The repeatability of the periodic measurement :param messages_per_second: The messages per second of the periodic measurement """ if messages_per_second == Mps.EVERY_TWO_SECONDS: if measurement_repeatability == Repeatability.HIGH: self.sht3x.start_measurement_0_5_mps_high_repeatability() elif measurement_repeatability == Repeatability.MEDIUM: self.sht3x.start_measurement_0_5_mps_medium_repeatability() elif measurement_repeatability == Repeatability.LOW: self.sht3x.start_measurement_0_5_mps_low_repeatability() elif messages_per_second == Mps.ONE_PER_SECOND: if measurement_repeatability == Repeatability.HIGH: self.sht3x.start_measurement_1_mps_high_repeatability() elif measurement_repeatability == Repeatability.MEDIUM: self.sht3x.start_measurement_1_mps_medium_repeatability() elif measurement_repeatability == Repeatability.LOW: self.sht3x.start_measurement_1_mps_low_repeatability() elif messages_per_second == Mps.TWO_PER_SECOND: if measurement_repeatability == Repeatability.HIGH: self.sht3x.start_measurement_2_mps_high_repeatability() elif measurement_repeatability == Repeatability.MEDIUM: self.sht3x.start_measurement_2_mps_medium_repeatability() elif measurement_repeatability == Repeatability.LOW: self.sht3x.start_measurement_2_mps_low_repeatability() elif messages_per_second == Mps.FOUR_PER_SECOND: if measurement_repeatability == Repeatability.HIGH: self.sht3x.start_measurement_4_mps_high_repeatability() elif measurement_repeatability == Repeatability.MEDIUM: self.sht3x.start_measurement_4_mps_medium_repeatability() elif measurement_repeatability == Repeatability.LOW: self.sht3x.start_measurement_4_mps_low_repeatability() elif messages_per_second == Mps.TEN_PER_SECOND: if measurement_repeatability == Repeatability.HIGH: self.sht3x.start_measurement_10_mps_high_repeatability() elif measurement_repeatability == Repeatability.MEDIUM: self.sht3x.start_measurement_10_mps_medium_repeatability() elif measurement_repeatability == Repeatability.LOW: self.sht3x.start_measurement_10_mps_low_repeatability() return
[docs] def blocking_read_measurement(self): """ This is a convenience method that combines polling the data ready flag and reading out the data. As the minimal measurement interval is 2s and we sleep for 100ms we iterate at most 200 times. Note that this is blocking the system for a considerable amount of time! :return a_temperature: Converted from ticks to degrees celsius by -45 + (175 * value / 65535) :return a_humidity: Converted from ticks to relative humidity by 100 * value / 65535 """ status = self.sht3x.read_status_register() data_ready_flag = (status >> 6) & 15 while data_ready_flag == 0: time.sleep(0.1) status = self.sht3x.read_status_register() data_ready_flag = (status >> 6) & 15 (raw_temp, raw_humi ) = self.sht3x.read_measurement() return (SignalTemperature(raw_temp), SignalHumidity(raw_humi))
[docs] def read_status_register(self): """ Read the contents of the status register :return a_status_register: """ status = self.sht3x.read_status_register() return StatusRegisterT(status)