Source code for sensirion_i2c_stcc4.device

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

from sensirion_driver_adapters.transfer import execute_transfer
from sensirion_driver_support_types.mixin_access import MixinAccess
from sensirion_i2c_stcc4.commands import (DisableTestingMode, EnableTestingMode, EnterSleepMode, ExitSleepMode,
                                          GetProductId, MeasureSingleShot, PerformConditioning, PerformFactoryReset,
                                          PerformForcedRecalibration, PerformSelfTest, ReadMeasurementRaw,
                                          SetPressureCompensationRaw, SetRhtCompensation, StartContinuousMeasurement,
                                          StopContinuousMeasurement)

from sensirion_i2c_stcc4.result_types import (SignalRelativeHumidity, SignalTemperature)


[docs]class Stcc4DeviceBase: """Low level API implementation of STCC4"""
[docs] def __init__(self, channel): self._channel = channel
@property def channel(self): return self._channel
[docs] def start_continuous_measurement(self): """ After sending the start_continuous_measurement command, the sensor will begin measuring the CO2 gas concentration continuously with a sampling interval of 1 second. The recommended communication sequence for continuous measurement is as follows: 1. The sensor is powered up into the idle state. 2. The I2C controller sends a start_continuous_measurement command. 3. The I2C controller periodically reads out data with the read_measurement command. 4. If desired, stop taking measurements using the stop_continuous_measurement command. """ transfer = StartContinuousMeasurement() return execute_transfer(self._channel, transfer)
[docs] def read_measurement_raw(self): """ After the command *start_continuous_measurement* has been sent, the chip continuously measures and updates the measurement results. New results (co2_concentration, temperature, and relative_humidity) can be read continuously with this command. :return co2_concentration_raw: CO2 concentration in ppm. No conversion needed. :return temperature_raw: Raw temperature signal. The signal s can be converted into a temperature in °C (t) by applying the formula t = -45 + 175 * s / 65535. :return relative_humidity_raw: Raw relative humidity signal. The signal s can be converted into relative humidity in % (rh) by applying the formula rh = -6 + 125 * s / 65535. :return sensor_status_raw: The sensor status. If the sensor is in testing mode, byte 10 is equal to 4. """ transfer = ReadMeasurementRaw() return execute_transfer(self._channel, transfer)
[docs] def stop_continuous_measurement(self): """ After receiving the stop_continuous_measurement command, the sensor will finish the currently running measurement before returning to idle mode. Therefore, a wait time of one measurement interval plus a 200 ms clock tolerance is required before a new command is acknowledged. """ transfer = StopContinuousMeasurement() return execute_transfer(self._channel, transfer)
[docs] def measure_single_shot(self): """ The measure_single_shot command conducts an on-demand measurement of CO2 gas concentration. The typical communication sequence is as follows: 1. The sensor is powered up with the exit_sleep_mode command if previously powered down using the enter_sleep_mode command. 2. The I2C controller sends a measure_single_shot command and waits for the execution time. 3. The I2C controller reads out data with the read_measurement command. 4. Repeat steps 2-3 as required by the application. 5. If desired, set the sensor to sleep with the enter_sleep_mode command. """ transfer = MeasureSingleShot() return execute_transfer(self._channel, transfer)
[docs] def perform_forced_recalibration(self, target_CO2_concentration): """ The forced recalibration (FRC) can be applied to correct the sensor's output by means of an externally obtained CO 2 reference value. The recommended communication sequence for a successful FRC in continuous mode is as follows: 1. Operate the STCC4 for at least 3 minutes. Ensure the sensor reading and environmental conditions, including CO2 concentration, are stable. 2. The I2C controller sends the stop_continuous_measurement command (Section 5.3.2) 3. Wait for the specified execution time of stop_continuous_measurement command. 4. Issue the perform_forced_recalibration command with the target CO2 concentration and read out the applied FRC correction. The recommended communication sequence for a successful FRC in single shot mode is as follows: 1. Operate the STCC4 for at least 3 minutes. For sampling intervals significantly larger than 10 seconds, increase the operation time accordingly. Ensure the sensor reading and environmental conditions, including CO2 concentration, are stable. 2. Issue the perform_forced_recalibration command with the reference CO2 concentration and read out the applied FRC correction. The sensor must remain in idle mode after operation before command execution. :param target_co2_concentration: Target CO₂ concentration in ppm. :return frc_correction: Returns the FRC correction value if FRC has been successful. 0xFFFF on failure. """ transfer = PerformForcedRecalibration(target_CO2_concentration) return execute_transfer(self._channel, transfer)[0]
[docs] def get_product_id(self): """ The get_product_ID command retrieves the product identifier and serial number. The command can be used to check communication with the sensor. :return product_id: 32-bit :return serial_number: 64-bit unique serial number of the sensor. """ transfer = GetProductId() return execute_transfer(self._channel, transfer)
[docs] def set_rht_compensation(self, raw_temperature, raw_humidity): """ When the SHT4x is not directly connected to the STCC4 for humidity and temperature compensation, the set_rht_compensation command allows external input of relative humidity (RH) and temperature (T) values. The temperature value must be provided from the RHT sensor providing the RH value for correct absolute humidity calculation. The written RHT values are applied for compensation after a maximum of one measurement interval. The default or last written values are used for RHT compensation until overwritten. Power cycling resets the sensor to the default values of 25°C and 50 %RH. :param raw_temperature: raw temperature ticks as provided by SHT4x sensor :param raw_humidity: raw humidity ticks as provided by SHT4x sensor :Example: .. code-block:: python sensor.set_rht_compensation(26214, 29359) """ transfer = SetRhtCompensation(raw_temperature, raw_humidity) return execute_transfer(self._channel, transfer)
[docs] def set_pressure_compensation_raw(self, pressure): """ External pressure values can be set through the set_pressure_compensation command. The written pressure value is applied for pressure compensation after a maximum of one measurement interval. Power cycling resets the sensor to the default value of 101'300 Pa. The default or the last written value is used in pressure compensation until overwritten. :param pressure: The pressure value as Pa divided by 2. E.g. for 101300 Pa you have to send 50650 :Example: .. code-block:: python sensor.set_pressure_compensation_raw(50650) """ transfer = SetPressureCompensationRaw(pressure) return execute_transfer(self._channel, transfer)
[docs] def perform_self_test(self): """ The perform_self_test command can be used as an end-of-line test to check the sensor functionality. Word[0] = 0 → pass Word[0] ≠ 0 → malfunction detected :return test_result: Test result of the self-test. If the result is ≠ 0, the self-test failed. """ transfer = PerformSelfTest() return execute_transfer(self._channel, transfer)[0]
[docs] def perform_conditioning(self): """ The perform_conditioning command is recommended to improve sensor performance when the sensor has not completed measurements for more than 3 hours. It is recommended to allow a settling time of 2 seconds after the execution time is complete. The recommended communication sequence is as follows: 1. The sensor is woken up with the exit_sleep_mode command if previously put to sleep using the enter_sleep_mode command. 2. The I2C controller sends a perform_conditioning command. 3. Wait for the specified execution time of perform_conditioning command plus a 2 second settling time. 4. The I2C controller sends a measurement command (e.g. start_continuous_measurement command or measure_single_shot command. """ transfer = PerformConditioning() return execute_transfer(self._channel, transfer)
[docs] def enter_sleep_mode(self): """ The enter_sleep_mode command sets the sensor to sleep mode through the I2C interface. The written relative humidity, temperature, and pressure compensation values as well as the ASC state will be retained while in sleep mode. """ transfer = EnterSleepMode() return execute_transfer(self._channel, transfer)
[docs] def exit_sleep_mode(self): """ The exit_sleep_mode command sets the sensor to idle mode through the I2C interface. The sensor's idle state can be verified by reading out the product ID. """ transfer = ExitSleepMode() return execute_transfer(self._channel, transfer)
[docs] def enable_testing_mode(self): """ The enable_testing_mode command is used to test the sensor with the ASC algorithm disabled. The sensor state can be verified by reading out the sensor status word in the read_measurement command. """ transfer = EnableTestingMode() return execute_transfer(self._channel, transfer)
[docs] def disable_testing_mode(self): """ The disable_testing_mode command is used to exit the testing mode. The sensor state can be verified by reading out the sensor status word in the read_measurement command. """ transfer = DisableTestingMode() return execute_transfer(self._channel, transfer)
[docs] def perform_factory_reset(self): """ The perform_factory_reset command can be used to reset the FRC and ASC algorithm history. :return factory_reset_result: The result of the factory reset. If the result is ≠ 0, the factory reset failed. """ transfer = PerformFactoryReset() return execute_transfer(self._channel, transfer)[0]
[docs]class Stcc4Device(Stcc4DeviceBase): """Driver class implementation of STCC4""" #: Access to base class stcc4 = MixinAccess()
[docs] def __init__(self, channel): super().__init__(channel)
[docs] def read_measurement(self): """ reads measurement data :return co2_concentration: :return temperature: :return relative_humidity: :return sensor_status: """ (raw_co2_concentration, raw_temperature, raw_relative_humidity, sensor_status_raw ) = self.stcc4.read_measurement_raw() return (raw_co2_concentration, SignalTemperature(raw_temperature), SignalRelativeHumidity(raw_relative_humidity), sensor_status_raw)
[docs] def set_pressure_compensation(self, pressure): """ External pressure values can be set through the set_pressure_compensation command. The written pressure value is applied for pressure compensation after a maximum of one measurement interval. Power cycling resets the sensor to the default value of 101'300 Pa. :param pressure: """ self.stcc4.set_pressure_compensation_raw(int(pressure / 2))