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