Found a cheap USB Panic Button at Radio Shack the other day, this code makes it go, I based this code on many random examples I found strewn around the interwebz let this be my official credit of their work
#!/usr/bin/python
import webbrowser
import time
import usb
dev = usb.core.find(idVendor=0x1d34, idProduct=0x000d)
try:
dev.detach_kernel_driver(0)
except:
pass
press_flag_button = False
press_flag_lid = False
while 1:
# USB setup packet. I think it's a USB HID SET_REPORT.
dev.ctrl_transfer(0x21, 0x09, 0x0200, 0x000, '\x00\x00\x00\x00\x00\x00\x00\x02')
try:
result = dev.read(0x81, 8, 0, 100)
button_press = bool(~result[0] & 0x01)
lid_open = bool((result[0] & 0x02))
if button_press and press_flag_button == False:
dummy = webbrowser.open('http://www.google.ca')
press_flag_button = True
elif button_press == False:
press_flag_button = False
if lid_open and press_flag_lid == False:
print('prepare for google time!')
press_flag_lid = True
elif lid_open == False and press_flag_lid == True:
print('google time stand down :(')
press_flag_lid = False
elif lid_open == False:
press_flag_lid = False
except Exception, e:
# Sometimes this fails. Unsure why.
pass
time.sleep(0.001) # 10ms
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment