简体   繁体   English

卡在python项目中

[英]stuck with python project

I'm working on a project of my own, and I'm at a point where i don't know anymore what to do.. 我正在做自己的项目,现在我已经不知道该怎么办了。

I'm trying to implement some sounds into my project where i press some tact. 我试图在我的项目中实现一些声音,然后按一些小技巧。 switches and they should make sounds.. I'm a complete newbie with python so i found a piece of code doing something similar... 开关,它们应该发出声音..我是python的新手,所以我发现一段代码做了类似的事情...

import os
from time import sleep
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN)
GPIO.setup(24, GPIO.IN)
GPIO.setup(25, GPIO.IN)

while True:
    if (GPIO.input(23) == False):
        os.system('mpg123 -q binary-language-moisture-evaporators.mp3 &')
    if (GPIO.input(24) == False):
        os.system('mpg123 -q power-converters.mp3 &')
    if (GPIO.input(25)== False):
        os.system('mpg123 -q vader.mp3 &')
    sleep(0.1);

I want the 1st sound to run in a continuous loop while input(23)==false and if one of the other two buttons is pressed it stops the first one and plays the other, only once, and returns to checking if input(23)==false 我希望第一个声音在input(23)==false连续循环运行,如果按下其他两个按钮之一,它将停止第一个声音并播放另一个声音,只有一次,然后返回检查是否input(23)==false

I need this to be done to finish my project, but I don't have the need really to learn python from scratch (at least for now). 我需要完成此工作才能完成我的项目,但是我并不需要真正地从头开始学习python(至少现在是这样)。 at least some guidelines would be greatly appreciated. 至少会有些指导方针。

How about this? 这个怎么样?

play = True
while True:

    if (GPIO.input(24) == False):
        play = False
        os.system('mpg123 -q power-converters.mp3 &')
    else:
        play = True

    if (GPIO.input(25)== False):
        play = False
        os.system('mpg123 -q vader.mp3 &')
    else:
        play = True

    if (play and GPIO.input(23) == False):
        os.system('mpg123 -q binary-language-moisture-evaporators.mp3 &')

    sleep(0.1);

This codes seems working (as I simulated it). 此代码似乎有效(如我所模拟的)。 You may need modify it to suite your need though. 但是,您可能需要对其进行修改以适合您的需求。 For example, if both 24 and 25 are pressed, which one should be played (24 takes precedence over 25 in the code). 例如,如果同时按下24和25,则应播放哪一个(24在代码中优先于25)。

import subprocess
from time import sleep
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN)
GPIO.setup(24, GPIO.IN)
GPIO.setup(25, GPIO.IN) 

proc1 = ''
proc2 = ''
proc3 = ''
while True:
    if GPIO.input(24) == False:
        if proc3:
            proc3.kill()
            proc3 = ''
        if proc2 and proc2.poll() is None:
            proc2.kill()
            proc2 = ''
        if not proc1 or proc1.poll() is not None:
            proc1 = subprocess.Popen(['mpg123','-q','power-converters.mp3'])
    elif GPIO.input(25) == False:
        if proc3:
            proc3.kill()
            proc3 = ''
        if proc1 and proc1.poll() is None:
            proc1.kill()
            proc1 = ''
        if not proc2 or proc2.poll() is not None:
            proc2 = subprocess.Popen(['mpg123','-q','vader.mp3'])
    elif GPIO.input(23) == False:
        if not (proc1 and proc1.poll() is None) and not (proc2 and proc2.poll() is None):
            if not proc3 or proc3.poll() is not None:
                proc3 = subprocess.Popen(['mpg123','-q','binary-language-moisture-evaporators.mp3'])
    elif proc3 and proc3.poll() is None:
        proc3.kill()
        proc3 = ''

    sleep(0.1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM