This repository has been archived by the owner on Jun 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpanicbutton.py
61 lines (55 loc) · 1.75 KB
/
panicbutton.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import usb.core
import time
import json
import os
import subprocess
def read_command():
home = os.path.expanduser('~')
f = open(home + '/.panicbutton/config.json')
config = json.load(f)
f.close()
return config['command']
def pressed(device):
""" Read the USB device
Return 1 if pressed and released, 0 otherwise.
"""
# Magic numbers are from http://search.cpan.org/~bkendi/Device-USB-PanicButton-0.04/lib/Device/USB/PanicButton.pm
try:
device.set_configuration()
ct = device.ctrl_transfer(bmRequestType=0xA1, bRequest=1, wValue=0x300, data_or_wLength=8, timeout=250)
result = ct[0]
except Exception, e:
result = 0
print e
return result
# Product ID: 0x0202
# Vendor ID: 0x1130 (Tenx Technology, Inc.)
# Version: 1.00
# Speed: Up to 1.5 Mb/sec
device = usb.core.find(idVendor=0x1130, idProduct=0x0202)
print "Panic Button time! Press Ctrl-C to quit."
print "Looking for the Panic Button... please wait"
# loop until device is found
while 1:
try:
if not device:
raise ValueError("Panic Button not found: Are you sure it's plugged in?")
except Exception, e:
print e
time.sleep(1)
device = usb.core.find(idVendor=0x1130, idProduct=0x0202)
continue
print "Panic Button found. Press away!"
break
# do an initial read to clear out any stale button presses
pressed(device)
if __name__ == "__main__":
try:
while 1:
if pressed(device):
shell_command = read_command()
print "Executing command: " + shell_command
subprocess.call(shell_command.split(' '))
time.sleep(.15)
except KeyboardInterrupt, e:
exit(0)