improvements to the hid console
This commit is contained in:
parent
8ee2940106
commit
058d81a767
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
# Python 2 only for now.
|
# Python 2 only for now.
|
||||||
|
|
||||||
|
import time
|
||||||
from binascii import hexlify, unhexlify
|
from binascii import hexlify, unhexlify
|
||||||
|
|
||||||
|
|
||||||
|
@ -21,26 +22,54 @@ def read_next(handle, timeout=1000, ignore_nodata=False):
|
||||||
|
|
||||||
|
|
||||||
def console_cycle(handle):
|
def console_cycle(handle):
|
||||||
|
last_data = None
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
if read_next(handle, timeout=100, ignore_nodata=True):
|
if read_next(handle, timeout=100, ignore_nodata=True):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
line = raw_input('!! Enter packet to send (hex bytes) or ^C to abort: ')
|
line = raw_input('!! Command: ')
|
||||||
line = line.strip().replace(' ', '').replace('-', '')
|
line = line.strip().replace(' ', '').replace('-', '')
|
||||||
if not line:
|
if not line:
|
||||||
continue
|
continue
|
||||||
if len(line) % 2 == 1:
|
|
||||||
line += '0'
|
|
||||||
|
|
||||||
try:
|
data = None
|
||||||
data = unhexlify(line)
|
|
||||||
except:
|
if line == 'h':
|
||||||
print "!! Invalid input."
|
print 'Commands:'
|
||||||
continue
|
print ' <hex bytes> - send a packet to the device'
|
||||||
hexs = hexlify(data)
|
print ' r - re-send last packet'
|
||||||
print "<< [%s %s %s %s] %s" % (hexs[0:2], hexs[2:4], hexs[4:8], hexs[8:], repr(data))
|
print ' w<float> - listen for events for <float> seconds'
|
||||||
hidapi.write(handle, data)
|
print ' h - this help screen'
|
||||||
read_next(handle)
|
print ' ^C - exit'
|
||||||
|
elif line == 'r':
|
||||||
|
data = last_data
|
||||||
|
elif line[0] == 'w':
|
||||||
|
line = line[1:].strip()
|
||||||
|
try:
|
||||||
|
seconds = float(line)
|
||||||
|
except:
|
||||||
|
print "!! Bad number <" + line + ">"
|
||||||
|
else:
|
||||||
|
count = 0
|
||||||
|
start_time = time.time()
|
||||||
|
while time.time() - start_time < seconds:
|
||||||
|
if read_next(handle, timeout=100, ignore_nodata=True):
|
||||||
|
count += 1
|
||||||
|
print "!! Got %d events" % count
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
data = unhexlify(line)
|
||||||
|
except:
|
||||||
|
print "!! Invalid input."
|
||||||
|
continue
|
||||||
|
|
||||||
|
if data:
|
||||||
|
hexs = hexlify(data)
|
||||||
|
print "<< [%s %s %s %s] %s" % (hexs[0:2], hexs[2:4], hexs[4:8], hexs[8:], repr(data))
|
||||||
|
last_data = data
|
||||||
|
hidapi.write(handle, data)
|
||||||
|
read_next(handle)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
@ -59,6 +88,7 @@ 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 "!! Type 'h' for help."
|
||||||
try:
|
try:
|
||||||
console_cycle(handle)
|
console_cycle(handle)
|
||||||
except:
|
except:
|
||||||
|
|
Loading…
Reference in New Issue