Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions platform/tests/test_public_browser_runtime.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import assert from 'node:assert/strict';
import {mock} from 'node:test';
import {PublicBrowserRuntime,getBrowserProfile,validateBrowserFirmware,decodeBrowserFrame,ESP32SIM_SOURCE_COMMIT,ESP32SIM_LEGACY_COMMIT} from '../web/public-browser-runtime.js';
import {createBrowserAudioPlayer} from '../web/public-browser-audio.js';
import {sha256Bytes} from '../web/sha256.js';
Expand Down Expand Up @@ -120,9 +121,27 @@ const c3Worker = new WorkerFixture(), pcm = [];
const c3 = new PublicBrowserRuntime({...newLane(c3Worker)});
const player = {push:packet=>pcm.push(packet),close:()=>pcm.push('closed')};
await c3.start({device:'cores3',firmware:firmware(),audio:{player}});
await c3.input({type:'button',id:'p',down:true});
assert.deepEqual(JSON.parse(c3Worker.calls.at(-1).data),{t:'power-key'});
await c3.input({type:'button',id:'p',down:false});
let powerKeyClock = 0;
const powerKeyNow = mock.method(performance,'now',() => powerKeyClock);
try {
const button = (id,down) => c3.input({type:'button',id,down});
const count = () => c3Worker.calls.filter(call=>call.op==='text').length;
for (const [duration,expected] of [[80,200],[1250,1250],[2700,2700],[5000,3000]]) {
const before = count();
await button('p',true);
await button('power',false);
assert.equal(count(),before);
powerKeyClock += 40;
await button('p',true);
powerKeyClock += duration - 40;
await button('p',false);
assert.deepEqual(JSON.parse(c3Worker.calls.at(-1).data),{t:'power-key',duration_ms:expected});
await button('p',false);
assert.equal(count(),before+1);
}
} finally {
powerKeyNow.mock.restore();
}
const audio = new Uint8Array([2,0x80,0xbb,0,0,0x12,0]).buffer;
c3Worker.emit({bin:audio});
assert.deepEqual(pcm,[audio]);
Expand Down
2 changes: 1 addition & 1 deletion platform/tools/bootstrap_browser_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

