From c6e6e0c4400e929b0739a39c60c2d22521327915 Mon Sep 17 00:00:00 2001 From: ewall Date: Thu, 2 Sep 2010 19:09:12 -0400 Subject: [PATCH] now fully functional --- pybuddylib.py | 158 +++++++++++++++++++++++++++++--------------------- 1 file changed, 91 insertions(+), 67 deletions(-) diff --git a/pybuddylib.py b/pybuddylib.py index 5612929..9c962b8 100644 --- a/pybuddylib.py +++ b/pybuddylib.py @@ -7,6 +7,10 @@ # by Jose.Carlos.Luna@gmail.com and luis.peralta@gmail.com # who got most of the code from http://cuntography.com/blog/?p=17 # which is based on http://scott.weston.id.au/software/pymissile/ +# +# TODO: - Why does libusb require sudo/root on Linux? +# - Why does libusb-win32 error on Windoze? +# import logging from time import sleep @@ -25,10 +29,51 @@ else: console_handler.setLevel(logging.ERROR) log.addHandler(console_handler) +### General USB Device Class +class UsbDevice: + def __init__(self, vendor_id, product_id, skip): + busses = usb.busses() + self.handle = None + count = 0 + for bus in busses: + devices = bus.devices + for dev in devices: + + if dev.idVendor==vendor_id and dev.idProduct==product_id: + if count==skip: + log.info("iBuddy device found (vend: %s, prod: %s)." % (dev.idVendor, dev.idProduct)) + self.dev = dev + + self.conf = self.dev.configurations[0] + self.intf = self.conf.interfaces[0][0] + self.endpoints = [] + for endpoint in self.intf.endpoints: + self.endpoints.append(endpoint) + log.info("Endpoint found.") + return + else: + count=count+1 + raise NoBuddyException() + + def open(self): + # TODO: should the HID driver detachment be optional? + self.handle = self.dev.open() + + # we need to detach HID interface + try: + self.handle.detachKernelDriver(0) + self.handle.detachKernelDriver(1) + except: + pass + + self.handle.setConfiguration(self.conf) + self.handle.claimInterface(self.intf) + self.handle.setAltInterface(self.intf) + ### iBuddy Device Class class iBuddyDevice: USB_VENDOR = 0x1130 - USB_PRODUCT = int(0001) + USB_PRODUCT = int(0x0001) BATTERY = 0 SETUP = (0x22, 0x09, 0x00, 0x02, 0x01, 0x00, 0x00, 0x00) MESS = (0x55, 0x53, 0x42, 0x43, 0x00, 0x40, 0x02) @@ -42,6 +87,14 @@ class iBuddyDevice: OFF = 0 ON = 1 + BLUE = (0,0,1) + GREEN = (0,1,0) + LTBLUE = (0,1,1) + PURPLE = (1,0,1) + RED = (1,0,0) + WHITE = (1,1,1) + YELLOW = (1,1,0) + CLEAR = 0xFF command = CLEAR @@ -82,11 +135,11 @@ class iBuddyDevice: def setReverseBitValue(self,num,value): """ commands are sent as disabled bits """ if (value==1): - temp = 0xFF - (1<