擅长:python、mysql、java
<p>您可以将GPIO代码包装到它自己的线程中,让程序的其余部分在GPIO等待输入时执行其他操作。查看<a href="http://docs.python.org/2/library/threading.html" rel="nofollow">threading module</a></p>
<p>首先我会把你的代码包装成一个函数</p>
<pre><code>def wait_input():
var=1
counter = 0
while var == 1:
if GPIO.input(7):
counter += 1
time.sleep(0.5)
else:
counter = 0
time.sleep(1)
if counter >= 3:
print "Movement!"
captureImage()
time.sleep(20)
</code></pre>
<p>然后在你的主程序中你可以这样做</p>
<pre><code>input_thread = threading.Thread(target = wait_input)
input_thread.start()
# do something in the meanwhile
input_thread.join()
</code></pre>
<p>关于python线程有很多问题,所以您可能需要深入了解它们。请注意,在使用线程时还需要考虑很多事情,特别是在python中,它有一个全局解释器锁(GIL),一次只允许运行一个进程。查看可以绕GIL路由的<a href="http://docs.python.org/2/library/multiprocessing.html" rel="nofollow">multiprocessing module</a>也可能是明智之举。</p>