Skip to content

Commit 99469cb

Browse files
chikuk.parkclaude
andcommitted
multimode: comparable versions, firmware check in the editor, hide unmeasurable voltages
- Build version is <PySwitch version>+mm<commit count>.<hash>[.local], so a higher number is newer (local builds had git describe with upstream tags, CI builds only a hash). - The editor compares the firmware files on the device with the latest build and shows "up to date" or the number of differing files. - VOLTAGE_MONITOR reads ~0 V on a USB powered Mini 6 (measured 0.016 V), so the power text only shows voltages from 0.2 V on. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 53e2331 commit 99469cb

4 files changed

Lines changed: 54 additions & 10 deletions

File tree

‎multimode/overlay/lib/multimode/power.py‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
##############################################################################################################################################
22
#
33
# Power source / voltage monitor for MIDI Captain devices. Enable with "power": true in a profile.
4-
# Sets the state variables "pw" (display text like "BAT > 1.0V > 3.0V") and "pws" (0 = USB/DC, 1 = battery, 2 = DC).
4+
# Sets the state variables "pw" (display text like "BAT > 1.0V > 3.0V", voltages only if measurable) and "pws"
5+
# (0 = USB/DC, 1 = battery, 2 = DC).
56
#
67
##############################################################################################################################################
78

89
from time import monotonic
910
from pyswitch.misc import Updateable
1011

1112
_POLL_INTERVAL_MS = 5000
13+
_MIN_VOLTAGE = 0.2
1214

1315

1416
class PowerSense(Updateable):
@@ -58,11 +60,14 @@ def update(self):
5860
else:
5961
source, code = "USB/DC", 0
6062

63+
text = source
6164
if self._vm:
65+
# VOLTAGE_MONITOR (GP29) reads ~0V on MIDI Captains powered by USB (measured on a Mini 6:
66+
# 0.016V), so only show it when something is connected. The factor 3 is the divider of the
67+
# Raspberry Pi Pico and has not been verified for the MIDI Captain.
6268
v = (self._vm.value * 3.3) / 65535.0
63-
text = source + " > " + ("%.1f" % v) + "V > " + ("%.1f" % (v * 3)) + "V"
64-
else:
65-
text = source
69+
if v >= _MIN_VOLTAGE:
70+
text = source + " > " + ("%.1f" % v) + "V > " + ("%.1f" % (v * 3)) + "V"
6671

6772
if text == self._last:
6873
return

‎multimode/tools/mmtool.py‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import hashlib
2222
import json
2323
import os
24+
import re
2425
import shutil
2526
import subprocess
2627
import sys
@@ -128,13 +129,23 @@ def build(out, device = None):
128129
return out
129130

130131

132+
# Version of the build: <PySwitch version>+mm<commit count>.<hash>[.local]. The commit count increases with every
133+
# commit, so versions can be compared. ".local" marks builds containing uncommitted changes.
131134
def version():
135+
with open(os.path.join(CONTENT, "lib", "pyswitch", "misc.py")) as f:
136+
m = re.search(r'PYSWITCH_VERSION\s*=\s*"([^"]+)"', f.read())
137+
base = m.group(1) if m else "0"
138+
139+
def git(*cmd):
140+
return subprocess.check_output(["git", "-C", REPO] + list(cmd), stderr = subprocess.DEVNULL).decode().strip()
141+
132142
try:
133-
out = subprocess.check_output(["git", "-C", REPO, "describe", "--always", "--dirty", "--tags"], stderr = subprocess.DEVNULL)
134-
rev = out.decode().strip()
143+
count = git("rev-list", "--count", "HEAD")
144+
rev = git("rev-parse", "--short=7", "HEAD")
145+
dirty = git("status", "--porcelain", "--untracked-files=no", "--", "content", "multimode/overlay") != ""
135146
except (OSError, subprocess.CalledProcessError):
136-
rev = "unknown"
137-
return time.strftime("%Y.%m.%d") + "-" + rev
147+
return base + "+mm0.unknown"
148+
return "%s+mm%s.%s%s" % (base, count, rev, ".local" if dirty else "")
138149

139150

140151
# Builds the web app: the editor (multimode/web), the default profiles, and the firmware files with a manifest

‎multimode/web/app.js‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ async function connect() {
248248
S.dirty = false;
249249
S.lastSaved = null;
250250
render();
251+
checkFirmware();
251252
}
252253

