Skip to content

Instantly share code, notes, and snippets.

@koenbollen
Created July 22, 2014 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koenbollen/6f1d4e7554d759b15497 to your computer and use it in GitHub Desktop.
Save koenbollen/6f1d4e7554d759b15497 to your computer and use it in GitHub Desktop.
Generic Panic Button - Python Script, to execute commands. (see: https://gist.github.com/koenbollen/a87b3eca1e92f2dade70)
#!/usr/bin/env python
#
# Copyright (c) 2014, Koen Bollen <meneer@koenbollen.nl>
#
# Permission to use, copy, modify, and/or distribute this software for any purpose
# with or without fee is hereby granted, provided that the above copyright notice
# and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
# THIS SOFTWARE.
from multiprocessing import Process
from subprocess import call
import argparse
import serial
import sys
import time
DEFAULT_PORT = '/dev/tty.OPIOT_BT_09-DevB'
def connect(port, tries=0, timeout=1):
while True:
try:
return serial.Serial(port, 9600, timeout=timeout)
except Exception, e:
if tries > 0:
tries -= 1
if tries == 0:
raise e
time.sleep(.1)
parser = argparse.ArgumentParser(prog='gpb', description='Generic Panic Button')
parser.add_argument('command', type=str, nargs=1, help='the command to run')
parser.add_argument('argument', type=str, nargs='*', help='arguments to pass')
parser.add_argument('--verbose', '-v', action='store_true', help='more verbose output')
parser.add_argument('--once', '-o', action='store_true', help='only try and bluetooth connect once')
parser.add_argument('--quit', '-q', action='store_true', help='quit after first execute')
parser.add_argument('--event', '-e', type=str, default='down', help='on which event to execute: down, up, connect')
parser.add_argument('--connect', '-c', action='store_true', help='same as `--event connect`, implies --quit')
parser.add_argument('--blink', '-b', action='store_true', help='blink LED on ping')
args = parser.parse_args()
if args.connect:
args.event = 'connect'
if args.event == 'connect':
args.connect = True
args.quit = True
if args.verbose:
print(args)
def execute_sync():
call(args.command+args.argument, shell=False)
def execute():
p=Process(target=execute_sync)
p.start()
if args.verbose:
print('connecting...')
ser = None
try:
try:
ser = connect(DEFAULT_PORT, args.once and 1 or 0, 20)
except Exception, e:
print >>sys.stderr, 'failed to make a bluetooth connection:', e
if args.verbose:
print('connected')
if args.event == 'connect':
execute()
else:
last = 0
quit = False
while True:
if ser.inWaiting() > 0:
c = ser.read()
run = False
if c == 'D':
if args.verbose:
print('down')
if args.event == 'down':
run = True
elif c == 'U':
if args.verbose:
print('up')
if args.event == 'up':
run = True
if quit:
break
elif c == 'P':
if args.verbose:
print('pong')
if run:
execute()
if args.quit:
if args.event == 'down':
quit = True
else:
break
now = time.time()
if now-last > 10:
if args.verbose:
print('ping')
ser.write('P')
if args.blink:
ser.write('L')
ser.flush()
last = now
time.sleep(0.1)
if args.verbose:
print('quit')
ser.flush()
time.sleep(0.9)
ser.write('Q')
ser.close()
except KeyboardInterrupt:
if ser != None:
ser.write('Q')
ser.close()
if args.verbose:
print('quit')
sys.exit()
# Sincerely,
# —Koen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment