如何显示一个类的函数的连续计算值?

时间:2019-05-30 20:04:55

标签: python multithreading

我希望程序在引脚上检测到信号梯度后立即连续运行并从方法(func1-func4)中输出值。

这只是了解我如何访问值的测试代码,因为我编写了一个“程序”,其表面显示了我仅使用全局变量的计算机上不间断的值,并且我不想发布因为令人尴尬 我想摆脱所有的全局变量

一个树莓派连接到我通过gpio截获4个信号的机器上。

cavity = 2
count = 0
scrap = 0
uptime = datetime.timedelta(0)
downtime = datetime.timedelta(0)

def func1(channel):
    if GPIO.input(7) == 0:
        while True:
            uptime = uptime + datetime.timedelta(0,1)
            time.sleep(1)
            if GPIO.input(7) == 1 or GPIO.input(37) == 0:
                break

def func2(channel):
    scrap = scrap + cavity

def func3(channel):
    count = count + cavity

def func4(channel):
    if GPIO.input(37) == 0:
        while True:
            downtime = downtime + datetime.timedelta(0,1)
            if GPIO.inpu(37) == 1:
                break

GPIO.add_event_detect(7, GPIO.RISING, callback = func1, bouncetime = 100)           #Run
GPIO.add_event_detect(29, GPIO.RISING, callback = func2, bouncetime = 100)          #Scrap
GPIO.add_event_detect(13, GPIO.RISING, callback = func3, bouncetime = 100)          #Count
GPIO.add_event_detect(37, GPIO.RISING, callback = func4, bouncetime = 100)          #Alarm

class Output(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.show()

    def show(self):
        print('ich werde aufgerufen')
        while True:
            print(uptime)
            print(scrap)
            print(count)
            print(downtime)
            print('############################')
            time.sleep(5)


thread2 = Output()
thread2.start()

仅输出零,这是预期的,但是我如何访问变量,以便在信号检测时值增加

2 个答案:

答案 0 :(得分:0)

您的函数只是在创建自己的局部变量,因此全局变量不会更改。

尝试一下:

def func1(channel):
    global uptime
    if GPIO.input(7) == 0:
        while True:
            uptime = uptime + datetime.timedelta(0,1)
            time.sleep(1)
            if GPIO.input(7) == 1 or GPIO.input(37) == 0:
                break

def func2(channel):
    global scrap 
    scrap = scrap + cavity

def func3(channel):
    global count
    count = count + cavity

def func4(channel):
    global downtime 
    if GPIO.input(37) == 0:
        while True:
            downtime = downtime + datetime.timedelta(0,1)
            if GPIO.inpu(37) == 1:
                break

答案 1 :(得分:0)

您的代码分为两类:

class Events:
    def __init__(self):
        GPIO.add_event_detect(7, GPIO.RISING, callback = self.func1, bouncetime = 100)           #Run
        GPIO.add_event_detect(29, GPIO.RISING, callback = self.func2, bouncetime = 100)          #Scrap
        GPIO.add_event_detect(13, GPIO.RISING, callback = self.func3, bouncetime = 100)          #Count
        GPIO.add_event_detect(37, GPIO.RISING, callback = self.func4, bouncetime = 100)          #Alarm

        self.cavity = 2
        self.count = 0
        self.scrap = 0
        self.uptime = datetime.timedelta(0)
        self.downtime = datetime.timedelta(0)

    def func1(self, channel):
        if GPIO.input(7) == 0:
            while True:
                self.uptime = self.uptime + datetime.timedelta(0,1)
                time.sleep(1)
                if GPIO.input(7) == 1 or GPIO.input(37) == 0:
                    break

    def func2(self, channel):
        self.scrap = self.scrap + cavity

    def func3(self, channel):
        self.count = self.count + cavity

    def func4(self, channel):
        if GPIO.input(37) == 0:
            while True:
                self.downtime = self.downtime + datetime.timedelta(0,1)
                if GPIO.inpu(37) == 1:
                    break


class Output(threading.Thread):
    def __init__(self, events):
        threading.Thread.__init__(self)
        self.events = events
        self.show()

    def show(self):
        print('ich werde aufgerufen')
        while True:
            print(self.events.uptime)
            print(self.events.scrap)
            print(self.events.count)
            print(self.events.downtime)
            print('############################')
            time.sleep(5)


events = Events()
thread2 = Output(events)
thread2.start()