#!/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: svm4x
# Model-Version: 2.0.3
#
"""
The class Svm4xDeviceBase implements the low level interface of the sensor.
The class Svm4xDevice extends the Svm4xDeviceBase. 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_uart_svm4x.commands import (DeviceReset, GetNoxTuningParameters, GetProductName,
GetProductType, GetSerialNumber, GetSystemUpTime,
GetTemperatureOffsetForRhtMeasurements, GetVersion, GetVocState,
GetVocTuningParameters, ReadMeasuredRawValues, ReadMeasuredValuesAsIntegers,
SetNoxTuningParameters, SetTemperatureOffsetForRhtMeasurements,
SetVocState, SetVocTuningParameters, StartMeasurement, StopMeasurement,
StoreNvData)
from sensirion_uart_svm4x.result_types import (SignalHumidity, SignalNoxIndex, SignalTemperature, SignalVocIndex)
[docs]class Svm4xDeviceBase:
"""Low level API implementation of SVM4X"""
[docs] def __init__(self, channel):
self._channel = channel
@property
def channel(self):
return self._channel
[docs] def get_product_type(self):
"""
Gets the product type from the device.
:return product_type:
Character array containing the product type.
"""
transfer = GetProductType()
return execute_transfer(self._channel, transfer)[0]
[docs] def get_product_name(self):
"""
Gets the product name from the device.
:return product_name:
Character array containing the product name.
"""
transfer = GetProductName()
return execute_transfer(self._channel, transfer)[0]
[docs] def get_serial_number(self):
"""
Gets the serial number from the device.
:return serial_number:
Character array containing the serial number.
"""
transfer = GetSerialNumber()
return execute_transfer(self._channel, transfer)[0]
[docs] def get_version(self):
"""
Gets the version information for the hardware, firmware and SHDLC protocol.
:return firmware_major:
Firmware major version number.
:return firmware_minor:
Firmware minor version number.
:return firmware_debug:
Firmware debug state. If the debug state is set, the firmware is in development.
:return hardware_major:
Hardware major version number.
:return hardware_minor:
Hardware minor version number.
:return protocol_major:
Protocol major version number.
:return protocol_minor:
Protocol minor version number.
"""
transfer = GetVersion()
return execute_transfer(self._channel, transfer)
[docs] def device_reset(self):
"""
Executs a reset on the device.
.. note::
The device will reply before executing the reset. If the command is send with
broadcast, the reset is done directly after the reception of the command.
"""
transfer = DeviceReset()
return execute_transfer(self._channel, transfer)
[docs] def get_system_up_time(self):
"""
Get the system up time of the device.
:return system_up_time:
The time since the last power-on or device reset in seconds.
"""
transfer = GetSystemUpTime()
return execute_transfer(self._channel, transfer)[0]
[docs] def start_measurement(self):
"""
Starts measurement in polling mode.
.. note::
This command is only available in idle mode.
"""
transfer = StartMeasurement()
return execute_transfer(self._channel, transfer)
[docs] def stop_measurement(self):
"""
Leaves the measurement mode and returns to the idle mode.
.. note::
This command is only available in measurement mode.
"""
transfer = StopMeasurement()
return execute_transfer(self._channel, transfer)
[docs] def read_measured_values_as_integers(self):
"""
Read measurement data as integers.
This command is named get_signals in the datasheet.
:return humidity:
Compensated ambient humidity in % RH with a scaling factor of 100.
:return temperature:
Compensated ambient temperature in degrees celsius with a scaling of 200.
:return voc_index:
VOC index with a scaling value of 10.
:return nox_index:
NOx index with a scaling value of 10.
.. note::
This command is only available in measurement mode. The firmware updates the
measurement values every second. Polling data with a faster sampling rate will
return the same values. The first measurement is available 1 second after the
start measurement command is issued. Any readout prior to this will return zero
initialized values.
"""
transfer = ReadMeasuredValuesAsIntegers()
return execute_transfer(self._channel, transfer)
[docs] def read_measured_raw_values(self):
"""
Read measuremed raw values.
This command is named get_signals_raw in the datasheet.
:return raw_humidity:
Uncompensated raw humidity in % RH as read from the SHT40 with a scaling factor
of 100.
:return raw_temperature:
Uncompensated raw temperature in degrees celsius as read from the SHT40 with a
scaling of 200.
:return raw_voc_ticks:
Raw VOC output ticks as read from the SGP sensor.
:return raw_nox_ticks:
Raw NOx output ticks as read from the SGP sensor.
.. note::
This command is only available in measurement mode. The firmware updates the
measurement values every second. Polling data with a faster sampling rate will
return the same values. The first measurement is available 1 second after the
start measurement command is issued. Any readout prior to this will return zero
initialized values.
"""
transfer = ReadMeasuredRawValues()
return execute_transfer(self._channel, transfer)
[docs] def get_temperature_offset_for_rht_measurements(self):
"""
Gets the T-Offset for the temperature compensation of the RHT algorithm.
This command is named get_temperature_offset in the datasheet.
:return t_offset:
Temperature offset which is used for the RHT measurements. Values are in degrees
celsius with a scaling of 200. Thus the received value must be divided by 200.
"""
transfer = GetTemperatureOffsetForRhtMeasurements()
return execute_transfer(self._channel, transfer)[0]
[docs] def get_voc_tuning_parameters(self):
"""
Gets the currently set parameters for customizing the VOC algorithm.
This command is named get_voc_parameters in the datasheet.
:return voc_index_offset:
VOC index representing typical (average) conditions.
:return learning_time_offset_hours:
Time constant to estimate the VOC algorithm offset from the history in hours.
Past events will be forgotten after about twice the learning time.
:return learning_time_gain_hours:
Time constant to estimate the VOC algorithm gain from the history in hours. Past
events will be forgotten after about twice the learning time.
:return gating_max_duration_minutes:
Maximum duration of gating in minutes (freeze of estimator during high VOC index
signal). Set to zero to disable the gating.
:return std_initial:
Initial estimate for standard deviation. Lower value boosts events during
initial learning period, but may result in larger device-to-device variations.
:return gain_factor:
Gain factor to amplify or to attenuate the VOC index output.
"""
transfer = GetVocTuningParameters()
return execute_transfer(self._channel, transfer)
[docs] def get_nox_tuning_parameters(self):
"""
Gets the currently set parameters for customizing the NOx algorithm.
This command is named get_nox_parameters in the datasheet.
:return nox_index_offset:
NOx index representing typical (average) conditions.
:return learning_time_offset_hours:
Time constant to estimate the NOx algorithm offset from the history in hours.
Past events will be forgotten after about twice the learning time.
:return learning_time_gain_hours:
The time constant to estimate the NOx algorithm gain from the history has no
impact for NOx. This parameter is still in place for consistency reasons with
the VOC tuning parameters command. This getter will always return the default
value.
:return gating_max_duration_minutes:
Maximum duration of gating in minutes (freeze of estimator during high NOx index
signal). Set to zero to disable the gating.
:return std_initial:
The initial estimate for standard deviation has no impact for NOx. This
parameter is still in place for consistency reasons with the VOC tuning
parameters command. This getter will always return the default value.
:return gain_factor:
Gain factor to amplify or to attenuate the NOx index output.
"""
transfer = GetNoxTuningParameters()
return execute_transfer(self._channel, transfer)
[docs] def store_nv_data(self):
"""
This command stores all parameters previously set with the commands
set_temperature_offset_for_rht_measurements, set_voc_tuning_parameters and
set_nox_tuning_parameters to the non-volatile
memory of SVM4x. These parameters will not be erased
during reset and will be used by the corresponding algorithms
after start-up. To reset the storage to factory settings the
master has to set all parameters to the default values followed
by a subsequent call of the store_nv_data command.
This command is named store_input_parameters in the datasheet.
"""
transfer = StoreNvData()
return execute_transfer(self._channel, transfer)
[docs] def set_temperature_offset_for_rht_measurements(self, t_offset):
"""
Sets the T-Offset for the temperature compensation of the RHT algorithm.
:param t_offset:
Temperature offset in degrees celsius. Accepted data formats is a int16 value (2
bytes). Values are in degrees celsius with a scaling of 200. Thus the provided
value must be multiplied by 200.
.. note::
Execute the store command after writing the parameter to store it in the non-
volatile memory of the device otherwise the parameter will be reset upton a
device reset.
"""
transfer = SetTemperatureOffsetForRhtMeasurements(t_offset)
return execute_transfer(self._channel, transfer)
[docs] def set_voc_tuning_parameters(self, voc_index_offset, learning_time_offset_hours, learning_time_gain_hours,
gating_max_duration_minutes, std_initial, gain_factor):
"""
Sets parameters to customize the VOC algorithm. This command is only available
in idle mode.
This command is named set_voc_parameters in the datasheet.
:param voc_index_offset:
VOC index representing typical (average) conditions. Allowed values are in range
1..250. The default value is 100.
:param learning_time_offset_hours:
Time constant to estimate the VOC algorithm offset from the history in hours.
Past events will be forgotten after about twice the learning time. Allowed
values are in range 1..1000. The default value is 12 hours.
:param learning_time_gain_hours:
Time constant to estimate the VOC algorithm gain from the history in hours. Past
events will be forgotten after about twice the learning time. Allowed values are
in range 1..1000. The default value is 12 hours.
:param gating_max_duration_minutes:
Maximum duration of gating in minutes (freeze of estimator during high VOC index
signal). Set to zero to disable the gating. Allowed values are in range 0..3000.
The default value is 180 minutes.
:param std_initial:
Initial estimate for standard deviation. Lower value boosts events during
initial learning period, but may result in larger device-to-device variations.
Allowed values are in range 10..5000. The default value is 50.
:param gain_factor:
Gain factor to amplify or to attenuate the VOC index output. Allowed values are
in range 1..1000. The default value is 230.
.. note::
Execute the store command after writing the parameter to store it in the non-
volatile memory of the device otherwise the parameter will be reset upton a
device reset.
"""
transfer = SetVocTuningParameters(voc_index_offset, learning_time_offset_hours, learning_time_gain_hours,
gating_max_duration_minutes, std_initial, gain_factor)
return execute_transfer(self._channel, transfer)
[docs] def set_nox_tuning_parameters(self, nox_index_offset, learning_time_offset_hours, learning_time_gain_hours,
gating_max_duration_minutes, std_initial, gain_factor):
"""
Sets parameters to customize the NOx algorithm. This command is only available
in idle mode.
This command is named set_nox_parameters in the datasheet.
:param nox_index_offset:
NOx index representing typical (average) conditions. Allowed values are in range
1..250. The default value is 1.
:param learning_time_offset_hours:
Time constant to estimate the NOx algorithm offset from the history in hours.
Past events will be forgotten after about twice the learning time. Allowed
values are in range 1..1000. The default value is 12 hours.
:param learning_time_gain_hours:
The time constant to estimate the NOx algorithm gain from the history has no
impact for the NOx algorithm. This parameter is still in place for consistency
reasons with the VOC tuning parameters command. This parameter must always be
set to 12 hours.
:param gating_max_duration_minutes:
Maximum duration of gating in minutes (freeze of estimator during high NOx index
signal). Set to zero to disable the gating. Allowed values are in range 0..3000.
The default value is 720 minutes.
:param std_initial:
The initial estimate for standard deviation parameter has no impact for the NOx
algorithm. This parameter is still in place for consistency reasons with the VOC
tuning parameters command. This parameter must always be set to 50.
:param gain_factor:
Gain factor to amplify or to attenuate the NOx index output. Allowed values are
in range 1..1000. The default value is 230.
.. note::
Execute the store command after writing the parameter to store it in the non-
volatile memory of the device otherwise the parameter will be reset upton a
device reset.
"""
transfer = SetNoxTuningParameters(nox_index_offset, learning_time_offset_hours, learning_time_gain_hours,
gating_max_duration_minutes, std_initial, gain_factor)
return execute_transfer(self._channel, transfer)
[docs] def get_voc_state(self):
"""
Gets the current VOC algorithm state. Retrieved values can be used to set the
VOC algorithm state to resume operation after a short interruption, skipping
initial learning phase. This command is only available during measurement mode.
:return state:
Current VOC algorithm state.
.. note::
This feature can only be used after at least 3 hours of continuous operation.
"""
transfer = GetVocState()
return execute_transfer(self._channel, transfer)[0]
[docs] def set_voc_state(self, state):
"""
Set previously retrieved VOC algorithm state to resume operation after a short
interruption, skipping initial learning phase. This command is only available in
idle mode.
:param state:
Current VOC algorithm state.
.. note::
This feature should not be used after interruptions of more than 10 minutes.
"""
transfer = SetVocState(state)
return execute_transfer(self._channel, transfer)
[docs]class Svm4xDevice(Svm4xDeviceBase):
"""Driver class implementation of SVM4X"""
#: Access to base class
svm4x = MixinAccess()
[docs] def __init__(self, channel):
super().__init__(channel)
[docs] def read_measured_values(self):
"""
Read measurement data and apply scaling.
:return a_humidity:
Compensated ambient humidity in %RH.
:return a_temperature:
Compensated ambient temperature in degrees celsius.
:return a_voc_index:
VOC Index
:return a_nox_index:
NOx Index
"""
(humidity_raw, temp_raw, voc_idx_raw, nox_idx_raw
) = self.read_measured_values_as_integers()
return (SignalHumidity(humidity_raw), SignalTemperature(temp_raw), SignalVocIndex(voc_idx_raw),
SignalNoxIndex(nox_idx_raw))
[docs] def get_temperature_offset_for_rht_measurements(self):
"""
Gets the T-Offset for the temperature compensation of the RHT algorithm.
:return arg_0:
"""
t_offset_int = self.svm4x.get_temperature_offset_for_rht_measurements()
return float(t_offset_int / 200)
[docs] def set_temperature_offset_for_rht_measurements(self, t_offset):
"""
This command sets the temperature offset used for the compensation of subsequent RHT measurement
:param t_offset:
Temperature offset as float in degrees celsius.
"""
t_offset_int = round(t_offset * 200)
self.svm4x.set_temperature_offset_for_rht_measurements(t_offset_int)
return
[docs] def get_product_type_as_int(self):
"""
Get the product type. The product type (sometimes also called "device
type") can be used to detect what kind of SHDLC product is connected.
:return: The product type as an integer.
:rtype: integer
"""
product_type = self.get_product_type()
return int(product_type, 16)