ROOT = Path(__file__).resolve().parents[2]
SOURCE_URL = "https://github.com/m5agent/esp32sim.git"
SOURCE_COMMIT = "c51955bc06e123b98a371208e12598a0152354ac"
SOURCE_COMMIT = "22e198350d26498d38f29fc63ecbf55ee8040dfd"
BOARD_PROFILES = json.loads((ROOT / "platform/web/esp32sim-browser-boards.json").read_text())
SOURCE_PROFILES = [
"m5stack-cores3", "m5stack-cores3+dual-kmeter",
Expand Down
23 changes: 19 additions & 4 deletions platform/web/public-browser-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import { USB_HID_FIRMWARE, usbHidExample } from './public-usb-hid-examples.js';
import { MODBUS_RTU_FIRMWARE, modbusRtuExample } from './public-modbus-rtu.js';
import admittedBoards from './esp32sim-browser-boards.json' with {type:'json'};

export const ESP32SIM_SOURCE_COMMIT = 'c51955bc06e123b98a371208e12598a0152354ac';
export const ESP32SIM_SOURCE_COMMIT = '22e198350d26498d38f29fc63ecbf55ee8040dfd';
export const ESP32SIM_LEGACY_COMMIT = '3cd3db3b7202e9fc559328c7247c11134c0437f9';
export const ESP32SIM_DUAL_KMETER_BOARD = 'm5stack-cores3+dual-kmeter';
const CAP_RADIO_KIND = Object.freeze({cc1101:'02',lora1262:'01',lora868:'01',sx1276:'03'});
Expand All @@ -67,6 +67,8 @@ const CHIP_LAYOUT = {
esp32p4: {id:18,bootloader:0x2000,rom:'esp32p4_rev0_rom.elf',sha256:'948f2c7d108d04a9c3a982f9c1c498fb17f2c4a68abc51f87782592034d7ce44'},
'esp32p4-rev3': {id:18,bootloader:0x2000,rom:'esp32p4_rev300_rom.elf',sha256:'90e723f71fe4701adee7e40bfab6971d8ecf5e356f4d5a81e77743248abc3a97'},
};
const CORES3_POWER_KEY_SHORT_MS = 200;
const CORES3_POWER_KEY_MAX_MS = 3000;
const ADV_REFERENCE_LIMIT = 'ADV 的 UIFlow2 v2.5.3 支持离线首页。浏览器运行尚未开放联网与云端绑定。';
const PROFILES = {
cores3: {deviceId:'cores3',board:'m5stack-cores3',chip:'esp32s3',flashMb:16,psramMb:8,jit:true,
Expand Down Expand Up @@ -179,7 +181,7 @@ export class PublicBrowserRuntime {
workerFactory = url => new Worker(url, {type:'module'}), requestTimeout = 30000, downloadImpl = downloadFirmware} = {}) {
Object.assign(this, {onSerial,onFrame,onCamera,onLeds,onServo,onState,onError,availableProfiles,workbenchProfiles,sdExamples,root:new URL(root),fetchImpl,workerFactory,requestTimeout,downloadImpl});
this.fetchImpl = (...args) => fetchImpl(...args);
this.status = 'stopped'; this.worker = null; this.pending = new Map();
this.status = 'stopped'; this.worker = null; this.pending = new Map(); this.powerKeyHold = null;
this.generation = 0; this.sequence = 0; this.inputSequence = 0; this.profile = null;
this.stats = {};
this.consoleEpoch = 0; this.uartOwner = null; this.uartListeners = new Set();
Expand Down Expand Up @@ -858,6 +860,7 @@ export class PublicBrowserRuntime {
});
}
stop() {
this.powerKeyHold = null;
this.fixtureCertificate = null;
this.generation++; this.consoleEpoch++; this.uartOwner = null; this.controller?.abort(); this.signalCleanup?.(); this.signalCleanup = null;
this.worker?.terminate(); this.worker = null;
Expand All @@ -880,8 +883,20 @@ export class PublicBrowserRuntime {
const down = event.down ?? event.pressed;
requireValue(typeof down === 'boolean', 'invalid_input', '按键需要明确的按下或释放状态。');
if (this.profile.inputs?.powerKey && ['power','p','btnPWR'].includes(event.id)) {
if (!down) return {accepted:true,delivery:'timed'};
message = {t:'power-key'};
if (this.profile.deviceId === 'cores3') {
if (down) {
this.powerKeyHold ??= {id:event.id,startedAt:performance.now()};
return {accepted:true,delivery:'timed'};
}
if (this.powerKeyHold?.id !== event.id) return {accepted:true,delivery:'timed'};
const elapsed = Math.round(performance.now() - this.powerKeyHold.startedAt);
this.powerKeyHold = null;
const duration = Math.min(CORES3_POWER_KEY_MAX_MS,Math.max(CORES3_POWER_KEY_SHORT_MS,elapsed));
message = {t:'power-key',duration_ms:duration};
} else {
if (!down) return {accepted:true,delivery:'timed'};
message = {t:'power-key'};
}
} else if (Object.hasOwn(this.profile.inputs?.plcKeys || {}, event.id)) {
message = {t:'plc-key',key:this.profile.inputs.plcKeys[event.id],down:down ? 1 : 0};
} else if (this.profile.deviceId === 'corematrix' && ['a','b','c'].includes(event.id)) {
Expand Down
2 changes: 1 addition & 1 deletion platform/web/vendor/esp32sim/UPSTREAM.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# esp32sim browser execution glue

The ES modules and MIT license in this directory come from the
[M5Stack esp32sim fork](https://github.com/m5agent/esp32sim/tree/c51955bc06e123b98a371208e12598a0152354ac).
[M5Stack esp32sim fork](https://github.com/m5agent/esp32sim/tree/22e198350d26498d38f29fc63ecbf55ee8040dfd).
The browser runtime extends the
[backend completion milestone](https://github.com/m5agent/esp32sim/tree/m5-milestone-backend-complete-20261201).
It includes PaperMono and PaperColor timed PM1 input and PaperColor SPI SD.
Expand Down
2 changes: 1 addition & 1 deletion platform/web/vendor/esp32sim/source.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"schema_version": 1,
"repository": "https://github.com/m5agent/esp32sim",
"upstream": "https://github.com/mikroverk/esp32sim",
"commit": "c51955bc06e123b98a371208e12598a0152354ac",
"commit": "22e198350d26498d38f29fc63ecbf55ee8040dfd",
"license": "MIT",
"scope": "WASM worker, pacing and block JIT glue only; no firmware or ROM",
"files": {
Expand Down
Loading