0

我正在做一个小项目,它相当简单,所以我希望有人能帮助我。

我正在使用树莓派通过一些非常粗糙的 PWM 来调暗单个 LED。

我的 PWM 代码如下所示:

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(7, GPIO.OUT)
frequency = 0.005
dwell = 0.0001
while True:
    time.sleep(frequency)
    GPIO.output(7, 1)
    time.sleep(dwell)
    GPIO.output(7, 0)

基本上,为了让 LED 以“停留”确定的亮度保持点亮,我需要那段代码来永远循环下去。

我想做的是使用类似的东西

dwell=raw_input('brightness:')

这样当 PWM 代码循环时,我可以输入一个新的值来调整 LED 的亮度。

到目前为止,我所做的所有努力都导致了以下结果之一:

a: 调光循环只执行一次并停止等待输入 b: 调光循环将无限执行但不允许进一步输入

你们中的一个好人可以为我提供一个代码示例来解释我如何实现这一目标吗?

对于那些感兴趣的人,最终我想做的是通过插座设置dwell的值并使用更好的PWM输出形式来驱动LED筒灯。宝贝步骤:)

4

1 回答 1

0

看起来你需要多线程

# import the module
import threading

# define a function to be called in the other thread
def get_input():
    while True:
        dwell=raw_input()

# create a Thread object
input_thread=threading.Thread(target=get_input)

# start the thread
input_thread.start()

# now enter the infinite loop
while True:
    time.sleep(frequency)
    GPIO.output(7, 1)
    time.sleep(dwell)
    GPIO.output(7, 0)

这里可能缺少关于信号量互斥体......es(互斥体?)的东西,但我对这些了解不多。像这样简单的事情似乎对我有用。

于 2015-03-01T05:59:32.137 回答