|
@@ -0,0 +1,87 @@
|
|
|
+/*Author: Sebastian Vendt, version 1.0 (2019)
|
|
|
+* compile with gcc -Wall -o bpi -lwirinigPi bpi.c
|
|
|
+* Usage: call ./bpi -p 10 in your bash script to wait for the event on pin 10
|
|
|
+* the command will terminate once the event occured
|
|
|
+*/
|
|
|
+
|
|
|
+#include <unistd.h>
|
|
|
+#include <stdlib.h>
|
|
|
+#include <stdio.h>
|
|
|
+#include <getopt.h>
|
|
|
+#include <wiringPi.h>
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+extern int optopt;
|
|
|
+void printUsage(char* command);
|
|
|
+void isr();
|
|
|
+int verbose = FALSE;
|
|
|
+
|
|
|
+int main(int argc, char* argv[]){
|
|
|
+ if(argc < 2){
|
|
|
+ fprintf(stderr, "%s requires at least option -p\n", argv[0]);
|
|
|
+ printUsage(argv[0]);
|
|
|
+ exit(EXIT_FAILURE);
|
|
|
+ }
|
|
|
+ int pin = -1;
|
|
|
+ int opt;
|
|
|
+ int edge = 1;
|
|
|
+
|
|
|
+ wiringPiSetup();
|
|
|
+
|
|
|
+ while((opt = getopt(argc, argv, ":p:ve:")) != -1){
|
|
|
+ switch(opt){
|
|
|
+ case 'p':
|
|
|
+ pin = atoi(optarg);
|
|
|
+ break;
|
|
|
+ case 'v':
|
|
|
+ verbose = TRUE;
|
|
|
+ break;
|
|
|
+ case 'e':
|
|
|
+ edge = atoi(optarg);
|
|
|
+ break;
|
|
|
+ case '?':
|
|
|
+ fprintf(stderr, "unknown option\n");
|
|
|
+ printUsage(argv[0]);
|
|
|
+ exit(EXIT_FAILURE);
|
|
|
+ break;
|
|
|
+ case ':':
|
|
|
+ fprintf(stderr, "option %c needs a value\n", optopt);
|
|
|
+ printUsage(argv[0]);
|
|
|
+ exit(EXIT_FAILURE);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(pin == -1) {
|
|
|
+ fprintf(stderr, "option -p requried\n");
|
|
|
+ printUsage(argv[0]);
|
|
|
+ exit(EXIT_FAILURE);
|
|
|
+ }
|
|
|
+ if(!(edge == 1 || edge == 2 || edge == 3)) {
|
|
|
+ fprintf(stderr, "option -e only takes values 1, 2 or 3\n");
|
|
|
+ printUsage(argv[0]);
|
|
|
+ exit(EXIT_FAILURE);
|
|
|
+ }
|
|
|
+ if(verbose) printf("Registering interrupt for pin %d with edge %d\n", pin, edge);
|
|
|
+ pinMode(pin, INPUT);
|
|
|
+ if(wiringPiISR(pin, edge, &isr) < 0) {
|
|
|
+ fprintf(stderr, "Failed registering interrupt for pin %d with edge %d\n", pin, edge);
|
|
|
+ exit(EXIT_FAILURE);
|
|
|
+ }
|
|
|
+ pause();
|
|
|
+ fprintf(stderr, "could not suspend execution\n");
|
|
|
+ exit(EXIT_FAILURE);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+void printUsage(char* command){
|
|
|
+ fprintf(stderr, "Usage: %s -p [-v] [-e]\n", command);
|
|
|
+ fprintf(stderr, "-p BCM pin number (required)\n");
|
|
|
+ fprintf(stderr, "-v be verbose\n");
|
|
|
+ fprintf(stderr, "-e interrupt mode 1:INT_EDGE_FALLING (default), 2:INT_EDGE_RISING, 3:INT_EDGE_BOTH\n");
|
|
|
+}
|
|
|
+
|
|
|
+void isr(){
|
|
|
+ if(verbose) printf("Triggered!\n");
|
|
|
+ exit(EXIT_SUCCESS);
|
|
|
+}
|