summaryrefslogtreecommitdiffstats
path: root/freestyle_hid/tools/hid_console.py
diff options
context:
space:
mode:
authorDiego Elio Pettenò <flameeyes@flameeyes.com>2020-10-04 16:08:43 +0200
committerDiego Elio Pettenò <flameeyes@flameeyes.com>2020-10-04 16:08:43 +0200
commitf745ce94f71c16927b7ddb91986d6c026c21e7ba (patch)
tree8201a1fb93c81d89407cdbfc022e0c3654c38357 /freestyle_hid/tools/hid_console.py
downloadfreestyle-hid-f745ce94f71c16927b7ddb91986d6c026c21e7ba.tar
freestyle-hid-f745ce94f71c16927b7ddb91986d6c026c21e7ba.tar.gz
freestyle-hid-f745ce94f71c16927b7ddb91986d6c026c21e7ba.tar.bz2
freestyle-hid-f745ce94f71c16927b7ddb91986d6c026c21e7ba.tar.lz
freestyle-hid-f745ce94f71c16927b7ddb91986d6c026c21e7ba.tar.xz
freestyle-hid-f745ce94f71c16927b7ddb91986d6c026c21e7ba.tar.zst
freestyle-hid-f745ce94f71c16927b7ddb91986d6c026c21e7ba.zip
Diffstat (limited to 'freestyle_hid/tools/hid_console.py')
-rwxr-xr-xfreestyle_hid/tools/hid_console.py80
1 files changed, 80 insertions, 0 deletions
diff --git a/freestyle_hid/tools/hid_console.py b/freestyle_hid/tools/hid_console.py
new file mode 100755
index 0000000..b3b3fee
--- /dev/null
+++ b/freestyle_hid/tools/hid_console.py
@@ -0,0 +1,80 @@
+#!/usr/bin/env python3
+# SPDX-FileCopyrightText: © 2019 The freestyle-hid Authors
+# SPDX-License-Identifier: Apache-2.0
+"""CLI tool to send messages through FreeStyle HID protocol."""
+
+import logging
+import pathlib
+import sys
+from typing import Optional
+
+import click
+import click_log
+
+import freestyle_hid
+
+logger = logging.getLogger()
+click_log.basic_config(logger)
+
+
+@click.command()
+@click_log.simple_verbosity_option(logger, "--vlog")
+@click.option(
+ "--text-command-type",
+ "-c",
+ type=int,
+ default=0x60,
+ help="Message type for text commands sent to the device.",
+)
+@click.option(
+ "--text-reply-type",
+ "-r",
+ type=int,
+ default=0x60,
+ help="Message type for text replies received from the device.",
+)
+@click.option(
+ "--product-id",
+ "-p",
+ type=int,
+ help="Optional product ID (in alternative to the device path)",
+)
+@click.argument(
+ "device-path",
+ type=click.Path(exists=True, dir_okay=False, writable=True, allow_dash=False),
+ callback=lambda ctx, param, value: pathlib.Path(value) if value else None,
+ required=False,
+)
+def main(
+ *,
+ text_command_type: int,
+ text_reply_type: int,
+ product_id: Optional[int],
+ device_path: Optional[pathlib.Path],
+):
+ if not product_id and not device_path:
+ raise click.UsageError(
+ "One of --product-id or DEVICE_PATH need to be provided."
+ )
+
+ session = freestyle_hid.Session(
+ product_id, device_path, text_command_type, text_reply_type
+ )
+
+ session.connect()
+
+ while True:
+ if sys.stdin.isatty():
+ command = input(">>> ")
+ else:
+ command = input()
+ print(f">>> {command}")
+
+ try:
+ print(session.send_text_command(bytes(command, "ascii")))
+ except freestyle_hid.CommandError as error:
+ print(f"! {error!r}")
+
+
+if __name__ == "__main__":
+ main()