Newer
Older
m5scp2_exp / lib.sh
@miuramo miuramo 1 day ago 2 KB d
#!/usr/bin/env bash

# 
lib_get_esptool_command() {
    WIN_PYTHON="python"
    if command -v esptool.py &> /dev/null
    then
        echo "esptool.py"
    else
    if ! command -v $WIN_PYTHON &> /dev/null
    then
        $WIN_PYTHON
        echo "エラー:Pythonコマンドを使用するため、まずPythonをインストールしてください。"
        echo "esptool.py nor $WIN_PYTHON could not be found. Please install it first."
        exit 1
    else
        # check if esptool is installed as a module
        if $WIN_PYTHON -m esptool --help &> /dev/null
        then
            echo "$WIN_PYTHON -m esptool"
            return
        else
            $WIN_PYTHON -m pip install --upgrade pip
            $WIN_PYTHON -m pip install esptool --no-warn-script-location
            return
        fi
        echo "$WIN_PYTHON -m esptool"
    fi
    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
}