- added README.md
- added systemd service descriptors - new features in gpio_trigger.py: - support waiting for a hold period (-H) - added option to ignore command's result code (-i) - added variable subtitution in the arguments (%PIN% and %STATE%)
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
#! /usr/bin/env python
|
||||
|
||||
#
|
||||
# gpio_trigger.py - execute a command when a GPIO pin is triggered (up or down)
|
||||
# gpio_trigger.py - execute a command when a GPIO input pin changes
|
||||
#
|
||||
# Version 1.0, latest version, documentation and bugtracker available at:
|
||||
# https://gitlab.lindenaar.net/scripts/raspberrypi
|
||||
@@ -24,8 +24,12 @@ from os import fork, system
|
||||
from argparse import ArgumentParser
|
||||
from RPi import GPIO
|
||||
|
||||
parser = ArgumentParser(description='Run command when a Raspberry Pi pin is'
|
||||
' high or low (e.g. a button is pressed)')
|
||||
parser = ArgumentParser(
|
||||
description='Run command when a Raspberry Pi pin changes to high or low.',
|
||||
epilog='(*) use \'--\' to stop argument parsing, arguments can contain the '
|
||||
'following variables: %PIN%=pin number, %STATE%=value (HIGH or LOW)')
|
||||
parser.add_argument('-D', '--daemon', action='store_true',
|
||||
help='run in background (as daemon)')
|
||||
pgroup = parser.add_mutually_exclusive_group(required=False)
|
||||
pgroup.add_argument('-P', '--pcb', action='store_const', dest='mode',
|
||||
const=GPIO.BOARD, default=GPIO.BOARD,
|
||||
@@ -50,22 +54,22 @@ pgroup.add_argument('-r', '--edge-rising', action='store_const', dest='edge',
|
||||
const=GPIO.RISING, help='respond to pin going up')
|
||||
pgroup.add_argument('-a', '--edge-any', action='store_const', const=GPIO.BOTH,
|
||||
dest='edge', help='respond to any pin change')
|
||||
parser.add_argument('-i', '--ignore-result', action='store_true', default=False,
|
||||
help='ignore command result code (default: exit if <> 0)')
|
||||
parser.add_argument('-b', '--debounce', type=int, default=200,
|
||||
help='debounce period (in milliseconds, default=200)')
|
||||
parser.add_argument('-H', '--hold', type=int, default=-1,
|
||||
help='optional hold time, pin must be held stable for the '
|
||||
'specified time in milliseconds to trigger cmd')
|
||||
parser.add_argument('-t', '--timeout', type=int, default=-1,
|
||||
help='optional timeout (in milliseconds) to wait')
|
||||
parser.add_argument('-i', '--ignore-result', action='store_true', default=False,
|
||||
help='ignore command result code (default: exit if <> 0)')
|
||||
pgroup = parser.add_mutually_exclusive_group(required=False)
|
||||
pgroup.add_argument('-c', '--continuous', default=False, action='store_true',
|
||||
help='continously monitor GPIO pin and run cmd upon change')
|
||||
pgroup.add_argument('-o', '--once', action='store_false', dest='continuous',
|
||||
help='monitor pin and run cmd once, then exit (default)')
|
||||
parser.add_argument('-D', '--daemon', action='store_true',
|
||||
help='run in background (as daemon)')
|
||||
parser.add_argument('cmd', help='command to execute when pin goes low')
|
||||
parser.add_argument('arg', nargs='*', help='argument(s) for the command, use --'
|
||||
' before first argument to stop parsing parameters')
|
||||
parser.add_argument('arg', nargs='*', help='argument(s) for the command (*)')
|
||||
|
||||
args = parser.parse_args() # parse command line
|
||||
GPIO.setmode(args.mode) # set GPIO number mode
|
||||
@@ -76,12 +80,21 @@ if args.daemon and fork() != 0: # Fork for daemon mode
|
||||
|
||||
ret = 0
|
||||
while args.ignore_result or ret == 0:
|
||||
# wait for GPIO pin to be changed in the right direction
|
||||
if GPIO.wait_for_edge(args.pin, args.edge, bouncetime=args.debounce,
|
||||
timeout=args.timeout):
|
||||
ret = system(' '.join([ args.cmd ] + args.arg)) # run the command
|
||||
if not args.continuous: # exit if running once
|
||||
break
|
||||
state = GPIO.input(args.pin)
|
||||
# pin changed, if required check if it is stays the same long enough
|
||||
if (args.hold < 0) or (
|
||||
GPIO.wait_for_edge(args.pin, GPIO.BOTH, bouncetime=args.debounce,
|
||||
timeout=args.hold - args.debounce) is None):
|
||||
ret = system(' '.join([ args.cmd ] + [
|
||||
[ 'LOW', 'HIGH' ][state] if arg == '%STATE%' else
|
||||
str(args.pin) if arg == '%PIN%' else arg for arg in args.arg]))
|
||||
if not args.continuous: # exit if running once
|
||||
break
|
||||
else: # exit if timeout
|
||||
ret = -1
|
||||
break
|
||||
|
||||
GPIO.cleanup(args.pin) # cleanup GPIO
|
||||
|
||||
Reference in New Issue
Block a user