您好我正在开发脚本,它会异步闪烁一个或几个LED。这个脚本将在另一个脚本中使用,这就是我想要使用启动线程闪烁的函数并闪烁直到其他人停止它的原因。问题是我无法告诉他们停止眨眼。我用它作为example并且无法从函数的“外部”设法做到这一点。我知道这与函数继承有关,但我无法解决它,我猜我是python的新手。
#!usr/bin/env python3
import time
import threading
import RPi.GPIO as GPIO
def blink_pin(pin, delay, pin_stop):
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin, GPIO.OUT)
print("Blinking pin ", pin)
while not pin_stop.is_set():
GPIO.output(pin, GPIO.HIGH)
print("pin ", pin, "is high.")
time.sleep(delay)
GPIO.output(pin, GPIO.LOW)
print("pin", pin, "is low.")
time.sleep(delay)
print("stopped Blinking pin", pin)
def low(pin):
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.LOW)
def blink(pin, delay, state):
pin12_stop = threading.Event()
pin12 = threading.Thread(target=blink_pin, args=(pin, delay, pin12_stop))
pin13_stop = threading.Event()
pin13 = threading.Thread(target=blink_pin, args=(pin, delay, pin13_stop))
if state == 1:
if pin == 12:
pin12.start()
elif pin == 13:
pin13.start()
else:
print("No such pin available")
elif state == 0:
if pin == 12:
pin12_stop.set()
low(12)
elif pin == 13:
pin13_stop.set()
low(13)
else:
print("No such pin available")
else:
print("No such state available", state)
def main():
blink(12, 0.25, 1)
time.sleep(5)
blink(12, 0.3, 0)
blink(13, 0.5, 1)
time.sleep(5)
blink(13, 0.1, 0)
GPIO.cleanup()
if __name__ == "__main__":
main()