コード例 #1
0
    def __init__(self):
        usb.init()

        # Get usb busses
        if not usb.get_busses():
            usb.find_busses()
            usb.find_devices()
        busses = usb.get_busses()

        # Find device by IDs
        found = False
        for bus in busses:
            for dev in bus.devices:
                #print 'idVendor: 0x%04x idProduct: 0x%04x'%(dev.descriptor.idVendor,
                #                                            dev.descriptor.idProduct)
                if (dev.descriptor.idVendor == USB_VENDOR_ID
                        and dev.descriptor.idProduct == USB_PRODUCT_ID):
                    found = True
                    break
            if found:
                break
        if not found:
            raise RuntimeError("Cannot find device.")

        self.libusb_handle = usb.open(dev)

        interface_nr = 0
        if hasattr(usb, 'get_driver_np'):
            # non-portable libusb function available
            name = usb.get_driver_np(self.libusb_handle, interface_nr)
            if name != '':
                debug("attached to kernel driver '%s', detaching." % name)
                usb.detach_kernel_driver_np(self.libusb_handle, interface_nr)

        if dev.descriptor.bNumConfigurations > 1:
            debug("WARNING: more than one configuration, choosing first")

        usb.set_configuration(self.libusb_handle,
                              dev.config[0].bConfigurationValue)
        usb.claim_interface(self.libusb_handle, interface_nr)

        self.output_buffer = ctypes.create_string_buffer(USB_BUFFER_SIZE)
        self.input_buffer = ctypes.create_string_buffer(USB_BUFFER_SIZE)
        for i in range(USB_BUFFER_SIZE):
            self.output_buffer[i] = chr(0x00)
            self.input_buffer[i] = chr(0x00)

        # Send dummy commmand - this is due to what appears to be a bug which makes first
        # bulk write not appear. The same thing happes to the bullkin so a send/receive
        # request is sent twice to initial a dummy bulkin. After this everything seems to
        # as it should.
        for i in range(0, 1):
            self.output_buffer[0] = chr(USB_CMD_DUMMY % 0x100)
            self._send_and_receive(in_timeout=100)

        # Get top value
        self.top = self._get_top()
コード例 #2
0
ファイル: ttrigger.py プロジェクト: Limpinho0/motmot-camtrig
    def _open_device(self):
        usb.init()
        if not usb.get_busses():
            usb.find_busses()
            usb.find_devices()

        busses = usb.get_busses()

        found = False
        for bus in busses:
            for dev in bus.devices:
                debug('idVendor: 0x%04x idProduct: 0x%04x'%
                      (dev.descriptor.idVendor,dev.descriptor.idProduct))
                if (dev.descriptor.idVendor == 0x1781 and
                    dev.descriptor.idProduct == 0x0BAF):
                    found = True
                    break
            if found:
                break
        if not found:
            if int(os.environ.get('REQUIRE_TRIGGER','1')):
                raise RuntimeError("Cannot find device. (Perhaps run with "
                                   "environment variable REQUIRE_TRIGGER=0.)")
            else:
                self._have_trigger = False
                return
        with self._lock:
            self._libusb_handle = usb.open(dev)

            manufacturer = usb.get_string_simple(self._libusb_handle,dev.descriptor.iManufacturer)
            product = usb.get_string_simple(self._libusb_handle,dev.descriptor.iProduct)
            serial = usb.get_string_simple(self._libusb_handle,dev.descriptor.iSerialNumber)

            assert manufacturer == 'Strawman', 'Wrong manufacturer: %s'%manufacturer
            valid_product = 'Camera Trigger 1.0'
            if product != valid_product:
                errmsg = 'Expected product "%s", but you have "%s"'%(
                    valid_product,product)
                if self.ignore_version_mismatch:
                    print 'WARNING:',errmsg
                else:
                    raise ValueError(errmsg)

            interface_nr = 0
            if hasattr(usb,'get_driver_np'):
                # non-portable libusb extension
                name = usb.get_driver_np(self._libusb_handle,interface_nr)
                if name != '':
                    usb.detach_kernel_driver_np(self._libusb_handle,interface_nr)

            if dev.descriptor.bNumConfigurations > 1:
                debug("WARNING: more than one configuration, choosing first")

            config = dev.config[0]
            usb.set_configuration(self._libusb_handle, config.bConfigurationValue)
            usb.claim_interface(self._libusb_handle, interface_nr)
