aboutsummaryrefslogtreecommitdiffstats
path: root/mpm/python/usrp_mpm
diff options
context:
space:
mode:
authorMichael Auchter <michael.auchter@ni.com>2020-01-29 14:02:59 -0600
committerAaron Rossetto <aaron.rossetto@ni.com>2021-06-03 09:34:55 -0500
commit15b94c53edc86031c74b27cd3d824d2291c0c91f (patch)
tree1e57c3e87262b8d156336d8c28b6f90e86eda1d5 /mpm/python/usrp_mpm
parent60a147a6a16ced611ab1a7dfdb233d3bd01a0d65 (diff)
downloaduhd-15b94c53edc86031c74b27cd3d824d2291c0c91f.tar.gz
uhd-15b94c53edc86031c74b27cd3d824d2291c0c91f.tar.bz2
uhd-15b94c53edc86031c74b27cd3d824d2291c0c91f.zip
mpm: sys_utils: add libgpiod-based Gpio helper
This adds a new Gpio helper class, which uses libgpiod under the hood instead of the deprecated sysfs GPIO access. This class provides the ability to get/set a specific GPIO, which is looked up by name.
Diffstat (limited to 'mpm/python/usrp_mpm')
-rw-r--r--mpm/python/usrp_mpm/sys_utils/gpio.py60
1 files changed, 60 insertions, 0 deletions
diff --git a/mpm/python/usrp_mpm/sys_utils/gpio.py b/mpm/python/usrp_mpm/sys_utils/gpio.py
new file mode 100644
index 000000000..b609479f1
--- /dev/null
+++ b/mpm/python/usrp_mpm/sys_utils/gpio.py
@@ -0,0 +1,60 @@
+#
+# Copyright 2020 Ettus Research, a National Instruments Brand
+#
+# SPDX-License-Identifier: GPL-3.0-or-later
+#
+"""
+Access to GPIOs via libgpiod
+"""
+
+import contextlib
+import gpiod
+
+
+@contextlib.contextmanager
+def request_gpio(line, direction):
+ """
+ Context manager for a GPIO line
+ """
+ line.request(consumer='mpm', type=direction)
+ try:
+ yield line
+ finally:
+ line.release()
+
+
+class Gpio:
+ """
+ Class for accessing a named GPIO line via libgpiod
+ """
+
+ INPUT = gpiod.LINE_REQ_DIR_IN
+ OUTPUT = gpiod.LINE_REQ_DIR_OUT
+
+ def __init__(self, name, direction=INPUT, default_val=None):
+ self._direction = direction
+ self._line = gpiod.find_line(name)
+ self._out_value = False
+ if self._line is None:
+ raise RuntimeError('failed to find gpio with name %s' % name)
+
+ if default_val is not None and direction == Gpio.OUTPUT:
+ self.set(default_val)
+
+ def get(self):
+ """
+ Read the value of this GPIO
+ """
+ if self._direction == self.OUTPUT:
+ return self._out_value
+
+ with request_gpio(self._line, self._direction) as gpio:
+ return bool(gpio.get_value())
+
+ def set(self, value):
+ """
+ Set the value of this GPIO
+ """
+ with request_gpio(self._line, self._direction) as gpio:
+ gpio.set_value(int(value))
+ self._out_value = bool(value)