4 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")
6 # Pins dont exist in CPython so...lets make our own!
15 _CONSUMER = 'adafruit_blinka'
21 def __init__(self, pin_id):
23 if type(pin_id) is tuple:
24 self._num = int(pin_id[1])
25 self._chip = gpiod.Chip(str(pin_id[0]), gpiod.Chip.OPEN_BY_NUMBER)
27 self._num = int(pin_id)
28 self._chip = gpiod.Chip("gpiochip0", gpiod.Chip.OPEN_BY_NAME)
34 def __eq__(self, other):
35 return self.id == other
37 def init(self, mode=IN, pull=None):
39 self._line = self._chip.get_line(int(self._num))
40 #print("init line: ", self.id, self._line)
46 if pull == self.PULL_UP:
47 raise NotImplementedError("Internal pullups not supported in libgpiod, use physical resistor instead!")
48 elif pull == self.PULL_DOWN:
49 raise NotImplementedError("Internal pulldowns not supported in libgpiod, use physical resistor instead!")
51 raise RuntimeError("Invalid pull for pin: %s" % self.id)
55 self._line.request(consumer=self._CONSUMER,
56 type=gpiod.LINE_REQ_DIR_IN,
59 elif mode == self.OUT:
61 raise RuntimeError("Cannot set pull resistor on output")
64 self._line.request(consumer=self._CONSUMER,
65 type=gpiod.LINE_REQ_DIR_OUT)
68 raise RuntimeError("Invalid mode for pin: %s" % self.id)
70 def value(self, val=None):
72 if val in (self.LOW, self.HIGH):
74 self._line.set_value(val)
76 raise RuntimeError("Invalid value for pin")
78 return self._line.get_value()