Module Name:    src
Committed By:   mbalmer
Date:           Sun May 19 15:31:23 UTC 2013

Modified Files:
        src/usr.sbin/gpioctl: gpioctl.8 gpioctl.c

Log Message:
Make it easier to use gpioctl(8) in shell scripts:  Add a -s flag which
instructs gpioctl(8) to only output a single numeric value, either the
number of pins if no pin number was passed as argumenti, or, the current
state of the pin.


To generate a diff of this commit:
cvs rdiff -u -r1.17 -r1.18 src/usr.sbin/gpioctl/gpioctl.8
cvs rdiff -u -r1.19 -r1.20 src/usr.sbin/gpioctl/gpioctl.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/gpioctl/gpioctl.8
diff -u src/usr.sbin/gpioctl/gpioctl.8:1.17 src/usr.sbin/gpioctl/gpioctl.8:1.18
--- src/usr.sbin/gpioctl/gpioctl.8:1.17	Sun May 19 14:06:35 2013
+++ src/usr.sbin/gpioctl/gpioctl.8	Sun May 19 15:31:23 2013
@@ -1,4 +1,4 @@
-.\" $NetBSD: gpioctl.8,v 1.17 2013/05/19 14:06:35 mbalmer Exp $
+.\" $NetBSD: gpioctl.8,v 1.18 2013/05/19 15:31:23 mbalmer Exp $
 .\"
 .\" Copyright (c) 2009, 2010, 2011, 2013 Marc Balmer <[email protected]>
 .\" Copyright (c) 2004 Alexander Yurchenko <[email protected]>
@@ -23,6 +23,9 @@
 .Nd control GPIO devices
 .Sh SYNOPSIS
 .Nm gpioctl
+.Op Fl qs
+.Ar device
+.Nm gpioctl
 .Op Fl q
 .Ar device
 .Cm attach
@@ -31,12 +34,12 @@
 .Ar mask
 .Op Ar flag
 .Nm gpioctl
-.Op Fl q
+.Op Fl qs
 .Ar device
 .Ar pin
 .Op Ar 0 | 1 | 2
 .Nm gpioctl
-.Op Fl q
+.Op Fl qs
 .Ar device
 .Ar pin
 .Op Ar on | off | toggle
@@ -156,6 +159,12 @@ The options are as follows:
 .Bl -tag -width Ds
 .It Fl q
 Operate quietly i.e. nothing is printed to stdout.
+.It Fl s
+Only output a single number on stdout, representing either the state of the
+pin or the number of available pins if no pin number was passed as argument.
+This option is useful e.g. when
+.Nm
+is used in shell scripts to query the state of a pin.
 .El
 .Sh FILES
 .Bl -tag -width "/dev/gpiou" -compact

Index: src/usr.sbin/gpioctl/gpioctl.c
diff -u src/usr.sbin/gpioctl/gpioctl.c:1.19 src/usr.sbin/gpioctl/gpioctl.c:1.20
--- src/usr.sbin/gpioctl/gpioctl.c:1.19	Sun Nov 13 13:20:02 2011
+++ src/usr.sbin/gpioctl/gpioctl.c	Sun May 19 15:31:23 2013
@@ -1,7 +1,7 @@
-/* $NetBSD: gpioctl.c,v 1.19 2011/11/13 13:20:02 mbalmer Exp $ */
+/* $NetBSD: gpioctl.c,v 1.20 2013/05/19 15:31:23 mbalmer Exp $ */
 
 /*
- * Copyright (c) 2008, 2010, 2011 Marc Balmer <[email protected]>
+ * Copyright (c) 2008, 2010, 2011, 2013 Marc Balmer <[email protected]>
  * Copyright (c) 2004 Alexander Yurchenko <[email protected]>
  *
  * Permission to use, copy, modify, and distribute this software for any
@@ -39,6 +39,7 @@
 static char *dev;
 static int devfd = -1;
 static int quiet = 0;
+static int state = 0;
 
 static void getinfo(void);
 static void gpioread(int, char *);
@@ -84,11 +85,14 @@ main(int argc, char *argv[])
 	char *flags;
 	char devn[32];
 
-	while ((ch = getopt(argc, argv, "q")) != -1)
+	while ((ch = getopt(argc, argv, "qs")) != -1)
 		switch (ch) {
 		case 'q':
 			quiet = 1;
 			break;
+		case 's':
+			quiet = state = 1;
+			break;
 		default:
 			usage();
 			/* NOTREACHED */
@@ -192,7 +196,6 @@ main(int argc, char *argv[])
 		} else
 			gpioread(pin, nm);
 	}
-
 	return EXIT_SUCCESS;
 }
 
@@ -204,6 +207,9 @@ getinfo(void)
 	if (ioctl(devfd, GPIOINFO, &info) == -1)
 		err(EXIT_FAILURE, "GPIOINFO");
 
+	if (state)
+		printf("%d\n", info.gpio_npins);
+
 	if (quiet)
 		return;
 
@@ -224,6 +230,9 @@ gpioread(int pin, char *gp_name)
 	if (ioctl(devfd, GPIOREAD, &req) == -1)
 		err(EXIT_FAILURE, "GPIOREAD");
 
+	if (state)
+		printf("%d\n", req.gp_value);
+
 	if (quiet)
 		return;
 
@@ -256,6 +265,9 @@ gpiowrite(int pin, char *gp_name, int va
 			err(EXIT_FAILURE, "GPIOTOGGLE");
 	}
 
+	if (state)
+		printf("%d\n", value < 2 ? value : 1 - req.gp_value);
+
 	if (quiet)
 		return;
 
@@ -344,7 +356,7 @@ usage(void)
 	const char *progname;
 
 	progname = getprogname();
-	fprintf(stderr, "usage: %s [-q] device [pin] [0 | 1 | 2 | "
+	fprintf(stderr, "usage: %s [-qs] device [pin] [0 | 1 | 2 | "
 	    "on | off | toggle]\n", progname);
 	fprintf(stderr, "       %s [-q] device pin set [flags] [name]\n",
 	    progname);

Reply via email to