]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/generic_linux/libgpiod_pin.py
Merge pull request #562 from fivetide/usb_hid
[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
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             self._chip = gpiod.Chip(str(pin_id[0]), gpiod.Chip.OPEN_BY_NUMBER)
35         else:
36             self._num = int(pin_id)
37             self._chip = gpiod.Chip("gpiochip0", gpiod.Chip.OPEN_BY_NAME)
38         self._line = None
39
40     def __repr__(self):
41         return str(self.id)
42
43     def __eq__(self, other):
44         return self.id == other
45
46     def init(self, mode=IN, pull=None):
47         """Initialize the Pin"""
48         if not self._line:
49             self._line = self._chip.get_line(int(self._num))
50             # print("init line: ", self.id, self._line)
51
52         if mode is not None:
53             if mode == self.IN:
54                 flags = 0
55                 if pull is not None:
56                     if pull == self.PULL_UP:
57                         raise NotImplementedError(
58                             "Internal pullups not supported in libgpiod, "
59                             "use physical resistor instead!"
60                         )
61                     if pull == self.PULL_DOWN:
62                         raise NotImplementedError(
63                             "Internal pulldowns not supported in libgpiod, "
64                             "use physical resistor instead!"
65                         )
66                     raise RuntimeError("Invalid pull for pin: %s" % self.id)
67
68                 self._mode = self.IN
69                 self._line.release()
70                 self._line.request(
71                     consumer=self._CONSUMER, type=gpiod.LINE_REQ_DIR_IN, flags=flags
72                 )
73
74             elif mode == self.OUT:
75                 if pull is not None:
76                     raise RuntimeError("Cannot set pull resistor on output")
77                 self._mode = self.OUT
78                 self._line.release()
79                 self._line.request(consumer=self._CONSUMER, type=gpiod.LINE_REQ_DIR_OUT)
80
81             else:
82                 raise RuntimeError("Invalid mode for pin: %s" % self.id)
83
84     def value(self, val=None):
85         """Set or return the Pin Value"""
86         if val is None:
87             return self._line.get_value()
88
89         if val in (self.LOW, self.HIGH):
90             self._value = val
91             self._line.set_value(val)
92             return None
93         raise RuntimeError("Invalid value for pin")