Skip to content

Instantly share code, notes, and snippets.

@ostcar
Created December 27, 2013 18:49
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 ostcar/8151022 to your computer and use it in GitHub Desktop.
Save ostcar/8151022 to your computer and use it in GitHub Desktop.
import os
import sys
from time import sleep
from usb.core import find as usb_find, USBError
def call_usb(func):
def wrapper(self, *args, **kwargs):
try:
func(self, *args, **kwargs)
except USBError:
self.init_usb()
func(self, *args, **kwargs)
return wrapper
class LaunchControl():
x = None
y = None
MAX_WIDTH = 100
MAX_HEIGHT = 100
FULL_WIDTH = 5.6
FULL_HEIGHT = 1.3
def __init__(self):
self.init_usb()
def __repr__(self):
return '<POSITON %s, %s>' % (self.x, self.y)
def goto(self, x=None, y=None):
"""
Go to a specific position.
The argument `x` is a integer. Positive for left, negative for right.
The argument `y` is a integer for the height. 0 for the bottom,
MAX_HEIGHT for the top.
"""
if x is not None and self.x is None:
self.reset_x()
if y is not None and self.y is None:
self.reset_y()
if x is not None:
x_diff = x - self.x
if x_diff:
seconds = x_diff * self.FULL_WIDTH / self.MAX_WIDTH / 2
if x_diff > 0:
self.left(seconds)
else:
self.right(-seconds)
self.x = x
if y is not None:
y_diff = y - self.y
if y_diff:
seconds = y_diff * self.FULL_HEIGHT / self.MAX_HEIGHT
if y_diff > 0:
self.up(seconds)
else:
self.down(-seconds)
self.y = y
def right(self, seconds):
self.turretRight()
sleep(seconds)
self.turretStop()
def left(self, seconds):
self.turretLeft()
sleep(seconds)
self.turretStop()
def up(self, seconds):
self.turretUp()
sleep(seconds)
self.turretStop()
def down(self, seconds):
self.turretDown()
sleep(seconds)
self.turretStop()
def reset(self):
"""
Reset the coordinates by moving the launcher to one end.
`seconds` are the seconds that are used.
"""
self.reset_x()
self.reset_y()
def reset_x(self, wait=6, go_right=True):
if go_right:
self.turretRight()
else:
self.turretLeft()
sleep(wait)
self.x = self.MAX_WIDTH - 2 * go_right * self.MAX_WIDTH
def reset_y(self, wait=1.3, go_up=True):
if go_up:
self.turretUp()
else:
self.turretDown()
sleep(wait)
self.turretStop()
self.y = MAX_HEIGHT * go_up
def init_usb(self):
self.dev = usb_find(idVendor=0x2123, idProduct=0x1010)
if self.dev is None:
raise ValueError('Launcher not found.')
if self.dev.is_kernel_driver_active(0) is True:
self.dev.detach_kernel_driver(0)
self.dev.set_configuration()
@call_usb
def turretUp(self):
try:
self.dev.ctrl_transfer(0x21, 0x09, 0, 0, [0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
except USBError:
self.init_usb()
@call_usb
def turretDown(self):
try:
self.dev.ctrl_transfer(0x21, 0x09, 0, 0, [0x02, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
except USBError:
self.init_usb()
@call_usb
def turretLeft(self):
self.dev.ctrl_transfer(0x21, 0x09, 0, 0, [0x02, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
@call_usb
def turretRight(self):
self.dev.ctrl_transfer(0x21,0x09,0,0,[0x02,0x08,0x00,0x00,0x00,0x00,0x00,0x00])
@call_usb
def turretStop(self):
self.dev.ctrl_transfer(0x21,0x09,0,0,[0x02,0x20,0x00,0x00,0x00,0x00,0x00,0x00])
@call_usb
def turretFire(self):
self.dev.ctrl_transfer(0x21,0x09,0,0,[0x02,0x10,0x00,0x00,0x00,0x00,0x00,0x00])
if __name__ == '__main__':
l = LaunchControl()
l.reset_x(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment