#Main program logic
if __name__=='__name__': # < Needs the colon
main() # < Needs the brackets
def my_callback(dt): # < Needs the dt parameter
def my_callback(*args): # TO be safe.
#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?
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
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()