]> Repositories - Adafruit_Blinka-hackapet.git/blob - python/digitalio/__init__.py
Draft implementation of digitalio to be hardened once unittest layout is settled
[Adafruit_Blinka-hackapet.git] / python / digitalio / __init__.py
1 import machine
2 from mcp import Enum
3
4 class DriveMode(Enum):
5     pass
6 DriveMode.PUSH_PULL=DriveMode()
7 DriveMode.OPEN_DRAIN=DriveMode()
8
9 class Direction(Enum):
10     pass
11 Direction.INPUT = Direction()
12 Direction.OUTPUT = Direction()
13
14 class Pull(Enum):
15     pass
16 Pull.UP = Pull()
17 Pull.DOWN = Pull()
18
19 class DigitalInOut:
20     _pin=None
21     def __init__(self, pin):
22         self.pin = pin
23         self._pin = None
24         self.switch_to_input()
25         pass
26
27     def deinit(self):
28         del self._pin
29
30     def __enter__(self):
31         pass
32
33     def __exit__(self):
34         self.deinit()
35
36     def __setattr__(self, key, val):
37         if self._pin is not None:
38             mode = self._pin.mode()
39             if key == "value":
40                 if mode is machine.Pin.INPUT:
41                     raise AttributeError("Pin is output")
42                 self._pin.value(val)
43             elif key == "direction":
44                 if val is Direction.OUTPUT:
45                     self._pin.mode(machine.Pin.OUT)
46                 elif val is Direction.INPUT:
47                     self._pin.mode(machine.Pin.IN)
48
49         else:
50             raise ValueError("Deinitialised")
51
52     def __getattr__(self, key):
53         if self._pin is not None:
54             mode = self._pin.mode()
55             if key=="value:":
56                 if mode is machine.Pin.OUTPUT:
57                     raise AttributeError("Pin is output")
58                 return self._pin.value()
59             elif key == "drive_mode":
60                 if mode is machine.Pin.OPEN_DRAIN:
61                     return DriveMode.OPEN_DRAIN
62                 elif mode is machine.Pin.OUT:
63                     return DriveMode.PUSH_PULL
64                 elif mode is machine.Pin.IN:
65                     raise AttributeError("Pin is input")
66             elif key=="direction":
67                 mode = self._pin.mode()
68                 if mode is machine.Pin.IN:
69                     return Direction.INPUT
70                 elif mode is machine.Pin.OUT:
71                     return Direction.OUTPUT
72                 elif mode is machine.Pin.OPEN_DRAIN:
73                     return Direction.OUTPUT
74             elif key=="pull":
75                 if mode is machine.Pin.OUTPUT:
76                     raise AttributeError("Pin is output")
77                 pull = self._pin.pull()
78                 if pull is machine.Pin.PULL_UP:
79                     return Pull.UP
80                 elif pull is machine.Pin.PULL_DOWN:
81                     return Pull.DOWN
82                 elif pull is None:
83                     return None
84         else:
85             raise ValueError("Deinitialised")
86
87
88     def switch_to_output(self, value=False, drive_mode=DriveMode.PUSH_PULL):
89         self._pin = machine.Pin(0, machine.Pin.IN, machine.Pin.PULL_UP)
90         pass
91
92     def switch_to_input(self, pull=None):
93         self._pin = machine.Pin(0, machine.Pin.IN, machine.Pin.PULL_UP)
94         pass
95
96     def direction(self, *a):
97         pass
98
99     def value(self, *a):
100         pass
101
102     def drive_mode(self, *a):
103         pass
104
105     def pull(self, *a):
106         pass
107
108 __all__ = ['DigitalInOut', 'DriveMode', 'Direction','Pull']