树莓派pigpio实现gpio中断(python版)

树莓派的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("Interrupt by user, clean up cb1 callback setting")
cb1.cancel()
发布了227 篇原创文章 · 获赞 148 · 访问量 34万+

猜你喜欢

转载自blog.csdn.net/toopoo/article/details/105107605
今日推荐