]> Repositories - Adafruit_Blinka-hackapet.git/blobdiff - src/adafruit_blinka/microcontroller/generic_linux/libgpiod_pin.py
Fix libgpiod 2.x library where self._num is not defined in certain instances
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / generic_linux / libgpiod_pin.py
index 396a94c63e3510a61794281c984a42eabf7df1f3..831bc1a47ad3e6bd0149039a98d0bb818bae7633 100644 (file)
@@ -1,74 +1,22 @@
+# SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
+#
+# SPDX-License-Identifier: MIT
+"""A Pin class for use with libgpiod."""
 try:
     import gpiod
 except ImportError:
-    raise ImportError("libgpiod Python bindings not found, please install and try again! See https://github.com/adafruit/Raspberry-Pi-Installer-Scripts/blob/master/libgpiod.sh")
-
-# Pins dont exist in CPython so...lets make our own!
-class Pin:
-    IN = 0
-    OUT = 1
-    LOW = 0
-    HIGH = 1
-    PULL_NONE = 0
-    PULL_UP = 1
-    PULL_DOWN = 2
-    _CONSUMER = 'adafruit_blinka'
-
-    id = None
-    _value = LOW
-    _mode = IN
-
-    def __init__(self, pin_number, gpiod_chipname="gpiochip0"):
-        self.id = int(pin_number)
-        # FIXME: Presumably this might vary by system:
-        self._chip = gpiod.Chip(gpiod_chipname, gpiod.Chip.OPEN_BY_NAME)
-        self._line = None
-
-    def __repr__(self):
-        return str(self.id)
-
-    def __eq__(self, other):
-        return self.id == other
-
-    def init(self, mode=IN, pull=None):
-        if not self._line:
-            self._line = self._chip.get_line(int(self.id))
-            #print("init line: ", int(self.id), self._line)
-
-        if mode != None:
-            if mode == self.IN:
-                flags = 0
-                if pull != None:
-                    if pull == self.PULL_UP:
-                        raise NotImplementedError("Internal pullups not supported in libgpiod, use physical resistor instead!")
-                    elif pull == self.PULL_DOWN:
-                        raise NotImplementedError("Internal pulldowns not supported in libgpiod, use physical resistor instead!")                    
-                    else:
-                        raise RuntimeError("Invalid pull for pin: %s" % self.id)
-
-                self._mode = self.IN
-                self._line.release()
-                self._line.request(consumer=self._CONSUMER,
-                                   type=gpiod.LINE_REQ_DIR_IN,
-                                   flags=flags)
-
-            elif mode == self.OUT:
-                if pull != None:
-                    raise RuntimeError("Cannot set pull resistor on output")
-                self._mode = self.OUT
-                self._line.release()
-                self._line.request(consumer=self._CONSUMER,
-                                   type=gpiod.LINE_REQ_DIR_OUT)
-
-            else:
-                raise RuntimeError("Invalid mode for pin: %s" % self.id)
-
-    def value(self, val=None):
-        if val != None:
-            if val in (self.LOW, self.HIGH):
-                self._value = val
-                self._line.set_value(val)
-            else:
-                raise RuntimeError("Invalid value for pin")
-        else:
-            return self._line.get_value()
+    raise ImportError(
+        "libgpiod Python bindings not found, please install and try again! See "
+        "https://github.com/adafruit/Raspberry-Pi-Installer-Scripts/blob/main/libgpiod.py"
+    ) from ImportError
+
+# Versions 1.5.4 and earlier have no __version__ attribute
+if hasattr(gpiod, "__version__"):
+    version = gpiod.__version__
+else:
+    version = "1.x"
+
+if version.startswith("1."):
+    from .libgpiod.libgpiod_pin_1_x import Pin  # pylint: disable=unused-import
+else:
+    from .libgpiod.libgpiod_pin_2_x import Pin  # pylint: disable=unused-import