Skip to content

Commit 265e33b

Browse files
committed
Add: [ALAS] Syntactic sugar for state machine with timeout
1 parent ed154c1 commit 265e33b

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

module/base/base.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff 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:

0 commit comments

Comments
 (0)