Advertisement
zmatt

sysfs gpio edge detector

Apr 18th, 2024
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.73 KB | None | 0 0
  1. #include "sysfs-gpio.h"  // https://github.com/mvduin/sysfs-gpio
  2. #include <stdio.h>
  3.  
  4. int main()
  5. {
  6.     struct Gpio gpio = GPIO_INITIALIZER;
  7.     gpio_open_input( &gpio, "/sys/class/gpio/gpio60", GPIO_EDGE_BOTH, GPIO_RDONLY );
  8.  
  9.     bool const invert = gpio_is_active_low( &gpio );
  10.  
  11.     bool level = gpio_read( &gpio ) ^ invert;
  12.  
  13.     printf( "%c ", level ? 'H' : 'L' );  // print initial level
  14.  
  15.     for(;;) {
  16.         fflush( stdout );
  17.  
  18.         gpio_wait_event( &gpio, -1 );
  19.  
  20.         bool prev_level = level;
  21.         level = gpio_read( &gpio ) ^ invert;
  22.  
  23.         printf( "%c ", prev_level ? 'F' : 'R' );
  24.  
  25.         // if an edge was detected but level didn't change then we must have had a second edge
  26.         if( level == prev_level )
  27.             printf( "%c ", prev_level ? 'R' : 'F' );
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement