https://discordapp.com/channels/918498540232253480/1094679043699392582/1119273583563251823
floitsch — 06/16/2023 7:33 AM
Explanation for the code:
Since with_timeout throws an exception, we want to catch it. We do this with catch.
However, we are only interested in DEADLINE_EXCEEDED_ERRORs. So we unwind (let the exception through) if the error isn't a DEADLINE_EXCEEDED_ERROR.
Inside the catch we call the with_timeout. If we are waiting for more than 3s the with_timeout will throw a DEADLINE_EXCEEDED_ERROR.
We then wait for the button.
If we receive a button, then we change the pressed variable, so we know whether we exited the catch because of the timeout or because of a button press.
We could also do it the following way:
exception := catch --unwind=(: it != DEADLINE_EXCEEDED_ERROR):
with_timeout --ms=3_000:
pin.wait_for 0
if exception:
print "didn't press. timeout hit"
else:
print "button pressed"