TiLDA MK3/timer: Difference between revisions

From EMF Badge
Jump to navigation Jump to search
(fix broken link to micropython timer docs)
m (use https)
Line 1: Line 1:
See the micropython documentation [http://docs.micropython.org/en/latest/pyboard/library/pyb.Timer.html] for a detailed explanation. Some examples are below.
See the micropython documentation [https://docs.micropython.org/en/latest/pyboard/library/pyb.Timer.html] for a detailed explanation. Some examples are below.





Revision as of 00:52, 2 March 2017

See the micropython documentation [1] for a detailed explanation. Some examples are below.


Function callbacks

flag = 0

#note, this callback needs to take one parameter, which is what timer is calling it
def tim_callback(t):
    global flag
    flag = 1

timer = pyb.Timer(3)
timer.init(freq=1)
timer.callback(tim_callback)

....

timer.deinit()

or, using lambdas

flag = 0

#the use of lambda avoids needing the function to take an argument
def tim_callback():
    global flag
    flag = 1

timer = pyb.Timer(3)
timer.init(freq=1)
timer.callback(lambda t: tim_callback())

....

timer.deinit()