コード例 #3
0
ファイル: clkdirpwm.py プロジェクト: willdickson/CLKDIRtoPWM
    def __init__(self):
        usb.init()

        # Get usb busses
        if not usb.get_busses():
            usb.find_busses()
            usb.find_devices()
        busses = usb.get_busses()

        # Find device by IDs
        found = False
        for bus in busses:
            for dev in bus.devices:
                #print 'idVendor: 0x%04x idProduct: 0x%04x'%(dev.descriptor.idVendor,
                #                                            dev.descriptor.idProduct)
                if (dev.descriptor.idVendor == USB_VENDOR_ID
                        and dev.descriptor.idProduct == USB_PRODUCT_ID):
                    found = True
                    break
            if found:
                break
        if not found:
            raise RuntimeError("Cannot find device.")

        self.libusb_handle = usb.open(dev)

        interface_nr = 0
        if hasattr(usb, 'get_driver_np'):
            # non-portable libusb function available
            name = usb.get_driver_np(self.libusb_handle, interface_nr)
            if name != '':
                debug("attached to kernel driver '%s', detaching." % name)
                usb.detach_kernel_driver_np(self.libusb_handle, interface_nr)

        if dev.descriptor.bNumConfigurations > 1:
            debug("WARNING: more than one configuration, choosing first")

        usb.set_configuration(self.libusb_handle,
                              dev.config[0].bConfigurationValue)
        usb.claim_interface(self.libusb_handle, interface_nr)

        self.output_buffer = ctypes.create_string_buffer(USB_BUFFER_SIZE)
        self.input_buffer = ctypes.create_string_buffer(USB_BUFFER_SIZE)
        for i in range(USB_BUFFER_SIZE):
            self.output_buffer[i] = chr(0x00)
            self.input_buffer[i] = chr(0x00)
コード例 #4
0
    def open(self) :
        """ Attache NIA interface
        """###
        if not self.device:
            raise RuntimeError("Cable isn't plugged in")

        self.handle = usb.open(self.device)
        if hasattr(usb,'get_driver_np'):
          # non-portable libusb extension
          name = usb.get_driver_np(self.handle,self.interface)
          if name != '':
            debug("attached to kernel driver '%s', detaching."%name )
            usb.detach_kernel_driver_np(self.handle,self.interface)  			#self.handle.detachKernelDriver(0)
        																		#self.handle.detachKernelDriver(1)
        usb.set_configuration(self.handle, self.config.bConfigurationValue)   	#self.handle.setConfiguration(self.config)
        usb.claim_interface(self.handle, self.interface)                      	#self.handle.claimInterface(self.interface)
        																		#self.handle.setAltInterface(self.interface)
        self.INPUT_BUFFER = ctypes.create_string_buffer(self.PACKET_LENGTH)
コード例 #5
0
    def open(self):
        """ Attache NIA interface
        """  ###
        if not self.device:
            raise RuntimeError("Cable isn't plugged in")

        self.handle = usb.open(self.device)
        if hasattr(usb, 'get_driver_np'):
            # non-portable libusb extension
            name = usb.get_driver_np(self.handle, self.interface)
            if name != '':
                debug("attached to kernel driver '%s', detaching." % name)
                usb.detach_kernel_driver_np(
                    self.handle,
                    self.interface)  #self.handle.detachKernelDriver(0)
        #self.handle.detachKernelDriver(1)
        usb.set_configuration(self.handle, self.config.bConfigurationValue
                              )  #self.handle.setConfiguration(self.config)
        usb.claim_interface(
            self.handle,
            self.interface)  #self.handle.claimInterface(self.interface)
        #self.handle.setAltInterface(self.interface)
        self.INPUT_BUFFER = ctypes.create_string_buffer(self.PACKET_LENGTH)
