Skip to content

Instantly share code, notes, and snippets.

@ia
Last active May 4, 2023 17:02
Show Gist options
  • Save ia/dc5f8a5d6f0901043d01ce12c050f4c7 to your computer and use it in GitHub Desktop.
Save ia/dc5f8a5d6f0901043d01ce12c050f4c7 to your computer and use it in GitHub Desktop.
femtocom - bash wrapper for picocom
#!/usr/bin/env bash
### Documentation (sort of)
#
# config sample of ~/.femtocomrc by hardcoded variable CONF
# $ cat ~/.femtocomrc
#
#
# ; options from this section will be applied to ALL configurations
# [GLOBAL]
# echo=
# ; turns into `--echo' for picocom command line
#
#
# ; GLOBAL options + classic default: 115200@8N1 on ttyUSB0
# [default]
# baud=115200
# ; turns into `--baud 115200' for picocom command line and so on
# databits=8
# parity=n
# flow=n
# stopbits=1
# port=/dev/ttyUSB0
# ; turns into `picocom OPTIONS /dev/ttyUSB0`
#
#
# [myhwtool]
# baud=9600
# databits=8
# parity=n
# flow=n
# stopbits=1
#
# $
#
# ; hence, the usage will be just as easy as:
#
# $ femtocom myhwtool
#
# which will turns into
#
# $ picocom --echo --baud 9600 --databits 8 --parity n --flow n --stopbits 1 /dev/myhwtool
#
### TODO: for v2 - add parser & options:
### --list option to list "devices" from config
### --dump option to generate args for picocom from device without run
### init
CONF=~/.femtocomrc
set -e
test -n "${DEBUG}" && set -x
### main
# extract global settings
globals="$(sed '1,/^\[GLOBAL\]/d;/^ *$/,$d; s/^\t*//g; s/^ *//g; s/=$//g; s/=/ /g; /^;/d; /^#/d; s/^/--/g;' ${CONF})"
if [ -n "${1}" ]; then
section="${1}"
else
section="default"
fi;
# extract settings for provided config section name
options="$(sed '1,/^\['"${section}"'\]/d;/^ *$/,$d; s/^\t*//g; s/^ *//g; s/=$//g; s/=/ /g; /^;/d; /^#/d; s/^/--/g;' ${CONF})"
# port value processing:
# if port is not provided, then use ttyUSB0 for default section
# if port is not provided and section is not default, then use name of section for port (must be pre-configured using udev)
if [ -z "`echo ${options} | grep "/dev/"`" ]; then
if [ "${section}" = "default" ]; then
args="${globals} ${options} /dev/ttyUSB0"
else
args="${globals} ${options} /dev/${section}"
fi;
else
args="${globals} "`echo "${options}" | sed 's,--port,,'`""
fi;
pretty_args="`echo ${args} | tr '\n' ' '`"
echo -ne "\n====>>>> exit: hold CTRL and press: A X\n\n"
echo -ne "====>>>> picocom ${pretty_args} \n\n"
picocom ${pretty_args}
exit 0
### #### HINTS section
### add your user to dialout group (no root for tty):
# sudo gpasswd -a ${USER} dialout
# OR
# sudo usermod -a -G dialout ${USER}
### little helpers
## generate config line for udev persistent rule, speculation way
port=/dev/ttyUSB0
name=alias
# v2.1
udevadm info -a -n ${port} | grep -e "^$" -e bcdDevice -e product -e removable -e serial -e version -e idProduct -e idVendor | grep -v SUBSYSTEMS | sed 's,"$,"\,,;' | sed ':a;N;$!ba; s/",\n\n/", CUT_HERE/; s/,\n/, /g; s/CUT_HERE/\n\n/g; s/ */ /g;' | grep -v ^$ | sed 's/^/SUBSYSTEM=="tty",/; s/$/SYMLINK+='\"${name}\"'/;'
# v1
# echo `udevadm info -a -n "${port}" | grep -e "^$" -e bcdDevice -e product -e removable -e serial -e version -e idProduct -e idVendor | grep -v SUBSYSTEMS | sed 's,"$,"\,,;' | sed ':a;N;$!ba; s/",\n\n.*/",/' | grep -v ^$ | tr '\n' ' ' | sed 's/ */ /g' | sed 's/^/SUBSYSTEM=="tty",/' | sed 's/$/SYMLINK+='"${name}"'/'`
# extract from recent log IDs of Vendor & Product for a just connected device:
dmesg | tail -20 | grep -e idVendor -e idProduct | sed 's/.*New USB device found, //g; s/idVendor=/ATTRS{idVendor}=="/g; s/idProduct=/ATTRS{idProduct}=="/g; s/,/"/; s/$/"/; s/ /\n/;'
# `dmesg | tail -20 | grep -e idVendor -e idProduct | sed 's/.*New USB device found, //g; s/, /\n/'`
### for debug:
udevadm info -a -n /dev/ttyUSB0 | grep -e "^$" -e bcdDevice -e product -e removable -e serial -e version -e idProduct -e idVendor | grep -v SUBSYSTEMS | sed 's,"$,"\,,;'
### config file: /etc/udev/rules.d/99-local.rules
### apply rules & trigger udev:
# $ sudo udevadm control --reload-rules && sudo udevadm trigger
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment