tinkerforge-async 1.6.1__tar.gz → 1.7.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. {tinkerforge_async-1.6.1/tinkerforge_async.egg-info → tinkerforge_async-1.7.0}/PKG-INFO +1 -1
  2. {tinkerforge_async-1.6.1 → tinkerforge_async-1.7.0}/tinkerforge_async/_version.py +1 -1
  3. tinkerforge_async-1.7.0/tinkerforge_async/bricklet_industrial_dual_0_20ma_v2.py +476 -0
  4. {tinkerforge_async-1.6.1 → tinkerforge_async-1.7.0}/tinkerforge_async/bricklet_io4_v2.py +7 -1
  5. {tinkerforge_async-1.6.1 → tinkerforge_async-1.7.0}/tinkerforge_async/devices.py +1 -0
  6. {tinkerforge_async-1.6.1 → tinkerforge_async-1.7.0/tinkerforge_async.egg-info}/PKG-INFO +1 -1
  7. {tinkerforge_async-1.6.1 → tinkerforge_async-1.7.0}/tinkerforge_async.egg-info/SOURCES.txt +1 -0
  8. {tinkerforge_async-1.6.1 → tinkerforge_async-1.7.0}/LICENSE +0 -0
  9. {tinkerforge_async-1.6.1 → tinkerforge_async-1.7.0}/README.md +0 -0
  10. {tinkerforge_async-1.6.1 → tinkerforge_async-1.7.0}/pyproject.toml +0 -0
  11. {tinkerforge_async-1.6.1 → tinkerforge_async-1.7.0}/setup.cfg +0 -0
  12. {tinkerforge_async-1.6.1 → tinkerforge_async-1.7.0}/setup.py +0 -0
  13. {tinkerforge_async-1.6.1 → tinkerforge_async-1.7.0}/tinkerforge_async/__init__.py +0 -0
  14. {tinkerforge_async-1.6.1 → tinkerforge_async-1.7.0}/tinkerforge_async/async_event_bus.py +0 -0
  15. {tinkerforge_async-1.6.1 → tinkerforge_async-1.7.0}/tinkerforge_async/brick_master.py +0 -0
  16. {tinkerforge_async-1.6.1 → tinkerforge_async-1.7.0}/tinkerforge_async/bricklet_ambient_light_v2.py +0 -0
  17. {tinkerforge_async-1.6.1 → tinkerforge_async-1.7.0}/tinkerforge_async/bricklet_ambient_light_v3.py +0 -0
  18. {tinkerforge_async-1.6.1 → tinkerforge_async-1.7.0}/tinkerforge_async/bricklet_analog_in.py +0 -0
  19. {tinkerforge_async-1.6.1 → tinkerforge_async-1.7.0}/tinkerforge_async/bricklet_barometer.py +0 -0
  20. {tinkerforge_async-1.6.1 → tinkerforge_async-1.7.0}/tinkerforge_async/bricklet_barometer_v2.py +0 -0
  21. {tinkerforge_async-1.6.1 → tinkerforge_async-1.7.0}/tinkerforge_async/bricklet_humidity.py +0 -0
  22. {tinkerforge_async-1.6.1 → tinkerforge_async-1.7.0}/tinkerforge_async/bricklet_humidity_v2.py +0 -0
  23. {tinkerforge_async-1.6.1 → tinkerforge_async-1.7.0}/tinkerforge_async/bricklet_industrial_dual_analog_in_v2.py +0 -0
  24. {tinkerforge_async-1.6.1 → tinkerforge_async-1.7.0}/tinkerforge_async/bricklet_industrial_ptc.py +0 -0
  25. {tinkerforge_async-1.6.1 → tinkerforge_async-1.7.0}/tinkerforge_async/bricklet_io16.py +0 -0
  26. {tinkerforge_async-1.6.1 → tinkerforge_async-1.7.0}/tinkerforge_async/bricklet_isolator.py +0 -0
  27. {tinkerforge_async-1.6.1 → tinkerforge_async-1.7.0}/tinkerforge_async/bricklet_moisture.py +0 -0
  28. {tinkerforge_async-1.6.1 → tinkerforge_async-1.7.0}/tinkerforge_async/bricklet_motion_detector_v2.py +0 -0
  29. {tinkerforge_async-1.6.1 → tinkerforge_async-1.7.0}/tinkerforge_async/bricklet_ptc.py +0 -0
  30. {tinkerforge_async-1.6.1 → tinkerforge_async-1.7.0}/tinkerforge_async/bricklet_ptc_v2.py +0 -0
  31. {tinkerforge_async-1.6.1 → tinkerforge_async-1.7.0}/tinkerforge_async/bricklet_rs232_v2.py +0 -0
  32. {tinkerforge_async-1.6.1 → tinkerforge_async-1.7.0}/tinkerforge_async/bricklet_segment_display_4x7.py +0 -0
  33. {tinkerforge_async-1.6.1 → tinkerforge_async-1.7.0}/tinkerforge_async/bricklet_segment_display_4x7_v2.py +0 -0
  34. {tinkerforge_async-1.6.1 → tinkerforge_async-1.7.0}/tinkerforge_async/bricklet_temperature.py +0 -0
  35. {tinkerforge_async-1.6.1 → tinkerforge_async-1.7.0}/tinkerforge_async/bricklet_temperature_v2.py +0 -0
  36. {tinkerforge_async-1.6.1 → tinkerforge_async-1.7.0}/tinkerforge_async/bricklet_thermocouple_v2.py +0 -0
  37. {tinkerforge_async-1.6.1 → tinkerforge_async-1.7.0}/tinkerforge_async/device_factory.py +0 -0
  38. {tinkerforge_async-1.6.1 → tinkerforge_async-1.7.0}/tinkerforge_async/helper.py +0 -0
  39. {tinkerforge_async-1.6.1 → tinkerforge_async-1.7.0}/tinkerforge_async/ip_connection.py +0 -0
  40. {tinkerforge_async-1.6.1 → tinkerforge_async-1.7.0}/tinkerforge_async/ip_connection_helper.py +0 -0
  41. {tinkerforge_async-1.6.1 → tinkerforge_async-1.7.0}/tinkerforge_async.egg-info/dependency_links.txt +0 -0
  42. {tinkerforge_async-1.6.1 → tinkerforge_async-1.7.0}/tinkerforge_async.egg-info/requires.txt +0 -0
  43. {tinkerforge_async-1.6.1 → tinkerforge_async-1.7.0}/tinkerforge_async.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: tinkerforge-async