コード例 #6
0
    def __init__(
        self,
        serial_number=None,
        usb_product_id=DEFAULT_USB_PRODUCT_ID,
        usb_vendor_id=DEFAULT_USB_VENDOR_ID,
        usb_bulkout_ep_address=DEFAULT_USB_BULKOUT_EP_ADDRESS,
        usb_bulkin_ep_address=DEFAULT_USB_BULKIN_EP_ADDRESS,
        usb_buffer_out_size=DEFAULT_USB_BUFFER_OUT_SIZE,
        usb_buffer_in_size=DEFAULT_USB_BUFFER_IN_SIZE,
    ):
        """
        Open and initialize usb device.
        
        Arguments: None
        
        Return: None.
        """
        self.serial_number = serial_number
        self.usb_product_id = usb_product_id
        self.usb_vendor_id = usb_vendor_id
        self.usb_bulkout_ep_address = usb_bulkout_ep_address
        self.usb_bulkin_ep_address = usb_bulkin_ep_address
        self.usb_buffer_out_size = usb_buffer_out_size
        self.usb_buffer_in_size = usb_buffer_in_size
        usb.init()

        # Get usb busses
        if not usb.get_busses():
            usb.find_busses()
            usb.find_devices()
        busses = usb.get_busses()

        # Find device by IDs
        found = False
        dev_list = []
        for bus in busses:
            for dev in bus.devices:
                if (dev.descriptor.idVendor == self.usb_vendor_id
                        and dev.descriptor.idProduct == self.usb_product_id):
                    dev_list.append(dev)
                    found = True
        if not found:
            raise RuntimeError("Cannot find device.")

        if serial_number == None:
            # No serial number specified - take first device
            dev = dev_list[0]
            self.libusb_handle = usb.open(dev)
            self.dev = dev
        else:
            # Try and find device with specified serial number
            found = False
            for dev in dev_list:
                self.dev = dev
                self.libusb_handle = usb.open(dev)
                sn = self.get_serial_number()
                if sn == serial_number:
                    found = True
                    break
                else:
                    ret = usb.close(self.libusb_handle)
            if not found:
                raise RuntimeError("Cannot find device w/ serial number %s." %
                                   (serial_number, ))

        self.interface_nr = 0
        if hasattr(usb, 'get_driver_np'):
            # non-portable libusb function available
            name = usb.get_driver_np(self.libusb_handle, self.interface_nr)
            if name != '':
                debug_print("attached to kernel driver '%s', detaching." %
                            name)
                usb.detach_kernel_driver_np(self.libusb_handle,
                                            self.interface_nr)

        if dev.descriptor.bNumConfigurations > 1:
            debug_print("WARNING: more than one configuration, choosing first")

        usb.set_configuration(self.libusb_handle,
                              self.dev.config[0].bConfigurationValue)
        usb.claim_interface(self.libusb_handle, self.interface_nr)

        self.output_buffer = ctypes.create_string_buffer(
            self.usb_buffer_out_size)
        self.input_buffer = ctypes.create_string_buffer(
            self.usb_buffer_in_size)
        for i in range(self.usb_buffer_in_size):
            self.input_buffer[i] = chr(0x00)
        for i in range(self.usb_buffer_out_size):
            self.output_buffer[i] = chr(0x00)

        # Clear any possible halt on the endpoints
        ret = usb.clear_halt(self.libusb_handle, self.usb_bulkout_ep_address)
        ret = usb.clear_halt(self.libusb_handle, self.usb_bulkin_ep_address)

        # Buffer position marker for reading from and writing to buffer
        self.output_buffer_pos = 0
        self.input_buffer_pos = 0

        # Set bref function based on python version
        if get_python_vnum() >= BYREF_EXISTS_VNUM:
            self.byref = ctypes.byref
        else:
            self.byref = byref_via_pointer
