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