Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Compile with:
- *
- * g++ -Wall test.cpp -o test -lrt -lpthread -lpigpio
- *
- * Run with:
- *
- * sudo ./test
- *
- *
- *
- */
- #include <pigpio.h>
- #include <stdio.h>
- #include <unistd.h>
- const int INPUT_PIN = 25;
- const int OUTPUT_PIN = 26;
- const int PERIOD = 50000 ; // usec
- const int DUTY_CYCLE = 0.001 ;
- //================================ Options =====================================
- unsigned int samplingRate = 1; // 1 microsecond (can be 1,2,4,5,10)
- unsigned int start = 0;
- unsigned int deltaTime;
- void ppmOnEdge2 (int gpio, int level, uint32_t tick)
- {
- if (level == 0) {
- start = tick;
- //fprintf(stderr, "\n");
- }
- if (level == 1) {
- deltaTime = tick - start;
- // Console output
- fprintf(stderr, "*** %d\n", deltaTime);
- }
- }
- //================================== Main ======================================
- int main(int argc, char *argv[])
- {
- int level = 1;
- // GPIO setup
- gpioCfgClock(samplingRate, PI_DEFAULT_CLK_PERIPHERAL, 0); /* last parameter is deprecated now */
- gpioInitialise();
- gpioSetMode(INPUT_PIN,PI_INPUT);
- gpioSetMode(OUTPUT_PIN,PI_OUTPUT);
- gpioWrite(26, level);
- gpioSetAlertFunc(INPUT_PIN, ppmOnEdge2);
- // Infinite sleep - all action is now happening in ppmOnEdge2() function
- while(1) {
- usleep(50000);
- level = 0;
- gpioWrite(OUTPUT_PIN, level);
- usleep (250);
- level = 1;
- gpioWrite(OUTPUT_PIN, level);
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment