Modify idf.sh to automatically detect serial devices and GID

This commit is contained in:
Andreas Tsouchlos 2023-08-28 22:36:59 +02:00
parent 9d179f668c
commit 96e2b183a3

43
idf.sh
View File

@ -1,9 +1,38 @@
# This variabe should contain the GUID of the group responsible for the serial
# devices (e.g., 'dialout' for Ubuntu or 'uucp' for Arch)
USB_GUID=986
#!/bin/bash
# This variable should contain the path to the serial device corresponding to
# the ESP32 board (e.g., '/dev/ttyUSB0' or '/dev/ttyACM0').
USB_DEV=/dev/ttyUSB0
docker run --device=${USB_DEV} --rm --user $(id -u):${USB_GUID} -v $PWD:/project -w /project -it hyperlink idf.py $@
#
# Find the GID of the group responsible for the serial devices
#
USB_GID=$(stat -c %g /dev/ttyS0)
#
# Find available serial devices and create appropriate flags
#
DEVICE_FLAGS=
# Taken from https://unix.stackexchange.com/a/144735 and modified slightly
for sysdevpath in $(find /sys/bus/usb/devices/usb*/ -name dev); do
DEVICE_FLAGS=${DEVICE_FLAGS}\ $(
syspath="${sysdevpath%/dev}"
devname="$(udevadm info -q name -p $syspath)"
[[ "$devname" == "bus/"* ]] && exit
eval "$(udevadm info -q property --export -p $syspath)"
[[ -z "$ID_SERIAL" ]] && exit
[[ "$devname" != "ttyUSB"* && "$devname" != "ttyACM"* ]] && exit
echo --device=/dev/$devname
)
done
#
# Run command in docker container
#
docker run ${DEVICE_FLAGS} --rm --user $(id -u):${USB_GID} -v $PWD:/project -w /project -it hyperlink idf.py $@