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):
33 self._pin = machine.Pin(pin.id)
34 self.switch_to_input()
46 def __setattr__(self, key, val):
47 if self._pin is not None:
48 mode = self._pin.mode()
50 if mode is machine.Pin.INPUT:
51 raise AttributeError("Pin is output")
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
61 raise ValueError("Deinitialised")
63 def __getattr__(self, key):
64 if self._pin is not None:
65 mode = self._pin.mode()
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
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:
91 elif pull is machine.Pin.PULL_DOWN:
96 raise ValueError("Deinitialised")
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)
102 def switch_to_input(self, pull=None):
103 self._pin = machine.Pin(0, machine.Pin.IN, machine.Pin.PULL_UP)
107 # __all__ = ['DigitalInOut', 'DriveMode', 'Direction','Pull']