Exemplo n.º 1
0
def main():
    args = _parse_arguments()
    handle = _open(args)

    if interactive:
        print('.. Press ^C/^D to exit, or type hex bytes to write to the device.')

        import readline
        if args.history is None:
            import os.path
            args.history = os.path.join(os.path.expanduser('~'), '.hidconsole-history')
        try:
            readline.read_history_file(args.history)
        except Exception:
            # file may not exist yet
            pass

    try:
        from threading import Thread
        t = Thread(target=_continuous_read, args=(handle, ))
        t.daemon = True
        t.start()

        if interactive:
            # move the cursor at the bottom of the screen
            sys.stdout.write('\033[300B')  # move cusor at most 300 lines down, don't scroll

        while t.is_alive():
            line = read_packet(prompt)
            line = line.strip().replace(' ', '')
            # print ("line", line)
            if not line:
                continue

            data = _validate_input(line, args.hidpp)
            if data is None:
                continue

            _print('<<', data)
            _hid.write(handle, data)
            # wait for some kind of reply
            if args.hidpp and not interactive:
                rlist, wlist, xlist = _select([handle], [], [], 1)
                if data[1:2] == b'\xFF':
                    # the receiver will reply very fast, in a few milliseconds
                    time.sleep(0.010)
                else:
                    # the devices might reply quite slow
                    time.sleep(0.700)
    except EOFError:
        if interactive:
            print('')
        else:
            time.sleep(1)

    finally:
        print('.. Closing handle %r' % handle)
        _hid.close(handle)
        if interactive:
            readline.write_history_file(args.history)
Exemplo n.º 2
0
def close(handle):
    """Closes a HID device handle."""
    if handle:
        try:
            if isinstance(handle, int):
                _hid.close(handle)
            else:
                handle.close()
            # _log.info("closed receiver handle %r", handle)
            return True
        except Exception:
            # _log.exception("closing receiver handle %r", handle)
            pass

    return False
Exemplo n.º 3
0
Arquivo: base.py Projeto: 3v1n0/Solaar
def close(handle):
	"""Closes a HID device handle."""
	if handle:
		try:
			if isinstance(handle, int):
				_hid.close(handle)
			else:
				handle.close()
			# _log.info("closed receiver handle %r", handle)
			return True
		except:
			# _log.exception("closing receiver handle %r", handle)
			pass

	return False
Exemplo n.º 4
0
def main():
	args = _parse_arguments()
	handle = _open(args.device, args.hidpp)

	if interactive:
		print (".. Press ^C/^D to exit, or type hex bytes to write to the device.")

		import readline
		if args.history is None:
			import os.path
			args.history = os.path.join(os.path.expanduser("~"), ".hidconsole-history")
		try:
			readline.read_history_file(args.history)
		except:
			# file may not exist yet
			pass

		# re-open stdout unbuffered
		try:
			sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
		except:
			# will fail in python3
			pass

	try:
		from threading import Thread
		t = Thread(target=_continuous_read, args=(handle,))
		t.daemon = True
		t.start()

		if interactive:
			# move the cursor at the bottom of the screen
			sys.stdout.write('\033[300B')  # move cusor at most 300 lines down, don't scroll

		while t.is_alive():
			line = read_packet(prompt)
			line = line.strip().replace(' ', '')
			# print ("line", line)
			if not line:
				continue

			data = _validate_input(line, args.hidpp)
			if data is None:
				continue

			_print("<<", data)
			hidapi.write(handle, data)
			# wait for some kind of reply
			if args.hidpp and not interactive:
				rlist, wlist, xlist = _select([handle], [], [], 1)
				if data[1:2] == b'\xFF':
					# the receiver will reply very fast, in a few milliseconds
					time.sleep(0.100)
				else:
					# the devices might reply quite slow
					time.sleep(1)
	except EOFError:
		if interactive:
			print ("")
		else:
			time.sleep(1)
	except Exception as e:
		print ('%s: %s' % (type(e).__name__, e))

	print (".. Closing handle %r" % handle)
	hidapi.close(handle)
	if interactive:
		readline.write_history_file(args.history)