https://toitlang.org/ logo
GPIO interrupts?
# help
a
Hi, I'm trying to configure one of the ESP32C6 GPIOs as an interrupt pin recieving signal from an external sensor and acting accordingly as per some routine which should run simultaneously with the rest of the program. any ideas/workarounds?
f
In Toit we typically do
pin.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.
Example:
Copy code
import 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