is_held 的 gpiozero 按钮问题
gpiozero button issue with is_held
我正在为万圣节制作一个南瓜派,但在集成按钮时遇到了问题;具体来说,is_held 位。它由构成蜡烛的 3 个 LED 灯(1 个黄色闪烁、1 个黄色 "breathing" 和 1 个橙色常亮)和一个红色 LED 灯组成,当蜡烛熄灭时,它会逐渐亮起,然后发出令人毛骨悚然的笑声。我已经集成了启动一切的按钮,但是当我按住按钮停止它时,没有任何反应。我对编码还很陌生,所以对我的主要问题或一般反馈的任何帮助都非常感谢!
from gpiozero import PWMLED, Button
from gpiozero.tools import random_values
from signal import pause
from time import sleep
import subprocess
import random
led_flicker_yellow = PWMLED(17, frequency = 1500)
led_breath_yellow = PWMLED(18, frequency = 1500)
led_steady_orange = PWMLED(27, frequency = 1500)
led_red = PWMLED(24, frequency = 1500)
button = Button(2, hold_time = 2)
def candle_lights():
led_flicker_yellow.source = random_values()
led_flicker_yellow.souce_delay = random.uniform(0.05,0.1)
led_breath_yellow.pulse(15,10)
led_steady_orange.value = 0.20
def candle_out():
led_flicker_yellow.source = led_pulse_yellow
led_breath_yellow.pulse(0,3, n=1)
led_steady_orange.off()
def pumpkin_pi():
subprocess.Popen (["omxplayer", "-o", "alsa", "--loop", "/home/pi/Desktop/LED_PROJECTS/SoundEffects/candle.mp3"])
while True:
candle_lights()
sleep(300)
candle_out()
subprocess.Popen(["omxplayer", "-o", "alsa", "/home/pi/Desktop/LED_PROJECTS/SoundEffects/laugh.mp3"])
led_red.pulse(3,0,n=1)
sleep(3)
led_red.blink(0.03,0.03,n=35)
sleep(2)
candle_lights()
if button.is_held:
subprocess.call(["pkill", "omx"])
break
button.when_pressed = pumpkin_pi
好的 - 归功于 RPi 论坛上的 thagrol,但这里就是答案。在这里,我正在控制一个 LED 灯序列,它通过 python 和 gpiozero.
使用带有按钮的 while True 循环
from gpiozero import PWMLED, Button
from time import sleep
from signal import pause
import threading
led = PWMLED(24)
button = Button(2, hold_time=2)
running = False
def pumpkin_pi_loop():
while running:
led.value = 0
sleep(1)
led.value = 0.5
sleep(1)
led.value = 1
sleep(1)
def button_press():
global running
if not running:
led.value = 1
sleep(3)
running = True
loop_thread = threading.Thread(target=pumpkin_pi_loop, daemon=True)
loop_thread.start()
else:
running = False
def button_hold():
global running
running = False
button.when_pressed = button_press
button.when_held = button_hold
pause()
我正在为万圣节制作一个南瓜派,但在集成按钮时遇到了问题;具体来说,is_held 位。它由构成蜡烛的 3 个 LED 灯(1 个黄色闪烁、1 个黄色 "breathing" 和 1 个橙色常亮)和一个红色 LED 灯组成,当蜡烛熄灭时,它会逐渐亮起,然后发出令人毛骨悚然的笑声。我已经集成了启动一切的按钮,但是当我按住按钮停止它时,没有任何反应。我对编码还很陌生,所以对我的主要问题或一般反馈的任何帮助都非常感谢!
from gpiozero import PWMLED, Button
from gpiozero.tools import random_values
from signal import pause
from time import sleep
import subprocess
import random
led_flicker_yellow = PWMLED(17, frequency = 1500)
led_breath_yellow = PWMLED(18, frequency = 1500)
led_steady_orange = PWMLED(27, frequency = 1500)
led_red = PWMLED(24, frequency = 1500)
button = Button(2, hold_time = 2)
def candle_lights():
led_flicker_yellow.source = random_values()
led_flicker_yellow.souce_delay = random.uniform(0.05,0.1)
led_breath_yellow.pulse(15,10)
led_steady_orange.value = 0.20
def candle_out():
led_flicker_yellow.source = led_pulse_yellow
led_breath_yellow.pulse(0,3, n=1)
led_steady_orange.off()
def pumpkin_pi():
subprocess.Popen (["omxplayer", "-o", "alsa", "--loop", "/home/pi/Desktop/LED_PROJECTS/SoundEffects/candle.mp3"])
while True:
candle_lights()
sleep(300)
candle_out()
subprocess.Popen(["omxplayer", "-o", "alsa", "/home/pi/Desktop/LED_PROJECTS/SoundEffects/laugh.mp3"])
led_red.pulse(3,0,n=1)
sleep(3)
led_red.blink(0.03,0.03,n=35)
sleep(2)
candle_lights()
if button.is_held:
subprocess.call(["pkill", "omx"])
break
button.when_pressed = pumpkin_pi
好的 - 归功于 RPi 论坛上的 thagrol,但这里就是答案。在这里,我正在控制一个 LED 灯序列,它通过 python 和 gpiozero.
使用带有按钮的 while True 循环from gpiozero import PWMLED, Button
from time import sleep
from signal import pause
import threading
led = PWMLED(24)
button = Button(2, hold_time=2)
running = False
def pumpkin_pi_loop():
while running:
led.value = 0
sleep(1)
led.value = 0.5
sleep(1)
led.value = 1
sleep(1)
def button_press():
global running
if not running:
led.value = 1
sleep(3)
running = True
loop_thread = threading.Thread(target=pumpkin_pi_loop, daemon=True)
loop_thread.start()
else:
running = False
def button_hold():
global running
running = False
button.when_pressed = button_press
button.when_held = button_hold
pause()