Check for required commands, cleanup, added help.
All checks were successful
Check code formatting / Check-C-Format (push) Successful in 7s
Check code formatting / Check-Python-Flake8 (push) Successful in 8s
Run unit tests on host / Run-Unit-Tests (push) Successful in 8s

This commit is contained in:
2024-12-10 21:27:56 +01:00
parent 50118aea49
commit 8b9d6ce9fc

View File

@@ -1,5 +1,20 @@
#!/bin/bash #!/bin/bash
set -eu
check_command()
{
name=$1
if ! command -v "$name" > /dev/null 2>&1; then
echo "'$name' is required, please install"
exit 1
fi
}
check_command udisksctl
check_command lsusb
check_command picotool
DEVICEPATH=/dev/disk/by-label/RPI-RP2 DEVICEPATH=/dev/disk/by-label/RPI-RP2
IMAGEPATH=lib/micropython/ports/rp2/build-TONBERRY_RPI_PICO_W/firmware.uf2 IMAGEPATH=lib/micropython/ports/rp2/build-TONBERRY_RPI_PICO_W/firmware.uf2
@@ -7,10 +22,6 @@ flash_via_mountpoint()
{ {
while [ ! -e "$DEVICEPATH" ] ; do sleep 1; echo 'Waiting for RP2...'; done while [ ! -e "$DEVICEPATH" ] ; do sleep 1; echo 'Waiting for RP2...'; done
set -eu
while [ ! -e "$DEVICEPATH" ] ; do sleep 1; echo 'Waiting for RP2...'; done
udisksctl mount -b "$DEVICEPATH" udisksctl mount -b "$DEVICEPATH"
cp "$IMAGEPATH" "$(findmnt "$DEVICEPATH" -n -o TARGET)" cp "$IMAGEPATH" "$(findmnt "$DEVICEPATH" -n -o TARGET)"
} }
@@ -21,10 +32,6 @@ VID="0003"
flash_via_picotool() flash_via_picotool()
{ {
while [ ! "$(lsusb -d $PID:$VID)" ] ; do sleep 1; echo 'Waiting for RP2 (USB)...'; done while [ ! "$(lsusb -d $PID:$VID)" ] ; do sleep 1; echo 'Waiting for RP2 (USB)...'; done
set -eu
while [ ! "$(lsusb -d $PID:$VID)" ] ; do sleep 1; echo 'Waiting for RP2 (USB)...'; done
local serial local serial
serial="$(lsusb -d $PID:$VID -v | grep "iSerial" | awk '{print $3}')" serial="$(lsusb -d $PID:$VID -v | grep "iSerial" | awk '{print $3}')"
@@ -41,13 +48,15 @@ FLASH_VIA_MOUNTPOINT=0
usage() usage()
{ {
echo "Usage: $0 [ -m | --via-mountpoint ]" echo "Usage: $0 [ -m | --via-mountpoint ]"
echo " [ -h | --help ]"
echo echo
echo " -m, --via-mountpoint Mount first found RP2 and flash image by" echo " -m, --via-mountpoint Mount first found RP2 and flash image by"
echo " copying to mountpoint." echo " copying to mountpoint."
echo " -h, --help Print this text and exit."
exit 2 exit 2
} }
PARSED_ARGUMENTS=$(getopt -a -n "$0" -o m --long via-mountpoint -- "$@") PARSED_ARGUMENTS=$(getopt -a -n "$0" -o mh --long via-mountpoint,help -- "$@")
# shellcheck disable=SC2181 # shellcheck disable=SC2181
# Indirect getopt return value checking is okay here # Indirect getopt return value checking is okay here
if [ "$?" != "0" ]; then if [ "$?" != "0" ]; then
@@ -58,14 +67,15 @@ eval set -- "$PARSED_ARGUMENTS"
while : while :
do do
case "$1" in case "$1" in
-m | --via-mountpoint) FLASH_VIA_MOUNTPOINT=1 ; shift ;; -m | --via-mountpoint) FLASH_VIA_MOUNTPOINT=1 ; shift ;;
-h | --help) usage ;;
--) shift; break ;; --) shift; break ;;
*) echo "Unexpected option: $1" *) echo "Unexpected option: $1"
usage ;; usage ;;
esac esac
done done
if [ "$1" ]; then if [ $# -gt 0 ]; then
echo "Unexpected positional arguments: $1" echo "Unexpected positional arguments: $1"
usage usage
fi fi