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