3
- Version: 1.6.1
3
+ Version: 1.7.0
4
4
  Summary: Python3 AsyncIO Tinkerforge driver
5
5
  Author-email: Patrick Baus <patrick.baus@physik.tu-darmstadt.de>
6
6
  License-Expression: GPL-3.0-or-later
@@ -1,2 +1,2 @@
1
1
  # pylint: disable=missing-module-docstring
2
- __version__ = "1.6.1"
2
+ __version__ = "1.7.0"
@@ -0,0 +1,476 @@
1
+ """
2
+ Module for the Tinkerforge Industrial Dual 0-20mA Bricklet 2.0
3
+ (https://www.tinkerforge.com/en/doc/Hardware/Bricklets/Industrial_Dual_020mA_V2.html) implemented using Python
4
+ asyncio. It does the low-level communication with the Tinkerforge ip connection and also handles conversion of raw
5
+ units to SI units.
6
+ """
7
+
8
+ # pylint: disable=duplicate-code # Many sensors of different generations have a similar API
9
+ from __future__ import annotations
10
+
11
+ from decimal import Decimal
12
+ from enum import Enum, unique
13
+ from typing import TYPE_CHECKING, AsyncGenerator, NamedTuple
14
+
15
+ from .devices import (
16
+ AdvancedCallbackConfiguration,
17
+ BrickletWithMCU,
18
+ DeviceIdentifier,
19
+ Event,
20
+ LedConfig,
21
+ )
22
+ from .devices import ThresholdOption as Threshold
23
+ from .devices import (
24
+ _FunctionID,
25
+ )
26
+ from .ip_connection_helper import pack_payload, unpack_payload
27
+
28
+ if TYPE_CHECKING:
29
+ from .ip_connection import IPConnectionAsync
30
+
31
+
32
+ @unique
33
+ class CallbackID(Enum):
34
+ """
35
+ The callbacks available to this bricklet
36
+ """
37
+
38
+ CURRENT = 4
39
+
40
+
41
+ _CallbackID = CallbackID
42
+
43
+
44
+ @unique
45
+ class FunctionID(_FunctionID):
46
+ """
47
+ The function calls available to this bricklet
48
+ """
49
+
50
+ GET_CURRENT = 1
51
+ SET_CURRENT_CALLBACK_CONFIGURATION = 2
52
+ GET_CURRENT_CALLBACK_CONFIGURATION = 3
53
+ SET_SAMPLE_RATE = 5
54
+ GET_SAMPLE_RATE = 6
55
+ SET_GAIN = 7
56
+ GET_GAIN = 8
57
+ SET_CHANNEL_LED_CONFIG = 9
58
+ GET_CHANNEL_LED_CONFIG = 10
59
+ SET_CHANNEL_LED_STATUS_CONFIG = 11
60
+ GET_CHANNEL_LED_STATUS_CONFIG = 12
61
+
62
+
63
+ @unique
64
+ class ChannelLedStatusConfig(Enum):
65
+ """
66
+ Defines the LED brightness when LedConfig.SHOW_STATUS
67
+ """
68
+
69
+ THRESHOLD = 0
70
+ INTENSITY = 1
71
+
72
+
73
+ _ChannelLedStatusConfig = ChannelLedStatusConfig # We need the alias for MyPy type hinting
74
+
75
+
76
+ @unique
77
+ class SamplingRate(Enum):
78
+ """
79
+ Defines the sampling rate of the ADC. The resolution of the ADC depends on the sampling rate: 12 bit at 240 SPS,
80
+ 14 bit at 60 SPS, 16 bit at 15 SPS and 18 bit at 4 SPS.
81
+ """
82
+
83
+ RATE_240_SPS = 0
84
+ RATE_60_SPS = 1
85
+ RATE_15_SPS = 2
86
+ RATE_4_SPS = 3
87
+
88
+
89
+ _SamplingRate = SamplingRate # We need the alias for MyPy type hinting
90
+
91
+
92
+ @unique
93
+ class Gain(Enum):
94
+ """
95
+ Defines the gain of the ADC
96
+ """
97
+
98
+ GAIN_1X = 0
99
+ GAIN_2X = 1
100
+ GAIN_4X = 2
101
+ GAIN_8X = 3
102
+
103
+
104
+ _Gain = Gain # We need the alias for MyPy type hinting
105
+
106
+
107
+ class GetChannelLEDStatusConfig(NamedTuple):
108
+ minimum: Decimal
109
+ maximum: Decimal
110
+ config: ChannelLedStatusConfig
111
+
112
+
113
+ class BrickletIndustrialDual020mAV2(BrickletWithMCU):
114
+ """
115
+ Measures two currents between 0mA and 22.5mA (IEC 60381-1 compatible, so 4-20mA sensors work as well) with up to
116
+ 18bit resolution each. All currents are returned in A.
117
+ """
118
+
119
+ DEVICE_IDENTIFIER = DeviceIdentifier.BRICKLET_INDUSTRIAL_DUAL_0_20MA_V2
120
+ DEVICE_DISPLAY_NAME = "Industrial Dual 0-20mA Bricklet 2.0"
121
+
122
+ # Convenience imports, so that the user does not need to additionally import them
123
+ CallbackID = CallbackID
124
+ FunctionID = FunctionID
125
+ ThresholdOption = Threshold
126
+ ChannelLedConfig = LedConfig
127
+ ChannelLedStatusConfig = ChannelLedStatusConfig
128
+ SamplingRate = SamplingRate
129
+ Gain = Gain
130
+
131
+ CALLBACK_FORMATS = {
132
+ CallbackID.CURRENT: "B i",
133
+ }
134
+
135
+ SID_TO_CALLBACK = {
136
+ 0: (CallbackID.CURRENT,),
137
+ 1: (CallbackID.CURRENT,),
138
+ }
139
+
140
+ def __init__(self, uid: int, ipcon: IPConnectionAsync) -> None:
141
+ """
142
+ Creates an object with the unique device ID *uid* and adds it to the IP connection *ipcon*.
143
+ """
144
+ super().__init__(self.DEVICE_DISPLAY_NAME, uid, ipcon)
145
+
146
+ self.api_version = (2, 0, 0)
147
+
148
+ async def get_value(self, sid: int) -> Decimal:
149
+ assert sid in (0, 1)
150
+
151
+ return await self.get_current(sid)
152
+
153
+ async def set_callback_configuration( # pylint: disable=too-many-arguments,too-many-positional-arguments
154
+ self,
155
+ sid: int,
156
+ period: int = 0,
157
+ value_has_to_change: bool = False,
158
+ option: Threshold | str = Threshold.OFF,
159
+ minimum: float | Decimal | None = None,
160
+ maximum: float | Decimal | None = None,
161
+ response_expected: bool = True,
162
+ ) -> None:
163
+ minimum = 0 if minimum is None else minimum
164
+ maximum = 0 if maximum is None else maximum
165
+
166
+ assert sid in (0, 1)
167
+
168
+ await self.set_current_callback_configuration(
169
+ sid, period, value_has_to_change, option, minimum, maximum, response_expected
170
+ )
171
+
172
+ async def get_callback_configuration(self, sid: int) -> AdvancedCallbackConfiguration:
173
+ assert sid in (0, 1)
174
+
175
+ return await self.get_current_callback_configuration(sid)
176
+
177
+ async def get_current(self, channel: int) -> Decimal:
178
+ """
179
+ Returns the current of the specified channel in A.
180
+
181
+ It is possible to detect if an IEC 60381-1 compatible sensor is connected and if it works probably.
182
+
183
+ If the returned current is below 4mA, there is likely no sensor connected or the connected sensor is defective.
184
+ If the returned current is over 20mA, there might be a short circuit or the sensor is defective.
185
+
186
+ If you want to get the value periodically, it is recommended to use the :cb:`Current` callback. You can set the
187
+ callback configuration with :func:`Set Current Callback Configuration`.
188
+ """
189
+ assert channel in (0, 1)
190
+
191
+ _, payload = await self.ipcon.send_request(
192
+ device=self,
193
+ function_id=FunctionID.GET_CURRENT,
194
+ data=pack_payload((int(channel),), "B"),
195
+ response_expected=True,
196
+ )
197
+ return self.__value_to_si(unpack_payload(payload, "i"))
198
+
199
+ async def set_current_callback_configuration( # pylint: disable=too-many-arguments,too-many-positional-arguments
200
+ self,
201
+ channel: int,
202
+ period: int = 0,
203
+ value_has_to_change: bool = False,
204
+ option: Threshold | str = Threshold.OFF,
205
+ minimum: float | Decimal = 0,
206
+ maximum: float | Decimal = 0,
207
+ response_expected: bool = True,
208
+ ) -> None:
209
+ """
210
+ The period is the period with which the :cb:`Current` callback is triggered periodically. A value of 0 turns
211
+ the callback off.
212
+
213
+ If the `value has to change`-parameter is set to true, the callback is only triggered after the value has
214
+ changed. If the value didn't change within the period, the callback is triggered immediately on change.
215
+
216
+ If it is set to false, the callback is continuously triggered with the period, independent of the value.
217
+
218
+ It is furthermore possible to constrain the callback with thresholds.
219
+
220
+ The `option`-parameter together with min/max sets a threshold for the :cb:`Current` callback. The thresholds
221
+ are given in A.
222
+
223
+ The following options are possible:
224
+
225
+ .. csv-table::
226
+ :header: "Option", "Description"
227
+ :widths: 10, 100
228
+
229
+ "'x'", "Threshold is turned off"
230
+ "'o'", "Threshold is triggered when the value is *outside* the min and max values"
231
+ "'i'", "Threshold is triggered when the value is *inside* or equal to the min and max values"
232
+ "'<'", "Threshold is triggered when the value is smaller than the min value (max is ignored)"
233
+ "'>'", "Threshold is triggered when the value is greater than the min value (max is ignored)"
234
+
235
+ If the option is set to 'x' (threshold turned off) the callback is triggered with the fixed period.
236
+ """
237
+ assert channel in (0, 1)
238
+ option = Threshold(option)
239
+ assert period >= 0
240
+
241
+ await self.ipcon.send_request(
242
+ device=self,
243
+ function_id=FunctionID.SET_CURRENT_CALLBACK_CONFIGURATION,
244
+ data=pack_payload(
245
+ (
246
+ int(channel),
247
+ int(period),
248
+ bool(value_has_to_change),
249
+ option.value,
250
+ self.__si_to_value(minimum),
251
+ self.__si_to_value(maximum),
252
+ ),
253
+ "B I ! c i i",
254
+ ),
255
+ response_expected=response_expected,
256
+ )
257
+
258
+ async def get_current_callback_configuration(self, channel: int) -> AdvancedCallbackConfiguration:
259
+ """
260
+ Returns the callback configuration as set by :func:`Set Current Callback Configuration`.
261
+ """
262
+ assert channel in (0, 1)
263
+
264
+ _, payload = await self.ipcon.send_request(
265
+ device=self,
266
+ function_id=FunctionID.GET_CURRENT_CALLBACK_CONFIGURATION,
267
+ data=pack_payload((int(channel),), "B"),
268
+ response_expected=True,
269
+ )
270
+ period, value_has_to_change, option, minimum, maximum = unpack_payload(payload, "I ! c i i")
271
+ return AdvancedCallbackConfiguration(
272
+ period, value_has_to_change, Threshold(option), self.__value_to_si(minimum), self.__value_to_si(maximum)
273
+ )
274
+
275
+ async def set_sample_rate(self, rate: _SamplingRate | int, response_expected: bool = True) -> None:
276
+ """
277
+ Sets the sample rate to either 240, 60, 15 or 4 samples per second. The resolution for the rates is 12, 14, 16
278
+ and 18 bit respectively.
279
+
280
+ .. csv-table::
281
+ :header: "Value", "Description"
282
+ :widths: 10, 100
283
+
284
+ "0", "240 samples per second, 12 bit resolution"
285
+ "1", "60 samples per second, 14 bit resolution"
286
+ "2", "15 samples per second, 16 bit resolution"
287
+ "3", "4 samples per second, 18 bit resolution"
288
+ """
289
+ rate = SamplingRate(rate)
290
+
291
+ await self.ipcon.send_request(
292
+ device=self,
293
+ function_id=FunctionID.SET_SAMPLE_RATE,
294
+ data=pack_payload((rate.value,), "B"),
295
+ response_expected=response_expected,
296
+ )
297
+
298
+ async def get_sample_rate(self) -> _SamplingRate:
299
+ """
300
+ Returns the sample rate as set by :func:`Set Sample Rate`.
301
+ """
302
+ _, payload = await self.ipcon.send_request(
303
+ device=self, function_id=FunctionID.GET_SAMPLE_RATE, response_expected=True
304
+ )
305
+
306
+ return SamplingRate(unpack_payload(payload, "B"))
307
+
308
+ async def set_gain(self, gain: _Gain | int, response_expected: bool = True) -> None:
309
+ """
310
+ Sets a gain between 1x and 8x. If you want to measure a very small current, you can increase the gain to get
311
+ some more resolution.
312
+
313
+ Example: If you measure 0.5mA with a gain of 8x the return value will be 4mA.
314
+ """
315
+ gain = Gain(gain)
316
+
317
+ await self.ipcon.send_request(
318
+ device=self,
319
+ function_id=FunctionID.SET_GAIN,
320
+ data=pack_payload((gain.value,), "B"),
321
+ response_expected=response_expected,
322
+ )
323
+
324
+ async def get_gain(self) -> _Gain:
325
+ """
326
+ Returns the gain as set by :func:`Set Gain`.
327
+ """
328
+ _, payload = await self.ipcon.send_request(device=self, function_id=FunctionID.GET_GAIN, response_expected=True)
329
+
330
+ return Gain(unpack_payload(payload, "B"))
331
+
332
+ async def set_channel_led_config(
333
+ self, channel: int, config: LedConfig | int, response_expected: bool = True
334
+ ) -> None:
335
+ """
336
+ Each channel has a corresponding LED. You can turn the LED off, on or show a heartbeat. You can also set the LED
337
+ to "Channel Status". In this mode the LED can either be turned on with a pre-defined threshold or the intensity
338
+ of the LED can change with the measured value.
339
+
340
+ You can configure the channel status behavior with :func:`Set Channel LED Status Config`.
341
+
342
+ By default, all channel LEDs are configured as "Channel Status".
343
+ """
344
+ assert channel in (0, 1)
345
+ config = LedConfig(config)
346
+
347
+ await self.ipcon.send_request(
348
+ device=self,
349
+ function_id=FunctionID.SET_CHANNEL_LED_CONFIG,
350
+ data=pack_payload((int(channel), config.value), "B B"),
351
+ response_expected=response_expected,
352
+ )
353
+
354
+ async def get_channel_led_config(self, channel: int) -> LedConfig:
355
+ """
356
+ Returns the channel LED configuration as set by :func:`Set Channel LED Config`.
357
+ """
358
+ assert channel in (0, 1)
359
+
360
+ _, payload = await self.ipcon.send_request(
361
+ device=self,
362
+ function_id=FunctionID.GET_CHANNEL_LED_CONFIG,
363
+ data=pack_payload((int(channel),), "B"),
364
+ response_expected=True,
365
+ )
366
+
367
+ return LedConfig(unpack_payload(payload, "B"))
368
+
369
+ async def set_channel_led_status_config( # pylint: disable=too-many-arguments,too-many-positional-arguments
370
+ self,
371
+ channel: int,
372
+ minimum: float | Decimal,
373
+ maximum: float | Decimal,
374
+ config: _ChannelLedStatusConfig | int,
375
+ response_expected: bool = True,
376
+ ) -> None:
377
+ """
378
+ Sets the channel LED status config. This config is used if the channel LED is configured as "Channel Status",
379
+ see :func:`Set Channel LED Config`. The `minimum` and `maximum` values are given in A.
380
+
381
+ For each channel you can choose between threshold and intensity mode.
382
+
383
+ In threshold mode you can define a positive or a negative threshold. For a positive threshold set the "min"
384
+ parameter to the threshold value in A above which the LED should turn on and set the "max" parameter to 0.
385
+ Example: If you set a positive threshold of 10mA, the LED will turn on as soon as the current exceeds 10mA and
386
+ turn off again if it goes below 10mA. For a negative threshold set the "max" parameter to the threshold value in
387
+ A below which the LED should turn on and set the "min" parameter to 0. Example: If you set a negative threshold
388
+ of 10mA, the LED will turn on as soon as the current goes below 10mA and the LED will turn off when the current
389
+ exceeds 10mA.
390
+
391
+ In intensity mode you can define a range in A that is used to scale the brightness of the LED. Example with
392
+ min=4mA and max=20mA: The LED is off at 4mA and below, on at 20mA and above and the brightness is linearly
393
+ scaled between the values 4mA and 20mA. If the min value is greater than the max value, the LED brightness is
394
+ scaled the other way around.
395
+ """
396
+ assert channel in (0, 1)
397
+ config = ChannelLedStatusConfig(config)
398
+
399
+ await self.ipcon.send_request(
400
+ device=self,
401
+ function_id=FunctionID.SET_CHANNEL_LED_STATUS_CONFIG,
402
+ data=pack_payload(
403
+ (
404
+ int(channel),
405
+ self.__si_to_value(minimum),
406
+ self.__si_to_value(maximum),
407
+ config.value,
408
+ ),
409
+ "B i i B",
410
+ ),
411
+ response_expected=response_expected,
412
+ )
413
+
414
+ async def get_channel_led_status_config(self, channel: int) -> GetChannelLEDStatusConfig:
415
+ """
416
+ Returns the channel LED status configuration as set by :func:`Set Channel LED Status Config`.
417
+ """
418
+ assert channel in (0, 1)
419
+
420
+ _, payload = await self.ipcon.send_request(
421
+ device=self,
422
+ function_id=FunctionID.GET_CHANNEL_LED_STATUS_CONFIG,
423
+ data=pack_payload((int(channel),), "B"),
424
+ response_expected=True,
425
+ )
426
+
427
+ minimum, maximum, config = unpack_payload(payload, "i i B")
428
+ return GetChannelLEDStatusConfig(
429
+ self.__value_to_si(minimum), self.__value_to_si(maximum), ChannelLedStatusConfig(config)
430
+ )
431
+
432
+ @staticmethod
433
+ def __value_to_si(value: int) -> Decimal:
434
+ """
435
+ Convert the sensor value (nA) to SI units (A)
436
+ """
437
+ return Decimal(value) / 1_000_000_000
438
+
439
+ @staticmethod
440
+ def __si_to_value(value: float | Decimal) -> int:
441
+ """
442
+ Convert SI units (A) to the sensor value (nA). Rounding avoids floating point errors, e.g. 0.0029 A * 1e9
443
+ would otherwise result in 2899999 nA.
444
+ """
445
+ return round(Decimal(value) * 1_000_000_000)
446
+
447
+ async def read_events(
448
+ self,
449
+ events: tuple[int | _CallbackID, ...] | list[int | _CallbackID] | None = None,
450
+ sids: tuple[int, ...] | list[int] | None = None,
451
+ ) -> AsyncGenerator[Event, None]:
452
+ registered_events = set()
453
+ if events:
454
+ for event in events:
455
+ registered_events.add(self.CallbackID(event))
456
+ if sids is not None:
457
+ for sid in sids:
458
+ for callback in self.SID_TO_CALLBACK.get(sid, []):
459
+ registered_events.add(callback)
460
+
461
+ if not events and sids is None:
462
+ registered_events = set(self.CALLBACK_FORMATS.keys())
463
+
464
+ async for header, payload in super()._read_events():
465
+ try:
466
+ function_id = CallbackID(header.function_id)
467
+ except ValueError:
468
+ # Invalid header. Drop the packet.
469
+ continue
470
+ if function_id in registered_events:
471
+ channel, value = unpack_payload(payload, self.CALLBACK_FORMATS[function_id])
472
+ if sids is not None and channel not in sids:
473
+ # We got a reply from the wrong channel, maybe there is another callback running on that other
474
+ # channel, so we ignore the value
475
+ continue
476
+ yield Event(self, channel, function_id, self.__value_to_si(value))
@@ -151,7 +151,13 @@ class BrickletIO4V2(BrickletWithMCU): # pylint: disable=too-many-public-methods
151
151
  CallbackID.MONOFLOP_DONE: "B !",
