]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/digitalio.py
fix cleanup fcn
[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         if channel == self:
78             Pin.cleanup(self._pin, self._pin)
79         else:
80             Pin.cleanup(self, channel)
81
82     @property
83     def direction(self):
84         return self.__direction
85
86     @direction.setter
87     def direction(self, dir):
88         self.__direction = dir
89         if dir is Direction.OUTPUT:
90             self._pin.init(mode=Pin.OUT)
91             self.value = False
92             self.drive_mode = DriveMode.PUSH_PULL
93         elif dir is Direction.INPUT:
94             self._pin.init(mode=Pin.IN)
95             self.pull = None
96         else:
97             raise AttributeError("Not a Direction")
98
99     @property
100     def value(self):
101         return self._pin.value() is 1
102
103     @value.setter
104     def value(self, val):
105         if self.direction is Direction.OUTPUT:
106             self._pin.value(1 if val else 0)
107         else:
108             raise AttributeError("Not an output")
109
110     @property
111     def pull(self):
112         if self.direction is Direction.INPUT:
113             return self.__pull
114         else:
115             raise AttributeError("Not an input")
116
117     @pull.setter
118     def pull(self, pul):
119         if self.direction is Direction.INPUT:
120             self.__pull = pul
121             if pul is Pull.UP:
122                 self._pin.init(mode=Pin.IN, pull=Pin.PULL_UP)
123             elif pul is Pull.DOWN:
124                 if hasattr(Pin, "PULL_DOWN"):
125                     self._pin.init(mode=Pin.IN, pull=Pin.PULL_DOWN)
126                 else:
127                     raise NotImplementedError("{} unsupported on {}".format(
128                         Pull.DOWN, board_id))
129             elif pul is None:
130                 self._pin.init(mode=Pin.IN, pull=None)
131             else:
132                 raise AttributeError("Not a Pull")
133         else:
134             raise AttributeError("Not an input")
135
136     @property
137     def drive_mode(self):
138         if self.direction is Direction.OUTPUT:
139             return self.__drive_mode  #
140         else:
141             raise AttributeError("Not an output")
142
143     @drive_mode.setter
144     def drive_mode(self, mod):
145         self.__drive_mode = mod
146         if mod is DriveMode.OPEN_DRAIN:
147             self._pin.init(mode=Pin.OPEN_DRAIN)
148         elif mod is DriveMode.PUSH_PULL:
149             self._pin.init(mode=Pin.OUT)