File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # check basic functionality of the timer class
2+
13import pyb
24from pyb import Timer
35
911print (tim .prescaler ())
1012tim .period (400 )
1113print (tim .period ())
12-
13- tim = Timer (4 , freq = 1 )
14- tim .init (freq = 2000 )
15- def f (t ):
16- print (1 )
17- t .callback (None )
18- tim .callback (f )
19- pyb .delay (10 )
20-
21- # f3 closes over f2.y
22- def f2 (x ):
23- y = x
24- def f3 (t ):
25- print (2 , y )
26- t .callback (None )
27- return f3
28- tim .callback (f2 (3 ))
29- pyb .delay (10 )
Original file line number Diff line number Diff line change 22200
33300
44400
5- 1
6- 2 3
Original file line number Diff line number Diff line change 1+ # check callback feature of the timer class
2+
3+ import pyb
4+ from pyb import Timer
5+
6+ # callback function that disables the callback when called
7+ def cb1 (t ):
8+ print ("cb1" )
9+ t .callback (None )
10+
11+ # callback function that disables the timer when called
12+ def cb2 (t ):
13+ print ("cb2" )
14+ t .deinit ()
15+
16+ # callback where cb4 closes over cb3.y
17+ def cb3 (x ):
18+ y = x
19+ def cb4 (t ):
20+ print ("cb4" , y )
21+ t .callback (None )
22+ return cb4
23+
24+ # create a timer with a callback, using callback(None) to stop
25+ tim = Timer (1 , freq = 1000 , callback = cb1 )
26+ pyb .delay (10 )
27+
28+ # create a timer with a callback, using deinit to stop
29+ tim = Timer (2 , freq = 1000 , callback = cb2 )
30+ pyb .delay (10 )
31+
32+ # create a timer, then set the freq, then set the callback
33+ tim = Timer (4 )
34+ tim .init (freq = 2000 )
35+ tim .callback (cb1 )
36+ pyb .delay (10 )
37+
38+ # test callback with a closure
39+ tim .init (freq = 3000 )
40+ tim .callback (cb3 (3 ))
41+ pyb .delay (10 )
Original file line number Diff line number Diff line change 1+ cb1
2+ cb2
3+ cb1
4+ cb4 3
You can’t perform that action at this time.
0 commit comments