def _skip_incoming(handle, ihandle, notifications_hook): """Read anything already in the input buffer. Used by request() and ping() before their write. """ while True: try: # read whatever is already in the buffer, if any data = _hid.read(ihandle, _MAX_READ_SIZE, 0) except Exception as reason: _log.error('read failed, assuming receiver %s no longer available', handle) close(handle) raise NoReceiver(reason=reason) if data: if check_message(data): # only process messages that pass check # report_id = ord(data[:1]) if notifications_hook: n = make_notification(ord(data[1:2]), data[2:]) if n: notifications_hook(n) else: # nothing in the input buffer, we're done return
def _skip_incoming(handle, ihandle, notifications_hook): """Read anything already in the input buffer. Used by request() and ping() before their write. """ while True: try: # read whatever is already in the buffer, if any data = _hid.read(ihandle, _MAX_READ_SIZE, 0) except Exception as reason: _log.error("read failed, assuming receiver %s no longer available", handle) close(handle) raise NoReceiver(reason=reason) if data: assert isinstance(data, bytes), (repr(data), type(data)) report_id = ord(data[:1]) if _log.isEnabledFor(_DEBUG): assert ((report_id & 0xF0 == 0) or (report_id == 0x10 and len(data) == _SHORT_MESSAGE_SIZE) or (report_id == 0x11 and len(data) == _LONG_MESSAGE_SIZE) or (report_id == 0x20 and len(data) == _MEDIUM_MESSAGE_SIZE)), \ "unexpected message size: report_id %02X message %s" % (report_id, _strhex(data)) if notifications_hook and report_id & 0xF0: n = make_notification(ord(data[1:2]), data[2:]) if n: notifications_hook(n) else: # nothing in the input buffer, we're done return
def _read(handle, timeout): """Read an incoming packet from the receiver. :returns: a tuple of (report_id, devnumber, data), or `None`. :raises NoReceiver: if the receiver is no longer available, i.e. has been physically removed from the machine, or the kernel driver has been unloaded. The handle will be closed automatically. """ try: # convert timeout to milliseconds, the hidapi expects it timeout = int(timeout * 1000) data = _hid.read(int(handle), _MAX_READ_SIZE, timeout) except Exception as reason: _log.warn('read failed, assuming handle %r no longer available', handle) close(handle) raise NoReceiver(reason=reason) if data and check_message(data): # ignore messages that fail check report_id = ord(data[:1]) devnumber = ord(data[1:2]) if _log.isEnabledFor(_DEBUG): _log.debug('(%s) => r[%02X %02X %s %s]', handle, report_id, devnumber, _strhex(data[2:4]), _strhex(data[4:])) return report_id, devnumber, data[2:]
def _skip_incoming(handle, ihandle, notifications_hook): """Read anything already in the input buffer. Used by request() and ping() before their write. """ while True: try: data = _hid.read(ihandle, _MAX_READ_SIZE, 0) except Exception as reason: _log.error("read failed, assuming receiver %s no longer available", handle) close(handle) raise NoReceiver(reason=reason) if data: if _log.isEnabledFor(_DEBUG): report_id = ord(data[:1]) assert (report_id == 0x10 and len(data) == _SHORT_MESSAGE_SIZE or report_id == 0x11 and len(data) == _LONG_MESSAGE_SIZE or report_id == 0x20 and len(data) == _MEDIUM_MESSAGE_SIZE) if notifications_hook: n = make_notification(ord(data[1:2]), data[2:]) if n: notifications_hook(n) else: return
def _read(handle, timeout): """Read an incoming packet from the receiver. :returns: a tuple of (report_id, devnumber, data), or `None`. :raises NoReceiver: if the receiver is no longer available, i.e. has been physically removed from the machine, or the kernel driver has been unloaded. The handle will be closed automatically. """ try: data = _hid.read(int(handle), _MAX_READ_SIZE, timeout) except Exception as reason: _log.error("read failed, assuming handle %r no longer available", handle) close(handle) raise NoReceiver(reason=reason) if data: report_id = ord(data[:1]) assert (report_id == 0x10 and len(data) == _SHORT_MESSAGE_SIZE or report_id == 0x11 and len(data) == _LONG_MESSAGE_SIZE or report_id == 0x20 and len(data) == _MEDIUM_MESSAGE_SIZE) devnumber = ord(data[1:2]) if _log.isEnabledFor(_DEBUG): _log.debug("(%s) => r[%02X %02X %s %s]", handle, report_id, devnumber, _strhex(data[2:4]), _strhex(data[4:])) return report_id, devnumber, data[2:]
def _continuous_read(handle, timeout=2000): while True: try: reply = _hid.read(handle, 128, timeout) except OSError as e: _error("Read failed, aborting: " + str(e), True) break assert reply is not None if reply: _print('>>', reply, True)
def _continuous_read(handle, timeout=2000): while True: try: reply = hidapi.read(handle, 128, timeout) except OSError as e: _error("Read failed, aborting: " + str(e), True) break assert reply is not None if reply: _print(">>", reply, True)
def _read(handle, timeout): """Read an incoming packet from the receiver. :returns: a tuple of (report_id, devnumber, data), or `None`. :raises NoReceiver: if the receiver is no longer available, i.e. has been physically removed from the machine, or the kernel driver has been unloaded. The handle will be closed automatically. """ try: # convert timeout to milliseconds, the hidapi expects it timeout = int(timeout * 1000) data = _hid.read(int(handle), _MAX_READ_SIZE, timeout) except Exception as reason: _log.error("read failed, assuming handle %r no longer available", handle) close(handle) raise NoReceiver(reason=reason) if data: assert isinstance(data, bytes), (repr(data), type(data)) report_id = ord(data[:1]) assert ((report_id & 0xF0 == 0) or (report_id == 0x10 and len(data) == _SHORT_MESSAGE_SIZE) or (report_id == 0x11 and len(data) == _LONG_MESSAGE_SIZE) or (report_id == 0x20 and len(data) == _MEDIUM_MESSAGE_SIZE)), \ "unexpected message size: report_id %02X message %s" % (report_id, _strhex(data)) if report_id & 0xF0 == 0x00: if _log.isEnabledFor(_DEBUG): _log.debug("(%s) => r[%02X %s] ignoring unknown report", handle, report_id, _strhex(data[1:])) return devnumber = ord(data[1:2]) if _log.isEnabledFor(_DEBUG): _log.debug("(%s) => r[%02X %02X %s %s]", handle, report_id, devnumber, _strhex(data[2:4]), _strhex(data[4:])) return report_id, devnumber, data[2:]