TiLDA MK3/timer

From EMF Badge
Revision as of 14:58, 23 July 2016 by Mbrejza (talk | contribs) (Created page with "See the micropython documentation [https://micropython.org/doc/module/pyb/Timer] for a detailed explanation. Some examples are below. == Function callbacks == <pre> flag =...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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()