赞
踩
说明:在这里对评论中的问题我统一做一个说明。
- gpio的中断Edge设置和回调设置,是在代码中
pi.callback(ir_pin, pigpio.RISING_EDGE, cb_func)
实现。- 代码最后的
try ... except KeyboardInterrupt
只是python的一个异常处理写法,代码的核心功能不在这里,不写try ... except KeyboardInterrupt
也是可以的。我写这段仅仅是为了表示用户可以通过中断while循环然后执行cb1.cancel()来取消gpio的中断功能。- 看不懂的同学,罚你们把代码再读100遍。没看懂代码就评论不是一个好习惯。
树莓派的pigpio库提供了一个方法来设置gpio中断,即callback():
callback(user_gpio, edge, func)
参数(Parameters):
user_gpio:= 0-31.
edge:= EITHER_EDGE, RISING_EDGE (default), or FALLING_EDGE.
func:= user supplied callback function.
回调函数func接受三个参数:
The user supplied callback receives three parameters, the GPIO, the level, and the tick.
Parameter | Value | Meaning |
---|---|---|
GPIO | 0-31 | The GPIO which has changed state |
level | 0-2 | 0 = change to low (a falling edge) 1 = change to high (a rising edge) 2 = no level change (a watchdog timeout) |
tick | 32 bit | The number of microseconds since boot WARNING: this wraps around from 4294967295 to 0 roughly every 72 minutes |
pigpio library - callback 文档地址
例程:
# -*- coding:UTF-8 -*- import pigpio import time ir_pin = 17 pi = pigpio.pi() def cb_func(gpio, level, tick): print(gpio, level, tick) print("\n GPIO:%d Rising Edge Detected.Sending message to DingTalk."%ir_pin) dingmessage() cb1 = pi.callback(ir_pin, pigpio.RISING_EDGE, cb_func) Count = 1 while True: try: print("%d Seconds in main loop"%(Count*5)) time.sleep(5) Count += 1 pass except KeyboardInterrupt: break pass pass print("Program terminated by user, clean up cb1 callback setting") cb1.cancel()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。