1 from machine import Pin
2 from agnostic import board as boardId
9 DriveMode.PUSH_PULL = DriveMode()
10 DriveMode.OPEN_DRAIN = DriveMode()
13 class Direction(Enum):
16 Direction.INPUT = Direction()
17 Direction.OUTPUT = Direction()
29 class DigitalInOut(object):
32 def __init__(self, pin):
33 self._pin = Pin(pin.id)
34 self.direction = Direction.INPUT
36 def switch_to_output(self, value=False, drive_mode=DriveMode.PUSH_PULL):
37 self.direction = Direction.OUTPUT
39 self.drive_mode = drive_mode
41 def switch_to_input(self, pull=None):
42 self.direction = Direction.INPUT
51 def __exit__(self, exc_type, exc_value, traceback):
56 return self.__direction
59 def direction(self, dir):
60 self.__direction = dir
61 if dir is Direction.OUTPUT:
62 self._pin.init(mode=Pin.OUT)
64 self.drive_mode = DriveMode.PUSH_PULL
65 elif dir is Direction.INPUT:
66 self._pin.init(mode=Pin.IN)
69 raise AttributeError("Not a Direction")
73 return self._pin.value() is 1
77 if self.direction is Direction.OUTPUT:
78 self._pin.value(1 if val else 0)
80 raise AttributeError("Not an output")
84 if self.direction is Direction.INPUT:
87 raise AttributeError("Not an input")
91 if self.direction is Direction.INPUT:
94 self._pin.init(mode=Pin.IN, pull=Pin.PULL_UP)
95 elif pul is Pull.DOWN:
96 if hasattr(Pin, "PULL_DOWN"):
97 self._pin.init(mode=Pin.IN, pull=Pin.PULL_DOWN)
99 raise NotImplementedError("{} unsupported on {}".format(Pull.DOWN, boardId))
101 self._pin.init(mode=Pin.IN, pull=None)
103 raise AttributeError("Not a Pull")
105 raise AttributeError("Not an input")
108 def drive_mode(self):
109 if self.direction is Direction.OUTPUT:
110 return self.__drive_mode #
112 raise AttributeError("Not an output")
115 def drive_mode(self, mod):
116 self.__drive_mode = mod
117 if mod is DriveMode.OPEN_DRAIN:
118 self._pin.init(mode=Pin.OPEN_DRAIN)
119 elif mod is DriveMode.PUSH_PULL:
120 self._pin.init(mode=Pin.OUT)
122 # __all__ = ['DigitalInOut', 'DriveMode', 'Direction','Pull']