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