@@ -10,11 +10,11 @@ class StopCoroutineException( Exception ):
1010ProgramStartTime = datetime .datetime .now ()
1111
1212class Scheduler ():
13-
13+
1414 # Public members:
1515 # latestSensorCoroutine - the most recent sensor/control coroutine to have been added.
1616 # latestActionCoroutine - the most recent motor control coroutine to have been added.
17-
17+
1818 # Answers the time in ms since program start.
1919 @staticmethod
2020 def currentTimeMillis ():
@@ -27,7 +27,7 @@ def currentTimeMillis():
2727 def nullCoroutine ():
2828 while True :
2929 yield
30-
30+
3131 def __init__ (self , timeMillisBetweenWorkCalls = 20 ):
3232 self .timeMillisBetweenWorkCalls = timeMillisBetweenWorkCalls
3333 self .coroutines = []
@@ -49,7 +49,7 @@ def doWork(self):
4949 except Exception as e :
5050 print "Got exception: " , e
5151 self .coroutines .remove ( coroutine )
52-
52+
5353 self .updateCoroutine .next ()
5454
5555 # Private: Wait time before the next doWork call should be called.
@@ -89,11 +89,11 @@ def stopAllCoroutines(self):
8989 # Number of active coroutines
9090 def numCoroutines ( self ):
9191 return len (self .coroutines )
92-
92+
9393 # Answers whether any of the given coroutines are still executing
9494 def stillRunning ( self , * coroutineList ):
9595 return any ( c in self .coroutines for c in coroutineList )
96-
96+
9797
9898 # Coroutine that executes the given coroutines until the first completes, then stops the others and finishes.
9999 def runTillFirstCompletes ( self , * coroutineList ):
@@ -107,7 +107,7 @@ def runTillFirstCompletes( self, *coroutineList ):
107107 print "Got exception: " , e
108108 return
109109 yield
110-
110+
111111 # Coroutine that executes the given coroutines until all have completed.
112112 def runTillAllComplete (self , * coroutineList ):
113113 coroutines = list ( coroutineList )
@@ -132,3 +132,7 @@ def doWait( self, timeMillis ):
132132 def withTimeout ( self , timeoutMillis , coroutine ):
133133 return self .runTillFirstCompletes ( coroutine , self .doWait ( timeoutMillis ) )
134134
135+ # Coroutine that waits until the given function (with optional parameters) returns True.
136+ def waitFor (self , function , * args ):
137+ while not function (* args ):
138+ yield
0 commit comments