Python is a great tool to create simple/fast test code, combine that with libgpiod, now you can toggle and monitor gpio pins.
In recent Linux kernels libgpiod is a library to write C/C++ and Python to toggle and monitor general purpose pins. There are plenty examples for python. I use buildroot to create a Linux distro, one option under hardware is to install libgpiod and if you have python3 selected now you can write python scripts.
Here is two basic classes gpiowrapper.py:
The EventPin class can be used with asyncio to wait for Edge Triggered GPIO. The key line is adding file descriptor of the event pin to asyncio and perform a callback when the FD is changes and push the event into a queue so await can be used.
Here is a simple blink.py:
Load both files onto the Orange PI Zero.
python blink.py -chip gpiochip0 -opin 1 --epin 0 --event True
This will use PIN11 (GPIO1) for output and toggle at 1 second. Pin13 will wait for Rising Edge trigger. If you connect both pins together then you will get:
# python blink.py -chip gpiochip0 -opin 1 --epin 0 --event True
1970-01-01 00:47:21,419 DEBUG blink.py:60 - <module>() main
1970-01-01 00:47:21,420 DEBUG blink.py:66 - <module>() monitor
1970-01-01 00:47:21,421 DEBUG selector_events.py:59 - __init__() Using selector: EpollSelector
1970-01-01 00:47:21,424 DEBUG gpiowrapper.py:10 - __init__() args chip=gpiochip0 pin=1
1970-01-01 00:47:21,558 DEBUG gpiowrapper.py:16 - __init__() type <class 'gpiod.Chip'> <class 'int'>
1970-01-01 00:47:21,648 DEBUG gpiowrapper.py:36 - __init__() type <class 'gpiod.Chip'> <class 'int'>
1970-01-01 00:47:22,153 DEBUG gpiowrapper.py:57 - event_cb() event RISING EDGE
1970-01-01 00:47:23,156 DEBUG gpiowrapper.py:57 - event_cb() event RISING EDGE
1970-01-01 00:47:24,159 DEBUG gpiowrapper.py:57 - event_cb() event RISING EDGE
1970-01-01 00:47:25,161 DEBUG gpiowrapper.py:57 - event_cb() event RISING EDGE
1970-01-01 00:47:26,164 DEBUG gpiowrapper.py:57 - event_cb() event RISING EDGE
1970-01-01 00:47:27,167 DEBUG gpiowrapper.py:57 - event_cb() event RISING EDGE
1970-01-01 00:47:28,170 DEBUG gpiowrapper.py:57 - event_cb() event RISING EDGE
1970-01-01 00:47:29,172 DEBUG gpiowrapper.py:57 - event_cb() event RISING EDGE
1970-01-01 00:47:30,175 DEBUG gpiowrapper.py:57 - event_cb() event RISING EDGE
1970-01-01 00:47:31,178 DEBUG gpiowrapper.py:57 - event_cb() event RISING EDGE
1970-01-01 00:47:32,180 DEBUG gpiowrapper.py:57 - event_cb() event RISING EDGE
1970-01-01 00:47:33,183 DEBUG gpiowrapper.py:57 - event_cb() event RISING EDGE
1970-01-01 00:47:34,186 DEBUG gpiowrapper.py:57 - event_cb() event RISING EDGE
1970-01-01 00:47:35,188 DEBUG gpiowrapper.py:57 - event_cb() event RISING EDGE
1970-01-01 00:47:36,191 DEBUG gpiowrapper.py:57 - event_cb() event RISING EDGE
1970-01-01 00:47:37,194 DEBUG gpiowrapper.py:57 - event_cb() event RISING EDGE
1970-01-01 00:47:38,197 DEBUG gpiowrapper.py:57 - event_cb() event RISING EDGE
1970-01-01 00:47:39,199 DEBUG gpiowrapper.py:57 - event_cb() event RISING EDGE
1970-01-01 00:47:40,202 DEBUG gpiowrapper.py:57 - event_cb() event RISING EDGE
1970-01-01 00:47:41,205 DEBUG gpiowrapper.py:57 - event_cb() event RISING EDGE
1970-01-01 00:47:42,207 DEBUG gpiowrapper.py:57 - event_cb() event RISING EDGE
1
Interesting example. I am looking on how to be notified asynchronously for any i/o gpio event with C++ and libgpiocxx, but I have no luck.
ReplyDeleteCould you post such an example / post for your OrangePi?
Sorry, I have missed you post. I have not tried libgpiocxx before. Are you still looking for an example?
ReplyDelete