2017-03-16 03:05:00 -04:00
|
|
|
# test micropython.schedule() function
|
|
|
|
|
|
|
|
import micropython
|
|
|
|
|
|
|
|
try:
|
|
|
|
micropython.schedule
|
|
|
|
except AttributeError:
|
2020-03-22 22:26:08 -04:00
|
|
|
print("SKIP")
|
2017-06-10 13:14:16 -04:00
|
|
|
raise SystemExit
|
2017-03-16 03:05:00 -04:00
|
|
|
|
|
|
|
# Basic test of scheduling a function.
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2017-03-16 03:05:00 -04:00
|
|
|
def callback(arg):
|
|
|
|
global done
|
|
|
|
print(arg)
|
|
|
|
done = True
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2017-03-16 03:05:00 -04:00
|
|
|
done = False
|
|
|
|
micropython.schedule(callback, 1)
|
|
|
|
while not done:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Test that callbacks can be scheduled from within a callback, but
|
|
|
|
# that they don't execute until the outer callback is finished.
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2017-03-16 03:05:00 -04:00
|
|
|
def callback_inner(arg):
|
|
|
|
global done
|
2020-03-22 22:26:08 -04:00
|
|
|
print("inner")
|
2017-03-16 03:05:00 -04:00
|
|
|
done += 1
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2017-03-16 03:05:00 -04:00
|
|
|
def callback_outer(arg):
|
|
|
|
global done
|
|
|
|
micropython.schedule(callback_inner, 0)
|
|
|
|
# need a loop so that the VM can check for pending events
|
|
|
|
for i in range(2):
|
|
|
|
pass
|
2020-03-22 22:26:08 -04:00
|
|
|
print("outer")
|
2017-03-16 03:05:00 -04:00
|
|
|
done += 1
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2017-03-16 03:05:00 -04:00
|
|
|
done = 0
|
|
|
|
micropython.schedule(callback_outer, 0)
|
|
|
|
while done != 2:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Test that scheduling too many callbacks leads to an exception. To do this we
|
|
|
|
# must schedule from within a callback to guarantee that the scheduler is locked.
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2017-03-16 03:05:00 -04:00
|
|
|
def callback(arg):
|
|
|
|
global done
|
|
|
|
try:
|
|
|
|
for i in range(100):
|
2020-03-22 22:26:08 -04:00
|
|
|
micropython.schedule(lambda x: x, None)
|
2017-03-16 03:05:00 -04:00
|
|
|
except RuntimeError:
|
2020-03-22 22:26:08 -04:00
|
|
|
print("RuntimeError")
|
2017-03-16 03:05:00 -04:00
|
|
|
done = True
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2017-03-16 03:05:00 -04:00
|
|
|
done = False
|
|
|
|
micropython.schedule(callback, None)
|
|
|
|
while not done:
|
|
|
|
pass
|