Skip to content

Commit 626ee90

Browse files
committed
tests: Add more tests for pyb.Timer class.
1 parent d38939e commit 626ee90

File tree

4 files changed

+47
-20
lines changed

4 files changed

+47
-20
lines changed

tests/pyb/timer.py

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# check basic functionality of the timer class
2+
13
import pyb
24
from pyb import Timer
35

@@ -9,21 +11,3 @@
911
print(tim.prescaler())
1012
tim.period(400)
1113
print(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)

tests/pyb/timer.py.exp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,3 @@
22
200
33
300
44
400
5-
1
6-
2 3

tests/pyb/timer_callback.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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)

tests/pyb/timer_callback.py.exp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
cb1
2+
cb2
3+
cb1
4+
cb4 3

0 commit comments

Comments
 (0)