]> Repositories - Adafruit_Blinka-hackapet.git/blob - python/digitalio/__init__.py
Simplified import
[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 = machine.Pin(pin.id)
34         self.switch_to_input()
35         pass
36
37     def deinit(self):
38         del self._pin
39
40     def __enter__(self):
41         pass
42
43     def __exit__(self):
44         self.deinit()
45
46     def __setattr__(self, key, val):
47         if self._pin is not None:
48             mode = self._pin.mode()
49             if key == "value":
50                 if mode is machine.Pin.INPUT:
51                     raise AttributeError("Pin is output")
52                 self._pin.value(val)
53             elif key == "direction":
54                 if val is Direction.OUTPUT:
55                     self._pin.mode(machine.Pin.OUT)
56                 elif val is Direction.INPUT:
57                     self._pin.mode(machine.Pin.IN)
58             #TODO more attribute assignments
59
60         else:
61             raise ValueError("Deinitialised")
62
63     def __getattr__(self, key):
64         if self._pin is not None:
65             mode = self._pin.mode()
66             if key == "value:":
67                 if mode is machine.Pin.OUTPUT:
68                     raise AttributeError("Pin is output")
69                 return self._pin.value()
70             elif key == "drive_mode":
71                 if mode is machine.Pin.OPEN_DRAIN:
72                     return DriveMode.OPEN_DRAIN
73                 elif mode is machine.Pin.OUT:
74                     return DriveMode.PUSH_PULL
75                 elif mode is machine.Pin.IN:
76                     raise AttributeError("Pin is input")
77             elif key == "direction":
78                 mode = self._pin.mode()
79                 if mode is machine.Pin.IN:
80                     return Direction.INPUT
81                 elif mode is machine.Pin.OUT:
82                     return Direction.OUTPUT
83                 elif mode is machine.Pin.OPEN_DRAIN:
84                     return Direction.OUTPUT
85             elif key == "pull":
86                 if mode is machine.Pin.OUTPUT:
87                     raise AttributeError("Pin is output")
88                 pull = self._pin.pull()
89                 if pull is machine.Pin.PULL_UP:
90                     return Pull.UP
91                 elif pull is machine.Pin.PULL_DOWN:
92                     return Pull.DOWN
93                 elif pull is None:
94                     return None
95         else:
96             raise ValueError("Deinitialised")
97
98     def switch_to_output(self, value=False, drive_mode=DriveMode.PUSH_PULL):
99         self._pin = machine.Pin(0, machine.Pin.IN, machine.Pin.PULL_UP)
100         pass
101
102     def switch_to_input(self, pull=None):
103         self._pin = machine.Pin(0, machine.Pin.IN, machine.Pin.PULL_UP)
104         pass
105
106
107 # __all__ = ['DigitalInOut', 'DriveMode', 'Direction','Pull']