]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/generic_linux/libgpiod_pin.py
Merge pull request #268 from makermelissa/master
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / generic_linux / libgpiod_pin.py
1 try:
2     import gpiod
3 except ImportError:
4     raise ImportError("libgpiod Python bindings not found, please install and try again! See https://github.com/adafruit/Raspberry-Pi-Installer-Scripts/blob/master/libgpiod.sh")
5
6 # Pins dont exist in CPython so...lets make our own!
7 class Pin:
8     IN = 0
9     OUT = 1
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._chip = gpiod.Chip(str(pin_id[0]), gpiod.Chip.OPEN_BY_NUMBER)
26         else:
27             self._num = int(pin_id)
28             self._chip = gpiod.Chip("gpiochip0", gpiod.Chip.OPEN_BY_NAME)
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 not self._line:
39             self._line = self._chip.get_line(int(self._num))
40             #print("init line: ", self.id, self._line)
41
42         if mode != None:
43             if mode == self.IN:
44                 flags = 0
45                 if pull != None:
46                     if pull == self.PULL_UP:
47                         raise NotImplementedError("Internal pullups not supported in libgpiod, use physical resistor instead!")
48                     elif pull == self.PULL_DOWN:
49                         raise NotImplementedError("Internal pulldowns not supported in libgpiod, use physical resistor instead!")                    
50                     else:
51                         raise RuntimeError("Invalid pull for pin: %s" % self.id)
52
53                 self._mode = self.IN
54                 self._line.release()
55                 self._line.request(consumer=self._CONSUMER,
56                                    type=gpiod.LINE_REQ_DIR_IN,
57                                    flags=flags)
58
59             elif mode == self.OUT:
60                 if pull != None:
61                     raise RuntimeError("Cannot set pull resistor on output")
62                 self._mode = self.OUT
63                 self._line.release()
64                 self._line.request(consumer=self._CONSUMER,
65                                    type=gpiod.LINE_REQ_DIR_OUT)
66
67             else:
68                 raise RuntimeError("Invalid mode for pin: %s" % self.id)
69
70     def value(self, val=None):
71         if val != None:
72             if val in (self.LOW, self.HIGH):
73                 self._value = val
74                 self._line.set_value(val)
75             else:
76                 raise RuntimeError("Invalid value for pin")
77         else:
78             return self._line.get_value()