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