0 python if-statement global-variables bottle raspberry-pi
我遇到了当前Python脚本的问题.'progress'变量的目的是在通过其中一个if循环时获取特定值.但是,该程序永远不会超过第一个if语句.看起来好像每个if语句都有自己的变量叫做'progress'.有人可以帮帮我吗?见下面的代码.
from bottle import run, route, template, error, static_file
import RPi.GPIO as GPIO
import time
switch1 = 21
switch2 = 20
switch3 = 26
switch4 = 16
switch5 = 19
led1 = 13
led2 = 12
led3 = 6
led4 = 5
led5 = 25
GPIO.setmode(GPIO.BCM)
GPIO.setup(switch1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(switch2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(switch3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(switch4, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(switch5, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(led1, GPIO.OUT)
GPIO.setup(led2, GPIO.OUT)
GPIO.setup(led3, GPIO.OUT)
GPIO.setup(led4, GPIO.OUT)
GPIO.setup(led5, GPIO.OUT)
@route("/")
def hello():
progress = 0
while True:
if progress == 0:
GPIO.output(led1, False)
GPIO.output(led2, False)
GPIO.output(led3, False)
GPIO.output(led4, False)
GPIO.output(led5, False)
progress = 1
return template('index.html')
if (not GPIO.input(switch1)) and progress == 1:
GPIO.output(led1, True)
progress = 2
return template('video1.html')
elif (not GPIO.input(switch2)) and progress == 2:
GPIO.output(led1, False)
GPIO.output(led2, True)
progress = 3
return template('video2.html')
elif (not GPIO.input(switch3)) and progress == 3:
GPIO.output(led2, False)
GPIO.output(led3, True)
progress = 4
return template('video3.html')
elif (not GPIO.input(switch4)) and progress == 4:
GPIO.output(led3, False)
GPIO.output(led4, True)
progress = 5
return template('video4.html')
elif (not GPIO.input(switch5)) and progress == 5:
GPIO.output(led4, False)
GPIO.output(led5, True)
progress = 6
return template('video5.html')
elif progress == 6:
while True:
GPIO.output(led1, True)
GPIO.output(led2, True)
GPIO.output(led3, True)
GPIO.output(led4, True)
GPIO.output(led5, True)
time.sleep(0.5)
GPIO.output(led1, False)
GPIO.output(led2, False)
GPIO.output(led3, False)
GPIO.output(led4, False)
GPIO.output(led5, False)
time.sleep(0.5)
return template('succes.html')
elif GPIO.input(switch1) and GPIO.input(switch2) and GPIO.input(switch3) and GPIO.input(switch4) and GPIO.input(switch5):
time.sleep(0.15)
else:
GPIO.output(led1, False)
GPIO.output(led2, False)
GPIO.output(led3, False)
GPIO.output(led4, False)
GPIO.output(led5, False)
return template('false.html')
time.sleep(0.05)
@route('/<filename>')
def server_static(filename):
return static_file(filename, root='static')
@error(404)
def error404(error):
return("Nothing here, keep searching!")
run(host='0.0.0.0', port=80)
Run Code Online (Sandbox Code Playgroud)
你正在使用if...elif...
.Python将选择一个匹配的测试(第一个if
或匹配的测试elif
),并且永远不会运行其他分支.
因此,改变progress
其中一个分支将不会导致其他分支之一被选中.
如果测试是分开的,你不应该使用elif
,但if
对于每一个分支.
但是,您使用return
语句在每个分支中完全退出视图.你的函数不会继续,循环退出,下一个请求将始终从头开始(你设置的地方progress = 0
.如果progress
你的服务器是一个全局状态,你应该设置它.请注意那是如果您使用使用多处理进行扩展的WSGI服务器,则不会是线程安全的,也不会跨进程共享该变量.
由于您正在控制一块硬件,因此使用全局可能没问题,但是您需要将WSGI服务器限制为仅运行一个线程,或者您需要使用锁定将此视图限制为一次一个线程.
要创建progress
全局,请global progress
在函数顶部添加,并将其放在函数progress = 0
外部:
progress = 0
@route("/")
def hello():
global progress
if progress == 0:
GPIO.output(led1, False)
GPIO.output(led2, False)
GPIO.output(led3, False)
GPIO.output(led4, False)
GPIO.output(led5, False)
progress = 1
return template('index.html')
if (not GPIO.input(switch1)) and progress == 1:
GPIO.output(led1, True)
progress = 2
return template('video1.html')
elif (not GPIO.input(switch2)) and progress == 2:
GPIO.output(led1, False)
GPIO.output(led2, True)
progress = 3
return template('video2.html')
elif (not GPIO.input(switch3)) and progress == 3:
GPIO.output(led2, False)
GPIO.output(led3, True)
progress = 4
return template('video3.html')
elif (not GPIO.input(switch4)) and progress == 4:
GPIO.output(led3, False)
GPIO.output(led4, True)
progress = 5
return template('video4.html')
elif (not GPIO.input(switch5)) and progress == 5:
GPIO.output(led4, False)
GPIO.output(led5, True)
progress = 6
return template('video5.html')
elif progress == 6:
while True:
GPIO.output(led1, True)
GPIO.output(led2, True)
GPIO.output(led3, True)
GPIO.output(led4, True)
GPIO.output(led5, True)
time.sleep(0.5)
GPIO.output(led1, False)
GPIO.output(led2, False)
GPIO.output(led3, False)
GPIO.output(led4, False)
GPIO.output(led5, False)
time.sleep(0.5)
return template('succes.html')
elif GPIO.input(switch1) and GPIO.input(switch2) and GPIO.input(switch3) and GPIO.input(switch4) and GPIO.input(switch5):
time.sleep(0.15)
else:
GPIO.output(led1, False)
GPIO.output(led2, False)
GPIO.output(led3, False)
GPIO.output(led4, False)
GPIO.output(led5, False)
return template('false.html')
Run Code Online (Sandbox Code Playgroud)
请注意,while
循环和sleep()
调用已消失.您必须将Javascript放在响应中,而不是在超时后重新加载页面.