0

I know it's possible to write and read GPIO values using the command line:

echo 1 > /sys/class/gpio/gpio0/value 

for example, puts the GPIO 0 pin on high.

Now, for reading, you use cat. However, this restricts me to polling and does not prevent me from missing a state inbetween poll events.

Is there a way for me to register some kind of listener via the command line as you would with RPIO in Python?

I want to listen for value changes without polling. I assume polling extremely quickly (to make missing a state less likely) is not very healthy either?

There is a target called /sys/class/gpio/gpio../uevent but I don't know what it is for.

2
  • Why not use a Python/C/C++/Perl/<insert language here> implementation that implements Interrupts, and just sits in the background?
    – stevieb
    Mar 17, 2017 at 22:51
  • I wanted my own very simple library for satisfaction's sake :)
    – Limnic
    Mar 18, 2017 at 8:50

1 Answer 1

2

According to https://www.kernel.org/doc/Documentation/gpio/sysfs.txt some of the GPIO can be configured as interrupt generating.

"value" ... reads as either 0 (low) or 1 (high). If the GPIO is configured as an output, this value may be written; any nonzero value is treated as high.

If the pin can be configured as interrupt-generating interrupt and if it has been configured to generate interrupts (see the description of "edge"), you can poll(2) on that file and poll(2) will return whenever the interrupt was triggered. If you use poll(2), set the events POLLPRI and POLLERR. If you use select(2), set the file descriptor in exceptfds. After poll(2) returns, either lseek(2) to the beginning of the sysfs file and read the new value or close the file and re-open it to read the value.

"edge" ... reads as either "none", "rising", "falling", or "both". Write these strings to select the signal edge(s) that will make poll(2) on the "value" file return.

This file exists only if the pin can be configured as an interrupt generating input pin.

Should be like this..

echo 17 > /sys/class/gpio/export
echo rising > /sys/class/gpio/gpio17/edge

Based on your comment, I recommend to check pigpio where a Java wrapper exists. The library​ is maintained by another user of this community. The wrapper is also well documented. I think with this you can develop bit more efficient.

3
  • Thank you very much, because I'm doing stuff in Java, I was able to manipulate the edge target and use a WatchService to detect changes and it works amazingly well!
    – Limnic
    Mar 18, 2017 at 8:40
  • @Limnic, I've updated the answer. The information that you want to use this in Java was not missing before :) Mar 18, 2017 at 15:05
  • After a bit of experimenting, it seems like pure java code to manage GPIO is too hard for my raspberry pi zero. It's under 100% cpu load pretty much after the tiniest loop. There can probably be optimizations but I thought "since all other libraries fall back to native calls, I'll just use PI4J." And even though I don't like the API as much, it performs a lot better haha :)
    – Limnic
    Mar 18, 2017 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.