Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.sbin/gpioctl Make it easier to use gpioctl(8) in shell s...



details:   https://anonhg.NetBSD.org/src/rev/63b11c25d1da
branches:  trunk
changeset: 786891:63b11c25d1da
user:      mbalmer <mbalmer%NetBSD.org@localhost>
date:      Sun May 19 15:31:23 2013 +0000

description:
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.

diffstat:

 usr.sbin/gpioctl/gpioctl.8 |  15 ++++++++++++---
 usr.sbin/gpioctl/gpioctl.c |  22 +++++++++++++++++-----
 2 files changed, 29 insertions(+), 8 deletions(-)

diffs (131 lines):

diff -r 13fc843a7233 -r 63b11c25d1da usr.sbin/gpioctl/gpioctl.8
--- a/usr.sbin/gpioctl/gpioctl.8        Sun May 19 15:04:39 2013 +0000
+++ b/usr.sbin/gpioctl/gpioctl.8        Sun May 19 15:31:23 2013 +0000
@@ -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 <marc%msys.ch@localhost>
 .\" Copyright (c) 2004 Alexander Yurchenko <grange%openbsd.org@localhost>
@@ -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 @@
 .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 -r 13fc843a7233 -r 63b11c25d1da usr.sbin/gpioctl/gpioctl.c
--- a/usr.sbin/gpioctl/gpioctl.c        Sun May 19 15:04:39 2013 +0000
+++ b/usr.sbin/gpioctl/gpioctl.c        Sun May 19 15:31:23 2013 +0000
@@ -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 <mbalmer%NetBSD.org@localhost>
+ * Copyright (c) 2008, 2010, 2011, 2013 Marc Balmer <mbalmer%NetBSD.org@localhost>
  * Copyright (c) 2004 Alexander Yurchenko <grange%openbsd.org@localhost>
  *
  * 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 @@
        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 @@
                } else
                        gpioread(pin, nm);
        }
-
        return EXIT_SUCCESS;
 }
 
@@ -204,6 +207,9 @@
        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 @@
        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 @@
                        err(EXIT_FAILURE, "GPIOTOGGLE");
        }
 
+       if (state)
+               printf("%d\n", value < 2 ? value : 1 - req.gp_value);
+
        if (quiet)
                return;
 
@@ -344,7 +356,7 @@
        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);



Home | Main Index | Thread Index | Old Index