コード例 #7
0
    def _open_device(self):
        require_trigger = int(os.environ.get('REQUIRE_TRIGGER','1'))
        if require_trigger:

            usb.init()
            if not usb.get_busses():
                usb.find_busses()
                usb.find_devices()

            busses = usb.get_busses()

            found = False
            for bus in busses:
                for dev in bus.devices:
                    debug('idVendor: 0x%04x idProduct: 0x%04x'%
                          (dev.descriptor.idVendor,dev.descriptor.idProduct))
                    if (dev.descriptor.idVendor == 0x1781 and
                        dev.descriptor.idProduct == 0x0BAF):
                        found = True
                        break
                if found:
                    break
            if not found:
                raise RuntimeError("Cannot find device. (Perhaps run with "
                                   "environment variable REQUIRE_TRIGGER=0.)")
        else:
            self.real_device = False
            return
        with self._lock:
            self._libusb_handle = usb.open(dev)

            manufacturer = usb.get_string_simple(self._libusb_handle,dev.descriptor.iManufacturer)
            product = usb.get_string_simple(self._libusb_handle,dev.descriptor.iProduct)
            serial = usb.get_string_simple(self._libusb_handle,dev.descriptor.iSerialNumber)

            assert manufacturer == 'Strawman', 'Wrong manufacturer: %s'%manufacturer
            valid_product = 'Camera Trigger 1.0'
            if product == valid_product:
                self.FOSC = 8000000.0
            elif product.startswith('Camera Trigger 1.01'):
                osc_re = r'Camera Trigger 1.01 \(F_CPU = (.*)\)\w*'
                match = re.search(osc_re,product)
                fosc_str = match.groups()[0]
                if fosc_str.endswith('UL'):
                    fosc_str = fosc_str[:-2]
                self.FOSC = float(fosc_str)
            else:
                errmsg = 'Expected product "%s", but you have "%s"'%(
                    valid_product,product)
                if self.ignore_version_mismatch:
                    print 'WARNING:',errmsg
                    self.FOSC = 8000000.0
                    print ' assuming FOSC=',self.FOSC
                else:
                    raise ValueError(errmsg)

            interface_nr = 0
            if hasattr(usb,'get_driver_np'):
                # non-portable libusb extension
                name = usb.get_driver_np(self._libusb_handle,interface_nr)
                if name != '':
                    usb.detach_kernel_driver_np(self._libusb_handle,interface_nr)

            if dev.descriptor.bNumConfigurations > 1:
                debug("WARNING: more than one configuration, choosing first")

            config = dev.config[0]
            usb.set_configuration(self._libusb_handle, config.bConfigurationValue)
            usb.claim_interface(self._libusb_handle, interface_nr)
        self.real_device = True
コード例 #8
0
ファイル: example.py プロジェクト: slivingston/pylibusb
found = False
for bus in busses:
    for dev in bus.devices:
        debug('idVendor: 0x%04x idProduct: 0x%04x'%(dev.descriptor.idVendor,
                                                    dev.descriptor.idProduct))
        if (dev.descriptor.idVendor == 0x046d and
            dev.descriptor.idProduct == 0xc01b): # Logitech MX310 optical mouse
            found = True
            break
    if found:
        break
if not found:
    raise RuntimeError("Cannot find device.")

debug('found device',dev)
libusb_handle = usb.open(dev)

interface_nr = 0
if hasattr(usb,'get_driver_np'):
    # non-portable libusb extension
    name = usb.get_driver_np(libusb_handle,interface_nr)
    if name != '':
        debug("attached to kernel driver '%s', detaching."%name )
        usb.detach_kernel_driver_np(libusb_handle,interface_nr)

if dev.descriptor.bNumConfigurations > 1:
    debug("WARNING: more than one configuration, choosing first")

debug('setting configuration')
debug('dev.config[0]',dev.config[0])
config = dev.config[0]
コード例 #9
0
    def __init__(self, serial_number=None):
        """
        Open and initialize usb device.
        
        Arguments: None
        
        Return: None.
        """
        usb.init()

        #usb.set_debug(3)

        # Get usb busses
        if not usb.get_busses():
            usb.find_busses()
            usb.find_devices()
        busses = usb.get_busses()

        # Find device by IDs
        found = False
        dev_list = []
        for bus in busses:
            for dev in bus.devices:
                if (dev.descriptor.idVendor == USB_VENDOR_ID
                        and dev.descriptor.idProduct == USB_PRODUCT_ID):
                    dev_list.append(dev)
                    found = True
                    #break
            #if found:
            #    break
        if not found:
            raise RuntimeError("Cannot find device.")

        if serial_number == None:
            # No serial number specified - take first device
            dev = dev_list[0]
            self.libusb_handle = usb.open(dev)
            self.dev = dev
        else:
            # Try and find device with specified serial number
            found = False
            for dev in dev_list:
                self.dev = dev
                self.libusb_handle = usb.open(dev)
                sn = self.get_serial_number()
                if sn == serial_number:
                    found = True
                    break
                else:
                    ret = usb.close(self.libusb_handle)
            if not found:
                raise RuntimeError("Cannot find device w/ serial number %s." %
                                   (serial_number, ))

        interface_nr = 0
        if hasattr(usb, 'get_driver_np'):
            # non-portable libusb function available
            name = usb.get_driver_np(self.libusb_handle, interface_nr)
            if name != '':
                debug("attached to kernel driver '%s', detaching." % name)
                usb.detach_kernel_driver_np(self.libusb_handle, interface_nr)

        if dev.descriptor.bNumConfigurations > 1:
            debug("WARNING: more than one configuration, choosing first")

        usb.set_configuration(self.libusb_handle,
                              dev.config[0].bConfigurationValue)
        usb.claim_interface(self.libusb_handle, interface_nr)

        self.output_buffer = ctypes.create_string_buffer(USB_BUFFER_SIZE)
        self.input_buffer = ctypes.create_string_buffer(USB_BUFFER_SIZE)
        for i in range(USB_BUFFER_SIZE):
            self.output_buffer[i] = chr(0x00)
            self.input_buffer[i] = chr(0x00)

        # Get max and min velocities
        self.max_vel = self.get_max_vel()
        self.min_vel = self.get_min_vel()
