【发布时间】:2015-11-19 11:17:57
【问题描述】:
我的项目是读取 PIR 传感器的读数并在人在传感器前面时播放歌曲,但我无法弄清楚我在网上找到并尝试修改的这段代码背后的逻辑。
我需要做的是:
- 如何循环这个,omxp.poll() 不起作用:(
编辑:现在它停止了,但有没有办法循环进程,有没有办法让脚本内存有效
这里是代码:(更新)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#from subprocess import Popen
from omxplayer import OMXPlayer
import RPi.GPIO as GPIO
import time
import subprocess
GPIO.setmode(GPIO.BCM)
PIR_PIN = 7
GPIO.setup(PIR_PIN, GPIO.IN)
song = OMXPlayer('/home/pi/5Seconds.mp3')
try:
print ("Pir Module Test (CTRL+C to exit)")
time.sleep(2)
print("Ready")
active = False
while True:
time.sleep(2)
if GPIO.input(PIR_PIN):
time.sleep(1)
print("Motion detected")
if not active:
active = True
print("Music started")
song.play()
time.sleep(10)
elif active:
print("No motion detected, stop the music")
song.pause()
song.can_control(song)
active = False
if active and song.poll() != None: # detect completion to allow another start
print("Music finished")
active = False
except KeyboardInterrupt:
print ("Quit")
GPIO.cleanup()
【问题讨论】:
-
是因为
song.quit()卸载了OMXPlayer吗?有没有其他方法可以停止歌曲? -
您也可以在条件检查中使用
ifelse而不是if和if。 -
但我需要循环整个代码
-
else 抛出语法错误 :(
标签: python raspberry-pi