]> Repositories - Adafruit_Blinka-hackapet.git/blob - python/digitalio/__init__.py
Property interdependencies now sensible
[Adafruit_Blinka-hackapet.git] / python / digitalio / __init__.py
1 from machine import Pin
2 from mcp import Enum
3
4
5 class DriveMode(Enum):
6     pass
7
8
9 DriveMode.PUSH_PULL = DriveMode()
10 DriveMode.OPEN_DRAIN = DriveMode()
11
12
13 class Direction(Enum):
14     pass
15
16
17 Direction.INPUT = Direction()
18 Direction.OUTPUT = Direction()
19
20
21 class Pull(Enum):
22     pass
23
24
25 Pull.UP = Pull()
26 Pull.DOWN = Pull()
27 Pull.NONE = Pull()
28
29
30 class DigitalInOut(object):
31     _pin = None
32
33     def __init__(self, pin):
34         self._pin = Pin(pin.id)
35         self.direction = Direction.INPUT
36
37
38     def switch_to_output(self, value=False, drive_mode=DriveMode.PUSH_PULL):
39         self.direction=Direction.OUTPUT
40         self.value=value
41         self.drive_mode=drive_mode
42
43
44     def switch_to_input(self, pull=None):
45         self.direction=Direction.INPUT
46         self.pull=pull
47
48
49     def deinit(self):
50         del self._pin
51
52     def __enter__(self):
53         pass
54
55     def __exit__(self):
56         self.deinit()
57
58     @property
59     def direction(self):
60         return self.__direction
61
62     @direction.setter
63     def direction(self, dir):
64         self.__direction = dir
65         if dir is Direction.OUTPUT:
66             self._pin.init(mode=Pin.OUT)
67             self.value = False
68             self.drive_mode = DriveMode.PUSH_PULL
69         elif dir is Direction.INPUT:
70             self._pin.init(mode=Pin.IN)
71             self.pull = None
72         else:
73             raise AttributeError("Not a Direction")
74
75     @property
76     def value(self):
77         return self._pin.value()
78
79     @value.setter
80     def value(self, val):
81         if self.direction is Direction.OUTPUT:
82             self._pin.value(val)
83         else:
84             raise AttributeError("Not an output")
85
86     @property
87     def pull(self):
88         if self.direction is Direction.INPUT:
89             return self.__pull
90         else:
91             raise AttributeError("Not an input")  #
92
93     @pull.setter
94     def pull(self, pul):
95         if self.direction is Direction.INPUT:
96             self.__pull = pul
97             if pul is Pull.UP:
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)
101             elif pul is None:
102                 self._pin.init(mode=Pin.IN, pull=None)
103             else:
104                 raise AttributeError("Not a Pull")#
105         else:
106             raise AttributeError("Not an input")  #
107
108     @property
109     def drive_mode(self):
110         if self.direction is Direction.OUTPUT:
111             return self.__drive_mode#
112         else:
113             raise AttributeError("Not an output")
114
115     @drive_mode.setter
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)
122
123 # __all__ = ['DigitalInOut', 'DriveMode', 'Direction','Pull']