我为switch创建了一个简单的python程序。 这个简单的程序工作正常。 代码如下:
import RPi.GPIO as GPIO
from time import sleep
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(40, GPIO.IN)
count = 0
flag = 0
while True:
input = GPIO.input(40)
if ((not flag) and input):
flag = input
count += 1
print "--------------------"
print "Button is pressed"
print "Flag = ", flag
print "Count = ", count
sleep(0.05)
elif ((not input) and flag):
flag = input
count += 1
print "--------------------"
print "Button is debounce"
print "Flag = ", flag
print "Count = ", count
sleep(0.05)
GPIO.cleanup()
但是当我尝试创建一个wxPython程序来显示输出时 第一次点击按钮的程序工作,但是当我释放按钮时,wxPython程序的输出保持不变,除了更新的时间,当我释放按钮时停止,当我按下按钮时继续。 wxPython程序如下:
import RPi.GPIO as GPIO
import datetime
import os
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(40, GPIO.IN)
count = 0
flag = 0
button = "None"
input = GPIO.input(40)
global update_time
update_time = datetime.datetime.strftime(datetime.datetime.now(), '%d-%m-%y %H:%M:%S')
try:
import wx
except ImportError:
raise ImportError, "The wxPython module is required to run this program."
class OnOffApp_wx(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, size = (500, 200), title = 'ON / OFF Status')
self.SetBackgroundColour(wx.WHITE)
self.SetWindowStyle(wx.STAY_ON_TOP)
self.parent = parent
self.initialize()
def initialize(self):
sizer = wx.GridBagSizer()
font = wx.Font(20, wx.DECORATIVE, wx.ITALIC, wx.NORMAL)
self.SetFont(font)
self.label1 = wx.StaticText(self, -1, label=u'Button Status : {}'.format(button))
self.label1.SetBackgroundColour(wx.WHITE)
self.label1.SetForegroundColour(wx.BLACK)
sizer.Add(self.label1, (1,0), (1,2), wx.EXPAND)
self.label2 = wx.StaticText(self, -1, label=u'Flag Status : {}'.format(flag))
self.label2.SetBackgroundColour(wx.WHITE)
self.label2.SetForegroundColour(wx.BLACK)
sizer.Add(self.label2, (2,0), (1,3), wx.EXPAND)
self.label3 = wx.StaticText(self, -1, label=u'Count Status : {}'.format(count))
self.label3.SetBackgroundColour(wx.WHITE)
self.label3.SetForegroundColour(wx.BLACK)
sizer.Add(self.label3, (3,0), (1,4), wx.EXPAND)
self.label4 = wx.StaticText(self, -1, label=u'Time Updated : {}'.format(update_time))
self.label4.SetBackgroundColour(wx.WHITE)
self.label4.SetForegroundColour(wx.BLACK)
sizer.Add(self.label4, (4,0), (1,5), wx.EXPAND)
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.on_timer, self.timer)
self.timer.Start(50)
self.SetSizer(sizer)
self.Show(True)
def on_timer(self,event):
global update_time
update_time = datetime.datetime.strftime(datetime.datetime.now(), '%d-%m-%y %H:%M:%S')
count = 0
flag = 0
input = GPIO.input(40)
if ((not flag) and input):
flag = input
count += 1
button = 'Pressed'
#self.label1.SetLabel("Button Status : Pressed")
self.label1.SetLabel("Button Status : {}".format(button))
self.label2.SetLabel("Flag Status : {}".format(flag))
self.label3.SetLabel("Count Status : {}".format(count))
self.label4.SetLabel("Time Updated : {}".format(update_time))
elif ((not input) and flag):
flag = input
count += 1
button = 'Debounce'
#self.label1.SetLabel("Button Status : Debounce")
self.label1.SetLabel("Button Status : {}".format(button))
self.label2.SetLabel("Flag Status : {}".format(flag))
self.label3.SetLabel("Count Status : {}".format(count))
self.label4.SetLabel("Time Updated : {}".format(update_time))
if __name__ == "__main__":
Rs = wx.App()
OnOffApp_wx(None, -1, 'ON / OFF Status')
Rs.MainLoop()
GPIO.cleanup()
帮助我使wxPython程序正常工作,因为按下按钮时输入和标志的状态变为1,计数增加1。当我释放按钮时,输入和标志的状态变为0,计数增加1。
答案 0 :(得分:1)
在on_timer
中,您每隔50毫秒将count
和flag
重新设置为零。
要么将它们作为变量传递到函数中,要么为了您的目的,在global
函数中将它们简单地声明为on_timer
会更容易。
所以:
count = 0
flag = 0
变为:
global count, flag