]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/digitalio.py
Merge pull request #105 from pdp7/issue104
[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.T210:
23     from adafruit_blinka.microcontroller.tegra.t210.pin import Pin
24 elif detector.chip.T186:
25     from adafruit_blinka.microcontroller.tegra.t186.pin import Pin
26 elif detector.chip.T194:
27     from adafruit_blinka.microcontroller.tegra.t194.pin import Pin
28 elif detector.chip.STM32:
29     from machine import Pin
30 from adafruit_blinka import Enum, ContextManaged
31
32 class DriveMode(Enum):
33     PUSH_PULL = None
34     OPEN_DRAIN = None
35
36
37 DriveMode.PUSH_PULL = DriveMode()
38 DriveMode.OPEN_DRAIN = DriveMode()
39
40
41 class Direction(Enum):
42     INPUT = None
43     OUTPUT = None
44
45
46 Direction.INPUT = Direction()
47 Direction.OUTPUT = Direction()
48
49
50 class Pull(Enum):
51     UP = None
52     DOWN = None
53     #NONE=None
54
55
56 Pull.UP = Pull()
57 Pull.DOWN = Pull()
58
59 #Pull.NONE = Pull()
60
61
62 class DigitalInOut(ContextManaged):
63     _pin = None
64
65     def __init__(self, pin):
66         self._pin = Pin(pin.id)
67         self.direction = Direction.INPUT
68
69     def switch_to_output(self, value=False, drive_mode=DriveMode.PUSH_PULL):
70         self.direction = Direction.OUTPUT
71         self.value = value
72         self.drive_mode = drive_mode
73
74     def switch_to_input(self, pull=None):
75         self.direction = Direction.INPUT
76         self.pull = pull
77
78     def deinit(self):
79         del self._pin
80
81     @property
82     def direction(self):
83         return self.__direction
84
85     @direction.setter
86     def direction(self, dir):
87         self.__direction = dir
88         if dir is Direction.OUTPUT:
89             self._pin.init(mode=Pin.OUT)
90             self.value = False
91             self.drive_mode = DriveMode.PUSH_PULL
92         elif dir is Direction.INPUT:
93             self._pin.init(mode=Pin.IN)
94             self.pull = None
95         else:
96             raise AttributeError("Not a Direction")
97
98     @property
99     def value(self):
100         return self._pin.value() is 1
101
102     @value.setter
103     def value(self, val):
104         if self.direction is Direction.OUTPUT:
105             self._pin.value(1 if val else 0)
106         else:
107             raise AttributeError("Not an output")
108
109     @property
110     def pull(self):
111         if self.direction is Direction.INPUT:
112             return self.__pull
113         else:
114             raise AttributeError("Not an input")
115
116     @pull.setter
117     def pull(self, pul):
118         if self.direction is Direction.INPUT:
119             self.__pull = pul
120             if pul is Pull.UP:
121                 self._pin.init(mode=Pin.IN, pull=Pin.PULL_UP)
122             elif pul is Pull.DOWN:
123                 if hasattr(Pin, "PULL_DOWN"):
124                     self._pin.init(mode=Pin.IN, pull=Pin.PULL_DOWN)
125                 else:
126                     raise NotImplementedError("{} unsupported on {}".format(
127                         Pull.DOWN, board_id))
128             elif pul is None:
129                 self._pin.init(mode=Pin.IN, pull=None)
130             else:
131                 raise AttributeError("Not a Pull")
132         else:
133             raise AttributeError("Not an input")
134
135     @property
136     def drive_mode(self):
137         if self.direction is Direction.OUTPUT:
138             return self.__drive_mode  #
139         else:
140             raise AttributeError("Not an output")
141
142     @drive_mode.setter
143     def drive_mode(self, mod):
144         self.__drive_mode = mod
145         if mod is DriveMode.OPEN_DRAIN:
146             self._pin.init(mode=Pin.OPEN_DRAIN)
147         elif mod is DriveMode.PUSH_PULL:
148             self._pin.init(mode=Pin.OUT)