1 # SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
3 # SPDX-License-Identifier: MIT
4 """A Pin class for use with libgpiod."""
9 "libgpiod Python bindings not found, please install and try again! See "
10 "https://github.com/adafruit/Raspberry-Pi-Installer-Scripts/blob/master/libgpiod.sh"
14 # pylint: disable=too-many-branches,too-many-statements
16 """Pins dont exist in CPython so...lets make our own!"""
25 _CONSUMER = "adafruit_blinka"
31 def __init__(self, pin_id):
33 if isinstance(pin_id, tuple):
34 self._num = int(pin_id[1])
35 if hasattr(gpiod, "Chip"):
36 self._chip = gpiod.Chip(str(pin_id[0]), gpiod.Chip.OPEN_BY_NUMBER)
38 self._chip = gpiod.chip(str(pin_id[0]), gpiod.chip.OPEN_BY_NUMBER)
40 self._num = int(pin_id)
41 if hasattr(gpiod, "Chip"):
42 self._chip = gpiod.Chip("gpiochip0", gpiod.Chip.OPEN_BY_NAME)
44 self._chip = gpiod.chip("gpiochip0", gpiod.chip.OPEN_BY_NAME)
50 def __eq__(self, other):
51 return self.id == other
53 def init(self, mode=IN, pull=None):
54 """Initialize the Pin"""
56 self._line = self._chip.get_line(int(self._num))
57 # print("init line: ", self.id, self._line)
64 if pull == self.PULL_UP:
65 if hasattr(gpiod, "line") and hasattr(
66 gpiod.line, "BIAS_PULL_UP"
68 config = gpiod.line_request()
69 config.consumer = self._CONSUMER
70 config.request_type = gpiod.line.BIAS_PULL_UP
71 self._line.request(config)
74 consumer=self._CONSUMER,
75 type=gpiod.LINE_REQ_DIR_IN,
78 raise NotImplementedError(
79 "Internal pullups not supported in this version of libgpiod, "
80 "use physical resistor instead!"
82 elif pull == self.PULL_DOWN:
83 if hasattr(gpiod, "line") and hasattr(
84 gpiod.line, "BIAS_PULL_DOWN"
86 config = gpiod.line_request()
87 config.consumer = self._CONSUMER
88 config.request_type = gpiod.line.BIAS_PULL_DOWN
89 self._line.request(config)
91 raise NotImplementedError(
92 "Internal pulldowns not supported in this version of libgpiod, "
93 "use physical resistor instead!"
95 elif pull == self.PULL_NONE:
96 if hasattr(gpiod, "line") and hasattr(
97 gpiod.line, "BIAS_DISABLE"
99 config = gpiod.line_request()
100 config.consumer = self._CONSUMER
101 config.request_type = gpiod.line.BIAS_DISABLE
102 self._line.request(config)
104 raise RuntimeError(f"Invalid pull for pin: {self.id}")
108 if hasattr(gpiod, "LINE_REQ_DIR_IN"):
110 consumer=self._CONSUMER, type=gpiod.LINE_REQ_DIR_IN, flags=flags
113 config = gpiod.line_request()
114 config.consumer = self._CONSUMER
115 config.request_type = gpiod.line_request.DIRECTION_INPUT
116 self._line.request(config)
118 elif mode == self.OUT:
120 raise RuntimeError("Cannot set pull resistor on output")
121 self._mode = self.OUT
123 if hasattr(gpiod, "LINE_REQ_DIR_OUT"):
125 consumer=self._CONSUMER, type=gpiod.LINE_REQ_DIR_OUT
128 config = gpiod.line_request()
129 config.consumer = self._CONSUMER
130 config.request_type = gpiod.line_request.DIRECTION_OUTPUT
131 self._line.request(config)
133 raise RuntimeError("Invalid mode for pin: %s" % self.id)
135 def value(self, val=None):
136 """Set or return the Pin Value"""
138 return self._line.get_value()
140 if val in (self.LOW, self.HIGH):
142 self._line.set_value(val)
144 raise RuntimeError("Invalid value for pin")