2015-02-23 08:18:33 -05:00
|
|
|
# check callback feature of the timer class
|
|
|
|
|
|
|
|
import pyb
|
|
|
|
from pyb import Timer
|
|
|
|
|
2023-02-01 19:51:48 -05:00
|
|
|
|
2015-02-23 08:18:33 -05:00
|
|
|
# callback function that disables the callback when called
|
|
|
|
def cb1(t):
|
|
|
|
print("cb1")
|
|
|
|
t.callback(None)
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2015-02-23 08:18:33 -05:00
|
|
|
# callback function that disables the timer when called
|
|
|
|
def cb2(t):
|
|
|
|
print("cb2")
|
|
|
|
t.deinit()
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2015-02-23 08:18:33 -05:00
|
|
|
# callback where cb4 closes over cb3.y
|
|
|
|
def cb3(x):
|
|
|
|
y = x
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2015-02-23 08:18:33 -05:00
|
|
|
def cb4(t):
|
|
|
|
print("cb4", y)
|
|
|
|
t.callback(None)
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2015-02-23 08:18:33 -05:00
|
|
|
return cb4
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2015-02-23 08:18:33 -05:00
|
|
|
# create a timer with a callback, using callback(None) to stop
|
2015-03-20 19:50:33 -04:00
|
|
|
tim = Timer(1, freq=100, callback=cb1)
|
|
|
|
pyb.delay(5)
|
|
|
|
print("before cb1")
|
|
|
|
pyb.delay(15)
|
2015-02-23 08:18:33 -05:00
|
|
|
|
|
|
|
# create a timer with a callback, using deinit to stop
|
2015-03-20 19:50:33 -04:00
|
|
|
tim = Timer(2, freq=100, callback=cb2)
|
|
|
|
pyb.delay(5)
|
|
|
|
print("before cb2")
|
|
|
|
pyb.delay(15)
|
2015-02-23 08:18:33 -05:00
|
|
|
|
|
|
|
# create a timer, then set the freq, then set the callback
|
|
|
|
tim = Timer(4)
|
2015-03-20 19:50:33 -04:00
|
|
|
tim.init(freq=100)
|
2015-02-23 08:18:33 -05:00
|
|
|
tim.callback(cb1)
|
2015-03-20 19:50:33 -04:00
|
|
|
pyb.delay(5)
|
|
|
|
print("before cb1")
|
|
|
|
pyb.delay(15)
|
2015-02-23 08:18:33 -05:00
|
|
|
|
|
|
|
# test callback with a closure
|
2015-03-20 19:50:33 -04:00
|
|
|
tim.init(freq=100)
|
2015-02-23 08:18:33 -05:00
|
|
|
tim.callback(cb3(3))
|
2015-03-20 19:50:33 -04:00
|
|
|
pyb.delay(5)
|
|
|
|
print("before cb4")
|
|
|
|
pyb.delay(15)
|