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.
This commit is contained in:
parent
67dea2f053
commit
4dfbc91d39
@ -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>
|
||||
.\" Copyright (c) 2004 Alexander Yurchenko <grange@openbsd.org>
|
||||
@ -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
|
||||
|
@ -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>
|
||||
* Copyright (c) 2008, 2010, 2011, 2013 Marc Balmer <mbalmer@NetBSD.org>
|
||||
* Copyright (c) 2004 Alexander Yurchenko <grange@openbsd.org>
|
||||
*
|
||||
* 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);
|
||||
|
Loading…
Reference in New Issue
Block a user