예제 #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
    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)
예제 #3
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)
예제 #4
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
예제 #5
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
예제 #6
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()
예제 #7
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)