コード例 #10
0
    def __init__(self, card_address):
        """
        Open a local 8055 type card via pylibusb.
        """
        self.card_address = card_address
        _debug('_libusb_io.__init__("%s")' % card_address)
        m = re.match('^card([0-9]+)$', card_address)
        if not m:
            raise ValueError("invalid card address '%s'" % card_address)
        self.card_number = int(m.groups()[0])
        _debug('  card_number = %d' % self.card_number)

        # ----
        # Find the actual device
        # ----
        busses = usb.get_busses()
        if not busses:
            usb.find_busses()
            usb.find_devices()
            busses = usb.get_busses()
        found = False
        for bus in busses:
            for dev in bus.devices:
                _debug('  idVendor: 0x%04x idProduct 0x%04x' % (dev.descriptor.idVendor, dev.descriptor.idProduct))
                if (dev.descriptor.idVendor == 0x10cf and
                    dev.descriptor.idProduct == 0x55F0 + self.card_number):
                    self.card_type = pyopen8055.OPEN8055
                    found = True
                    break
                if (dev.descriptor.idVendor == 0x10cf and
                    dev.descriptor.idProduct == 0x5500 + self.card_number):
                    self.card_type = pyopen8055.K8055
                    found = True
                    break
            if found:
                break
        if not found:
            raise RuntimeError("'%s' not found" % card_address)
        _debug("  found device '%s'" % dev)

        # ----
        # Open the device and detach an eventually existing kernel driver.
        # ----
        self.handle = usb.open(dev)
        if hasattr(usb, 'get_driver_np'):
            name = usb.get_driver_np(self.handle, 0)
            if name != '':
                _debug("  attached to kernel driver '%s' - detaching" % name)
                usb.detach_kernel_driver_np(self.handle, 0)

        # ----
        # Set the active configuration. 
        # ----
        config = dev.config[0]
        usb.set_configuration(self.handle, config.bConfigurationValue)

        # ----
        # If the code above identified the card as a K8055, it could also be
        # a K8055N. Try switching it to K8055N protocol to find out.
        # ----
        if self.card_type == pyopen8055.K8055:
            _debug("  attempting to switch to K8055N protocol")
            buf = ctypes.create_string_buffer(8)
            buf[0] = chr(pyopen8055.TAG_K8055N_SET_PROTO)
            _debug("    sending SET_PROTO")
            self.send_pkt(buf)
            _debug("    reading reply")
            reply = self.recv_pkt(8)
            if ord(reply[1]) > 10:
                _debug("    reply card_id=%d" % ord(reply[1]))
                if ord(reply[1]) <= 20:
                    self.send_pkt(buf)
                while ord(reply[1]) <= 20:
                    reply = self.recv_pkt(8)
                    _debug("    reply card_id=%d" % ord(reply[1]))
                self.card_type = pyopen8055.K8055N
                _debug("  successfully switched to K8055N protocol")
            else:
                _debug("  found original K8055 - no protocol change")

        _debug("  %s: card_type = %s" % (card_address, self.card_type))
