等待输入时执行无限循环 [英] performing infinite loop while awaiting input

查看:300
本文介绍了等待输入时执行无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在处理的小项目,它非常简单,所以我希望有人可以帮助我.

I have a little project I'm working on, it's fairly simple so I'm hoping someone can help me.

我正在使用覆盆子pi通过一些非常粗糙的PWM对单个LED进行调光.

I'm using a raspberry pi to dim a single LED with some very crude PWM.

我的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保持在驻留"所确定的亮度下,我需要那部分代码才能永远循环.

Basically, in order for the LED to remain lit at the brightness determined by "dwell" I need that bit of code to continue looping forever.

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

What I would like to do is use something like

dwell=raw_input('brightness:')

因此,当PWM代码循环时,我可以添加一个新的驻留时间值来调整LED的亮度.

so that while the PWM code is looping, I can drop in a new value for dwell to adjust the brightness of the LED.

到目前为止,我所有的努力都导致以下情况之一:

all of my efforts so far result in one of the following:

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

a: the dimming loop executes once only and stops to await input b: the dimming loop will execute infinitely but not allow further input

你们中的一个好人能否为我提供一个代码示例,说明我如何实现这一目标?

can one of you fine people provide me with a code example that explains how I can achieve this?

对于那些感兴趣的人,最终我想做的是通过插座设置驻留时间的值,并使用更好形式的PWM输出来驱动LED筒灯.婴儿脚步:)

for those interested, ultimately what I would like to do is set the value of dwell via sockets and use a better form of PWM output to drive LED downlights. Baby steps :)

推荐答案

您似乎需要多线程

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

关于信号灯

There's probably something about locks or semaphores or mutex...es (mutices?) missing here, but I don't know much about those. Simple things like this seem to work for me.

这篇关于等待输入时执行无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