坚持用python项目

2024-04-25 05:03:17 发布

您现在位置:Python中文网/ 问答频道 /正文


Tags: python
2条回答

这个怎么样?你知道吗

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);

这个代码似乎工作(因为我模拟它)。你可能需要修改它来满足你的需要。例如,如果同时按下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)

相关问题 更多 >