253254
function validate() {
@@ -318,10 +319,11 @@ async function installFirmware(fromWizard = false) {
318319
await writeFile(S.dir, f.path, new Uint8Array(await r.arrayBuffer()));
319320
written++;
320321
}
321-
if (fromWizard) return;
322+
if (fromWizard) { S.fwDiff = 0; return; }
322323
const hasProfiles = await readText(S.dir, "profiles/modes.json");
323324
if (!hasProfiles) await writeProfiles(S.dir);
324325
S.installed = S.manifest.version;
326+
S.fwDiff = 0;
325327
notice(t("fw.done", { v: S.manifest.version, n: written }), "ok");
326328
} catch (e) {
327329
if (fromWizard) throw e;
@@ -330,6 +332,24 @@ async function installFirmware(fromWizard = false) {
330332
if (!fromWizard) render();
331333
}
332334

335+
// Compares the firmware files on the device with the manifest of the latest build (the version file is
336+
// ignored: it only names the build). Sets S.fwDiff to the number of differing files.
337+
async function checkFirmware() {
338+
S.fwDiff = null;
339+
if (!S.dir || !S.manifest || S.kind !== "multimode") return;
340+
S.fwChecking = true;
341+
renderSide();
342+
let diff = 0;
343+
for (const f of S.manifest.files) {
344+
if (f.path === "lib/multimode/VERSION") continue;
345+
const cur = await readBytes(S.dir, f.path);
346+
if (!cur || cur.length !== f.size || await sha256(cur) !== f.sha256) diff++;
347+
}
348+
S.fwDiff = diff;
349+
S.fwChecking = false;
350+
renderSide();
351+
}
352+
333353
/* ---------- Install wizard ---------- */
334354

335355
// Entries the operating system creates on the drive; never copied or deleted
@@ -641,12 +661,16 @@ function renderSide() {
641661
$("#connState").classList.toggle("on", !!S.dir);
642662
renderSaveState();
643663
$("#fwVersion").textContent = S.manifest ? t("app.firmware", { v: S.manifest.version }) : "";
644-
$("#btnFirmware").disabled = !S.dir || !S.manifest || S.kind !== "multimode";
664+
$("#btnFirmware").disabled = !S.dir || !S.manifest || S.kind !== "multimode" || S.fwChecking;
665+
$("#btnFirmware").classList.toggle("primary", S.fwDiff > 0);
645666

646667
const info = [];
647668
if (S.boot) info.push([t("side.os"), S.boot.replace("Adafruit CircuitPython ", "CircuitPython ")]);
648669
if (S.dir) info.push([t("side.installed"), S.installed || t("side.none")]);
649670
if (S.manifest) info.push([t("side.latest"), S.manifest.version]);
671+
if (S.dir && S.kind === "multimode" && S.manifest) {
672+
info.push([t("fw.state"), S.fwChecking ? t("fw.checkingShort") : S.fwDiff == null ? "?" : S.fwDiff === 0 ? t("fw.upToDate") : t("fw.outdated", { n: S.fwDiff })]);
673+
}
650674
$("#devInfo").replaceChildren(...info.flatMap(([k, v]) => [h("dt", {}, k), h("dd", {}, v)]));
651675
}
652676

‎multimode/web/i18n.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,10 @@ const D = {
333333
"save.statusLoaded": { en: "Same as on the device", zh: "与设备上的内容相同", ja: "デバイスの内容と同じ", ko: "장치 내용과 같음" },
334334
"toast.close": { en: "Close", zh: "关闭", ja: "閉じる", ko: "닫기" },
335335
"save.failed": { en: "Could not save: {e}. Check that the drive is still connected.", zh: "无法保存:{e}。请确认磁盘仍处于连接状态。", ja: "保存できませんでした:{e}。ドライブが接続されているか確認してください。", ko: "저장하지 못했어요: {e}. 드라이브가 연결돼 있는지 확인하세요." },
336+
"fw.state": { en: "Firmware", zh: "固件", ja: "ファームウェア", ko: "펌웨어" },
337+
"fw.checkingShort": { en: "comparing with the latest build…", zh: "正在与最新版本比较…", ja: "最新ビルドと比較中…", ko: "최신 빌드와 비교 중…" },
338+
"fw.upToDate": { en: "up to date (identical to the latest build)", zh: "已是最新(与最新版本完全相同)", ja: "最新(最新ビルドと同一)", ko: "최신 (최신 빌드와 파일 동일)" },
339+
"fw.outdated": { en: "update available: {n} file(s) differ", zh: "有可用更新:{n} 个文件不同", ja: "更新あり:{n} 個のファイルが異なります", ko: "업데이트 있음: 파일 {n}개 다름" },
336340
"fw.checking": { en: "Checking firmware {i}/{n}: {path}", zh: "正在检查固件 {i}/{n}:{path}", ja: "ファームウェアを確認中 {i}/{n}:{path}", ko: "펌웨어 확인 중 {i}/{n}: {path}" },
337341
"fw.done": { en: "Firmware {v} installed: wrote {n} file(s), the others were up to date.", zh: "固件 {v} 已安装:写入了 {n} 个文件,其余已是最新。", ja: "ファームウェア {v} をインストールしました:{n} 個のファイルを書き込みました(残りは最新)。", ko: "펌웨어 {v} 설치 완료: 파일 {n}개를 새로 썼어요 (나머지는 이미 최신)." },
338342
"fw.failed": { en: "The firmware installation stopped: {e}. Run it again to write the remaining files.", zh: "固件安装中断:{e}。再次运行将只写入剩余文件。", ja: "ファームウェアのインストールが途中で止まりました:{e}。もう一度実行すると残りのファイルだけを書き込みます。", ko: "펌웨어 설치가 중간에 멈췄어요: {e}. 다시 실행하면 남은 파일만 씁니다." },

0 commit comments

Comments
 (0)