- def __setattr__(self, key, val):
- if self._pin is not None:
- mode = self._pin.mode()
- if key == "value":
- if mode is machine.Pin.INPUT:
- raise AttributeError("Pin is output")
- self._pin.value(val)
- elif key == "direction":
- if val is Direction.OUTPUT:
- self._pin.mode(machine.Pin.OUT)
- elif val is Direction.INPUT:
- self._pin.mode(machine.Pin.IN)
- #TODO more attribute assignments
-
- else:
- raise ValueError("Deinitialised")
-
- def __getattr__(self, key):
- if self._pin is not None:
- mode = self._pin.mode()
- if key == "value:":
- if mode is machine.Pin.OUTPUT:
- raise AttributeError("Pin is output")
- return self._pin.value()
- elif key == "drive_mode":
- if mode is machine.Pin.OPEN_DRAIN:
- return DriveMode.OPEN_DRAIN
- elif mode is machine.Pin.OUT:
- return DriveMode.PUSH_PULL
- elif mode is machine.Pin.IN:
- raise AttributeError("Pin is input")
- elif key == "direction":
- mode = self._pin.mode()
- if mode is machine.Pin.IN:
- return Direction.INPUT
- elif mode is machine.Pin.OUT:
- return Direction.OUTPUT
- elif mode is machine.Pin.OPEN_DRAIN:
- return Direction.OUTPUT
- elif key == "pull":
- if mode is machine.Pin.OUTPUT:
- raise AttributeError("Pin is output")
- pull = self._pin.pull()
- if pull is machine.Pin.PULL_UP:
- return Pull.UP
- elif pull is machine.Pin.PULL_DOWN:
- return Pull.DOWN
- elif pull is None:
- return None
+ @property
+ def direction(self):
+ return self.__direction
+
+ @direction.setter
+ def direction(self, dir):
+ self.__direction = dir
+ if dir is Direction.OUTPUT:
+ self._pin.init(mode=Pin.OUT)
+ self.value = False
+ self.drive_mode = DriveMode.PUSH_PULL
+ elif dir is Direction.INPUT:
+ self._pin.init(mode=Pin.IN)
+ self.pull = None