The clock class of Kivy can't work using raspberry Pi2

144 views
Skip to first unread message

周曉超

unread,
Nov 25, 2015, 3:51:46 AM11/25/15
to Kivy users support
Dear sir:
My project code is as following:

#Clock interrupt at 1/60 Hz for GPIO18
import RPi.GPIO as GPIO
from kivy.clock import Clock
toggle_port=18

def main():
       print "System Start........\n"
       GPIO.setmode(GPIO.BCM)
       GPIO.setwarning(False)
       GPIO.setup(toggle_port,GPIO.OUT,initial=GPIO.LOW)
        print "Toggle Port = %d \n" %GPIO.input(toggle_port)
        Clock.schedule_interval(my_callback, 1/60)
        try :
              whie True:
                       pass
        except KeyboardInterrupt:
              Clock.unschedule(my_callback)
              GPIO.cleanup()

def my_callback():
      GPIO.output(toggle_port, not GPIO.input(toggle_port))
      print "Interrupt Toggle Port = %d \n" %GPIO.input(toggle_port)

#Main program logic
if __name__=='__name__'
      main

My question is the console message is as following:
System Start........
Toggle Port =0

It's seem like that the "my_callback()" is not work.
Why the kivy.clock.schedule_interval() is not work?
By the way,....kivy.clock.schedule_once() is not work either.


ZenCODE

unread,
Nov 25, 2015, 6:00:53 AM11/25/15
to Kivy users support
Could be a few things.

#Main program logic
if __name__=='__name__':  # < Needs the colon

      main
() # < Needs the brackets

def my_callback(dt):  #  < Needs the dt parameter



or

def my_callback(*args):  # TO be safe.

Do you not get any errors on the console?



周曉超

unread,
Nov 25, 2015, 7:57:36 PM11/25/15
to Kivy users support
Hi! ZenCODE:
Nice to get your hint. There are  the typing mistakes in my project example. So that I would like to post my original code as following:
clock_sch.py
 
#Clock interrupt at 1/60 Hz for GPIO18
import RPi.GPIO as GPIO
from kivy.clock import Clock
toggle_port=18

def main():
   print "System Start.....\n"
   GPIO.setmode(GPIO.BCM)
   GPIO.setwarnings(False)
   GPIO.setup(toggle_port, GPIO.OUT,initial=GPIO.LOW)
   #GPIO.output(toggle_port, GPIO.LOW)
   print "Toggle Port = %d \n" %GPIO.input(toggle_port)
   Clock.schedule_interval(my_callback, 1/60)
   #my_callback()
   try :
        while True:
              pass
   except KeyboardInterrupt:
        Clock.unschedule(my_callback)
        GPIO.cleanup()

def my_callback(*args):
   GPIO.output(toggle_port, not GPIO.input(toggle_port))
   print "Interrupt Toggle Port = %d \n" %GPIO.input(toggle_port)

#Main program logic
if __name__=='__main__':
   main()   

I think the code statement for my_call() is not suitable. So that I fix it by your hit.
Howwvwer, I still can't get the correct result as the attached picture:

Can anyone tell me that why the callback function can't work?


David Goadby

unread,
Nov 25, 2015, 8:13:21 PM11/25/15
to Kivy users support
Hi,

I am using RPi and want to use similar code in my project although with a lower interrupt rate of 1 per second. I will try this code later today and see what result I get too.

周曉超

unread,
Nov 25, 2015, 9:37:08 PM11/25/15
to Kivy users support
Hi! David:
Nice to get your information. You may try the example for free. However, I think that the problems maybe is some configuration setting or what. I use the official 7 inch TFT panel with touchscreen for the project. As before, the kivy.uix.widget for touch screen (on_touch_down) is not worked either. The case is the ~/.kivy/config.ini setting problems. I wonder that  clock.schedule_interval is the same isue. The condition is happened also in clock.schedule_once() function.
Best regards.
  

David Goadby於 2015年11月26日星期四 UTC+8上午9時13分21秒寫道:

ZenCODE

unread,
Nov 26, 2015, 1:01:57 AM11/26/15
to Kivy users support
Okay, this is certainly not the standard kivy app structure.

   try :
        while True:

Will keep the program in an infinite loop and never free the CPU up to do the clock schedule. Normal kivy apps return a root widget and exit. There is a main Kivy loop which processes messages. Because your are creating your own loop, I suspect the Clock schedule is never given time to execute.

Try again using the standard Kivy app structure.

http://kivy.org/docs/api-kivy.app.html

Cheers

周曉超

unread,
Nov 26, 2015, 3:32:08 AM11/26/15
to Kivy users support
Hi! ZenCODE:
Thanks for your suggestion and recommanded. I will try the kivy.app structure to implement the project.
May I ask a stupid question? Where can I find the examples code or demostration for kivy.clock in programming guide or wiki?
I think that it's better for us to make the try and error.
I try another way. Not to use the kivy.clock schedule as following:
from threading import Timer
....
....
t=Timer(1/60,my_callback)
t.start()
......
.....
But the "Interrupt Toggle Port = 1" just run one time.
Thanks for your explain and kindness answer.
Best regards.
                              Tim Chou

ZenCODE於 2015年11月26日星期四 UTC+8下午2時01分57秒寫道:

ZenCODE

unread,
Nov 27, 2015, 5:37:52 AM11/27/15
to Kivy users support
How about this?

from kivy.app import App
from kivy.uix.label import Label
from kivy.clock import Clock




class TestApp(App):
   
   
def build(self):
       
# instance variables
       
self.label = Label(text='Hello')
       
self.counter = 0
       
Clock.schedule_interval(self.my_callback, 0.1)
       
return self.label


   
def my_callback(self, dt):
       
self.label.text = "{0} - {1}".format(self.counter, dt)
       
self.counter += 1


if __name__ == '__main__':
   
TestApp().run()

周曉超

unread,
Nov 29, 2015, 7:54:22 AM11/29/15
to Kivy users support
Hi! ZenCode:
Have a nice holiday!. Nice to get your sample code. It's very useful for me to work the project.
However, I find another way to handle the animation picture with zip file.
It can be used the kivy.uix.animation calss function to do this. It's more convience than the clock.schdule.
Best regards.
                        Tim Chou.

ZenCODE於 2015年11月27日星期五 UTC+8下午6時37分52秒寫道:

ZenCODE

unread,
Nov 30, 2015, 12:53:56 AM11/30/15
to Kivy users support
Yes, and ir's a better wway to handle it. That's what animation are designed for.

NIce, and good luck :-)
Reply all
Reply to author
Forward
0 new messages