Guest User

Untitled

a guest
Jul 22nd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. /*
  2. * Compile with:
  3. *
  4. * g++ -Wall test.cpp -o test -lrt -lpthread -lpigpio
  5. *
  6. * Run with:
  7. *
  8. * sudo ./test
  9. *
  10. *
  11. *
  12. */
  13.  
  14.  
  15. #include <pigpio.h>
  16. #include <stdio.h>
  17. #include <unistd.h>
  18.  
  19.  
  20. const int INPUT_PIN = 25;
  21. const int OUTPUT_PIN = 26;
  22.  
  23. const int PERIOD = 50000 ; // usec
  24.  
  25. const int DUTY_CYCLE = 0.001 ;
  26.  
  27. //================================ Options =====================================
  28.  
  29. unsigned int samplingRate = 1; // 1 microsecond (can be 1,2,4,5,10)
  30.  
  31. unsigned int start = 0;
  32. unsigned int deltaTime;
  33.  
  34. void ppmOnEdge2 (int gpio, int level, uint32_t tick)
  35. {
  36. if (level == 0) {
  37. start = tick;
  38. //fprintf(stderr, "\n");
  39. }
  40.  
  41. if (level == 1) {
  42. deltaTime = tick - start;
  43. // Console output
  44. fprintf(stderr, "*** %d\n", deltaTime);
  45.  
  46. }
  47.  
  48.  
  49. }
  50.  
  51.  
  52. //================================== Main ======================================
  53.  
  54. int main(int argc, char *argv[])
  55. {
  56. int level = 1;
  57.  
  58. // GPIO setup
  59.  
  60. gpioCfgClock(samplingRate, PI_DEFAULT_CLK_PERIPHERAL, 0); /* last parameter is deprecated now */
  61. gpioInitialise();
  62.  
  63. gpioSetMode(INPUT_PIN,PI_INPUT);
  64.  
  65. gpioSetMode(OUTPUT_PIN,PI_OUTPUT);
  66. gpioWrite(26, level);
  67.  
  68. gpioSetAlertFunc(INPUT_PIN, ppmOnEdge2);
  69.  
  70. // Infinite sleep - all action is now happening in ppmOnEdge2() function
  71.  
  72. while(1) {
  73. usleep(50000);
  74.  
  75. level = 0;
  76. gpioWrite(OUTPUT_PIN, level);
  77. usleep (250);
  78. level = 1;
  79. gpioWrite(OUTPUT_PIN, level);
  80.  
  81. }
  82. return 0;
  83. }
Add Comment
Please, Sign In to add comment