improved hidconsole in tty and batch mode
This commit is contained in:
parent
a59ad221a1
commit
ec6a2d892a
|
@ -1,12 +1,14 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
import sys
|
|
||||||
import os
|
import os
|
||||||
|
import sys
|
||||||
|
from select import select as _select
|
||||||
import time
|
import time
|
||||||
from binascii import hexlify, unhexlify
|
from binascii import hexlify, unhexlify
|
||||||
_hex = lambda d: hexlify(d).decode('ascii').upper()
|
_hex = lambda d: hexlify(d).decode('ascii').upper()
|
||||||
|
|
||||||
|
|
||||||
|
interactive = os.isatty(0)
|
||||||
start_time = 0
|
start_time = 0
|
||||||
try:
|
try:
|
||||||
read_packet = raw_input
|
read_packet = raw_input
|
||||||
|
@ -15,19 +17,18 @@ except:
|
||||||
|
|
||||||
|
|
||||||
def _print(marker, data, scroll=False):
|
def _print(marker, data, scroll=False):
|
||||||
hexs = _hex(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))
|
|
||||||
|
|
||||||
if scroll:
|
if interactive and scroll:
|
||||||
sys.stdout.write('\033[s')
|
sys.stdout.write('\033[s')
|
||||||
sys.stdout.write('\033[S') # scroll up
|
sys.stdout.write('\033[S') # scroll up
|
||||||
sys.stdout.write('\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
|
||||||
|
|
||||||
|
hexs = _hex(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))
|
||||||
sys.stdout.write(s)
|
sys.stdout.write(s)
|
||||||
|
|
||||||
if scroll:
|
if interactive and scroll:
|
||||||
sys.stdout.write('\033[u')
|
sys.stdout.write('\033[u')
|
||||||
else:
|
else:
|
||||||
sys.stdout.write('\n')
|
sys.stdout.write('\n')
|
||||||
|
@ -58,17 +59,18 @@ if __name__ == '__main__':
|
||||||
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.")
|
if interactive:
|
||||||
|
print (".. Press ^C/^D to exit, or type hex bytes to write to the device.")
|
||||||
|
|
||||||
import readline
|
import readline
|
||||||
if args.history is None:
|
if args.history is None:
|
||||||
import os.path
|
import os.path
|
||||||
args.history = os.path.join(os.path.expanduser("~"), ".hidconsole-history")
|
args.history = os.path.join(os.path.expanduser("~"), ".hidconsole-history")
|
||||||
try:
|
try:
|
||||||
readline.read_history_file(args.history)
|
readline.read_history_file(args.history)
|
||||||
except:
|
except:
|
||||||
# file may not exist yet
|
# file may not exist yet
|
||||||
pass
|
pass
|
||||||
|
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
|
|
||||||
|
@ -78,7 +80,7 @@ if __name__ == '__main__':
|
||||||
t.daemon = True
|
t.daemon = True
|
||||||
t.start()
|
t.start()
|
||||||
|
|
||||||
prompt = '?? Input: ' if os.isatty(0) else ''
|
prompt = '?? Input: ' if interactive else ''
|
||||||
|
|
||||||
while t.is_alive():
|
while t.is_alive():
|
||||||
line = read_packet(prompt).strip().replace(' ', '')
|
line = read_packet(prompt).strip().replace(' ', '')
|
||||||
|
@ -90,13 +92,18 @@ if __name__ == '__main__':
|
||||||
else:
|
else:
|
||||||
_print('<<', data)
|
_print('<<', data)
|
||||||
hidapi.write(handle, data)
|
hidapi.write(handle, data)
|
||||||
|
# wait for some kind of reply
|
||||||
|
if not interactive:
|
||||||
|
rlist, wlist, xlist = _select([handle], [], [], 1)
|
||||||
|
time.sleep(0.1)
|
||||||
except EOFError:
|
except EOFError:
|
||||||
time.sleep(0.01 if os.isatty(0) else 2)
|
pass
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print ('%s: %s' % (type(e).__name__, e))
|
print ('%s: %s' % (type(e).__name__, e))
|
||||||
|
|
||||||
print (".. Closing handle %X" % handle)
|
print (".. Closing handle %X" % handle)
|
||||||
hidapi.close(handle)
|
hidapi.close(handle)
|
||||||
readline.write_history_file(args.history)
|
if interactive:
|
||||||
|
readline.write_history_file(args.history)
|
||||||
else:
|
else:
|
||||||
print ("!! Failed to open %s, aborting" % args.device)
|
print ("!! Failed to open %s, aborting" % args.device)
|
||||||
|
|
Loading…
Reference in New Issue