GPIO interrupt callback with gpiod

Not entirely certain how to set a callback for an input event.

What I want to do -
-Set a GPIO to trigger on a rising edge (non blocking)

  • when it is triggered, go to a callback function and increment a counter

I see these two functions and not sure if these are blocking or non-blocking
gpiod_ctxless_event_monitor
and
gpiod_ctxless_event_loop

There descriptions are basically the same.
Do you have an example of how to use each of these?

I was really looking for an example using gpiod_ctxless_event_monitor(). I want keep using the what is in gpiod.h

so far I have
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//Callback function
int myEvent(int x, unsigned int c, const struct timespec * v, void * b)
{
printf(“hello/r/n”);
}
// in my main function
gpiod_ctxless_event_handle_cb p = &myEvent; //setup callback
const char* pBank = “gpiochip0”; //name of gpiochip
const char* c = “gpio-event”; // name of consumer
int in_line = 5; // offset for gpio

rc = gpiod_ctxless_event_monitor(pBank,
GPIOD_LINE_EVENT_RISING_EDGE,
in_line,
1,
c,
&k,
NULL,
p,
NULL);

printf(“%d\r\n”, rc);
/////////////////////////////////////////////////////////
rc always gives me a -1. Which means I’m doing something wrong to set this up

I got the gpiod_ctxless_event_monitor to work, but it is a blocking function and the articles you refenced elude to polling the input and not a true event trigger. Can we conclude Linux does not have a way to jump to a callback when an input changes state with out constantly polling for the input? a non-blocking type event trigger? Only way I can think to do this is a separate thread, but for a real time event this is not great. any other ideas on how to get a real time event trigger. I’m trying to read a flow sensor, that gives me pulses based on flow rate.

Hi @jeffbelz,

I think thread with for(;;){gpiod_line_event_wait(); …} is the right way to use GPIO interrupts. From this thread you may call some simple callbacks or use pthread_cond_signal() to unlock other waiting threads

Edward