]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/digitalio.py
jetson integration
[Adafruit_Blinka-hackapet.git] / src / digitalio.py
1 """
2 `digitalio` - Digital input and output control
3 =================================================
4
5 See `CircuitPython:digitalio` in CircuitPython for more details.
6
7 * Author(s): cefn
8 """
9
10 from adafruit_blinka.agnostic import board_id, detector
11
12 # pylint: disable=ungrouped-imports,wrong-import-position
13
14 if detector.chip.BCM2XXX:
15     from adafruit_blinka.microcontroller.bcm283x.pin import Pin
16 elif detector.chip.AM33XX:
17     from adafruit_blinka.microcontroller.am335x.pin import Pin
18 elif detector.chip.SUN8I:
19     from adafruit_blinka.microcontroller.allwinner_h3.pin import Pin
20 elif detector.chip.SAMA5:
21     from adafruit_blinka.microcontroller.sama5.pin import Pin
22 elif detector.chip.TEGRAXXX:
23     from adafruit_blinka.microcontroller.tegra.pin import Pin
24 elif detector.chip.STM32:
25     from machine import Pin
26 from adafruit_blinka import Enum, ContextManaged
27
28 class DriveMode(Enum):
29     PUSH_PULL = None
30     OPEN_DRAIN = None
31
32
33 DriveMode.PUSH_PULL = DriveMode()
34 DriveMode.OPEN_DRAIN = DriveMode()
35
36
37 class Direction(Enum):
38     INPUT = None
39     OUTPUT = None
40
41
42 Direction.INPUT = Direction()
43 Direction.OUTPUT = Direction()
44
45
46 class Pull(Enum):
47     UP = None
48     DOWN = None
49     #NONE=None
50
51
52 Pull.UP = Pull()
53 Pull.DOWN = Pull()
54
55 #Pull.NONE = Pull()
56
57 class DigitalInOut(ContextManaged):
58     _pin = None
59
60     def __init__(self, pin):
61         self._pin = Pin(pin.id)
62         self.direction = Direction.INPUT
63
64     def switch_to_output(self, value=False, drive_mode=DriveMode.PUSH_PULL):
65         self.direction = Direction.OUTPUT
66         self.value = value
67         self.drive_mode = drive_mode
68
69     def switch_to_input(self, pull=None):
70         self.direction = Direction.INPUT
71         self.pull = pull
72
73     def deinit(self):
74         del self._pin
75
76     def cleanup(self, channel=None):
77         Pin.cleanup(self, channel)
78
79     @property
80     def direction(self):
81         return self.__direction
82
83     @direction.setter
84     def direction(self, dir):
85         self.__direction = dir
86         if dir is Direction.OUTPUT:
87             self._pin.init(mode=Pin.OUT)
88             self.value = False
89             self.drive_mode = DriveMode.PUSH_PULL
90         elif dir is Direction.INPUT:
91             self._pin.init(mode=Pin.IN)
92             self.pull = None
93         else:
94             raise AttributeError("Not a Direction")
95
96     @property
97     def value(self):
98         return self._pin.value() is 1
99
100     @value.setter
101     def value(self, val):
102         if self.direction is Direction.OUTPUT:
103             self._pin.value(1 if val else 0)
104         else:
105             raise AttributeError("Not an output")
106
107     @property
108     def pull(self):
109         if self.direction is Direction.INPUT:
110             return self.__pull
111         else:
112             raise AttributeError("Not an input")
113
114     @pull.setter
115     def pull(self, pul):
116         if self.direction is Direction.INPUT:
117             self.__pull = pul
118             if pul is Pull.UP:
119                 self._pin.init(mode=Pin.IN, pull=Pin.PULL_UP)
120             elif pul is Pull.DOWN:
121                 if hasattr(Pin, "PULL_DOWN"):
122                     self._pin.init(mode=Pin.IN, pull=Pin.PULL_DOWN)
123                 else:
124                     raise NotImplementedError("{} unsupported on {}".format(
125                         Pull.DOWN, board_id))
126             elif pul is None:
127                 self._pin.init(mode=Pin.IN, pull=None)
128             else:
129                 raise AttributeError("Not a Pull")
130         else:
131             raise AttributeError("Not an input")
132
133     @property
134     def drive_mode(self):
135         if self.direction is Direction.OUTPUT:
136             return self.__drive_mode  #
137         else:
138             raise AttributeError("Not an output")
139
140     @drive_mode.setter
141     def drive_mode(self, mod):
142         self.__drive_mode = mod
143         if mod is DriveMode.OPEN_DRAIN:
144             self._pin.init(mode=Pin.OPEN_DRAIN)
145         elif mod is DriveMode.PUSH_PULL:
146             self._pin.init(mode=Pin.OUT)