6 DriveMode.PUSH_PULL=DriveMode()
7 DriveMode.OPEN_DRAIN=DriveMode()
11 Direction.INPUT = Direction()
12 Direction.OUTPUT = Direction()
21 def __init__(self, pin):
24 self.switch_to_input()
36 def __setattr__(self, key, val):
37 if self._pin is not None:
38 mode = self._pin.mode()
40 if mode is machine.Pin.INPUT:
41 raise AttributeError("Pin is output")
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)
50 raise ValueError("Deinitialised")
52 def __getattr__(self, key):
53 if self._pin is not None:
54 mode = self._pin.mode()
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
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:
80 elif pull is machine.Pin.PULL_DOWN:
85 raise ValueError("Deinitialised")
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)
92 def switch_to_input(self, pull=None):
93 self._pin = machine.Pin(0, machine.Pin.IN, machine.Pin.PULL_UP)
96 def direction(self, *a):
102 def drive_mode(self, *a):
108 __all__ = ['DigitalInOut', 'DriveMode', 'Direction','Pull']