File tree Expand file tree Collapse file tree 1 file changed +23
-1
lines changed
Expand file tree Collapse file tree 1 file changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -124,28 +124,50 @@ def ensure_button(self, button):
124124
125125 return button
126126
127- def loop (self , skip_first = True ):
127+ def loop (self , skip_first = True , timeout = None ):
128128 """
129129 A syntactic sugar to start a state loop
130130
131131 Args:
132132 skip_first (bool): Usually to be True to reuse the previous screenshot
133+ timeout (int | float | Timer): Seconds of timeout or a Timer object
133134
134135 Yields:
135136 np.ndarray: screenshot
136137
137138 Examples:
139+ # state machine that handle clicking until destination
138140 for _ in self.loop():
139141 if self.appear(...):
140142 break
141143 if self.appear_then_click(...):
142144 continue
145+
146+ Examples:
147+ # state machine with timeout
148+ for _ in self.loop(timeout=2):
149+ if self.appear(...):
150+ logger.info('Wait success')
151+ break
152+ else:
153+ logger.warning('Wait timeout')
143154 """
155+ if timeout is not None :
156+ if isinstance (timeout , Timer ):
157+ timeout .reset ()
158+ else :
159+ timeout = Timer .from_seconds (timeout ).start ()
160+
144161 while 1 :
162+ if timeout is not None :
163+ if timeout .reached ():
164+ return
165+
145166 if skip_first :
146167 skip_first = False
147168 else :
148169 self .device .screenshot ()
170+
149171 try :
150172 yield self .device .image
151173 except AttributeError :
You can’t perform that action at this time.
0 commit comments