352 lines
11 KiB
HTML
352 lines
11 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>Pybricks Hub Loader</title>
|
|
<style>
|
|
body {
|
|
background: #0a0a0f;
|
|
color: #e0e0f0;
|
|
font-family: monospace;
|
|
padding: 24px;
|
|
}
|
|
|
|
button {
|
|
background: #e8ff47;
|
|
color: #0a0a0f;
|
|
border: none;
|
|
padding: 8px 18px;
|
|
font-family: monospace;
|
|
font-weight: bold;
|
|
cursor: pointer;
|
|
margin-right: 8px;
|
|
margin-bottom: 8px;
|
|
}
|
|
|
|
button:disabled {
|
|
opacity: 0.4;
|
|
cursor: not-allowed;
|
|
}
|
|
|
|
#log {
|
|
margin-top: 16px;
|
|
background: #111118;
|
|
padding: 16px;
|
|
min-height: 200px;
|
|
white-space: pre-wrap;
|
|
font-size: 13px;
|
|
line-height: 1.6;
|
|
border: 1px solid #222;
|
|
}
|
|
|
|
.status {
|
|
margin-top: 8px;
|
|
font-size: 12px;
|
|
color: #888;
|
|
}
|
|
</style>
|
|
</head>
|
|
|
|
<body>
|
|
<button id="connectBtn" onclick="connect()">Connect</button>
|
|
<button id="uploadBtn" onclick="upload()" disabled>Upload & Run</button>
|
|
<button id="stopBtn" onclick="stopProgram()" disabled>Stop</button>
|
|
<button id="disconnectBtn" onclick="disconnect()" disabled>Disconnect</button>
|
|
<button onclick="document.getElementById('log').textContent=''">Clear</button>
|
|
|
|
<div class="status" id="status">Disconnected</div>
|
|
<div id="log"></div>
|
|
|
|
<script>
|
|
// UUIDs for Pybricks service / characteristics (fixed by firmware)
|
|
const PYBRICKS_SERVICE_UUID = 'c5f50001-8280-46da-89f4-6d8051e4aeef';
|
|
const PYBRICKS_CMD_EVENT_UUID = 'c5f50002-8280-46da-89f4-6d8051e4aeef';
|
|
const PYBRICKS_CAPABILITIES_UUID = 'c5f50003-8280-46da-89f4-6d8051e4aeef';
|
|
|
|
// Device Information Service UUIDs
|
|
const DI_SERVICE_UUID = '0000180a-0000-1000-8000-00805f9b34fb';
|
|
const SW_REV_UUID = '00002a28-0000-1000-8000-00805f9b34fb';
|
|
const FW_REV_UUID = '00002a26-0000-1000-8000-00805f9b34fb';
|
|
|
|
// Commands (PBIO_PYBRICKS_COMMAND_*)
|
|
const CMD_STOP_USER_PROGRAM = 0;
|
|
const CMD_START_USER_PROGRAM = 1;
|
|
const CMD_WRITE_USER_PROGRAM_META = 3; // not used here
|
|
const CMD_WRITE_USER_RAM = 4;
|
|
|
|
// Events (PBIO_PYBRICKS_EVENT_*)
|
|
const EVT_STATUS_REPORT = 0;
|
|
const EVT_WRITE_STDOUT = 1;
|
|
|
|
let device, server, cmdChar;
|
|
let maxCharSize = 20;
|
|
let lastStatusFlags = 0;
|
|
let stdoutBuffer = '';
|
|
function log(msg) {
|
|
const el = document.getElementById('log');
|
|
el.textContent += msg + '\n';
|
|
el.scrollTop = el.scrollHeight;
|
|
}
|
|
function setStatus(msg) {
|
|
document.getElementById('status').textContent = msg;
|
|
}
|
|
function sleep(ms) {
|
|
return new Promise(r => setTimeout(r, ms));
|
|
}
|
|
|
|
function u32le(n) {
|
|
const b = new Uint8Array(4);
|
|
new DataView(b.buffer).setUint32(0, n, true);
|
|
return b;
|
|
}
|
|
function concat(...arrays) {
|
|
const total = arrays.reduce((s, a) => s + a.length, 0);
|
|
const out = new Uint8Array(total);
|
|
let off = 0;
|
|
for (const a of arrays) {
|
|
out.set(a, off);
|
|
off += a.length;
|
|
}
|
|
return out;
|
|
}
|
|
function hexpreview(data, n = 12) {
|
|
return Array.from(data.slice(0, n))
|
|
.map(b => b.toString(16).padStart(2, '0')).join(' ') +
|
|
(data.length > n ? ' ...' : '');
|
|
}
|
|
|
|
async function writeCmd(data, label = '') {
|
|
log(` TX${label ? ' [' + label + ']' : ''} ${data.length}b: ${hexpreview(data)}`);
|
|
try {
|
|
await cmdChar.writeValueWithResponse(data);
|
|
} catch (e) {
|
|
log(` TX error (${label}): name=${e.name} message=${e.message}`);
|
|
if (e.name === 'NetworkError' || e.name === 'NotSupportedError') {
|
|
await sleep(200);
|
|
log(' Retrying once...');
|
|
await cmdChar.writeValueWithResponse(data);
|
|
} else {
|
|
throw e;
|
|
}
|
|
}
|
|
}
|
|
|
|
function decodeStatus(flags) {
|
|
lastStatusFlags = flags;
|
|
const bits = {
|
|
battLow: !!(flags & (1 << 0)),
|
|
battCrit: !!(flags & (1 << 1)),
|
|
running: !!(flags & (1 << 6)),
|
|
shutdown: !!(flags & (1 << 7)),
|
|
btn: !!(flags & (1 << 5)),
|
|
hostConn: !!(flags & (1 << 9)),
|
|
fileIO: !!(flags & (1 << 13)),
|
|
};
|
|
return `running=${bits.running} btn=${bits.btn} hostConn=${bits.hostConn} fileIO=${bits.fileIO} shutdown=${bits.shutdown}`;
|
|
}
|
|
|
|
function isBusy() {
|
|
const running = !!(lastStatusFlags & (1 << 6));
|
|
const fileIO = !!(lastStatusFlags & (1 << 13));
|
|
return running || fileIO;
|
|
}
|
|
|
|
function onNotify(event) {
|
|
const data = new Uint8Array(event.target.value.buffer);
|
|
if (data.length === 0) {
|
|
log('[event] empty notification');
|
|
return;
|
|
}
|
|
const type = data[0];
|
|
|
|
if (type === EVT_WRITE_STDOUT) {
|
|
// Buffer and reassemble split packets
|
|
stdoutBuffer += new TextDecoder().decode(data.slice(1));
|
|
const lines = stdoutBuffer.split('\n');
|
|
stdoutBuffer = lines.pop() || ''; // Keep incomplete line
|
|
|
|
for (const line of lines) {
|
|
if (line.trim()) {
|
|
log('[stdout] ' + line);
|
|
}
|
|
}
|
|
} else if (type === EVT_STATUS_REPORT) {
|
|
const view = new DataView(data.buffer);
|
|
const flags = view.getUint32(1, true);
|
|
log(`[status] 0x${flags.toString(16).padStart(8, '0')} | ${decodeStatus(flags)}`);
|
|
} else {
|
|
log(`[event 0x${type.toString(16)}] ${hexpreview(data)}`);
|
|
}
|
|
}
|
|
|
|
async function connect() {
|
|
try {
|
|
setStatus('Scanning...');
|
|
device = await navigator.bluetooth.requestDevice({
|
|
filters: [{ services: [PYBRICKS_SERVICE_UUID] }],
|
|
optionalServices: [
|
|
PYBRICKS_SERVICE_UUID,
|
|
PYBRICKS_CMD_EVENT_UUID,
|
|
PYBRICKS_CAPABILITIES_UUID,
|
|
DI_SERVICE_UUID,
|
|
]
|
|
});
|
|
|
|
device.addEventListener('gattserverdisconnected', onDisconnect);
|
|
|
|
setStatus('Connecting GATT...');
|
|
server = await device.gatt.connect();
|
|
|
|
// Device Information
|
|
try {
|
|
const di = await server.getPrimaryService(DI_SERVICE_UUID);
|
|
const sw = new TextDecoder().decode(
|
|
await (await di.getCharacteristic(SW_REV_UUID)).readValue()
|
|
);
|
|
const fw = new TextDecoder().decode(
|
|
await (await di.getCharacteristic(FW_REV_UUID)).readValue()
|
|
);
|
|
log(`Protocol version : ${sw}`);
|
|
log(`Firmware version : ${fw}`);
|
|
} catch (e) {
|
|
log(`DI service: ${e.message}`);
|
|
}
|
|
|
|
const svc = await server.getPrimaryService(PYBRICKS_SERVICE_UUID);
|
|
|
|
// Capabilities
|
|
try {
|
|
const capChar = await svc.getCharacteristic(PYBRICKS_CAPABILITIES_UUID);
|
|
const raw = await capChar.readValue();
|
|
const caps = new DataView(raw.buffer);
|
|
maxCharSize = caps.getUint16(0, true);
|
|
const features = caps.getUint32(2, true);
|
|
const maxProg = caps.getUint32(6, true);
|
|
const numSlots = raw.byteLength >= 11 ? caps.getUint8(10) : 0;
|
|
log(`Max char write : ${maxCharSize} bytes`);
|
|
log(`Feature flags : 0x${features.toString(16)}`);
|
|
log(`Max program size : ${maxProg} bytes`);
|
|
log(`Slots : ${numSlots}`);
|
|
log(`MPY v6: ${!!(features & 2)} | v6.1: ${!!(features & 4)} | v6.3: ${!!(features & 32)}`);
|
|
} catch (e) {
|
|
log(`Capabilities: ${e.message}`);
|
|
}
|
|
|
|
cmdChar = await svc.getCharacteristic(PYBRICKS_CMD_EVENT_UUID);
|
|
log(`Char props: write=${cmdChar.properties.write} writeWOR=${cmdChar.properties.writeWithoutResponse} notify=${cmdChar.properties.notify}`);
|
|
|
|
await cmdChar.startNotifications();
|
|
cmdChar.addEventListener('characteristicvaluechanged', onNotify);
|
|
|
|
await sleep(500); // wait for first status
|
|
|
|
log(`Connected to ${device.name}`);
|
|
setStatus(`Connected: ${device.name}`);
|
|
document.getElementById('connectBtn').disabled = true;
|
|
document.getElementById('uploadBtn').disabled = false;
|
|
document.getElementById('stopBtn').disabled = false;
|
|
document.getElementById('disconnectBtn').disabled = false;
|
|
|
|
} catch (e) {
|
|
setStatus('Connection failed');
|
|
log(`Error: ${e.name} ${e.message}`);
|
|
console.error(e);
|
|
}
|
|
}
|
|
|
|
function onDisconnect() {
|
|
setStatus('Disconnected');
|
|
log('--- Hub disconnected ---');
|
|
document.getElementById('connectBtn').disabled = false;
|
|
document.getElementById('uploadBtn').disabled = true;
|
|
document.getElementById('stopBtn').disabled = true;
|
|
document.getElementById('disconnectBtn').disabled = true;
|
|
}
|
|
|
|
async function upload() {
|
|
if (!cmdChar) {
|
|
log('Not connected to hub.');
|
|
return;
|
|
}
|
|
// Always stop any running program first
|
|
log('Stopping any running program...');
|
|
try {
|
|
await writeCmd(new Uint8Array([CMD_STOP_USER_PROGRAM]), 'STOP');
|
|
await sleep(200);
|
|
} catch (e) {
|
|
log('Stop command ignored (no program running)');
|
|
}
|
|
/*if (isBusy()) {
|
|
log('Hub is busy (program running or file IO), not uploading.');
|
|
return;
|
|
}*/
|
|
|
|
document.getElementById('uploadBtn').disabled = true;
|
|
try {
|
|
const resp = await fetch('./main.bin');
|
|
if (!resp.ok) {
|
|
throw new Error(`fetch main.bin: ${resp.status} ${resp.statusText}`);
|
|
}
|
|
const blob = new Uint8Array(await resp.arrayBuffer());
|
|
log(`\nLoaded main.bin: ${blob.length} bytes`);
|
|
log(`Preview : ${hexpreview(blob, 20)}`);
|
|
|
|
const maxProgSize = 261512;
|
|
if (blob.length > maxProgSize) {
|
|
throw new Error(`Program too large for hub (max ${maxProgSize} bytes)`);
|
|
}
|
|
|
|
setStatus('Uploading...');
|
|
|
|
const PAYLOAD = maxCharSize - 5; // 1 cmd + 4 offset
|
|
const chunks = Math.ceil(blob.length / PAYLOAD);
|
|
log(`\n── Upload ${blob.length} bytes in ${chunks} chunks ──`);
|
|
|
|
for (let offset = 0; offset < blob.length; offset += PAYLOAD) {
|
|
const chunk = blob.slice(offset, Math.min(offset + PAYLOAD, blob.length));
|
|
const packet = concat(
|
|
new Uint8Array([CMD_WRITE_USER_RAM]),
|
|
u32le(offset),
|
|
chunk
|
|
);
|
|
await writeCmd(packet, `RAM@${offset}`);
|
|
}
|
|
log('All chunks sent ✓');
|
|
|
|
await sleep(150);
|
|
|
|
log('\n── Start program ──');
|
|
await writeCmd(new Uint8Array([CMD_START_USER_PROGRAM, 0]), 'START');
|
|
|
|
setStatus('Running');
|
|
log('Program started ✓ — stdout will show as [stdout] lines');
|
|
|
|
} catch (e) {
|
|
setStatus('Upload failed');
|
|
log(`\nUpload error: ${e.name} ${e.message}`);
|
|
console.error(e);
|
|
} finally {
|
|
document.getElementById('uploadBtn').disabled = false;
|
|
}
|
|
}
|
|
|
|
async function stopProgram() {
|
|
if (!cmdChar) return;
|
|
try {
|
|
await writeCmd(new Uint8Array([CMD_STOP_USER_PROGRAM]), 'STOP');
|
|
log('Stop sent ✓');
|
|
} catch (e) {
|
|
log(`Stop error: ${e.name} ${e.message}`);
|
|
}
|
|
}
|
|
|
|
async function disconnect() {
|
|
if (device && device.gatt.connected) {
|
|
device.gatt.disconnect();
|
|
}
|
|
}
|
|
</script>
|
|
</body>
|
|
|
|
</html> |