]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/generic_linux/libgpiod_pin.py
Add Banana Pi M5 support to board.py
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / generic_linux / libgpiod_pin.py
1 # SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
2 #
3 # SPDX-License-Identifier: MIT
4 """A Pin class for use with libgpiod."""
5 try:
6     import gpiod
7 except ImportError:
8     raise ImportError(
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"
11     ) from ImportError
12
13 # pylint: disable=too-many-branches,too-many-statements
14 class Pin:
15     """Pins dont exist in CPython so...lets make our own!"""
16
17     IN = 0
18     OUT = 1
19     LOW = 0
20     HIGH = 1
21     PULL_NONE = 0
22     PULL_UP = 1
23     PULL_DOWN = 2
24     _CONSUMER = "adafruit_blinka"
25
26     id = None
27     _value = LOW
28     _mode = IN
29
30     def __init__(self, pin_id):
31         self.id = pin_id
32         if isinstance(pin_id, tuple):
33             self._num = int(pin_id[1])
34             if hasattr(gpiod, "Chip"):
35                 self._chip = gpiod.Chip(str(pin_id[0]), gpiod.Chip.OPEN_BY_NUMBER)
36             else:
37                 self._chip = gpiod.chip(str(pin_id[0]), gpiod.chip.OPEN_BY_NUMBER)
38         else:
39             self._num = int(pin_id)
40             if hasattr(gpiod, "Chip"):
41                 self._chip = gpiod.Chip("gpiochip0", gpiod.Chip.OPEN_BY_NAME)
42             else:
43                 self._chip = gpiod.chip("gpiochip0", gpiod.chip.OPEN_BY_NAME)
44         self._line = None
45
46     def __repr__(self):
47         return str(self.id)
48
49     def __eq__(self, other):
50         return self.id == other
51
52     def init(self, mode=IN, pull=None):
53         """Initialize the Pin"""
54         if not self._line:
55             self._line = self._chip.get_line(int(self._num))
56             # print("init line: ", self.id, self._line)
57
58         if mode is not None:
59             if mode == self.IN:
60                 flags = 0
61                 self._line.release()
62                 if pull is not None:
63                     if pull == self.PULL_UP:
64                         if hasattr(gpiod, "line") and hasattr(
65                             gpiod.line, "BIAS_PULL_UP"
66                         ):
67                             config = gpiod.line_request()
68                             config.consumer = self._CONSUMER
69                             config.request_type = gpiod.line.BIAS_PULL_UP
70                             self._line.request(config)
71                         else:
72                             self._line.request(
73                                 consumer=self._CONSUMER,
74                                 type=gpiod.LINE_REQ_DIR_IN,
75                                 flags=flags,
76                             )
77                             raise NotImplementedError(
78                                 "Internal pullups not supported in this version of libgpiod, "
79                                 "use physical resistor instead!"
80                             )
81                     elif pull == self.PULL_DOWN:
82                         if hasattr(gpiod, "line") and hasattr(
83                             gpiod.line, "BIAS_PULL_DOWN"
84                         ):
85                             config = gpiod.line_request()
86                             config.consumer = self._CONSUMER
87                             config.request_type = gpiod.line.BIAS_PULL_DOWN
88                             self._line.request(config)
89                         else:
90                             raise NotImplementedError(
91                                 "Internal pulldowns not supported in this version of libgpiod, "
92                                 "use physical resistor instead!"
93                             )
94                     elif pull == self.PULL_NONE:
95                         if hasattr(gpiod, "line") and hasattr(
96                             gpiod.line, "BIAS_DISABLE"
97                         ):
98                             config = gpiod.line_request()
99                             config.consumer = self._CONSUMER
100                             config.request_type = gpiod.line.BIAS_DISABLE
101                             self._line.request(config)
102                     else:
103                         raise RuntimeError(f"Invalid pull for pin: {self.id}")
104
105                 self._mode = self.IN
106                 self._line.release()
107                 if hasattr(gpiod, "LINE_REQ_DIR_IN"):
108                     self._line.request(
109                         consumer=self._CONSUMER, type=gpiod.LINE_REQ_DIR_IN, flags=flags
110                     )
111                 else:
112                     config = gpiod.line_request()
113                     config.consumer = self._CONSUMER
114                     config.request_type = gpiod.line_request.DIRECTION_INPUT
115                     self._line.request(config)
116
117             elif mode == self.OUT:
118                 if pull is not None:
119                     raise RuntimeError("Cannot set pull resistor on output")
120                 self._mode = self.OUT
121                 self._line.release()
122                 if hasattr(gpiod, "LINE_REQ_DIR_OUT"):
123                     self._line.request(
124                         consumer=self._CONSUMER, type=gpiod.LINE_REQ_DIR_OUT
125                     )
126                 else:
127                     config = gpiod.line_request()
128                     config.consumer = self._CONSUMER
129                     config.request_type = gpiod.line_request.DIRECTION_OUTPUT
130                     self._line.request(config)
131             else:
132                 raise RuntimeError("Invalid mode for pin: %s" % self.id)
133
134     def value(self, val=None):
135         """Set or return the Pin Value"""
136         if val is None:
137             return self._line.get_value()
138
139         if val in (self.LOW, self.HIGH):
140             self._value = val
141             self._line.set_value(val)
142             return None
143         raise RuntimeError("Invalid value for pin")