forked from johneast2/keychainbackup
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathonionGpio3.py
223 lines (157 loc) · 5.05 KB
/
onionGpio3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import sys
__version__ = "0.1"
_EXIT_SUCCESS = 0
_EXIT_FAILURE = -1
GPIO_BASE_PATH = '/sys/class/gpio'
GPIO_EXPORT = GPIO_BASE_PATH + '/export'
GPIO_UNEXPORT = GPIO_BASE_PATH + '/unexport'
GPIO_PATH = GPIO_BASE_PATH + '/gpio%d'
GPIO_VALUE_FILE = 'value'
GPIO_DIRECTION_FILE = 'direction'
GPIO_ACTIVE_LOW_FILE = 'active_low'
_GPIO_INPUT_DIRECTION = 'in'
_GPIO_OUTPUT_DIRECTION = 'out'
_GPIO_OUTPUT_DIRECTION_LOW = 'low'
_GPIO_OUTPUT_DIRECTION_HIGH = 'high'
_GPIO_ACTIVE_HIGH = 0
_GPIO_ACTIVE_LOW = 1
class OnionGpio:
"""Base class for sysfs GPIO access"""
def __init__(self, gpio, verbose=0):
self.gpio = gpio
self.path = GPIO_PATH%(self.gpio)
self.verbose = verbose
if self.verbose > 0:
print('GPIO%d path: %s'%(self.gpio, self.path))
def _initGpio(self):
"""Write to the gpio export to make the gpio available in sysfs"""
with open(GPIO_EXPORT, 'w') as fd:
fd.write(str(self.gpio))
fd.close()
return _EXIT_SUCCESS
return _EXIT_FAILURE
def _freeGpio(self):
"""Write to the gpio unexport to release the gpio sysfs instance"""
with open(GPIO_UNEXPORT, 'w') as fd:
fd.write(str(self.gpio))
fd.close()
return _EXIT_SUCCESS
return _EXIT_FAILURE
# value functions
def getValue(self):
"""Read current GPIO value"""
# generate the gpio sysfs instance
status = self._initGpio()
if status == _EXIT_SUCCESS:
gpioFile = self.path + "/" + GPIO_VALUE_FILE
value = 0
with open(gpioFile, 'r') as fd:
value = fd.read()
fd.close()
# release the gpio sysfs instance
status = self._freeGpio()
return value
return _EXIT_FAILURE
def setValue(self, value):
"""Set the desired GPIO value"""
ret = _EXIT_FAILURE
# generate the gpio sysfs instance
status = self._initGpio()
if status == _EXIT_SUCCESS:
gpioFile = self.path + "/" + GPIO_VALUE_FILE
with open(gpioFile, 'w') as fd:
fd.write(str(value))
fd.close()
ret = _EXIT_SUCCESS
# release the gpio sysfs instance
status = self._freeGpio()
return ret
return _EXIT_FAILURE
# direction functions
def getDirection(self):
"""Read current GPIO direction"""
# generate the gpio sysfs instance
status = self._initGpio()
if status == _EXIT_SUCCESS:
gpioFile = self.path + "/" + GPIO_DIRECTION_FILE
direction = _EXIT_FAILURE
# read from the direction file
with open(gpioFile, 'r') as fd:
direction = fd.read()
fd.close()
# release the gpio sysfs instance
status = self._freeGpio()
return direction
return _EXIT_FAILURE
def _setDirection(self, direction):
"""Set the desired GPIO direction"""
ret = _EXIT_FAILURE
# check the direction argument
if direction != _GPIO_INPUT_DIRECTION and direction != _GPIO_OUTPUT_DIRECTION and direction != _GPIO_OUTPUT_DIRECTION_LOW and direction != _GPIO_OUTPUT_DIRECTION_HIGH:
return _EXIT_FAILURE
# generate the gpio sysfs instance
status = self._initGpio()
if status == _EXIT_SUCCESS:
gpioFile = self.path + "/" + GPIO_DIRECTION_FILE
# write to the direction file
with open(gpioFile, 'w') as fd:
fd.write(direction)
fd.close()
ret = _EXIT_SUCCESS
# release the gpio sysfs instance
status = self._freeGpio()
return ret
return _EXIT_FAILURE
def setInputDirection(self):
ret = self._setDirection(_GPIO_INPUT_DIRECTION)
return ret
def setOutputDirection(self, initial=-1):
argument = _GPIO_OUTPUT_DIRECTION
if initial == 0:
argument = _GPIO_OUTPUT_DIRECTION_LOW
elif initial == 1:
argument = _GPIO_OUTPUT_DIRECTION_HIGH
ret = self._setDirection(argument)
return ret
# active-low functions
def getActiveLow(self):
"""Read if current GPIO is active-low"""
# generate the gpio sysfs instance
status = self._initGpio()
if status == _EXIT_SUCCESS:
gpioFile = self.path + "/" + GPIO_ACTIVE_LOW_FILE
activeLow = _EXIT_FAILURE
with open(gpioFile, 'r') as fd:
activeLow = fd.read()
fd.close()
if self.verbose > 0:
print('onionGpio:getActiveLow:: Reading %s file ... Read %s'%(gpioFile, activeLow))
# release the gpio sysfs instance
status = self._freeGpio()
return activeLow
return _EXIT_FAILURE
def _setActiveLow(self, activeLow):
"""Set the desired GPIO direction"""
ret = _EXIT_FAILURE
# generate the gpio sysfs instance
status = self._initGpio()
if status == _EXIT_SUCCESS:
gpioFile = self.path + "/" + GPIO_ACTIVE_LOW_FILE
if activeLow == _GPIO_ACTIVE_HIGH or activeLow == _GPIO_ACTIVE_LOW:
with open(gpioFile, 'w') as fd:
if self.verbose > 0:
print('onionGpio:_setActiveLow:: Writing %s to %s file'%(str(activeLow), gpioFile))
fd.write(str(activeLow))
fd.close()
ret = _EXIT_SUCCESS
# release the gpio sysfs instance
status = self._freeGpio()
# note: active_low setting is reset when the gpio sysfs interface is released!
return ret
return _EXIT_FAILURE
def setActiveHigh(self):
ret = self._setActiveLow(_GPIO_ACTIVE_HIGH)
return ret
def setActiveLow(self):
ret = self._setActiveLow(_GPIO_ACTIVE_LOW)
return ret