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