]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/generic_linux/periphery_pin.py
Revert back, because i have an older version of libgpiod.
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / generic_linux / periphery_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 periphery."""
5 try:
6     from periphery import GPIO
7 except ImportError:
8     raise ImportError(
9         "Periphery Python bindings not found, please install and try again! "
10         "Try running 'pip3 install python-periphery'"
11     ) from ImportError
12
13
14 class Pin:
15     """Pins dont exist in CPython so...lets make our own!"""
16
17     IN = "in"
18     OUT = "out"
19     LOW = 0
20     HIGH = 1
21     PULL_NONE = 0
22     PULL_UP = 1
23     PULL_DOWN = 2
24
25     id = None
26     _value = LOW
27     _mode = IN
28
29     def __init__(self, pin_id):
30         self.id = pin_id
31         if isinstance(pin_id, tuple):
32             self._num = int(pin_id[1])
33             self._chippath = "/dev/gpiochip{}".format(pin_id[0])
34         else:
35             self._num = int(pin_id)
36             self._chippath = "/dev/gpiochip0"
37         self._line = None
38
39     def __repr__(self):
40         return str(self.id)
41
42     def __eq__(self, other):
43         return self.id == other
44
45     def init(self, mode=IN, pull=None):
46         """Initialize the Pin"""
47         if mode is not None:
48             if mode == self.IN:
49                 self._mode = self.IN
50                 if self._line is not None:
51                     self._line.close()
52                 self._line = GPIO(self._chippath, int(self._num), self.IN)
53             elif mode == self.OUT:
54                 self._mode = self.OUT
55                 if self._line is not None:
56                     self._line.close()
57                 self._line = GPIO(self._chippath, int(self._num), self.OUT)
58             else:
59                 raise RuntimeError("Invalid mode for pin: %s" % self.id)
60
61             if pull is not None:
62                 if pull == self.PULL_UP:
63                     raise NotImplementedError(
64                         "Internal pullups not supported in periphery, "
65                         "use physical resistor instead!"
66                     )
67                 if pull == self.PULL_DOWN:
68                     raise NotImplementedError(
69                         "Internal pulldowns not supported in periphery, "
70                         "use physical resistor instead!"
71                     )
72                 raise RuntimeError("Invalid pull for pin: %s" % self.id)
73
74     def value(self, val=None):
75         """Set or return the Pin Value"""
76         if val is not None:
77             if val == self.LOW:
78                 self._value = val
79                 self._line.write(False)
80                 return None
81             if val == self.HIGH:
82                 self._value = val
83                 self._line.write(True)
84                 return None
85             raise RuntimeError("Invalid value for pin")
86         return self.HIGH if self._line.read() else self.LOW