-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredbutton.py
73 lines (57 loc) · 1.86 KB
/
redbutton.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
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/python
import time
import usb
import syslog
import os
import sys
def onButtonDown():
syslog.syslog('BIG RED BUTTON PRESSED!!')
os.system('sudo -u james -n DISPLAY=:0.0 /home/james/scripts/brb-py/onbuttonpress.sh')
def onButtonUp():
syslog.syslog('BIG RED BUTTON RELEASED!!')
def findButton():
for bus in usb.busses():
for dev in bus.devices:
if dev.idVendor == 0x1d34 and dev.idProduct == 0x000d:
return dev
dev = findButton()
if dev is None:
syslog.syslog('redbutton.py executed but no Big Red Button could be found')
print '>>> The Big Red Button could not be found. Please plug it into a USB port.'
sys.exit()
handle = dev.open()
interface = dev.configurations[0].interfaces[0][0]
endpoint = interface.endpoints[0]
try:
handle.detachKernelDriver(interface)
except Exception, e:
# It may already be unloaded.
pass
handle.claimInterface(interface)
syslog.syslog('Started Big Red Button listener')
button_depressed = 0
button_depressed_last = 0
while 1:
# USB setup packet. I think it's a USB HID SET_REPORT.
result = handle.controlMsg(requestType=0x21, # OUT | CLASS | INTERFACE
request= 0x09, # SET_REPORT
value= 0x0200, # report type: OUTPUT
buffer="\x00\x00\x00\x00\x00\x00\x00\x02")
try:
result = handle.interruptRead(endpoint.address, endpoint.maxPacketSize)
if result[0] == 22:
button_depressed = 1
else:
button_depressed = 0
if (button_depressed_last != button_depressed):
if (button_depressed):
onButtonDown()
else:
onButtonUp()
button_depressed_last = button_depressed
#print [hex(x) for x in result]
except Exception, e:
# Sometimes this fails. Unsure why.
pass
time.sleep(endpoint.interval * 0.001) # 10ms
handle.releaseInterface(interface)