1 """A Pin class for use with periphery."""
 
   3     from periphery import GPIO
 
   6         "Periphery Python bindings not found, please install and try again! "
 
   7         "Try running 'pip3 install python-periphery'"
 
  12     """Pins dont exist in CPython so...lets make our own!"""
 
  26     def __init__(self, pin_id):
 
  28         if isinstance(pin_id, tuple):
 
  29             self._num = int(pin_id[1])
 
  30             self._chippath = "/dev/gpiochip{}".format(pin_id[0])
 
  32             self._num = int(pin_id)
 
  33             self._chippath = "/dev/gpiochip0"
 
  39     def __eq__(self, other):
 
  40         return self.id == other
 
  42     def init(self, mode=IN, pull=None):
 
  43         """Initialize the Pin"""
 
  47                 if self._line is not None:
 
  49                 self._line = GPIO(self._chippath, int(self._num), self.IN)
 
  50             elif mode == self.OUT:
 
  52                 if self._line is not None:
 
  54                 self._line = GPIO(self._chippath, int(self._num), self.OUT)
 
  56                 raise RuntimeError("Invalid mode for pin: %s" % self.id)
 
  59                 if pull == self.PULL_UP:
 
  60                     raise NotImplementedError(
 
  61                         "Internal pullups not supported in periphery, "
 
  62                         "use physical resistor instead!"
 
  64                 if pull == self.PULL_DOWN:
 
  65                     raise NotImplementedError(
 
  66                         "Internal pulldowns not supported in periphery, "
 
  67                         "use physical resistor instead!"
 
  69                 raise RuntimeError("Invalid pull for pin: %s" % self.id)
 
  71     def value(self, val=None):
 
  72         """Set or return the Pin Value"""
 
  76                 self._line.write(False)
 
  80                 self._line.write(True)
 
  82             raise RuntimeError("Invalid value for pin")
 
  83         return self.HIGH if self._line.read() else self.LOW