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