-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathcontext_manager.py
51 lines (43 loc) · 1.38 KB
/
context_manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Copyright (C) 2019-2025 Analog Devices, Inc.
#
# SPDX short identifier: ADIBSD
import iio
class context_manager(object):
_uri_auto = "ip:analog"
_ctx = None
@property
def ctx(self) -> iio.Context:
"""IIO Context"""
return self._ctx
def __init__(self, uri="", _device_name=""):
if self._ctx:
return
self.uri = uri
try:
if self.uri == "":
# Try USB contexts first
if _device_name != "":
contexts = iio.scan_contexts()
for c in contexts:
if _device_name in contexts[c]:
self._ctx = iio.Context(c)
break
# Try auto discover
if not self._ctx and self._uri_auto != "":
self._ctx = iio.Context(self._uri_auto)
if not self._ctx:
raise Exception("No device found")
else:
self._ctx = iio.Context(self.uri)
except BaseException:
raise Exception("No device found")
def close(self):
"""Close the IIO context"""
if self._ctx:
del self._ctx
self._ctx = None
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
self.close()
return False