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