forked from danicampora/Micropython-scheduler
/
roundrobin.py
33 lines (25 loc) · 821 Bytes
/
roundrobin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# roundrobin.py Runs three threads in round robin fashion. Stops after a duration via a timeout thread.
# Author: Peter Hinch
# V1.02 6th Sep 2014
import pyb
from usched import Sched, Roundrobin, wait
# Run on MicroPython board bare hardware
# THREADS:
def stop(fTim, objSch): # Stop the scheduler after fTim seconds
yield from wait(fTim)
objSch.stop()
def robin(text):
wf = Roundrobin()
while True:
print(text)
yield wf()
# USER TEST PROGRAM
def test(duration = 0):
objSched = Sched()
objSched.add_thread(robin("Thread 1"))
objSched.add_thread(robin("Thread 2"))
objSched.add_thread(robin("Thread 3"))
if duration:
objSched.add_thread(stop(duration, objSched)) # Kill after a period
objSched.run()
test(5)