made hidconsole work in python2 and 3

This commit is contained in:
Daniel Pavel 2012-10-08 15:03:36 +03:00
parent 257f74b496
commit 377d7c844d
1 changed files with 28 additions and 24 deletions

View File

@ -1,40 +1,41 @@
#!/usr/bin/env python #!/usr/bin/env python
# Python 2 only for now.
import sys import sys
import time import time
import readline
import threading
from binascii import hexlify, unhexlify from binascii import hexlify, unhexlify
start_time = 0 start_time = 0
try:
read_packet = raw_input
except:
read_packet = input
def _print(marker, data, scroll=False): def _print(marker, data, scroll=False):
hexs = hexlify(data) hexs = str(hexlify(data))
t = time.time() - start_time t = time.time() - start_time
s = '%s (% 8.3f) [%s %s %s %s] %s' % (marker, t, hexs[0:2], hexs[2:4], hexs[4:8], hexs[8:], repr(data)) s = '%s (% 8.3f) [%s %s %s %s] %s' % (marker, t, hexs[0:2], hexs[2:4], hexs[4:8], hexs[8:], repr(data))
if scroll: if scroll:
sys.stdout.write(b'\033[s') sys.stdout.write('\033[s')
sys.stdout.write(b'\033[S') # scroll up sys.stdout.write('\033[S') # scroll up
sys.stdout.write(b'\033[A\033[L\033[G') # insert new line above the current one, position on first column sys.stdout.write('\033[A\033[L\033[G') # insert new line above the current one, position on first column
sys.stdout.write(s) sys.stdout.write(s)
if scroll: if scroll:
sys.stdout.write(b'\033[u') sys.stdout.write('\033[u')
else: else:
sys.stdout.write(b'\n') sys.stdout.write('\n')
def _continuous_read(handle, timeout=1000): def _continuous_read(handle, timeout=1000):
while True: while True:
reply = hidapi.read(handle, 128, timeout) reply = hidapi.read(handle, 128, timeout)
if reply is None: if reply is None:
print "!! Read failed, aborting" print ("!! Read failed, aborting")
break break
elif reply: elif reply:
_print('>>', reply, True) _print('>>', reply, True)
@ -50,38 +51,41 @@ if __name__ == '__main__':
args = arg_parser.parse_args() args = arg_parser.parse_args()
import hidapi import hidapi
print ".. Opening device ", args.device print (".. Opening device %s" % args.device)
handle = hidapi.open_path(args.device) handle = hidapi.open_path(args.device.encode('utf-8'))
if handle: if handle:
print ".. Opened handle %x, vendor %s product %s serial %s" % (handle, print (".. Opened handle %x, vendor %s product %s serial %s" % (handle,
repr(hidapi.get_manufacturer(handle)), repr(hidapi.get_manufacturer(handle)),
repr(hidapi.get_product(handle)), repr(hidapi.get_product(handle)),
repr(hidapi.get_serial(handle))) repr(hidapi.get_serial(handle))))
print ".. Press ^C/^D to exit, or type hex bytes to write to the device." print (".. Press ^C/^D to exit, or type hex bytes to write to the device.")
import readline
readline.read_history_file(args.history) readline.read_history_file(args.history)
start_time = time.time() start_time = time.time()
try: try:
t = threading.Thread(target=_continuous_read, args=(handle,)) from threading import Thread
t = Thread(target=_continuous_read, args=(handle,))
t.daemon = True t.daemon = True
t.start() t.start()
while t.is_alive(): while t.is_alive():
line = raw_input('?? Input: ').strip().replace(' ', '') line = read_packet ('?? Input: ').strip().replace(' ', '')
if line: if line:
try: try:
data = unhexlify(line) data = unhexlify(line.encode('ascii'))
except Exception as e:
print ("!! Invalid input.")
else:
_print('<<', data) _print('<<', data)
hidapi.write(handle, data) hidapi.write(handle, data)
except: except Exception as e:
print "!! Invalid input."
except:
pass pass
print ".. Closing handle %x" % handle print (".. Closing handle %x" % handle)
hidapi.close(handle) hidapi.close(handle)
readline.write_history_file(args.history) readline.write_history_file(args.history)
else: else:
print "!! Failed to open %s, aborting" % args.device print ("!! Failed to open %s, aborting" % args.device)