Created
July 25, 2022 02:27
-
-
Save tai/3e6ad8e95b63f802959fa8b6cd364ef7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
def usage(): | |
p = os.path.basename(sys.argv[0]) | |
return f""" | |
{p} - Cat USB HID device input | |
Usage: {p} -a bus.dev [-i ifnum] | |
Example: | |
$ {p} -a 1.31 | |
""".lstrip() | |
def help(): | |
sys.stderr.write(usage()) | |
sys.exit(0) | |
import os | |
import sys | |
import hid | |
import re | |
from argparse import ArgumentParser | |
def list(): | |
for i in hid.enumerate(): | |
print(i) | |
def find_path(opt): | |
bus, dev = map(lambda v: int(v, 0), re.split('[.:]', opt.address)) | |
return f"{bus:04x}:{dev:04x}:{opt.interface:02x}".encode() | |
def main(opt): | |
path = find_path(opt) | |
dev = hid.device() | |
dev.open_path(path) | |
dev.set_nonblocking(1) | |
while True: | |
buf = dev.read(64, 100) | |
if buf: | |
sys.stdout.buffer.write(bytes(buf)) | |
sys.stdout.buffer.flush() | |
if __name__ == '__main__': | |
ap = ArgumentParser() | |
ap.print_help = help | |
ap.add_argument('-a', '--address') | |
ap.add_argument('-i', '--interface', default=0) | |
ap.add_argument('args', nargs='*') | |
opt = ap.parse_args() | |
if opt.address is None: | |
help() | |
main(opt) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment