我是 Python 3 的新手,我一直在编写代码,当按下五个分配的按钮之一时,将播放 4 个不同的视频,其中 3 个视频使用 time.sleep() 定时闪烁 LED。我的所有时间都运行良好;但是我不能让一个视频播放/闪烁 LED,然后在另一个按钮上做同样的事情,即使它中断了之前的视频/闪烁。一旦按下按钮并且 LED 闪烁,如果视频结束并再次按下按钮,它们将不再闪烁。第 5 个按钮设置为终止程序(作为紧急情况),但有时我必须从 LXterminal 手动执行“sudo killall omxplayer”。另外,在操作系统升级后,我收到 omxplayer.bin 'terminating on line 67' 的新错误。我需要一些帮助来编写此代码以及我可能缺少或需要删除的内容:
import RPi.GPIO as GPIO
import time,signal,sys,os
from omxplayer.player import OMXPlayer
def playerExit(code):
global command1
print('exit',code)
os.system(command1)
global videoPlaying
videoPlaying=False
def playVideo(video):
global player,videoPlaying
if player==None:
player = OMXPlayer(video, args=['--no-osd', '--no-keys',])
player.exitEvent += lambda _, exit_code: playerExit(exit_code)
if buttonVideo[1] == "/home/pi/Videos/noman.mp4":
time.sleep(164)
nomanh16()
nomanr16()
if buttonVideo[1] == "/home/pi/Videos/cobro.mp4":
time.sleep(45)
cobro1()
cobro2()
cobror1()
else:
player.load(video)
videoPlaying = True
def signalHandler(sig,frame):
print('Ctrl+C pressed')
os.system(command1)
sys.exit(0)
def quitPlayerLED():
global ledGPIO
for index in ledGPIO:
GPIO.output(index,GPIO.LOW)
if player != None:
player.exit()
def nomanh16():
for f in range(0,62):
GPIO.output(13,GPIO.HIGH) #<<< Turn ON LED
time.sleep(0.7) #<<< wait 1/8 of a second
GPIO.output(13,GPIO.LOW) #<<< Turn OFF LED
time.sleep(0.7) #<<< wait 1/8 of a second
def nomanr16():
for f in range(0,34):
GPIO.output(13,GPIO.HIGH)
GPIO.output(17,GPIO.HIGH)
GPIO.output(18,GPIO.HIGH)
GPIO.output(23,GPIO.HIGH)
time.sleep(0.7)
GPIO.output(13,GPIO.LOW)
GPIO.output(17,GPIO.LOW)
GPIO.output(18,GPIO.LOW)
GPIO.output(23,GPIO.LOW)
time.sleep(0.7)
def cobro1():
for f in range(0,28):
GPIO.output(13,GPIO.HIGH) #<<< Turn ON LED
time.sleep(0.6) #<<< wait 1/8 of a second
GPIO.output(13,GPIO.LOW) #<<< Turn OFF LED
time.sleep(0.6) #<<< wait 1/8 of a second
def cobro2():
for f in range(0,107):
if f >= 86:
time.sleep(0.4)
GPIO.output(17,GPIO.HIGH)
GPIO.output(18,GPIO.HIGH)
GPIO.output(23,GPIO.HIGH)
GPIO.output(13,GPIO.HIGH)
time.sleep(0.4)
GPIO.output(13,GPIO.LOW)
GPIO.output(17,GPIO.LOW)
GPIO.output(18,GPIO.LOW)
GPIO.output(23,GPIO.LOW)
time.sleep(0.4)
def cobror1():
for j in range(0,104):
if j >= 42:
GPIO.output(13,GPIO.HIGH)
GPIO.output(17,GPIO.HIGH)
GPIO.output(18,GPIO.HIGH)
GPIO.output(23,GPIO.HIGH)
time.sleep(0.7)
GPIO.output(13,GPIO.LOW)
GPIO.output(17,GPIO.LOW)
GPIO.output(18,GPIO.LOW)
GPIO.output(23,GPIO.LOW)
time.sleep(0.7)
# gpio,video/quit,blink T/F
buttonVideos = [[16,"/home/pi/Videos/noman.mp4",False],
[20,"/home/pi/Videos/cobro.mp4",False],
[26,"quit",False]]
ledGPIO = [13,17,18,23] #<==== LEDS
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
for index in ledGPIO:
GPIO.setup(index,GPIO.OUT)
GPIO.output(index,GPIO.LOW)
for buttonVideo in buttonVideos:
GPIO.setup(buttonVideo[0], GPIO.IN, pull_up_down=GPIO.PUD_UP)
signal.signal(signal.SIGINT,signalHandler)
command1 = "sudo killall omxplayer.bin"
player = None
videoPlaying = False
try:
while True:
# check buttons
for buttonVideo in buttonVideos:
if GPIO.input(buttonVideo[0]) == GPIO.LOW:
print(buttonVideo[0],'pressed')
if(buttonVideo[1] == "quit"):
quitPlayerLED()
else:
playVideo(buttonVideo[1])
break
except KeyboardInterrupt:
command1 = "sudo killall -s 9 omxplayer.bin"
os.system(command1)
os.system("exit")
except NameError as error:
command1 = "sudo killall -s 9 omxplayer.bin"
os.system(command1)
os.system("exit")
except TypeError as error:
command1 = "sudo killall -s 9 omxplayer.bin"
os.system(command1)
os.system("exit")
except Exception as exception:
command1 = "sudo killall -s 9 omxplayer.bin"
os.system(command1)
os.system("exit")