コード例 #11
0
ファイル: trigger.py プロジェクト: Limpinho0/motmot-camtrig
    def __init__(self,ignore_version_mismatch=False):
        usb.init()

        if not usb.get_busses():
            usb.find_busses()
            usb.find_devices()

        busses = usb.get_busses()

        found = False
        for bus in busses:
            for dev in bus.devices:
                debug('idVendor: 0x%04x idProduct: 0x%04x'%(dev.descriptor.idVendor,
                                                            dev.descriptor.idProduct))
                if (dev.descriptor.idVendor == 0x1781 and
                    dev.descriptor.idProduct == 0x0BAF):
                    found = True
                    break
            if found:
                break
        if not found:
            raise RuntimeError("Cannot find device. (Perhaps run with environment variable REQUIRE_TRIGGER=0.)")

        debug('found device',dev)
        self.libusb_handle = usb.open(dev)

        manufacturer = usb.get_string_simple(self.libusb_handle,dev.descriptor.iManufacturer)
        product = usb.get_string_simple(self.libusb_handle,dev.descriptor.iProduct)
        serial = usb.get_string_simple(self.libusb_handle,dev.descriptor.iSerialNumber)

        assert manufacturer == 'Strawman', 'Wrong manufacturer: %s'%manufacturer
        valid_product = 'Camera Trigger 1.0'
        if product != valid_product:
            errmsg = 'Expected product "%s", but you have "%s"'%(
                valid_product,product)
            if ignore_version_mismatch:
                print 'WARNING:',errmsg
            else:
                raise ValueError(errmsg)

        interface_nr = 0
        if hasattr(usb,'get_driver_np'):
            # non-portable libusb extension
            name = usb.get_driver_np(self.libusb_handle,interface_nr)
            if name != '':
                debug("attached to kernel driver '%s', detaching."%name )
                usb.detach_kernel_driver_np(self.libusb_handle,interface_nr)

        if dev.descriptor.bNumConfigurations > 1:
            debug("WARNING: more than one configuration, choosing first")

        debug('setting configuration')
        debug('dev.config[0]',dev.config[0])
        config = dev.config[0]
        debug('config.bConfigurationValue',config.bConfigurationValue)
        usb.set_configuration(self.libusb_handle, config.bConfigurationValue)
        debug('claiming interface')
        debug('config.bNumInterfaces',config.bNumInterfaces)
        #print 'config.interface',config.interface
##        for i in range(config.bNumInterfaces):
##            iface = config.interface[i]
##            print iface
##            print iface.altsetting

        usb.claim_interface(self.libusb_handle, interface_nr)

        self.OUTPUT_BUFFER = ctypes.create_string_buffer(16)

        self.FOSC = 8000000 # 8 MHz
        trigger_carrier_freq = 0.0 # stopped

        self.timer3_TOP = None # unknown
        self.timer3_CS = 8
        #print 'set A self.timer3_CS to',self.timer3_CS
        self._set_timer3_metadata(trigger_carrier_freq)
