toradex / torizon-samples

All sample code related to TorizonCore project.
The Unlicense
32 stars 24 forks source link

Python GPIO example with event driven IO #4

Closed Skill3t closed 2 years ago

Skill3t commented 2 years ago

Is there a possibility to provide a GPIO example with event / callback to GPIO input. The Example is just a while loop wich will consume a lot CPU because it is busy waiting. The used GPIO libary did not support this natively. I found the RPi:GPIO libary with the support of events driven approach but this libary is did not support toradex modules.

leograba commented 2 years ago

Hi @Skill3t,

In our example, we use the https://pypi.org/project/gpiod/ library, whose project is https://github.com/hhk7734/python3-gpiod. They have an open issue that aims to solve your request: https://github.com/hhk7734/python3-gpiod/issues/8, along with an example that you can try.

Alternatively, I notice that the https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/ library has Python bindings, and they have an example for GPIO monitoring: https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/tree/bindings/python/examples/gpiomon.py.

Let me know if any of these can help you.

Skill3t commented 2 years ago

Hi @leograba, Thank you for your quick response. The Python binding should be the way to go i thing. Important is to use the system package (not the pip). So i installed gpiod and python3-libgpiod and it worked quit good. The example you mentioned under https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/tree/bindings/python/examples/gpiomon.py helped a lot. Thank you very much.

leograba commented 2 years ago

@Skill3t glad to know you are unblocked.

Btw, your feedback is captured, and we will evaluate improving the Python GPIO samples. It will happen on a low priority, it may take some time until we reach out to it in our backlog.