1 """A Pin class for use with libgpiod."""
6 "libgpiod Python bindings not found, please install and try again! See "
7 "https://github.com/adafruit/Raspberry-Pi-Installer-Scripts/blob/master/libgpiod.sh"
12 """Pins dont exist in CPython so...lets make our own!"""
21 _CONSUMER = "adafruit_blinka"
27 def __init__(self, pin_id):
29 if isinstance(pin_id, tuple):
30 self._num = int(pin_id[1])
31 self._chip = gpiod.Chip(str(pin_id[0]), gpiod.Chip.OPEN_BY_NUMBER)
33 self._num = int(pin_id)
34 self._chip = gpiod.Chip("gpiochip0", gpiod.Chip.OPEN_BY_NAME)
40 def __eq__(self, other):
41 return self.id == other
43 def init(self, mode=IN, pull=None):
44 """Initialize the Pin"""
46 self._line = self._chip.get_line(int(self._num))
47 # print("init line: ", self.id, self._line)
53 if pull == self.PULL_UP:
54 raise NotImplementedError(
55 "Internal pullups not supported in libgpiod, "
56 "use physical resistor instead!"
58 if pull == self.PULL_DOWN:
59 raise NotImplementedError(
60 "Internal pulldowns not supported in libgpiod, "
61 "use physical resistor instead!"
63 raise RuntimeError("Invalid pull for pin: %s" % self.id)
68 consumer=self._CONSUMER, type=gpiod.LINE_REQ_DIR_IN, flags=flags
71 elif mode == self.OUT:
73 raise RuntimeError("Cannot set pull resistor on output")
76 self._line.request(consumer=self._CONSUMER, type=gpiod.LINE_REQ_DIR_OUT)
79 raise RuntimeError("Invalid mode for pin: %s" % self.id)
81 def value(self, val=None):
82 """Set or return the Pin Value"""
84 return self._line.get_value()
86 if val in (self.LOW, self.HIGH):
88 self._line.set_value(val)
90 raise RuntimeError("Invalid value for pin")