Newer
Older
m5scp2_exp / m5latest.sh
#!/bin/bash

source ./lib.sh

if [ "$1" == "-h" ] || [ "$1" == "help" ] || [ "$1" == "h" ] ; then
  echo "Usage: m5latest.sh [y] [y/n]"
  echo " 第一引数の y   : ビルド完了まで待ち、ダウンロード後に自動で書込みを行います。"
  echo " 第二引数の y/n : シリアルモニタを開く場合はyを指定してください。"
  echo "例: m5latest.sh y y   # ビルド完了まで待ち、書込みを行い、シリアルモニタを開く。"
  exit
fi

# ダウンロードURL
URL="https://lattr.istlab.info/ino_download_latest"
URL_SHOW="https://lattr.istlab.info/ino_show_latest"


## あなたの情報 https://lattr.istlab.info/dashboard に書かれていますので設定してください。 
# (例)
# MYUID="1234"
# LOGIN="23a5000"
MYUID="0"
LOGIN="23a5000"

if [ ${MYUID} -eq 0 ] ; then
  echo "m5latest.sh をつかうには、20行目付近に、あなたの情報(MYUID と LOGIN)を書き込む必要があります。"
  echo "あなたの情報は https://lattr.istlab.info/dashboard に書かれています。"
  read -p "あなたの情報を確認するため、ブラウザを開きますか?(y/n): " yn
  echo "一旦終了します。エディタで m5latest.sh をひらき、MYUID と LOGIN を設定し、保存してください。"
  echo "(sedコマンドによる文字列置換で設定したいときは、99_m5latest_setup.sh を実行してください。)"
  sleep 2 
  if [ $yn = "y" ]; then
    url=https://lattr.istlab.info/dashboard
    unameOut="$(uname -s)"
    case "${unameOut}" in
      Darwin*)
        # macOS の場合
        open "$url"
        ;;
      MINGW*|MSYS*|CYGWIN*)
        # Windows (Git Bash, MSYS, Cygwin) の場合
        start "$url"
        ;;
      *)
        echo "このOSでは自動でブラウザを開けません。URL: $url"
        ;;
    esac
  fi
  exit
fi


# echo connecting to ${URL}
info=`curl -s -o /dev/null -X POST -d "uid=${MYUID}&login=${LOGIN}" ${URL_SHOW}`
echo "------------------------------"
echo ${info}
# もし、infoにhash:p1 が含まれていたら、注意喚起する
if [[ $info =~ hash:p1 ]]; then
  echo "注:Plus2(黄色)ではなく、Plus(オレンジ色)用のプログラムです。"
  board="plus"
else
  board="plus2"
fi
echo "------------------------------"

# POSTリクエストを送信し、レスポンスコードを取得
RESPONSE_CODE=$(curl -o latest.bin -s -w "%{http_code}" -X POST -d "uid=${MYUID}&login=${LOGIN}" ${URL})

if [ "$RESPONSE_CODE" -ne 200 ]; then
  echo "エラー: レスポンスコード $RESPONSE_CODE が返されました。"
  if [ "$RESPONSE_CODE" -eq 403 ]; then
    echo "MYUID と LOGIN が間違っています。https://lattr.istlab.info/dashboard を確認してください。"
    exit
  fi
fi
# レスポンスコードが404でない場合、メッセージを表示
if [ "$RESPONSE_CODE" -eq 404 ]; then
  echo "まだビルド(コンパイル)が終わっていません。"
  if [ "$1" == "y" ]; then
    while true ; do
      sleep 5
      RESPONSE_CODE=$(curl -o latest.bin -s -w "%{http_code}" -X POST -d "uid=${MYUID}&login=${LOGIN}" ${URL})
      if [ "$RESPONSE_CODE" -eq 200 ]; then
        break
      fi
      echo -n "."
    done
  fi
fi
if [ "$RESPONSE_CODE" -eq 200 ]; then
  echo "ダウンロード成功: (レスポンスコード $RESPONSE_CODE)"
  curl -o latest.partitions.bin -s -X POST -d "uid=${MYUID}&login=${LOGIN}&type=partitions" ${URL}
  curl -o latest.bootloader.bin -s -X POST -d "uid=${MYUID}&login=${LOGIN}&type=bootloader" ${URL}
  # 書き込みを行う
  if [ "$1" == "y" ]; then
    yn='y'
  else
    read -p "書き込みを行いますか?(y/n): " yn
  fi
  if [ $yn = "y" ]; then
    # Get esptool command
    BUILD_TOOL=$(lib_get_esptool_command)
    # BUILD_TOOLの文字列の長さが20以上の場合は、終了する
    if [ ${#BUILD_TOOL} -ge 20 ]; then
        echo $BUILD_TOOL
        echo "==="
        echo "Please execute the script again."
        echo "(上記のメッセージを確認したあと) もう一度実行してください。"
        echo "==="
        exit 1
    fi
    
    # Find port - Cross-platform detection
    PORT=$(lib_detect_esptool_port)
    
    # Check if PORT is set, otherwise prompt user
    if [ -z "$PORT" ]; then
        lib_no_esp_device_message_and_exit
    fi
    
    echo "Using port: $PORT"
    
    # Check if all required files exist
    if [ ! -f "latest.bootloader.bin" ]; then
        echo "Bootloader file not found: latest.bootloader.bin"
        exit 1
    fi
    if [ ! -f "latest.partitions.bin" ]; then
        echo "Partitions file not found: latest.partitions.bin"
        exit 1
    fi
    if [ ! -f "latest.bin" ]; then
        echo "Firmware file not found: latest.bin"
        exit 1
    fi
    
    echo "Flashing ESP32 firmware using esptool..."
    echo "Bootloader: latest.bootloader.bin"
    echo "Partitions: latest.partitions.bin"
    echo "Main firmware: latest.bin"
    
    # Upload the firmware using esptool.py
    $BUILD_TOOL --chip esp32 --port "$PORT" --baud 460800 write-flash -z \
        0x1000 "latest.bootloader.bin" \
        0x8000 "latest.partitions.bin" \
        0x10000 "latest.bin"
    
    echo "Flashing completed."
  fi
  # シリアルモニタを開くか確認
  if [ -z "$2" ]; then
    read -p "シリアルモニタを開きますか?(y/n): " yn
  else
    yn=$2
  fi
  if [ $yn = "y" ]; then
    echo "終了するときはCtrl+Cを押してください。"
    arduino-cli monitor -p "$PORT" --config 115200
  fi
fi