ykaw / PiCW

A morse code keyer running on Raspberry Pi
6 stars 3 forks source link

Feature Request: CLI option for PiCW #5

Open jacksnodgrass opened 8 months ago

jacksnodgrass commented 8 months ago

Another thought.... I'd like to be able to maybe do some PiCW stuff from a web page. To do that, I think that having a CLI of some sort for PiCW would be nice to have available. Something like: PiCW --speed=15 --cmd=kb --data="cq cq cq de ka5ojd" or PiCW --speed=10 --cmd=xmit --data="/var/www/html/morse_code/cq_cq.cw" Having this cli would let me have a web page to do a GUI interface with clickable buttons or access my rig from a different room in my home or my laptop.

ykaw commented 8 months ago

As a quick hack, I wrote a shell script that writes PiCW actions specified on the command line to standard output.

$ ./PiCWwrappher.sh --speed 15 --cmd kb --data "Hello, world"
15
KB
Hello, world
$ ./PiCWwrappher.sh --speed 15 --cmd xmit --data "hello.txt"
15
XMIT hello.txt

I think you can control the keyer by piping this output to PiCW. However, I don't have an actual Raspberry Pi at hand, so I can't actually try it out. Note that an equal sign is not required between the option and its argument.

#!/bin/sh

# PiCWwrapper.sh - a command line wrapper
#                  for PiCW morse keyer

PROGNAME=${0##*/}
usage () {
    cat <<EOF >&2
Usage: $PROGNAME [options]

options:
  --speed s   :  set speed to s WPM
  --cmd kb    :  execute KB subcommand
  --cmd xmit  :  execut XMIT subcommand
  --data text :  specify a text to send
EOF
}

# parsing command line options
#
while [ -n "$1" ]; do
    case "$1" in
        -h|--help|'X-?')
            opt=h
            opt_h=1
            ;;
        --speed|--cmd|--xmit|--data)
            case "$1" in
                --speed) opt=s;;
                --cmd)   opt=c;;
                --xmit)  opt=x;;
                --data)  opt=d;;
            esac
            shift
            case "$1" in
                -*|'')
                    echo "${1}: no arguments given" >&2
                    usage;
                    exit 1
                    ;;
                *)
                    eval opt_${opt}=\""$1"\"
                    ;;
            esac
            ;;
        *) echo "unknown option: $opt" >&2
           usage;
           exit 1
           ;;
    esac
    shift
done

# help option
if [ -n "$opt_h" ]; then
    usage
    exit
fi

# set speed
#
if [ -n "$opt_s" ]; then
    echo "$opt_s"
fi

# process cmd with data
#
case "$opt_c" in
    kb|xmit)
        if [ -z "$opt_d" ]; then
            echo "no data to kb or xmit" >&2
            usage
            exit 1
        fi
        case "$opt_c" in
            kb)
                echo "KB"
                echo "$opt_d";;
            xmit)
                echo "XMIT ${opt_d}";;
        esac;;
    '') ;;
    *)
        usage
        exit 1;;
esac