]> Repositories - Adafruit_Blinka-hackapet.git/blobdiff - src/adafruit_blinka/microcontroller/generic_linux/libgpiod_pin.py
Merge pull request #276 from makermelissa/master
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / generic_linux / libgpiod_pin.py
index 0d023082abb2ca1c4e22d90b52b8866db41424ce..4ffa5c1725da19bf4e2d049e02940f7da1e527fd 100644 (file)
@@ -1,10 +1,16 @@
+"""A Pin class for use with libgpiod."""
 try:
     import gpiod
 except ImportError:
 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")
+    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:
 class Pin:
+    """Pins dont exist in CPython so...lets make our own!"""
+
     IN = 0
     OUT = 1
     LOW = 0
     IN = 0
     OUT = 1
     LOW = 0
@@ -12,7 +18,7 @@ class Pin:
     PULL_NONE = 0
     PULL_UP = 1
     PULL_DOWN = 2
     PULL_NONE = 0
     PULL_UP = 1
     PULL_DOWN = 2
-    _CONSUMER = 'adafruit_blinka'
+    _CONSUMER = "adafruit_blinka"
 
     id = None
     _value = LOW
 
     id = None
     _value = LOW
@@ -20,7 +26,7 @@ class Pin:
 
     def __init__(self, pin_id):
         self.id = pin_id
 
     def __init__(self, pin_id):
         self.id = pin_id
-        if type(pin_id) is tuple:
+        if isinstance(pin_id, tuple):
             self._num = int(pin_id[1])
             self._chip = gpiod.Chip(str(pin_id[0]), gpiod.Chip.OPEN_BY_NUMBER)
         else:
             self._num = int(pin_id[1])
             self._chip = gpiod.Chip(str(pin_id[0]), gpiod.Chip.OPEN_BY_NUMBER)
         else:
@@ -35,44 +41,50 @@ class Pin:
         return self.id == other
 
     def init(self, mode=IN, pull=None):
         return self.id == other
 
     def init(self, mode=IN, pull=None):
+        """Initialize the Pin"""
         if not self._line:
             self._line = self._chip.get_line(int(self._num))
         if not self._line:
             self._line = self._chip.get_line(int(self._num))
-            #print("init line: ", self.id, self._line)
+            # print("init line: ", self.id, self._line)
 
 
-        if mode != None:
+        if mode is not None:
             if mode == self.IN:
                 flags = 0
             if mode == self.IN:
                 flags = 0
-                if pull != None:
+                if pull is not None:
                     if pull == self.PULL_UP:
                     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)
+                        raise NotImplementedError(
+                            "Internal pullups not supported in libgpiod, "
+                            "use physical resistor instead!"
+                        )
+                    if pull == self.PULL_DOWN:
+                        raise NotImplementedError(
+                            "Internal pulldowns not supported in libgpiod, "
+                            "use physical resistor instead!"
+                        )
+                    raise RuntimeError("Invalid pull for pin: %s" % self.id)
 
                 self._mode = self.IN
                 self._line.release()
 
                 self._mode = self.IN
                 self._line.release()
-                self._line.request(consumer=self._CONSUMER,
-                                   type=gpiod.LINE_REQ_DIR_IN,
-                                   flags=flags)
+                self._line.request(
+                    consumer=self._CONSUMER, type=gpiod.LINE_REQ_DIR_IN, flags=flags
+                )
 
             elif mode == self.OUT:
 
             elif mode == self.OUT:
-                if pull != None:
+                if pull is not None:
                     raise RuntimeError("Cannot set pull resistor on output")
                 self._mode = self.OUT
                 self._line.release()
                     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)
+                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):
 
             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:
+        """Set or return the Pin Value"""
+        if val is None:
             return self._line.get_value()
             return self._line.get_value()
+
+        if val in (self.LOW, self.HIGH):
+            self._value = val
+            self._line.set_value(val)
+            return None
+        raise RuntimeError("Invalid value for pin")