1 # SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
 
   3 # SPDX-License-Identifier: MIT
 
   4 """A Pin class for use with periphery."""
 
   6     from periphery import GPIO
 
   9         "Periphery Python bindings not found, please install and try again! "
 
  10         "Try running 'pip3 install python-periphery'"
 
  15     """Pins dont exist in CPython so...lets make our own!"""
 
  29     def __init__(self, pin_id):
 
  31         if isinstance(pin_id, tuple):
 
  32             self._num = int(pin_id[1])
 
  33             self._chippath = "/dev/gpiochip{}".format(pin_id[0])
 
  35             self._num = int(pin_id)
 
  36             self._chippath = "/dev/gpiochip0"
 
  42     def __eq__(self, other):
 
  43         return self.id == other
 
  45     def init(self, mode=IN, pull=None):
 
  46         """Initialize the Pin"""
 
  50                 if self._line is not None:
 
  52                 self._line = GPIO(self._chippath, int(self._num), self.IN)
 
  53             elif mode == self.OUT:
 
  55                 if self._line is not None:
 
  57                 self._line = GPIO(self._chippath, int(self._num), self.OUT)
 
  59                 raise RuntimeError("Invalid mode for pin: %s" % self.id)
 
  62                 if pull == self.PULL_UP:
 
  63                     raise NotImplementedError(
 
  64                         "Internal pullups not supported in periphery, "
 
  65                         "use physical resistor instead!"
 
  67                 if pull == self.PULL_DOWN:
 
  68                     raise NotImplementedError(
 
  69                         "Internal pulldowns not supported in periphery, "
 
  70                         "use physical resistor instead!"
 
  72                 raise RuntimeError("Invalid pull for pin: %s" % self.id)
 
  74     def value(self, val=None):
 
  75         """Set or return the Pin Value"""
 
  79                 self._line.write(False)
 
  83                 self._line.write(True)
 
  85             raise RuntimeError("Invalid value for pin")
 
  86         return self.HIGH if self._line.read() else self.LOW