1 from machine import Pin
9 DriveMode.PUSH_PULL = DriveMode()
10 DriveMode.OPEN_DRAIN = DriveMode()
13 class Direction(Enum):
17 Direction.INPUT = Direction()
18 Direction.OUTPUT = Direction()
30 class DigitalInOut(object):
33 def __init__(self, pin):
34 self._pin = Pin(pin.id)
35 self.direction = Direction.INPUT
38 def switch_to_output(self, value=False, drive_mode=DriveMode.PUSH_PULL):
39 self.direction=Direction.OUTPUT
41 self.drive_mode=drive_mode
44 def switch_to_input(self, pull=None):
45 self.direction=Direction.INPUT
60 return self.__direction
63 def direction(self, dir):
64 self.__direction = dir
65 if dir is Direction.OUTPUT:
66 self._pin.init(mode=Pin.OUT)
68 self.drive_mode = DriveMode.PUSH_PULL
69 elif dir is Direction.INPUT:
70 self._pin.init(mode=Pin.IN)
73 raise AttributeError("Not a Direction")
77 return self._pin.value()
81 if self.direction is Direction.OUTPUT:
84 raise AttributeError("Not an output")
88 if self.direction is Direction.INPUT:
91 raise AttributeError("Not an input") #
95 if self.direction is Direction.INPUT:
98 self._pin.init(mode=Pin.IN, pull=Pin.PULL_UP)
99 elif pul is Pull.DOWN:
100 self._pin.init(mode=Pin.IN, pull=Pin.PULL_DOWN)
102 self._pin.init(mode=Pin.IN, pull=None)
104 raise AttributeError("Not a Pull")#
106 raise AttributeError("Not an input") #
109 def drive_mode(self):
110 if self.direction is Direction.OUTPUT:
111 return self.__drive_mode#
113 raise AttributeError("Not an output")
116 def drive_mode(self, mod):
117 self.__drive_mode = mod
118 if mod is DriveMode.OPEN_DRAIN:
119 self._pin.init(mode=Pin.OPEN_DRAIN)
120 elif mod is DriveMode.PUSH_PULL:
121 self._pin.init(mode=Pin.OUT)
123 # __all__ = ['DigitalInOut', 'DriveMode', 'Direction','Pull']