iOSでmicro:bitを使用する時のHEXファイルをダウンロードして書き込むスクリプトを作ってみた。(windows用)

2020-02-05

たまに、iPad のMakeCode で micro:bit のプログラミングをするけど、その度にhex ファイルをダウンロードして書き込むのが面倒くさいです。

一連の流れをスクリプトにしてみました。

内容

iOSでmicro:bitを使用する – Bluetooth経由の接続

公式サイトの手順にある 、1番目の作業を自動でするスクリプトです。

動作は

  1. hexファイルをダウンロードフォルダへダウンロード
  2. 接続されている micro:bit へ hexファイルをコピー

手順

1.micro:bit をPCに接続

2.スタートメニューからPowerShellを起動

3.以下のコードをコピペ

# WebClientを生成
$webClient = New-Object System.Net.WebClient

# 対象ファイルのURL
$targetUri = New-Object System.Uri("https://microbit.org/assets/microbit-pair-180109.hex")

# ファイル名を取得
$fileName = Split-Path $targetUri.AbsolutePath -Leaf

# ダウンロード先を取得
$downloadFolder = [Environment]::GetFolderPath('UserProfile') + "\DownLoads"

# ダウンロードファイルのパス
$downloadFile = (Join-Path $downloadFolder $fileName)

# ダウンロード実行
$webClient.DownloadFile($targetUri, $downloadFile)

# micro:bitのドライブを取得
$targetDrive = Get-Volume -FriendlyName "MICROBIT"

# ファイルをmicro:bit へコピー
Copy-Item $downloadFile ($targetDrive.DriveLetter + ":\" +  $fileName)

その他

例外処理は入れていません。

拡張子ps1のファイルに上のコードを保存すれば、そのファイルを右クリックして「PowerShellで実行」で実行できます。(Windows10のみ?)