diff options
| author | HampusM <hampus@hampusmat.com> | 2022-02-14 09:53:28 +0100 | 
|---|---|---|
| committer | HampusM <hampus@hampusmat.com> | 2022-02-14 10:05:53 +0100 | 
| commit | a8049bb66f7bc62c9a33488615d0990e0a920520 (patch) | |
| tree | 1372e18896897490650203efe72c8244a23204cf /tools | |
| parent | 336d968dd2a4473b3acd71cfef86ad47a7901fbc (diff) | |
build: add utility scripts
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/monitor.py | 44 | ||||
| -rw-r--r-- | tools/reset.py | 73 | ||||
| -rw-r--r-- | tools/reset_leonardo.py | 109 | ||||
| -rw-r--r-- | tools/upload.sh | 12 | 
4 files changed, 129 insertions, 109 deletions
| diff --git a/tools/monitor.py b/tools/monitor.py new file mode 100644 index 0000000..f909efb --- /dev/null +++ b/tools/monitor.py @@ -0,0 +1,44 @@ +"""Serial monitor.""" +import argparse +import sys +import asyncio +import os + +if os.name == "nt":  # NT-based operating systems (Windows) +    from serial.serialwin32 import Serial +elif os.name == "posix": +    from serial.serialposix import Serial +else: +    raise NotImplementedError( +        "Sorry no implementation for your platform ({}) available." +        .format(sys.platform) +    ) + + +async def read(port: str, baud_rate: int): +    """Reads a serial port.""" +    with Serial(port, baud_rate) as serial_port: +        while(serial_port.is_open): +            sys.stdout.buffer.write(serial_port.read()) +            sys.stdout.flush() + + +async def main(): +    """Monitors serial output.""" +    parser = argparse.ArgumentParser( +        description="A tool for monitoring a Arduino") + +    parser.add_argument( +        "port", help="A serial device port e.g. /dev/ttyACM0 or com3") +    parser.add_argument("baud_rate", help="The serial device baud rate") + +    args = parser.parse_args() + +    await read(args.port, args.baud_rate) + +if __name__ == "__main__": +    try: +        asyncio.run(main()) +    except KeyboardInterrupt: +        print("\nReceived keyboard interrupt. Exiting...") +        exit(0) diff --git a/tools/reset.py b/tools/reset.py new file mode 100644 index 0000000..6398c50 --- /dev/null +++ b/tools/reset.py @@ -0,0 +1,73 @@ +"""A tool to reset a Arduino.""" +import os +import sys +from typing import List +import argparse +from time import sleep + +import serial.tools.list_ports +from serial.tools.list_ports_common import ListPortInfo + + +if os.name == "nt":  # NT-based operating systems (Windows) +    from serial.serialwin32 import Serial +elif os.name == "posix": +    from serial.serialposix import Serial +else: +    raise NotImplementedError( +        "Sorry no implementation for your platform ({}) available." +        .format(sys.platform) +    ) + + +def get_ports(): +    """Returns a list of serial ports attached.""" +    serial_ports = serial.tools.list_ports.comports() + +    return [port.device for port in serial_ports] + + +def compare_ports( +    avail_ports: List[str], +    wanted_ports: List[str] +): +    return [port for port in avail_ports if port in wanted_ports] + + +class ParsedArguments: +    """Parsed command-line arguments.""" +    port: str + + +if __name__ == "__main__": +    parser = argparse.ArgumentParser(description='Reset an Arduino') + +    parser.add_argument('port', help='Serial device e.g. /dev/ttyACM0 or com3') + +    args = parser.parse_args(namespace=ParsedArguments) + +    ports: List[str] = [] + +    while args.port not in ports: +        ports = get_ports() + +        sys.stderr.write( +            f"PORTS {{{', '.join(ports)}}} / {{{args.port}}} " +            f"=> {{{', '.join(compare_ports(ports, [args.port]))}}}\n" +        ) +        sys.stderr.flush() + +        sleep(0.5) + +    ser = Serial(args.port, 57600) + +    ser.baudrate = 1200 + +    # ser.setRTS(True) +    # ser.setDTR(False) + +    ser.close() + +    sleep(2) + +    print(get_ports()[0]) diff --git a/tools/reset_leonardo.py b/tools/reset_leonardo.py deleted file mode 100644 index be53aa1..0000000 --- a/tools/reset_leonardo.py +++ /dev/null @@ -1,109 +0,0 @@ -"""Tool used to reset a Arduino Leonardo board.""" -from os import O_NDELAY, O_NOCTTY, O_RDWR, open as os_open, close as os_close -import sys -from termios import ( -    B1200, -    TCSANOW, -    TIOCM_DTR, -    TIOCM_RTS, -    TIOCMBIC, -    TIOCMBIS, -    tcgetattr, -    tcsetattr -) -from fcntl import ioctl -from argparse import ArgumentParser -from dataclasses import dataclass -from pathlib import Path -from struct import pack -from time import sleep - - -@dataclass -class ParsedArguments: -    """Parsed command-line arguments.""" -    serial_port: Path - - -class TTYAttributes: -    """TTY attributes.""" - -    def __init__(self, tty_fd: int): -        """Initializes the TTY attributes.""" -        self.__tty_fd = tty_fd -        self.__attrs = tcgetattr(tty_fd) - -    @property -    def ispeed(self): -        """The ispeed.""" -        return self.__attrs[4] - -    @ispeed.setter -    def ispeed(self, ispeed: int): -        self.__attrs[4] = ispeed - -    @property -    def ospeed(self): -        """The ospeed.""" -        return self.__attrs[5] - -    @ospeed.setter -    def ospeed(self, ospeed: int): -        self.__attrs[5] = ospeed - -    def set(self): -        """Sets the TTY attributes.""" -        tcsetattr(self.__tty_fd, TCSANOW, self.__attrs) - - -RESET_BAUD_RATE = B1200 - -DESCRIPTION = "Tool used to reset a Arduino Leonardo board." - - -def main(): -    """Main.""" -    parser = ArgumentParser(allow_abbrev=False, description=DESCRIPTION) - -    parser.add_argument( -        "serial_port", -        type=Path, -        help="The serial port for a Arduino Leonardo board" -    ) - -    arguments = parser.parse_args(namespace=ParsedArguments) - -    is_serial_port_char_device = arguments.serial_port.is_char_device() - -    # Verify that the serial port exists -    if not arguments.serial_port.is_file() and not is_serial_port_char_device: -        print("Error: The serial port file doesn't exist") -        sys.exit(1) - -    # Verify that the serial port is a character device -    if not is_serial_port_char_device: -        print("Error: The serial port file is not a character device") -        sys.exit(1) - -    serial_port_fd = os_open( -        str(arguments.serial_port), -        O_RDWR | O_NOCTTY | O_NDELAY -    ) - -    tty_attributes = TTYAttributes(serial_port_fd) - -    tty_attributes.ispeed = RESET_BAUD_RATE -    tty_attributes.ospeed = RESET_BAUD_RATE - -    tty_attributes.set() - -    ioctl(serial_port_fd, TIOCMBIS, pack("I", TIOCM_RTS)) -    ioctl(serial_port_fd, TIOCMBIC, pack("I", TIOCM_DTR)) - -    os_close(serial_port_fd) - -    sleep(1.98) - - -if __name__ == "__main__": -    main() diff --git a/tools/upload.sh b/tools/upload.sh new file mode 100644 index 0000000..eddf4de --- /dev/null +++ b/tools/upload.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +DEVICE_PATH=$1 +PYTHON_CMD=$2 +AVRDUDE=$3 +AVRDUDE_COM_OPTS=$4 +AVRDUDE_ARD_OPTS=$5 +AVRDUDE_UPLOAD_HEX=$6 + +new_port=$($PYTHON_CMD ./tools/reset.py $DEVICE_PATH 2> /dev/tty) + +$AVRDUDE $AVRDUDE_COM_OPTS $AVRDUDE_ARD_OPTS $new_port $AVRDUDE_UPLOAD_HEX | 
