|
| 1 | +# |
| 2 | +# @file scripts/wordcopy.py |
| 3 | +# @brief Trainer script for copying English words. |
| 4 | +# |
| 5 | +# @author Chris Vig (chris@invictus.so) |
| 6 | +# @date 2025-09-10 |
| 7 | +# @cpyrt © 2025 by Chris Vig. Licensed under the GNU General Public License v3 (GPLv3). |
| 8 | +# |
| 9 | + |
| 10 | +# ------------------------------------------------------ IMPORTS ------------------------------------------------------- |
| 11 | + |
| 12 | +import argparse |
| 13 | +import time |
| 14 | + |
| 15 | +from superkey import * |
| 16 | +from utility import wordlist |
| 17 | + |
| 18 | +# ----------------------------------------------------- CONSTANTS ------------------------------------------------------ |
| 19 | + |
| 20 | +DEFAULT_COUNT = 20 |
| 21 | +DEFAULT_DELAY = 3. |
| 22 | +DEFAULT_WPM = 20. |
| 23 | +DEFAULT_MINLEN = 2 |
| 24 | +DEFAULT_MAXLEN = 8 |
| 25 | + |
| 26 | +# ----------------------------------------------------- PROCEDURES ----------------------------------------------------- |
| 27 | + |
| 28 | +def _parse_args(): |
| 29 | + """ |
| 30 | + Parses the command line arguments. |
| 31 | + """ |
| 32 | + parser = argparse.ArgumentParser(description='Word Copy Trainer') |
| 33 | + parser.add_argument('--port', type=str, default=SUPERKEY_DEFAULT_PORT, help='Serial port to connect to.') |
| 34 | + parser.add_argument('--baudrate', type=int, default=SUPERKEY_DEFAULT_BAUDRATE, help='Serial port baud rate.') |
| 35 | + parser.add_argument('--timeout', type=float, default=SUPERKEY_DEFAULT_TIMEOUT, help='Serial port timeout (s).') |
| 36 | + parser.add_argument('--count', type=int, default=DEFAULT_COUNT, help='Number of words to key.') |
| 37 | + parser.add_argument('--delay', type=float, default=DEFAULT_DELAY, help='Seconds to delay between each word.') |
| 38 | + parser.add_argument('--wpm', type=float, default=DEFAULT_WPM, help='Words per minute.') |
| 39 | + parser.add_argument('--minlen', type=int, default=DEFAULT_MINLEN, help='Minimum word length.') |
| 40 | + parser.add_argument('--maxlen', type=int, default=DEFAULT_MAXLEN, help='Maximum word length.') |
| 41 | + return parser.parse_args() |
| 42 | + |
| 43 | +def _main(port: str, |
| 44 | + baudrate: int, |
| 45 | + timeout: float, |
| 46 | + count: int, |
| 47 | + delay: float, |
| 48 | + wpm: float, |
| 49 | + minlen: int, |
| 50 | + maxlen: int): |
| 51 | + """ |
| 52 | + Runs the trainer. |
| 53 | + """ |
| 54 | + # Build word list |
| 55 | + wl = wordlist.WordList() |
| 56 | + |
| 57 | + # Open SuperKey interface |
| 58 | + with Interface(port = port, baudrate = baudrate, timeout = timeout) as intf: |
| 59 | + |
| 60 | + try: |
| 61 | + |
| 62 | + # Get initial settings |
| 63 | + initial_wpm = intf.get_wpm() |
| 64 | + initial_trainer_mode = intf.get_trainer_mode() |
| 65 | + |
| 66 | + # Override settings |
| 67 | + intf.set_wpm(wpm) |
| 68 | + intf.set_trainer_mode(True) |
| 69 | + |
| 70 | + # Run as many times as commanded |
| 71 | + for _ in range(count): |
| 72 | + |
| 73 | + # Get a random word and key it |
| 74 | + word = wl.random(min_length=minlen, max_length=maxlen).upper() |
| 75 | + intf.autokey(word, block=True) |
| 76 | + |
| 77 | + # Print the word after a delay |
| 78 | + time.sleep(delay) |
| 79 | + print(word) |
| 80 | + time.sleep(delay * .5) |
| 81 | + |
| 82 | + except KeyboardInterrupt: |
| 83 | + |
| 84 | + # Cancel autokey |
| 85 | + intf.panic() |
| 86 | + |
| 87 | + finally: |
| 88 | + |
| 89 | + # Restore initial settings |
| 90 | + intf.set_wpm(initial_wpm) |
| 91 | + intf.set_trainer_mode(initial_trainer_mode) |
| 92 | + |
| 93 | + |
| 94 | +# -------------------------------------------------- IMPORT PROCEDURE -------------------------------------------------- |
| 95 | + |
| 96 | +if __name__ == '__main__': |
| 97 | + |
| 98 | + # Parse arguments |
| 99 | + args = _parse_args() |
| 100 | + |
| 101 | + # Run main procedure |
| 102 | + _main(port = args.port, |
| 103 | + baudrate = args.baudrate, |
| 104 | + timeout = args.timeout, |
| 105 | + count = args.count, |
| 106 | + delay = args.delay, |
| 107 | + wpm = args.wpm, |
| 108 | + minlen = args.minlen, |
| 109 | + maxlen = args.maxlen) |
0 commit comments