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