PDA

View Full Version : Python/PyGTK gobject.timeout_add


Nunslaughter
April 15th, 2008, 08:18 AM
Here a bit of my code (very stripped):

self.maintimer = gobject.timeout_add(1000, self.checktemp)

def checktemp(self):
if temp < 40 :
print "lower then 40"
# do a command 1
else:
print "higher then 40"
# do command 2
return True

OK, nothing wrong with this, it works just fine. It prints out every second if the temp is higher or lower then 40. The problem is the commands. If I enter a command, it will also do that command every second, but it only needs to be done once.

I mean: if the temp is below 40, do command 1 (only once!). Then if the temp rises to above 40, it should do command 2 (but again only once!). When the temp drops again to below 40, it should do command 1 again (and again only once!).

How can this be done?

loell
April 15th, 2008, 08:31 AM
probably

return False

according to gtk doc

The function is called repeatedly until it returns FALSE, at which point the timeout is automatically destroyed and the function will not be called again.

Nunslaughter
April 15th, 2008, 08:34 AM
Then the whole loop will stop. It should keep checking for the temp, but the commands should only be executed once if the temp is above or below 40.

EXCiD3
April 15th, 2008, 08:38 AM
What if you use another variable to keep track of the previous temp recorded. If this value does not match (temp < 40) then it will know to display it. Do the same thing for the other one also and then you should only get the temp change displayed once :)

loell
April 15th, 2008, 08:41 AM
yeah, just use a global or a static variable , for tracking :)

Siph0n
April 15th, 2008, 08:42 AM
Just made this really quick, without testing... but get the idea?

first = True
over = True
def checktemp(self):
if temp < 40 :
if over == True:
first = True
over = False
print "lower then 40"
if first:
do command 1
first = False
else:
if over == False:
first = True
over = True
print "higher then 40"
if first:
do command 2
first = False
return True

Nunslaughter
April 15th, 2008, 08:59 AM
Nope, he keeps doing the commands every second.

WW
April 15th, 2008, 09:54 AM
Perhaps something like this...


prev_command = 0


def checktemp(self):
if temp < 40 and prev_command != 1:
print "lower than 40"
# do a command 1
prev_command = 1
elif temp > 40 and prev_command != 2:
print "higher than 40"
# do command 2
prev_command = 2
return True

(I also corrected the spelling. :) )

EXCiD3
April 15th, 2008, 10:19 AM
Perhaps something like this...


prev_command = 0


def checktemp(self):
if temp < 40 and prev_command != 1:
print "lower than 40"
# do a command 1
prev_command = 1
elif temp > 40 and prev_command != 2:
print "higher than 40"
# do command 2
prev_command = 2
return True
(I also corrected the spelling. :) )

Yep thats what i was trying to recommend pretty much, only problem was i dont know any python so its hard to give an example :lolflag:

Nunslaughter
April 15th, 2008, 10:28 AM
I have no idea why, but it doesn't work either. I still get the output of print and the command every second.


Then, than, all the same ;)

WW
April 15th, 2008, 10:37 AM
It could be a scoping problem. My code snippet was not necessarily working code. prev_command must persist between calls. Perhaps change it to self.prev_command.

EXCiD3
April 15th, 2008, 10:42 AM
nvm...lol

Nunslaughter
April 15th, 2008, 10:50 AM
Damn, I already tried that, but made a typo. Tried again, and it seems that it works. I'll have a further look at it, but thanks already!

WW
April 15th, 2008, 11:01 AM
Then, than, all the same ;)
Than I guess this sentence will bother you less then it bothers me. :-D