#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# (c) Copyright 2026 Sensirion AG, Switzerland
#
# THIS FILE IS AUTOMATICALLY GENERATED!
#
# Generator: sensirion-driver-generator 1.5.1
# Product: sbn4x
# Model-Version: 2.0.0
#
"""
The class Sbn4xDeviceBase implements the low level interface of the sensor.
The class Sbn4xDevice extends the Sbn4xDeviceBase. 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_sbn4x.commands import (DeviceReset, ForceO2Recalibration, GetArticleCode, GetDeviceUid,
GetForcedCO2RecalibrationStatus, GetProductName, GetProductType,
GetSerialNumber, GetVersion, ReadAndClearDeviceStatus, ReadDeviceStatus,
ReadMeasurementsRaw, StartForcedCO2Recalibration,
StartPeriodicMeasurementMode, StopMeasurement)
from sensirion_i2c_sbn4x.result_types import (SignalI16ToFloat, SignalU32ToFloat)
[docs]
class Sbn4xDeviceBase:
"""Low level API implementation of SBN4X"""
[docs]
def __init__(self, channel):
self._channel = channel
@property
def channel(self):
return self._channel
[docs]
def start_periodic_measurement_mode(self, sampling_rate):
"""
Starts periodic measurement of O2, humidity, temperature, pressure
and CO2.
After starting the measurement, it takes some time (~20ms) until the
first measurement results are available. The command 0x02 `read
measured values` will return an error 0x20 `no measurement data
available` until the first results are available.
This command is only available in idle mode. If the device is
already in any measure mode, or the requested sampling rate is not
supported, this command has no effect.
:param sampling_rate:
Sampling rate of the measurement in [mHz].
The actual sampling rate in Hz is the value divided by 1000.
The only valid sampling rate is 30 Hz (30000 mHz).
:Example:
.. code-block:: python
sensor.start_periodic_measurement_mode(30000)
"""
transfer = StartPeriodicMeasurementMode(sampling_rate)
return execute_transfer(self._channel, transfer)
[docs]
def stop_measurement(self):
"""
Stops the measurement and returns to idle mode.
The last measurement results can still be read out, if available.
If the device is already in idle mode, this command has no effect.
"""
transfer = StopMeasurement()
return execute_transfer(self._channel, transfer)
[docs]
def read_measurements_raw(self):
"""
Read the measured values O2, CO2 and ambient conditions from
the sensor.
Each set of measurements is identified by the measurement counter.
:return measurement_counter:
The measurement counter is incremented with each measurement in the periodic measurement mode. A
counter value of 0 indicates that no measurement has been taken so far. The measurement counters can
be used to disambiguate the data returned by the sensor.
:return o2_dry_percent:
The O2 concentration in 1/256 % of the measured gas when the humidity in the gas is compensated. In
order to obtain the O2 concentration in %, the value has to be divided by 256.
:return co2_dry_percent:
The CO2 concentration in 1/256 % of the measured gas. In order to obtain the CO2 concentration in %,
the value has to be divided by 256.
:return respiratory_exchange_ratio:
Respiratory exchange ratio (RER), dimensionless. The value is provided in 1/256 units.
To obtain the RER as a floating-point value, divide by 256.
:return pressure:
The pressure 1/256 Pa. In order to obtain the pressure in Pa, the value has to be divided by 256.
"""
transfer = ReadMeasurementsRaw()
return execute_transfer(self._channel, transfer)
[docs]
def force_o2_recalibration(self):
"""
Force a recalibration of the O2 sensor. This command is executed in
measurement mode only.
The measurement loop is interrupted until the recalibration is
done and the new calibration value is written to flash.
After this, the sensor resumes measuring using the updated
configuration value.
"""
transfer = ForceO2Recalibration()
return execute_transfer(self._channel, transfer)
[docs]
def start_forced_co2_recalibration(self):
"""
Force a recalibration of the CO2 sensor. This command is executed only when the sensor is in
periodic mode. There is no measurement data available until the recalibration is fully applied to
the CO2 sensor. After this, the sensor resumes measuring using the updated calibration value.
"""
transfer = StartForcedCO2Recalibration()
return execute_transfer(self._channel, transfer)
[docs]
def get_forced_co2_recalibration_status(self):
"""
Get the status of the forced recalibration. This function can be polled after starting a forced CO2
recalibration. The returned value is 0xFFFF as long as the forced recalibration is running, and
changes to 0 if the forced recalibration is completed.
:return forced_recalibration_status:
Status of the forced recalibration.
"""
transfer = GetForcedCO2RecalibrationStatus()
return execute_transfer(self._channel, transfer)[0]
[docs]
def get_product_type(self):
"""
Gets the product type from the device.
:return product_type:
Null-terminated string 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:
Null-terminated ASCII string containing the product name. Up to 32 characters can be read from the
device.
"""
transfer = GetProductName()
return execute_transfer(self._channel, transfer)[0]
[docs]
def get_article_code(self):
"""
Gets the article code from the device.
:return article_code:
Null-terminated ASCII string containing the article code. Up to 32 characters can be read from the
device.
"""
transfer = GetArticleCode()
return execute_transfer(self._channel, transfer)[0]
[docs]
def get_serial_number(self):
"""
Gets the serial number from the device.
:return serial_number:
Null-terminated ASCII string containing the serial number. Up to 32 characters can be read from the
device.
"""
transfer = GetSerialNumber()
return execute_transfer(self._channel, transfer)[0]
[docs]
def get_device_uid(self):
"""
Gets the device uid from the device.
:return device_uid:
96 bit unique microcontroller UID in binary format.
"""
transfer = GetDeviceUid()
return execute_transfer(self._channel, transfer)[0]
[docs]
def get_version(self):
"""
Gets the version information for the hardware, firmware and communication protocol.
:return firmware_major:
Firmware major version number.
:return firmware_minor:
Firmware minor version number.
:return firmware_develop_flag:
Firmware develop flag. False for released firmware.
: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.
:return padding:
Padding byte, ignore this.
"""
transfer = GetVersion()
return execute_transfer(self._channel, transfer)
[docs]
def read_device_status(self):
"""
Reads the current device status.
Use this command to get detailed information about the device status.
The device status is encoded in flags. Each device status flag
represents a single bit in a 32-bit integer value. If more than one
error is present, the device status register value is the sum of the
corresponding flag values. For details about the available flags,
refer to the device status flags documentation.
:return device_status:
Device status (32 flags as an integer value). For details, please refer to the device status flags
documentation.
.. note::
The status flags of type "Error" are sticky, i.e. they are not cleared automatically even if the
error condition no longer exists. So they can only be cleared manually with the command 0xD210
"Read And Clear Device Status" or with a device reset. All other flags are not sticky, i.e. they
are cleared automatically if the trigger condition disappears.
"""
transfer = ReadDeviceStatus()
return execute_transfer(self._channel, transfer)[0]
[docs]
def read_and_clear_device_status(self):
"""
Reads the current device status (like command 0xD206 "Read Device Status") and afterwards clears
all flags.
:return device_status:
Device status (32 flags as an integer value) **before** clearing it. For details, please refer to
the device status flags documentation.
"""
transfer = ReadAndClearDeviceStatus()
return execute_transfer(self._channel, transfer)[0]
[docs]
def device_reset(self):
"""Executes a reset on the device. This has the same effect as a power cycle."""
transfer = DeviceReset()
return execute_transfer(self._channel, transfer)
[docs]
class Sbn4xDevice(Sbn4xDeviceBase):
"""Driver class implementation of SBN4X"""
#: Access to base class
sbn4x = MixinAccess()
[docs]
def __init__(self, channel):
super().__init__(channel)
[docs]
def read_measurements(self):
"""
Read the measured values of the sensor
:return measurement_counter:
The measurement counter is incremented with each measurement in the periodic measurement mode. A counter value of 0 indicates that no measurement has been taken so far. The measurement counters can be used to disambiguate the data returned by the sensor.
:return o2_dry_percent:
The measured O₂ concentration
:return co2_dry_percent:
The measured CO₂ concentration
:return respiratory_exchange_ratio:
The measured respiratory exchange ratio (RER), dimensionless
:return pressure:
The measured pressure in Pa
"""
(counter, o2, co2, rer, p
) = self.sbn4x.read_measurements_raw()
return (counter, SignalI16ToFloat(o2), SignalI16ToFloat(co2), SignalI16ToFloat(rer), SignalU32ToFloat(p))