AbedN
05/15/2025, 12:48 PMfloitsch
05/15/2025, 1:05 PMpin.wait-for x the value we are waiting for.
Under the hood, Toit creates an interrupt and listens for the change. In the Toit layer the currently running task is suspended and will be woken up once the signal from the lower layer is received.
If you want to do other work in the meantime you use task to create another cooperative task. Whether the code that waits for the pin or whether the other code is in a task depends on your program.floitsch
05/15/2025, 1:07 PMimport gpio
main:
pin := gpio.Pin 32 --input --pull-up
task::
while true:
pin.wait-for 0
print "Got pulled to ground"
sleep --ms=200 // Debounce.
pin.wait-for 1
sleep --ms=200 // Debounce.
while true:
print "In main-loop"
// Let other tasks run.
// The 'sleep' function automatically yields. Otherwise a call to 'yield' would
// also give other tasks a chance to run.
sleep --ms=1_000