Bläddra i källkod

finished bash interupt

Sebastian Vendt@Lhotse 6 år sedan
förälder
incheckning
45845d9616
3 ändrade filer med 106 tillägg och 0 borttagningar
  1. BIN
      bpi
  2. 87 0
      bpi.c
  3. 19 0
      test.sh

BIN
bpi


+ 87 - 0
bpi.c

@@ -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);
+}

+ 19 - 0
test.sh

@@ -0,0 +1,19 @@
+#!/bin/bash
+gcc bpi.c -o bpi
+
+echo testing no command
+./bpi
+echo testing wrong command
+./bpi -k
+echo testing command without value
+./bpi -p
+echo testing missing required command
+./bpi -v -e 1
+echo testing wrong edge
+./bpi -p 10 -v -e 3
+echo testing command with value no edge
+./bpi -p 10
+echo testing command with value and edge
+./bpi -p 10 -e 1
+echo testing command with value and edge and verbose
+./bpi -p 10 -e 1 -v