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