#!/usr/bin/env bash
#
lib_get_esptool_command() {
if command -v esptool.py &> /dev/null
then
echo "esptool.py"
else
if ! command -v py &> /dev/null
then
echo "esptool.py nor py could not be found. Please install it first."
exit 1
fi
# check if esptool is installed as a module
if py -m esptool --help &> /dev/null
then
echo "py -m esptool"
else
echo "install esptool module for python first."
py -m pip install esptool
fi
echo "py -m esptool"
fi
}
lib_detect_esptool_port() {
local PORT=""
local OS_TYPE=$(uname -s)
case "$OS_TYPE" in
"Darwin") # macOS
# Try common macOS patterns
for pattern in "/dev/cu.usbserial*" "/dev/cu.usbmodem*" "/dev/cu.wchusbserial*"; do
PORT=$(ls $pattern 2>/dev/null | head -n 1)
if [ ! -z "$PORT" ]; then
break
fi
done
;;
"Linux") # Linux
# Try common Linux patterns
for pattern in "/dev/ttyUSB*" "/dev/ttyACM*"; do
PORT=$(ls $pattern 2>/dev/null | head -n 1)
if [ ! -z "$PORT" ]; then
break
fi
done
;;
"CYGWIN"*|"MINGW"*|"MSYS"*) # Windows (Git Bash, Cygwin, etc.)
# For Windows, try to detect COM ports
# Note: This works in Git Bash or similar environments
for num in {3..20}; do
if [ -e "/dev/ttyS$((num-1))" ]; then
PORT="COM$num"
break
fi
done
# Alternative: use mode command if available
if [ -z "$PORT" ] && command -v mode.com &> /dev/null; then
PORT=$(mode.com 2>/dev/null | grep -o "COM[0-9]*" | head -n 1)
fi
;;
*)
echo "Unknown OS: $OS_TYPE"
echo "Please manually specify the port using PORT environment variable"
echo "Example: PORT=/dev/ttyUSB0 $0"
;;
esac
echo "$PORT"
}
lib_no_esp_device_message_and_exit(){
echo "No ESP device found automatically."
echo "Please connect your ESP device and ensure drivers are installed."
echo "Common ports:"
echo " Linux: /dev/ttyUSB0, /dev/ttyACM0"
echo " macOS: /dev/cu.usbserial*, /dev/cu.usbmodem*"
echo " Windows: COM3, COM4, etc."
echo "エラー:デバイスが接続されていない?"
exit 1
}