【问题标题】:Python subscribe to an event from separate filePython 从单独的文件订阅事件
【发布时间】:2021-04-04 20:14:53
【问题描述】:

好的,所以我有监控输入引脚的 pi_io.py 文件。每次输入状态更改时都会触发。我想要做的是在另一个文件的另一个类中订阅这个事件。这样事件就会在两个类中触发。

import RPi.GPIO as GPIO

class Pi_Io:
    def __init__(self):        
        self.iscyclepressed = False
        self.cyclestart = 13
        # setup inputs
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(self.cyclestart, GPIO.IN, pull_up_down=GPIO.PUD_UP)
        GPIO.add_event_detect(self.cyclestart, GPIO.BOTH,
                            callback=self.CycleStart_In, bouncetime=50)

    def CycleStart_In(self, channel):
        if not GPIO.input(self.cyclestart):
            self.iscyclepressed = True
            print("Cycle Button Pressed")
        else:
            self.iscyclepressed = False
            print("Cycle Button Released")

Pi_Io()
while True:
    pass

【问题讨论】:

    标签: python events io raspberry-pi


    【解决方案1】:

    在单独的文件中创建您的类的另一个实例。

    from pi_io import Pi_Io
    
    
    instance = Pi_Io()
    # rest of your code
    

    【讨论】:

      猜你喜欢
      • 2010-11-04
      • 1970-01-01
      • 1970-01-01
      • 2021-09-08
      • 1970-01-01
      • 2010-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多