def eventloop(): with AwesomeDeviceHoarderEventLoop(onAwesomeDeviceArrived) as awesome_device_hoarder: base_poller = SelectPoller() # In real code, file descriptor would be independently registered # to base_poller. # The event loop would be something like: poller = usb1.USBPoller(awesome_device_hoarder.context, base_poller) print('Monitoring events, ^C to exit') while True: poller.poll()
def create_poller(self, poller): """ Integrate waiting for USB transfers into an event loop, by taking an object conforming to the Python ``poll`` interface and returning another conforming to the same interface. Note that if ``create_poller`` is called more than once, events will only be delivered to the last returned poller instance. """ return usb1.USBPoller(self.usb_context, poll_wrapper.wrap_poller_for_libusb(poller))
def run(self, system_poller): """Runs the event loop, after calling the driver initialisation method. Driver event handlers and timers are handled by the event loop. Optionally, a user supplied poller can be passed in. The poller must implement the interface described in the python-libusb1 library.""" poller = usb1.USBPoller(self._context, system_poller) self._initiate_interrupt_read() self._initialise_internals() self._running = True while self._running: if len(self._timers_heap) >= 1: # There are timers to eventually call: next_timer = self._timers_heap[0] next_timer_time = next_timer._expiry_time - time.time() if next_timer_time < 0.0: next_timer_time = 0.0 poller.poll(next_timer_time) self._run_timer_callbacks() else: # No timers, just wait for file events: poller.poll() self._close() # If stop was called with an exception (wrapped in a twisted # failure), raise it now: if self._run_failure is not None: self._run_failure.raiseException()
def main(): with usb1.USBContext() as context: for device in context.getDeviceIterator(skip_on_error=True): try: handle = device.open() except usb1.USBErrorAccess: continue for interface in device[handle.getConfiguration() - 1]: if len(interface) != 1: continue interface_setting = interface[0] if (interface_setting.getNumEndpoints() == 2 and interface_setting.getClass() == usb1.CLASS_VENDOR_SPEC and handle.getStringDescriptor( interface_setting.getDescriptor(), 0x0409, ) == u'USBCat'): interface_number = interface_setting.getNumber() print('Device found at %03i:%03i interface %i' % ( device.getBusNumber(), device.getDeviceAddress(), interface_number, )) handle.claimInterface(interface_number) to_device, = [ x.getAddress() for x in interface_setting if x.getAddress() & usb1.ENDPOINT_DIR_MASK == usb1.ENDPOINT_OUT ] from_device, = [ x.getAddress() for x in interface_setting if x.getAddress() & usb1.ENDPOINT_DIR_MASK == usb1.ENDPOINT_IN ] break else: continue break else: print('Device not found') return fcntl.fcntl( sys.stdin, fcntl.F_SETFL, fcntl.fcntl(sys.stdin, fcntl.F_GETFL) | os.O_NONBLOCK, ) def sender(): buf = sys.stdin.read(BUF_SIZE) print('sending', len(buf), 'bytes', file=sys.stderr) handle.bulkWrite(to_device, buf) def onReceive(transfer): length = transfer.getActualLength() print('received', length, 'bytes', file=sys.stderr) sys.stdout.write(transfer.getBuffer()[:length]) return True transfer_helper = usb1.USBTransferHelper() transfer_helper.setEventCallback(usb1.TRANSFER_COMPLETED, onReceive) transfer_list = [] for _ in xrange(PENDING_READ_COUNT): transfer = handle.getTransfer() transfer.setBulk(from_device, BUF_SIZE, transfer_helper) transfer.submit() transfer_list.append(transfer) epoll = usb1.USBPoller(context, select.epoll()) event_dispatcher_dict = {} def register(file_object, handler): epoll.register(file_object, select.EPOLLIN) event_dispatcher_dict[file_object.fileno()] = handler register(sys.stdin, sender) try: while True: for fd, event in epoll.poll(10): print( 'epoll: fd %r got event %r' % (fd, event), file=sys.stderr, ) event_dispatcher_dict[fd]() except (KeyboardInterrupt, EOFError): pass