-
-
Notifications
You must be signed in to change notification settings - Fork 3
Closed
Description
Microbit拡張機能と同様に、MicrobitMore拡張機能でもファームウェア(.hexファイル)を自動的にダウンロードし、接続トラブル時に「周辺機器を更新 (Update Peripheral)」できるようにする機能を実装する。
背景と目的
現在、smalruby3-gui では Microbit 拡張機能利用時に、互換性のあるファームウェアを自動的にダウンロードし、接続できない場合にユーザーがファームウェアを更新できる仕組みがある。
MicrobitMore 拡張機能においても同様の仕組みを導入し、ユーザーが適切なファームウェア(v2対応)を容易に利用できるようにする。
調査結果
現在の実装(Microbit拡張機能)
| ファイル | 役割 |
|---|---|
scripts/prepublish.mjs |
ビルド時に Universal Hex をダウンロード(ZIP形式) |
src/lib/microbit-update.js |
ファームウェア書き込み処理(Universal Hex 形式必須) |
src/containers/connection-modal.jsx |
UI制御(148行目: extensionId === 'microbit' にハードコード) |
src/generated/microbit-hex-url.cjs |
ビルド時に自動生成されるhex URLのエクスポート |
ファームウェア更新の仕組み
- ビルド時:
prepublish.mjsが Scratch の microbit hex (Universal Hex形式) をダウンロード - 実行時: デバイスが見つからない場合、
connection-modal.jsxが「Update Peripheral」UIを表示 - 更新処理:
microbit-update.jsが WebUSB + DAPLink でファームウェアを書き込み
重要なコード箇所
microbit-update.js (77-83行目):
const getHexMap = async () => {
const response = await fetch(hexUrl);
const hex = await response.text();
if (!isUniversalHex(hex)) {
throw new Error('Hex file must be in universal format'); // ← V2専用hexでエラー
}
// ...
};connection-modal.jsx (148行目):
const canUpdatePeripheral = (this.props.extensionId === 'microbit') && isMicroBitUpdateSupported();
// ↑ 'microbit' にハードコードされているconnection-modal.jsx (145行目):
// TODO: get this functionality from the extension ← 将来的な拡張が想定されていた
return selectAndUpdateMicroBit(progressCallback);MicrobitMore 固有の課題
重大な問題: Universal Hex 非対応
MicrobitMore は V2専用 hex のみを提供しており、Universal Hex 形式ではありません。
| 項目 | Microbit (Scratch) | MicrobitMore |
|---|---|---|
| Hex 形式 | Universal Hex (V1+V2) | V2専用 |
| ファイルサイズ | 約1.8MB | 約700KB |
| V1 対応 | あり | なし |
| ダウンロードURL | https://downloads.scratch.mit.edu/microbit/scratch-microbit.hex.zip |
https://github.com/microbit-more/pxt-mbit-more-v2/releases/download/0.2.5/microbit-mbit-more-v2-0_2_5.hex |
技術的な制約
isUniversalHex()チェックで V2専用 hex は失敗するseparateUniversalHex()が V2専用 hex では動作しない- micro:bit V1 ユーザーは MicrobitMore を使用できない(これは MicrobitMore 自体の制約)
実装オプション
オプション A: ビルド時に Universal Hex を作成(推奨)
V2専用 hex と「error hex for V1」を組み合わせて Universal Hex を作成する。
手順:
prepublish.mjsで MicrobitMore V2 hex をダウンロード- micro:bit Foundation の error hex (V1用) を取得
@microbit/microbit-universal-hexのcreateUniversalHex()で結合- 結合した Universal Hex を
static/microbitMore/に保存
メリット:
- 既存の
microbit-update.jsをほぼそのまま利用可能 - V1 ユーザーには適切なエラーメッセージが表示される
デメリット:
- error hex の入手・管理が必要
- ファイルサイズが増加(約1.8MB)
- ビルドプロセスが複雑化
必要な作業:
- error hex for V1 の入手先を確認
-
prepublish.mjsに MicrobitMore 用ダウンロード処理を追加 -
createUniversalHex()による結合処理を実装 -
connection-modal.jsxの条件分岐を追加
オプション B: V2専用処理に対応
microbit-update.js を修正して V2専用 hex を許容する。
変更点:
const getHexMap = async (isUniversalRequired = true) => {
const response = await fetch(hexUrl);
const hex = await response.text();
if (isUniversalRequired && !isUniversalHex(hex)) {
throw new Error('Hex file must be in universal format');
}
if (!isUniversalHex(hex)) {
// V2専用として処理
const hexMap = new Map();
const binary = new TextEncoder().encode(hex);
hexMap.set(DeviceVersion.V2, binary);
return hexMap;
}
// ... 既存の Universal Hex 処理
};メリット:
- ファイルサイズが小さい(約700KB)
- 実装が比較的シンプル
デメリット:
- V1 ユーザーに対するエラーハンドリングが必要
- 既存コードの変更が必要
必要な作業:
-
microbit-update.jsの修正 - V1 デバイス検出時のエラーメッセージ追加
-
connection-modal.jsxの条件分岐を追加
オプション C: 別モジュールを作成
MicrobitMore 専用の microbit-more-update.js を新規作成する。
メリット:
- 既存コードに影響なし
- 関心の分離が明確
デメリット:
- コードの重複が発生
- メンテナンスコストの増加
必要な作業:
-
src/lib/microbit-more-update.jsを新規作成 -
connection-modal.jsxで拡張機能ごとに更新関数を選択
推奨実装方針
オプション B(V2専用処理に対応)を推奨
理由:
- MicrobitMore は元々 V2専用であり、V1 をサポートする必要がない
- ファイルサイズを小さく保てる
- error hex の管理が不要
- 実装がシンプル
推奨実装ステップ
-
prepublish.mjsの修正- MicrobitMore V2 hex をダウンロード(ZIP形式ではなく直接hex)
static/microbitMore/microbit-mbit-more-v2.hexに保存src/generated/microbit-more-hex-url.cjsを生成
-
microbit-more-update.jsの作成microbit-update.jsをベースに作成isUniversalHexチェックを削除- V2専用として処理
- V1 デバイス検出時はエラーメッセージを表示
-
connection-modal.jsxの修正canUpdatePeripheralの条件にmicrobitMoreを追加handleSendUpdateで拡張機能IDに応じた更新関数を呼び出し
参考リンク
- Universal Hex Creator
- Universal Hex Format Specification
- microbit-universal-hex Library
- MicrobitMore Releases
完了条件
- ビルド時に MicrobitMore の Hex がダウンロードされること
- GUI 上で MicrobitMore 接続モーダルからファームウェア更新が実行できること
- V2 デバイスでファームウェア更新が成功すること
- V1 デバイスでは適切なエラーメッセージが表示されること
Metadata
Metadata
Assignees
Labels
No labels