summaryrefslogtreecommitdiffstatshomepage
path: root/tests
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2015-02-23 13:18:33 +0000
committerDamien George <damien.p.george@gmail.com>2015-02-23 13:18:33 +0000
commit626ee90ce194616f72eb49750f007e1604e3309c (patch)
tree2003b664ac51d7c7977deb676bc3effdc9600310 /tests
parentd38939e676d0c36d81b1763fa3fbfcb53c649710 (diff)
downloadmicropython-626ee90ce194616f72eb49750f007e1604e3309c.tar.gz
micropython-626ee90ce194616f72eb49750f007e1604e3309c.zip
tests: Add more tests for pyb.Timer class.
Diffstat (limited to 'tests')
-rw-r--r--tests/pyb/timer.py20
-rw-r--r--tests/pyb/timer.py.exp2
-rw-r--r--tests/pyb/timer_callback.py41
-rw-r--r--tests/pyb/timer_callback.py.exp4
4 files changed, 47 insertions, 20 deletions
diff --git a/tests/pyb/timer.py b/tests/pyb/timer.py
index 45cb8550dc..61320690a6 100644
--- a/tests/pyb/timer.py
+++ b/tests/pyb/timer.py
@@ -1,3 +1,5 @@
+# check basic functionality of the timer class
+
import pyb
from pyb import Timer
@@ -9,21 +11,3 @@ tim.prescaler(300)
print(tim.prescaler())
tim.period(400)
print(tim.period())
-
-tim = Timer(4, freq=1)
-tim.init(freq=2000)
-def f(t):
- print(1)
- t.callback(None)
-tim.callback(f)
-pyb.delay(10)
-
-# f3 closes over f2.y
-def f2(x):
- y = x
- def f3(t):
- print(2, y)
- t.callback(None)
- return f3
-tim.callback(f2(3))
-pyb.delay(10)
diff --git a/tests/pyb/timer.py.exp b/tests/pyb/timer.py.exp
index 58b81e2af1..5c46230303 100644
--- a/tests/pyb/timer.py.exp
+++ b/tests/pyb/timer.py.exp
@@ -2,5 +2,3 @@
200
300
400
-1
-2 3
diff --git a/tests/pyb/timer_callback.py b/tests/pyb/timer_callback.py
new file mode 100644
index 0000000000..060182053d
--- /dev/null
+++ b/tests/pyb/timer_callback.py
@@ -0,0 +1,41 @@
+# check callback feature of the timer class
+
+import pyb
+from pyb import Timer
+
+# callback function that disables the callback when called
+def cb1(t):
+ print("cb1")
+ t.callback(None)
+
+# callback function that disables the timer when called
+def cb2(t):
+ print("cb2")
+ t.deinit()
+
+# callback where cb4 closes over cb3.y
+def cb3(x):
+ y = x
+ def cb4(t):
+ print("cb4", y)
+ t.callback(None)
+ return cb4
+
+# create a timer with a callback, using callback(None) to stop
+tim = Timer(1, freq=1000, callback=cb1)
+pyb.delay(10)
+
+# create a timer with a callback, using deinit to stop
+tim = Timer(2, freq=1000, callback=cb2)
+pyb.delay(10)
+
+# create a timer, then set the freq, then set the callback
+tim = Timer(4)
+tim.init(freq=2000)
+tim.callback(cb1)
+pyb.delay(10)
+
+# test callback with a closure
+tim.init(freq=3000)
+tim.callback(cb3(3))
+pyb.delay(10)
diff --git a/tests/pyb/timer_callback.py.exp b/tests/pyb/timer_callback.py.exp
new file mode 100644
index 0000000000..5fd751fde3
--- /dev/null
+++ b/tests/pyb/timer_callback.py.exp
@@ -0,0 +1,4 @@
+cb1
+cb2
+cb1
+cb4 3