diff --git a/usr.sbin/gpioctl/gpioctl.8 b/usr.sbin/gpioctl/gpioctl.8 index 5b9f7b89b809..d73b9ccfe7da 100644 --- a/usr.sbin/gpioctl/gpioctl.8 +++ b/usr.sbin/gpioctl/gpioctl.8 @@ -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 .\" Copyright (c) 2004 Alexander Yurchenko @@ -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 diff --git a/usr.sbin/gpioctl/gpioctl.c b/usr.sbin/gpioctl/gpioctl.c index 6f36e82b8d3d..c1a853c40cdb 100644 --- a/usr.sbin/gpioctl/gpioctl.c +++ b/usr.sbin/gpioctl/gpioctl.c @@ -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 + * Copyright (c) 2008, 2010, 2011, 2013 Marc Balmer * Copyright (c) 2004 Alexander Yurchenko * * 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 value) 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);