コード例 #12
0
ファイル: usb_device.py プロジェクト: pfalcon/OmniReflow
    def __init__(
            self,
            serial_number=None,
            usb_product_id=DEFAULT_USB_PRODUCT_ID,
            usb_vendor_id=DEFAULT_USB_VENDOR_ID,
            usb_bulkout_ep_address=DEFAULT_USB_BULKOUT_EP_ADDRESS,
            usb_bulkin_ep_address=DEFAULT_USB_BULKIN_EP_ADDRESS,
            usb_buffer_out_size = DEFAULT_USB_BUFFER_OUT_SIZE,
            usb_buffer_in_size = DEFAULT_USB_BUFFER_IN_SIZE,
            ):
        """
        Open and initialize usb device.
        
        Arguments: None
        
        Return: None.
        """
        self.serial_number = serial_number
        self.usb_product_id = usb_product_id
        self.usb_vendor_id = usb_vendor_id
        self.usb_bulkout_ep_address = usb_bulkout_ep_address
        self.usb_bulkin_ep_address = usb_bulkin_ep_address
        self.usb_buffer_out_size = usb_buffer_out_size
        self.usb_buffer_in_size = usb_buffer_in_size
        usb.init()
        
        # Get usb busses
        if not usb.get_busses():
            usb.find_busses()            
            usb.find_devices()
        busses = usb.get_busses()

        # Find device by IDs
        found = False
        dev_list = []
        for bus in busses:
            for dev in bus.devices:
                if (dev.descriptor.idVendor == self.usb_vendor_id and
                    dev.descriptor.idProduct == self.usb_product_id):
                    dev_list.append(dev)
                    found = True
        if not found:
            raise RuntimeError("Cannot find device.")

        if serial_number == None:
            # No serial number specified - take first device
            dev = dev_list[0]
            self.libusb_handle = usb.open(dev)
            self.dev = dev
        else:
            # Try and find device with specified serial number
            found = False
            for dev in dev_list:
                self.dev = dev
                self.libusb_handle = usb.open(dev)
                sn = self.get_serial_number()
                if sn == serial_number:
                    found = True
                    break
                else:
                    ret = usb.close(self.libusb_handle)
            if not found:
                raise RuntimeError("Cannot find device w/ serial number %s."%(serial_number,))

        self.interface_nr = 0
        if hasattr(usb,'get_driver_np'):
            # non-portable libusb function available
            name = usb.get_driver_np(self.libusb_handle,self.interface_nr)
            if name != '':
                debug_print("attached to kernel driver '%s', detaching."%name )
                usb.detach_kernel_driver_np(self.libusb_handle,self.interface_nr)

        if dev.descriptor.bNumConfigurations > 1:
            debug_print("WARNING: more than one configuration, choosing first")

        usb.set_configuration(self.libusb_handle, self.dev.config[0].bConfigurationValue)
        usb.claim_interface(self.libusb_handle, self.interface_nr)

        self.output_buffer = ctypes.create_string_buffer(self.usb_buffer_out_size)
        self.input_buffer = ctypes.create_string_buffer(self.usb_buffer_in_size)
        for i in range(self.usb_buffer_in_size):
            self.input_buffer[i] = chr(0x00)
        for i in range(self.usb_buffer_out_size):
            self.output_buffer[i] = chr(0x00)

        # Clear any possible halt on the endpoints
        ret = usb.clear_halt(self.libusb_handle,self.usb_bulkout_ep_address)
        ret = usb.clear_halt(self.libusb_handle,self.usb_bulkin_ep_address)

        # Buffer position marker for reading from and writing to buffer
        self.output_buffer_pos = 0
        self.input_buffer_pos = 0

        # Set bref function based on python version
        if get_python_vnum() >= BYREF_EXISTS_VNUM:
            self.byref = ctypes.byref
        else:
            self.byref = byref_via_pointer
コード例 #13
0
    def __init__(self, ignore_version_mismatch=False):
        usb.init()

        if not usb.get_busses():
            usb.find_busses()
            usb.find_devices()

        busses = usb.get_busses()

        found = False
        for bus in busses:
            for dev in bus.devices:
                debug('idVendor: 0x%04x idProduct: 0x%04x' %
                      (dev.descriptor.idVendor, dev.descriptor.idProduct))
                if (dev.descriptor.idVendor == 0x1781
                        and dev.descriptor.idProduct == 0x0BAF):
                    found = True
                    break
            if found:
                break
        if not found:
            raise RuntimeError(
                "Cannot find device. (Perhaps run with environment variable REQUIRE_TRIGGER=0.)"
            )

        debug('found device', dev)
        self.libusb_handle = usb.open(dev)

        manufacturer = usb.get_string_simple(self.libusb_handle,
                                             dev.descriptor.iManufacturer)
        product = usb.get_string_simple(self.libusb_handle,
                                        dev.descriptor.iProduct)
        serial = usb.get_string_simple(self.libusb_handle,
                                       dev.descriptor.iSerialNumber)

        assert manufacturer == 'Strawman', 'Wrong manufacturer: %s' % manufacturer
        valid_product = 'Camera Trigger 1.0'
        if product == valid_product:
            self.FOSC = 8000000.0
        elif product.startswith('Camera Trigger 1.01'):
            osc_re = r'Camera Trigger 1.01 \(F_CPU = (.*)\)\w*'
            match = re.search(osc_re, product)
            fosc_str = match.groups()[0]
            if fosc_str.endswith('UL'):
                fosc_str = fosc_str[:-2]
            self.FOSC = float(fosc_str)
        else:
            errmsg = 'Expected product "%s", but you have "%s"' % (
                valid_product, product)
            if ignore_version_mismatch:
                print 'WARNING:', errmsg
            else:
                raise ValueError(errmsg)

        interface_nr = 0
        if hasattr(usb, 'get_driver_np'):
            # non-portable libusb extension
            name = usb.get_driver_np(self.libusb_handle, interface_nr)
            if name != '':
                debug("attached to kernel driver '%s', detaching." % name)
                usb.detach_kernel_driver_np(self.libusb_handle, interface_nr)

        if dev.descriptor.bNumConfigurations > 1:
            debug("WARNING: more than one configuration, choosing first")

        debug('setting configuration')
        debug('dev.config[0]', dev.config[0])
        config = dev.config[0]
        debug('config.bConfigurationValue', config.bConfigurationValue)
        usb.set_configuration(self.libusb_handle, config.bConfigurationValue)
        debug('claiming interface')
        debug('config.bNumInterfaces', config.bNumInterfaces)
        #print 'config.interface',config.interface
        ##        for i in range(config.bNumInterfaces):
        ##            iface = config.interface[i]
        ##            print iface
        ##            print iface.altsetting

        usb.claim_interface(self.libusb_handle, interface_nr)

        self.OUTPUT_BUFFER = ctypes.create_string_buffer(16)

        trigger_carrier_freq = 0.0  # stopped

        self.timer3_TOP = None  # unknown
        self.timer3_CS = 8
        #print 'set A self.timer3_CS to',self.timer3_CS
        self._set_timer3_metadata(trigger_carrier_freq)
