Reputation: 671
We have a timer and we want to stop one of the timer when one of the GPIO goes to 1
so far we have this code:
#!/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()
we are trying to get it so that the timers stop independently from each other, but both of them are stopping at the same time
we know that the problem is with self.Stop()
but we have no clue what to replace it with
how could we make this work?
Upvotes: 0
Views: 84
Reputation: 89077
Your problem is that all your objects check both GPIOs, and stop on either one - if you want your objects to have different behaviour, they have to be different. The easiest way to do this is to tell each object what GPIO it should check when you construct it. As a condensed example:
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)
Upvotes: 2