9 DriveMode.PUSH_PULL = DriveMode()
10 DriveMode.OPEN_DRAIN = DriveMode()
13 class Direction(Enum):
17 Direction.INPUT = Direction()
18 Direction.OUTPUT = Direction()
32 def __init__(self, pin):
35 self.switch_to_input()
47 def __setattr__(self, key, val):
48 if self._pin is not None:
49 mode = self._pin.mode()
51 if mode is machine.Pin.INPUT:
52 raise AttributeError("Pin is output")
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
62 raise ValueError("Deinitialised")
64 def __getattr__(self, key):
65 if self._pin is not None:
66 mode = self._pin.mode()
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
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:
92 elif pull is machine.Pin.PULL_DOWN:
97 raise ValueError("Deinitialised")
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)
103 def switch_to_input(self, pull=None):
104 self._pin = machine.Pin(0, machine.Pin.IN, machine.Pin.PULL_UP)
107 def direction(self, *a):
113 def drive_mode(self, *a):
119 # __all__ = ['DigitalInOut', 'DriveMode', 'Direction','Pull']