152
152
  }
153
153
 
154
- SID_TO_CALLBACK = {i: (CallbackID.INPUT_VALUE, CallbackID.MONOFLOP_DONE) for i in range(4)}
154
+ SID_TO_CALLBACK = {
155
+ 0: (CallbackID.INPUT_VALUE, CallbackID.MONOFLOP_DONE),
156
+ 1: (CallbackID.INPUT_VALUE, CallbackID.MONOFLOP_DONE),
157
+ 2: (CallbackID.INPUT_VALUE, CallbackID.MONOFLOP_DONE),
158
+ 3: (CallbackID.INPUT_VALUE, CallbackID.MONOFLOP_DONE),
159
+ 4: (CallbackID.EDGE_COUNT,),
160
+ }
155
161
 
156
162
  def __init__(self, uid: int, ipcon: IPConnectionAsync) -> None:
157
163
  """
@@ -61,6 +61,7 @@ class DeviceIdentifier(Enum):
61
61
  BRICKLET_IO_4_V2 = 2111
62
62
  BRICKLET_TEMPERATURE_V2 = 2113
63
63
  BRICKLET_BAROMETER_V2 = 2117
64
+ BRICKLET_INDUSTRIAL_DUAL_0_20MA_V2 = 2120
64
65
  BRICKLET_INDUSTRIAL_DUAL_ANALOG_IN_V2 = 2121
65
66
  BRICKLET_ISOLATOR = 2122
66
67
  BRICKLET_AMBIENT_LIGHT_V3 = 2131
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: tinkerforge-async
3
- Version: 1.6.1
3
+ Version: 1.7.0
4
4
  Summary: Python3 AsyncIO Tinkerforge driver
5
5
  Author-email: Patrick Baus <patrick.baus@physik.tu-darmstadt.de>
6
6
  License-Expression: GPL-3.0-or-later
@@ -13,6 +13,7 @@ tinkerforge_async/bricklet_barometer.py
13
13
  tinkerforge_async/bricklet_barometer_v2.py
14
14
  tinkerforge_async/bricklet_humidity.py
15
15
  tinkerforge_async/bricklet_humidity_v2.py
16
+ tinkerforge_async/bricklet_industrial_dual_0_20ma_v2.py
16
17
  tinkerforge_async/bricklet_industrial_dual_analog_in_v2.py
17
18
  tinkerforge_async/bricklet_industrial_ptc.py
18
19
  tinkerforge_async/bricklet_io16.py