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