aboutsummaryrefslogtreecommitdiffstats
path: root/mpm/python
diff options
context:
space:
mode:
authorMartin Braun <martin.braun@ettus.com>2017-10-18 13:18:07 -0700
committerMartin Braun <martin.braun@ettus.com>2017-12-22 15:04:02 -0800
commit99c5b6fff8426dd9f2fb8f0d26b12fc9405697f4 (patch)
tree4f48a485d16eab525c0c1f1a939110f0b3c36146 /mpm/python
parent219b04e99dceb075a91655ddc4d16d06a968db77 (diff)
downloaduhd-99c5b6fff8426dd9f2fb8f0d26b12fc9405697f4.tar.gz
uhd-99c5b6fff8426dd9f2fb8f0d26b12fc9405697f4.tar.bz2
uhd-99c5b6fff8426dd9f2fb8f0d26b12fc9405697f4.zip
mpm: Add mpmutils module, includes poll_with_timeout
This is a convenience function for polling with sleeps and timeouts.
Diffstat (limited to 'mpm/python')
-rw-r--r--mpm/python/usrp_mpm/CMakeLists.txt5
-rw-r--r--mpm/python/usrp_mpm/mpmutils.py38
2 files changed, 41 insertions, 2 deletions
diff --git a/mpm/python/usrp_mpm/CMakeLists.txt b/mpm/python/usrp_mpm/CMakeLists.txt
index a90844956..ba640f7cc 100644
--- a/mpm/python/usrp_mpm/CMakeLists.txt
+++ b/mpm/python/usrp_mpm/CMakeLists.txt
@@ -27,11 +27,12 @@ SET(USRP_MPM_TOP_FILES
${CMAKE_CURRENT_SOURCE_DIR}/dtoverlay.py
${CMAKE_CURRENT_SOURCE_DIR}/eeprom.py
${CMAKE_CURRENT_SOURCE_DIR}/mpmlog.py
+ ${CMAKE_CURRENT_SOURCE_DIR}/mpmtypes.py
+ ${CMAKE_CURRENT_SOURCE_DIR}/mpmutils.py
+ ${CMAKE_CURRENT_SOURCE_DIR}/net.py
${CMAKE_CURRENT_SOURCE_DIR}/nijesdcore.py
${CMAKE_CURRENT_SOURCE_DIR}/rpc_server.py
${CMAKE_CURRENT_SOURCE_DIR}/sysfs_gpio.py
- ${CMAKE_CURRENT_SOURCE_DIR}/mpmtypes.py
- ${CMAKE_CURRENT_SOURCE_DIR}/net.py
${CMAKE_CURRENT_SOURCE_DIR}/uio.py
)
LIST(APPEND USRP_MPM_FILES ${USRP_MPM_TOP_FILES})
diff --git a/mpm/python/usrp_mpm/mpmutils.py b/mpm/python/usrp_mpm/mpmutils.py
new file mode 100644
index 000000000..ad4457419
--- /dev/null
+++ b/mpm/python/usrp_mpm/mpmutils.py
@@ -0,0 +1,38 @@
+#
+# Copyright 2017 Ettus Research, National Instruments Company
+#
+# SPDX-License-Identifier: GPL-3.0
+#
+"""
+Miscellaneous utilities for MPM
+"""
+
+import time
+
+def poll_with_timeout(state_check, timeout_ms, interval_ms):
+ """
+ Calls state_check() every interval_ms until it returns a positive value, or
+ until a timeout is exceeded.
+
+ Returns True if state_check() returned True within the timeout.
+
+ Arguments:
+ state_check -- Functor that returns a Boolean success value, and takes no
+ arguments.
+ timeout_ms -- The total timeout in milliseconds. state_check() has to
+ return True within this time.
+ interval_ms -- Sleep time between calls to state_check(). Note that if
+ interval_ms is larger than timeout_ms, state_check() will be
+ called exactly once, and then poll_with_timeout() will still
+ sleep for interval_ms milliseconds. Typically, interval_ms
+ should be chosen much smaller than timeout_ms, but not too
+ small for this to become a busy loop.
+ """
+ max_time = time.time() + (float(timeout_ms) / 1000)
+ interval_s = float(interval_ms) / 1000
+ while time.time() < max_time:
+ if state_check():
+ return True
+ time.sleep(interval_s)
+ return False
+