コード例 #14
0
ファイル: ttrigger.py プロジェクト: motmot/fview_ext_trig
    def _open_device(self):
        if require_trigger:

            usb.init()
            if not usb.get_busses():
                usb.find_busses()
                usb.find_devices()

            busses = usb.get_busses()

            found = False
            for bus in busses:
                for dev in bus.devices:
                    debug('idVendor: 0x%04x idProduct: 0x%04x'%
                          (dev.descriptor.idVendor,dev.descriptor.idProduct))
                    if (dev.descriptor.idVendor == 0x1781 and
                        dev.descriptor.idProduct == 0x0BAF):
                        found = True
                        break
                if found:
                    break
            if not found:
                raise RuntimeError("Cannot find device. (Perhaps run with "
                                   "environment variable REQUIRE_TRIGGER=0.)")
        else:
            self.real_device = False
            return
        with self._lock:
            self._libusb_handle = usb.open(dev)

            manufacturer = usb.get_string_simple(self._libusb_handle,dev.descriptor.iManufacturer)
            product = usb.get_string_simple(self._libusb_handle,dev.descriptor.iProduct)
            serial = usb.get_string_simple(self._libusb_handle,dev.descriptor.iSerialNumber)

            assert manufacturer == 'Strawman', 'Wrong manufacturer: %s'%manufacturer
            valid_product = 'Camera Trigger 1.0'
            if product == valid_product:
                self.FOSC = 8000000.0
            elif product.startswith('Camera Trigger 1.01'):
                osc_re = r'Camera Trigger 1.01 \(F_CPU = (.*)\)\w*'
                match = re.search(osc_re,product)
                fosc_str = match.groups()[0]
                if fosc_str.endswith('UL'):
                    fosc_str = fosc_str[:-2]
                self.FOSC = float(fosc_str)
            else:
                errmsg = 'Expected product "%s", but you have "%s"'%(
                    valid_product,product)
                if self.ignore_version_mismatch:
                    print 'WARNING:',errmsg
                    self.FOSC = 8000000.0
                    print ' assuming FOSC=',self.FOSC
                else:
                    raise ValueError(errmsg)

            interface_nr = 0
            if hasattr(usb,'get_driver_np'):
                # non-portable libusb extension
                name = usb.get_driver_np(self._libusb_handle,interface_nr)
                if name != '':
                    usb.detach_kernel_driver_np(self._libusb_handle,interface_nr)

            if dev.descriptor.bNumConfigurations > 1:
                debug("WARNING: more than one configuration, choosing first")

            config = dev.config[0]
            usb.set_configuration(self._libusb_handle, config.bConfigurationValue)
            usb.claim_interface(self._libusb_handle, interface_nr)
        self.real_device = True