【创龙TL570x-EVM】按键控制LED灯
<div class='showpostmsg'> 本帖最后由 lugl4313820 于 2022-5-23 07:07 编辑<p>【前言】例程给了Python控制LED闪烁、检测按键的程序,结合两个案例学习综合一个按键控制例子。</p>
<p>【分析】LED闪灯,是循环检测是否退出的标志来维护的,我这里加入thread线程来开启。按键程序是even来检测的,如果再加入线程就会出现不能退出的现象。</p>
<p>【实现思路】按测键检来判断按键是否按下,然后更新延时秒数来控制LED灯闪烁的快慢。</p>
<p>1、初始化LED:</p>
<pre>
<code> def __init__(self):
self.led_path = "/sys/class/leds/" # Led path
self.leds = [] # Save the led found
self.enumerate_led()
self.led_time = 0.1
self.key_sta = 1
""" enumerate all led """
def enumerate_led(self):
led_name = "user-led"
""" find led """
for filename in os.listdir(self.led_path):
if led_name in filename:
self.leds.append(os.path.join(self.led_path, filename))
if len(self.leds) == 0:
return False
"""
led sort, e.g.
/sys/class/leds/user-led0
/sys/class/leds/user-led1
/sys/class/leds/user-led2
"""
self.leds.sort()
print "find leds:"
for led in self.leds:
print led
return True</code></pre>
<p>2、LED闪烁程序:</p>
<pre>
<code>""" control led flash """
def flash_led(self):
led_num = len(self.leds)
while not KeyDevice.quit_flag:
""" Turn on leds """
for i in range(led_num):
""" Set the led brightness value to 1 to turn on the led """
ret = os.system("echo 1 > %s/brightness" % (self.leds<i>))
if ret != 0:
print "Error: Failed to turn on %s" % self.leds<i>
""" Keep the leds on for 500 ms """
time.sleep(self.led_time*self.key_sta)
for i in range(led_num):
""" Set the led brightness value to 0 to turn off the LED """
os.system("echo 0 > %s/brightness" % (self.leds<i>))
""" Keep the leds off for 500 ms """
time.sleep(self.led_time*self.key_sta)</i></i></i></code></pre>
<p><i><i><i>3、按键检测程序:</i></i></i></p>
<pre>
<i><i><i>
<code>def open_device(self, key_dev):
try:
""" open key device, if success, return True"""
self.key_dev = evdev.InputDevice(key_dev)
return True
except Exception as e:
print e
return False
def listen_key_pressed(self):
try:
print "Please press the key to test.\n"
"""
Listen for the button to be pressed
event.value: 1: pressed; 0: released
"""
for event in self.key_dev.read_loop():
if event.type == evdev.ecodes.EV_KEY:
if event.code == evdev.ecodes.KEY_PROG1 and event.value == 1:
print "User key0 pressed!\n"
self.key_sta = 1
elif event.code == evdev.ecodes.KEY_PROG2 and event.value == 1:
print "User key1 pressed!\n"
self.key_sta = self.key_sta + 1
if self.key_sta >20:
self.key_sta = 1
except Exception as e:
if not KeyDevice.quit_flag:
print e
</code></i></i></i></pre>
<p><i><i><i>4、CTRL+C退出程序:</i></i></i></p>
<pre>
<i><i><i>
<code> @classmethod
def stop(cls):
print "ctrl + c ..."
cls.quit_flag = True
sys.exit()</code></i></i></i></pre>
<p><i><i><i>5、CTRL+C信号:</i></i></i></p>
<pre>
<i><i><i>
<code>def sig_handle(signum, frame):
print "ctrl + c ..."
KeyDevice.stop()
sys.exit()</code></i></i></i></pre>
<p><i><i><i>6、启动:</i></i></i></p>
<p><i><i><i>if __name__ == '__main__':</i></i></i></p>
<p><i><i><i> key_dev = "/dev/input/event0" # key deivce path</i></i></i></p>
<p><i><i><i> try:</i></i></i></p>
<p><i><i><i> """ Ctrl+c handler """</i></i></i></p>
<p><i><i><i> signal.signal(signal.SIGINT, sig_handle)</i></i></i></p>
<p><i><i><i> key_device = KeyDevice()</i></i></i></p>
<p><i><i><i> </i></i></i></p>
<p><i><i><i> """ open key device """</i></i></i></p>
<p><i><i><i> res = key_device.open_device(key_dev)</i></i></i></p>
<p><i><i><i> if not res:</i></i></i></p>
<p><i><i><i> print "open %s failed!" % key_dev</i></i></i></p>
<p><i><i><i> exit(3)</i></i></i></p>
<p><i><i><i> t1 = threading.Thread(target=key_device.flash_led)</i></i></i></p>
<p><i><i><i> t1.start()</i></i></i></p>
<p><i><i><i> """ Listen for the button to be pressed """</i></i></i></p>
<p><i><i><i> key_device.listen_key_pressed()</i></i></i></p>
<p><i><i><i> except Exception, exc:</i></i></i></p>
<p><i><i><i> print exc</i></i></i></p>
<p><i><i><i>【实验现象】程序启动后,三个灯快速闪烁,按下按键4,灯闪得慢下来,慢到2秒一次时,再按一下又回到原来的闪烁频率。</i></i></i></p>
<p><i><i><i>本次实验主要学习了python 检测GPIO的输入输出控制。</i></i></i></p>
<p><i><i><i>【总结】python编程还是相对容易的,但是遗憾的就是event只在python2.7中支持,现象已经用习惯了python3,突然回到python2还有点不习惯,python中pip也不支持安装包,所以发挥不出来。希望官方出个如何添加包的教程。</i></i></i></p>
<p><i><i><i><iframe allowfullscreen="true" frameborder="0" height="510" src="https://training.eeworld.com.cn/shareOpenCourseAPI?isauto=true&lessonid=33389" style="background:#eee;margin-bottom:10px;" width="700"></iframe><br />
</i></i></i></p>
</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
(function($){
var postHeight = getTextHeight(400);
$(".showpostmsg").html($(".showpostmsg").html());
$(".showpostmsg").after(loginstr);
$(".showpostmsg").css({height:postHeight,overflow:"hidden"});
})(jQuery);
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script> <p>学习了</p> <p>用什么命令可以控制led</p> hahhk 发表于 2022-5-25 10:31
用什么命令可以控制led
<p>好几种命令都可以,.sh操作文件,C也可以、Python也可以,N种</p>
<p>感谢楼主的分享,谢谢................</p>
页:
[1]