#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# (c) Copyright 2026 Sensirion AG, Switzerland
#
# THIS FILE IS AUTOMATICALLY GENERATED!
#
# Generator: sensirion-driver-generator 1.8.0
# Product: ld20
# Model-Version: 2.0.0
#
"""
The class Ld20DeviceBase implements the low level interface of the sensor.
The class Ld20Device extends the Ld20DeviceBase. 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_ld20.commands import (EnterSleep, ExitSleep, ReadMeasurementDataRaw, ReadProductIdentifier,
ReadProductIdentifierPrepare, SignalingFlagsT, StartContinuousMeasurement,
StopContinuousMeasurement)
from sensirion_i2c_ld20.result_types import (SignalFlow, SignalTemperature)
[docs]
class Ld20DeviceBase:
"""Low level API implementation of LD20"""
[docs]
def __init__(self, channel):
self._channel = channel
@property
def channel(self):
return self._channel
[docs]
def start_continuous_measurement(self):
"""
Starts continuous measurement mode.
The sensor measures both the flow rate and the temperature. After the command has been sent, the chip
continuously measures and updates the measurement results which can be read with *read_measurement_data_raw*.
.. note::
The first measurement result will be available after 12ms. Due to the thermal measurement principle small
accuracy deviations (% m.v.) can occur while the sensor warms-up (120ms including the 12 ms for measurement initialization).
"""
transfer = StartContinuousMeasurement()
return execute_transfer(self._channel, transfer)
[docs]
def read_measurement_data_raw(self):
"""
After the command *start_h2o_continuous_measurement* has been sent, the chip continuously measures and updates
the measurement results. New results (flow, temperature, and signaling flags) can be read continuously
with this command.
:return raw_flow:
For LD20-0600L convert to ml/h by applying: flow = raw_flow / 1200
For LD20-2600B convert to ml/h by applying: flow = raw_flow / 20
:return raw_temperature:
Convert to degrees celsius by temperature = raw_temperature / 200
:return signaling_flags:
Gives additional information about the measurement status. Refer to the
sensor data sheet for detailed information. Following flags are defined:
Air-in-Line flag (Bit 0), High Flow flag (Bit 1), Exponential smoothing active (Bit 5)
.. note::
The first measurement result will be available 12ms after starting the measurement.
Small accuracy deviations (% m.v.) can occur during the first 120ms (including the 12ms initialization)
"""
transfer = ReadMeasurementDataRaw()
res_0, res_1, res_2 = execute_transfer(self._channel, transfer)
return res_0, res_1, SignalingFlagsT(res_2)
[docs]
def stop_continuous_measurement(self):
"""
This command stops the continuous measurement and puts the sensor in idle mode.
After it receives the stop command, the sensor needs up to 0.5ms to power down the heater,
enter idle mode and be receptive for a new command.
"""
transfer = StopContinuousMeasurement()
return execute_transfer(self._channel, transfer)
[docs]
def enter_sleep(self):
"""
In sleep mode the sensor uses a minimum amount of power. 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.
Supported by products: LD20-0600L, LD20-2600B
"""
transfer = EnterSleep()
return execute_transfer(self._channel, transfer)
[docs]
def exit_sleep(self):
"""
The sensor exits the sleep mode and enters the idle mode when it receives the valid I2C address and a write
bit (‘0’).
Note that the I2C address is not acknowledged. It is necessary to poll the sensor to see whether the sensor has
received the address and has woken up. This should take typically 25ms.
Supported by products: LD20-0600L, LD20-2600B
"""
transfer = ExitSleep()
return execute_transfer(self._channel, transfer)
[docs]
def read_product_identifier_prepare(self):
"""
Prepare to read the product identifier and the serial number.
The command can only be executed from the idle mode, i.e. when the sensor is not performing measurements.
"""
transfer = ReadProductIdentifierPrepare()
return execute_transfer(self._channel, transfer)
[docs]
def read_product_identifier(self):
"""
This command allows to read product identifier and the serial number.
The command can only be executed from the idle mode, i.e. when the sensor is not performing measurements
and *read_product_identifier_prepare* is called before.
:return product_identifier:
Note that the last 8 bits are the sensor’s revision number and are subject to change in case of an
update of the specifications.
:return serial_number_high:
Higher 32-bit of the 64-bit unique serial number
:return serial_number_low:
Lower 32-bit of the 64-bit unique serial number
"""
transfer = ReadProductIdentifier()
return execute_transfer(self._channel, transfer)
[docs]
class Ld20Device(Ld20DeviceBase):
"""Driver class implementation of LD20"""
#: Access to base class
ld20 = MixinAccess()
[docs]
def __init__(self, channel):
super().__init__(channel)
[docs]
def read_measurement_data(self, inv_flow_scale_factor):
"""
Reads the raw measurement values and converts them to their physcial units where applicable.
For the flow the scaling factor and resulting flow unit depends on the specific sensor.
The scaling factor is passed as an argument and the raw flow value is converted by applying:
flow = raw_flow / inv_flow_scale_factor
The scaling factors for the supported sensor are defined in enum *inv_flow_scale_factors*
:param inv_flow_scale_factor:
used to convert raw flow value
:return a_flow:
As the flow scaling differs for specific sensor types the scaling factor
must be passed as an argument. See the enum *inv_flow_scale_factors* for
scaling factors of supported sensors.
The raw value is converted by: flow = raw_flow / inv_flow_scale_factor
Resulting unit depends on your specific sensor type.
:return a_temperature:
temperature in degree celsius
:return a_signaling_flags:
"""
(raw_flow, raw_temp, signaling_flags
) = self.ld20.read_measurement_data_raw()
return (SignalFlow(raw_flow, inv_flow_scale_factor), SignalTemperature(raw_temp), signaling_flags)
[docs]
def read_product_identifier(self):
"""
Read product identifier and the serial number.
The command can only be executed from the idle mode, i.e. when the sensor is not performing measurements.
:return product_identifier:
:return serial_number:
"""
self.ld20.read_product_identifier_prepare()
(product_id, serial_number_high, serial_number_low
) = self.ld20.read_product_identifier()
return (product_id, (int(serial_number_high) * 4294967296) + serial_number_low)