我们有一个计时器,我们想在其中一个GPIO进入1
时停止其中一个计时器到目前为止,我们有这段代码:
type.GetConstructor(<constructor signature>).Invoke(<arguments>);
我们正试图让定时器彼此独立停止,但两者都在同一时间停止
我们知道问题出在#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from Tkinter import *
import time
from functools import partial
import RPi.GPIO as GPIO
class Chronometre(Frame): # definition de la class'Chronomètre' dans Frame
def __init__(self, parent=None, **kw): # definition initiale de la class
Frame.__init__(self, parent, kw)
self._start = 0.0
self._elapsedtime = 0.0
self._running = 0
self.timestr = StringVar()
self.makeWidgets()
def makeWidgets(self): #definition pour afficher le temps
l = Label(self, textvariable=self.timestr)
self._setTime(self._elapsedtime)
l.pack(fill=X, expand=NO, pady=10, padx=10)
def _update(self): # definition du reset
self._elapsedtime = time.time() - self._start
self._setTime(self._elapsedtime)
self._timer = self.after(50, self._update)
if(GPIO.input(22) ==1):
print("Faux Départ!")
if(GPIO.input(24) ==1):
self.Stop()
if(GPIO.input(23) ==1):
self.Stop()
def _setTime(self, elap): #définition du temps en minutes/secondes/millisecondes
minutes = int(elap/60)
seconds = int(elap - minutes*60.0)
hseconds = int((elap - minutes*60.0 - seconds)*100)
self.timestr.set('%02d:%02d:%03d' % (minutes, seconds, hseconds))
def Start(self, event=None): #lancement du chrono s'il est en arrêt
if not self._running:
self._start = time.time() - self._elapsedtime
self._update()
self._running = 1
def Stop(self): # arrêt du chrono s'il est en marche
if self._running:
self.after_cancel(self._timer)
self._elapsedtime = time.time() - self._start
self._setTime(self._elapsedtime)
self._running = 0
def Reset(self): # remise à zéro du chrono
self._start = time.time()
self._elapsedtime = 0.0
self._setTime(self._elapsedtime)
def get(self) :
return self._running
def start_chronos(chronos, event):
for chrono in chronos:
chrono.Start()
def reset_chronos(chronos, event):
for chrono in chronos:
chrono.Reset()
def stop_chrono_gauche(chrono, event):
chrono.Stop()
def stop_chrono_droit(chrono, event):
chrono.Stop()
def main(): # definition de la fenetre de tkinter
root = Tk()
root.title('projet 2014-2015')
chronoDroit = Chronometre(root)
chronoDroit.pack(side=LEFT,padx=30,pady=30)
chronoGauche = Chronometre(root)
chronoGauche.pack(side=RIGHT,padx=30,pady=30)
root.bind('<a>', partial(start_chronos, (chronoDroit, chronoGauche)))
root.bind('<z>', partial(stop_chrono_gauche, (chronoDroit)))
root.bind('<e>', partial(stop_chrono_droit, (chronoGauche)))
root.bind('<r>', partial(reset_chronos, (chronoDroit, chronoGauche)))
GPIO.setmode(GPIO.BCM)
GPIO.setup(22, GPIO.IN)
GPIO.setup(23, GPIO.IN)
GPIO.setup(24, GPIO.IN)
root.mainloop()
if __name__ == '__main__':
main()
,但我们不知道用
我们怎么能做到这一点?
答案 0 :(得分:2)
您的问题是所有对象都检查两个GPIO,并在任何一个上停止 - 如果您希望对象具有不同的行为,则它们必须不同。最简单的方法是告诉每个对象构建它时应检查的GPIO。作为一个浓缩的例子:
class Chronometre(Frame):
def __init__(self, gpio, parent=None, **kw):
...
self._stop_gpio = gpio
...
def _update(self):
...
if(GPIO.input(self._stop_gpio) == 1):
self.Stop()
...
chronoDroit = Chronometre(23, root)
chronoGauche = Chronometre(24, root)