17 import RPi.GPIO
as GPIO
26 test1outputs = [ 2, 4,14,17,22,10,11,24,8, 5, 6,19,26]
27 test1inputs = [ 3,27,15,18,23, 9,21,25,7,13,12,16,20]
28 test2outputs = [ 2, 4,14,17,22,10,11,24,8, 5, 6,19,26]
29 test2inputs = [ 3,27,15,18,23, 9,21,25,7,13,12,16,20]
32 scriptVersion =
'20181029.1'
35 scriptStartTime = time.time()
39 logFilePath =
'/home/ubuntu'
48 GPIO.setwarnings(
False)
51 GPIO.setmode(GPIO.BCM)
57 f = open(logFilePath,
'a')
59 lineForLog = time.asctime(time.gmtime()) +
' ' + line +
'\n'
68 def blinkLed( ledGpioLine, numBlinks, timeOfBlinks, finalLedState ):
69 for x
in range (0, numBlinks):
70 GPIO.output(ledGpioLine, 1)
71 time.sleep(timeOfBlinks)
72 GPIO.output(ledGpioLine, 0)
73 time.sleep(timeOfBlinks)
74 GPIO.output(ledGpioLine, finalLedState)
77 print(
'usage: python testPiGpio.py')
82 if not os.path.isfile(logFilePath):
83 os.system(
'touch ' + logFilePath +
' 2> /dev/null');
87 logLine(os.path.basename(__file__) +
' version ' + scriptVersion +
' test starting now.')
91 opts, args = getopt.getopt(sys.argv[1:],
"hrsf")
92 except getopt.GetoptError
as err:
95 logLine(os.path.basename(__file__) +
' Pi Connector test exiting due to bad option entererd.')
111 print(
"Test of Raspberry Pi GPIO lines. Version: " + str(scriptVersion))
112 print(
"(The test fixture to connect associated GPIO lines is required)")
116 for i
in range(0, len(test1outputs)):
117 if g_simpleDebug == 1:
118 print(
"Dir 1 Test: Output port " + str(test1outputs[i]) +
" input port " + str(test1inputs[i]))
119 GPIO.setup(test1inputs[i], GPIO.IN, pull_up_down=GPIO.PUD_UP)
120 GPIO.setup(test1outputs[i], GPIO.OUT)
122 print(
"Testing forward direction for digital GPIO connectivity...")
126 print(
"Testing first direction for one level")
127 for i
in range(0, len(test1outputs)):
129 if ((i & 1) ==
True):
131 GPIO.output(test1outputs[i], 1)
134 GPIO.output(test1outputs[i], 0)
137 for i
in range(0, len(test1inputs)):
140 if ((i & 1) ==
True):
141 if GPIO.input(test1inputs[i]) != 1:
142 print(
"FAILURE! Readback of port " + str(test1inputs[i]) +
" set by port " + str(test1outputs[i]) +
" was not 1!")
145 if GPIO.input(test1inputs[i]) != 0:
146 print(
"FAILURE! Readback of port " + str(test1inputs[i]) +
" set by port " + str(test1outputs[i]) +
" was not 0!")
150 print(
"Testing first direction for other level")
151 for i
in range(0, len(test1outputs)):
153 if ((i & 1) ==
True):
155 GPIO.output(test1outputs[i], 0)
158 GPIO.output(test1outputs[i], 1)
161 for i
in range(0, len(test1inputs)):
164 if ((i & 1) ==
True):
165 if GPIO.input(test1inputs[i]) != 0:
166 print(
"FAILURE! Readback of port " + str(test1inputs[i]) +
" set by port " + str(test1outputs[i]) +
" was not 0!")
169 if GPIO.input(test1inputs[i]) != 1:
170 print(
"FAILURE! Readback of port " + str(test1inputs[i]) +
" set by port " + str(test1outputs[i]) +
" was not 1!")
174 print(
"Testing reverse direction for digital GPIO connectivity...")
175 for i
in range(0, len(test2outputs)):
176 if g_simpleDebug == 1:
177 print(
"Dir 2 Test: Output port " + str(test2outputs[i]) +
" input port " + str(test2inputs[i]))
178 GPIO.setup(test2inputs[i], GPIO.IN, pull_up_down=GPIO.PUD_UP)
179 GPIO.setup(test2outputs[i], GPIO.OUT)
183 print(
"Testing reverse direction for one level")
184 for i
in range(0, len(test2outputs)):
186 if ((i & 1) ==
True):
188 GPIO.output(test2outputs[i], 1)
191 GPIO.output(test2outputs[i], 0)
194 for i
in range(0, len(test2inputs)):
197 if ((i & 1) ==
True):
198 if GPIO.input(test2inputs[i]) != 1:
199 print(
"FAILURE! Readback of port " + str(test2inputs[i]) +
" set by port " + str(test2outputs[i]) +
" was not 1!")
202 if GPIO.input(test1inputs[i]) != 0:
203 print(
"FAILURE! Readback of port " + str(test2inputs[i]) +
" set by port " + str(test2outputs[i]) +
" was not 0!")
207 print(
"Testing reverse direction for other level")
208 for i
in range(0, len(test2outputs)):
210 if ((i & 1) ==
True):
212 GPIO.output(test2outputs[i], 0)
215 GPIO.output(test2outputs[i], 1)
218 for i
in range(0, len(test2inputs)):
221 if ((i & 1) ==
True):
222 if GPIO.input(test2inputs[i]) != 0:
223 print(
"FAILURE! Readback of port " + str(test2inputs[i]) +
" set by port " + str(test2outputs[i]) +
" was not 0!")
226 if GPIO.input(test2inputs[i]) != 1:
227 print(
"FAILURE! Readback of port " + str(test2inputs[i]) +
" set by port " + str(test2outputs[i]) +
" was not 1!")
233 print(
"All tests passed!")
236 print(
"One or more failures were detected on this pass!")
237 print(
" Note that the test fixture to connect associated GPIO lines is required")
238 print(
" Intermittent failures are often a sign that some other task is controlling GPIO")
243 except KeyboardInterrupt:
244 logLine(os.path.basename(__file__) +
' keyboard exception. background monitor done.')
247 logLine(os.path.basename(__file__) +
' exception or program exit. background monitor done.')