-
Notifications
You must be signed in to change notification settings - Fork 8
/
Project 18.js
150 lines (139 loc) · 3.76 KB
/
Project 18.js
1
import sysimport platformimport timeimport urllib2import usb.coreimport usb.utilCOMMAND_SETS = { "attack" : ( ("zero", 0), ("led", 1), ("right", 3250), ("up", 540), ("fire", 4), ("led", 0), ("zero", 0), ),}DOWN = 0x01UP = 0x02LEFT = 0x04RIGHT = 0x08FIRE = 0x10STOP = 0x20DEVICE = NoneDEVICE_TYPE = Nonedef usage(): print "Usage: missile.py [command] [value]"def setup_usb(): global DEVICE global DEVICE_TYPE DEVICE = usb.core.find(idVendor=0x2123, idProduct=0x1010) if DEVICE is None: DEVICE = usb.core.find(idVendor=0x0a81, idProduct=0x0701) if DEVICE is None: raise ValueError('Missile device not found') else: DEVICE_TYPE = "Original" else: DEVICE_TYPE = "Thunder" if "Linux" == platform.system(): try: DEVICE.detach_kernel_driver(0) except Exception, e: pass DEVICE.set_configuration()def send_cmd(cmd): if "Thunder" == DEVICE_TYPE: DEVICE.ctrl_transfer(0x21, 0x09, 0, 0, [0x02, cmd, 0x00,0x00,0x00,0x00,0x00,0x00]) elif "Original" == DEVICE_TYPE: DEVICE.ctrl_transfer(0x21, 0x09, 0x0200, 0, [cmd])def led(cmd): if "Thunder" == DEVICE_TYPE: DEVICE.ctrl_transfer(0x21, 0x09, 0, 0, [0x03, cmd, 0x00,0x00,0x00,0x00,0x00,0x00]) elif "Original" == DEVICE_TYPE: print("There is no LED on this device")def send_move(cmd, duration_ms): send_cmd(cmd) time.sleep(duration_ms / 1000.0) send_cmd(STOP)def run_command(command, value): command = command.lower() if command == "right": send_move(RIGHT, value) elif command == "left": send_move(LEFT, value) elif command == "up": send_move(UP, value) elif command == "down": send_move(DOWN, value) elif command == "zero" or command == "park" or command == "reset": # Move to bottom-left send_move(DOWN, 2000) send_move(LEFT, 8000) elif command == "pause" or command == "sleep": time.sleep(value / 1000.0)send_cmd(STOP)def run_command(command, value): command = command.lower() if command == "right": send_move(RIGHT, value) elif command == "left": send_move(LEFT, value) elif command == "up": send_move(UP, value) elif command == "down": send_move(DOWN, value) elif command == "zero" or command == "park" or command == "reset": # Move to bottom-left send_move(DOWN, 2000) send_move(LEFT, 8000) elif command == "pause" or command == "sleep": time.sleep(value / 1000.0) elif command == "led": if value == 0: led(0x00) else: led(0x01) elif command == "fire" or command == "shoot": if value < 1 or value > 4: value = 1 time.sleep(0.5) for i in range(value): send_cmd(FIRE) time.sleep(4.5) else: print "Error: Unknown command: '%s'" % commanddef run_command_set(commands): for cmd, value in commands: run_command(cmd, value)def main(args): if len(args) < 2: usage() sys.exit(1) setup_usb() command = args[1] value = 0 if len(args) > 2: value = int(args[2]) if command in COMMAND_SETS: run_command_set(COMMAND_SETS[command]) else: run_command(command, value)def main(args): if len(args) < 2: usage() sys.exit(1) setup_usb() command = args[1] value = 0 if len(args) > 2: value = int(args[2]) if command in COMMAND_SETS: run_command_set(COMMAND_SETS[command]) else: run_command(command, value)if __name__ == '__main__': main(sys.argv)