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