2026-02-18 15:24:00 +00:00
|
|
|
import * as Blockly from 'blockly';
|
|
|
|
|
import { pythonGenerator } from 'blockly/python';
|
|
|
|
|
import './blocks/esp32_blocks.js';
|
|
|
|
|
import './blocks/esp32_generators.js';
|
2026-02-20 07:27:56 +00:00
|
|
|
import {
|
|
|
|
|
getDeviceId,
|
|
|
|
|
setDeviceId,
|
|
|
|
|
getDevice,
|
|
|
|
|
canFlashInBrowser,
|
|
|
|
|
buildToolbox,
|
|
|
|
|
} from './devices/registry.js';
|
2026-02-18 15:24:00 +00:00
|
|
|
import { connect, disconnect, isConnected, onData, writeString } from './serial/connection.js';
|
2026-02-18 15:51:57 +00:00
|
|
|
import { executeCode, stopExecution, saveToDevice, writeFileToDevice } from './serial/repl.js';
|
2026-02-18 15:24:00 +00:00
|
|
|
import { flashFirmware } from './serial/flasher.js';
|
|
|
|
|
import { appendToTerminal, clearTerminal } from './ui/terminal.js';
|
|
|
|
|
import { initResizablePanels } from './ui/panels.js';
|
2026-02-20 07:57:54 +00:00
|
|
|
import { initProjectsDialog, open as openProjects } from './ui/projectsDialog.js';
|
2026-02-18 15:24:00 +00:00
|
|
|
import './style.css';
|
|
|
|
|
|
|
|
|
|
// ─── Blockly Workspace ───────────────────────────────────
|
|
|
|
|
|
|
|
|
|
const workspace = Blockly.inject('blockly-div', {
|
2026-02-20 07:27:56 +00:00
|
|
|
toolbox: buildToolbox(getDeviceId()),
|
2026-02-18 15:24:00 +00:00
|
|
|
theme: Blockly.Themes.Dark,
|
|
|
|
|
grid: { spacing: 25, length: 3, colour: '#333', snap: true },
|
|
|
|
|
zoom: { controls: true, wheel: true, startScale: 0.9, maxScale: 3, minScale: 0.3, scaleSpeed: 1.2 },
|
|
|
|
|
trashcan: true,
|
|
|
|
|
renderer: 'zelos',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// ─── Live Code Preview ───────────────────────────────────
|
|
|
|
|
|
|
|
|
|
const codeOutput = document.getElementById('code-output');
|
|
|
|
|
|
|
|
|
|
function updateCodePreview() {
|
|
|
|
|
const code = pythonGenerator.workspaceToCode(workspace);
|
|
|
|
|
codeOutput.textContent = code || '# Drag blocks to generate MicroPython code';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
workspace.addChangeListener((event) => {
|
|
|
|
|
if (event.isUiEvent) return;
|
|
|
|
|
updateCodePreview();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
updateCodePreview();
|
|
|
|
|
|
|
|
|
|
// ─── Workspace Persistence (localStorage) ────────────────
|
|
|
|
|
|
|
|
|
|
const STORAGE_KEY = 'esp32block_workspace';
|
|
|
|
|
|
|
|
|
|
function saveWorkspace() {
|
|
|
|
|
const state = Blockly.serialization.workspaces.save(workspace);
|
|
|
|
|
localStorage.setItem(STORAGE_KEY, JSON.stringify(state));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function loadWorkspace() {
|
|
|
|
|
const json = localStorage.getItem(STORAGE_KEY);
|
|
|
|
|
if (json) {
|
|
|
|
|
try {
|
|
|
|
|
const state = JSON.parse(json);
|
|
|
|
|
Blockly.serialization.workspaces.load(state, workspace);
|
|
|
|
|
} catch (_) {
|
|
|
|
|
/* corrupted state, ignore */
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
workspace.addChangeListener((event) => {
|
|
|
|
|
if (event.isUiEvent) return;
|
|
|
|
|
saveWorkspace();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
loadWorkspace();
|
|
|
|
|
|
|
|
|
|
// ─── Resize Handling ─────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
function onResize() {
|
|
|
|
|
const blocklyArea = document.getElementById('blockly-area');
|
|
|
|
|
const blocklyDiv = document.getElementById('blockly-div');
|
|
|
|
|
blocklyDiv.style.width = blocklyArea.offsetWidth + 'px';
|
|
|
|
|
blocklyDiv.style.height = blocklyArea.offsetHeight + 'px';
|
|
|
|
|
Blockly.svgResize(workspace);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
window.addEventListener('resize', onResize);
|
|
|
|
|
onResize();
|
|
|
|
|
initResizablePanels();
|
|
|
|
|
|
|
|
|
|
// ─── UI State Helpers ────────────────────────────────────
|
|
|
|
|
|
2026-02-20 07:27:56 +00:00
|
|
|
const deviceSelect = document.getElementById('device-select');
|
2026-02-18 15:24:00 +00:00
|
|
|
const btnConnect = document.getElementById('btn-connect');
|
|
|
|
|
const btnFlash = document.getElementById('btn-flash');
|
|
|
|
|
const btnRun = document.getElementById('btn-run');
|
|
|
|
|
const btnStop = document.getElementById('btn-stop');
|
|
|
|
|
const btnSave = document.getElementById('btn-save');
|
2026-02-20 07:57:54 +00:00
|
|
|
const btnProjects = document.getElementById('btn-projects');
|
2026-02-18 15:24:00 +00:00
|
|
|
const statusEl = document.getElementById('connection-status');
|
|
|
|
|
const terminalInput = document.getElementById('terminal-input');
|
|
|
|
|
|
2026-02-20 07:27:56 +00:00
|
|
|
// Sync device dropdown with stored device
|
|
|
|
|
deviceSelect.value = getDeviceId();
|
|
|
|
|
deviceSelect.addEventListener('change', () => {
|
|
|
|
|
setDeviceId(deviceSelect.value);
|
|
|
|
|
workspace.updateToolbox(buildToolbox(getDeviceId()));
|
|
|
|
|
updateCodePreview();
|
|
|
|
|
// Update Flash button tooltip/label based on device
|
|
|
|
|
btnFlash.title = canFlashInBrowser()
|
|
|
|
|
? 'Flash MicroPython firmware'
|
|
|
|
|
: 'Download firmware (drag to device)';
|
|
|
|
|
});
|
|
|
|
|
btnFlash.title = canFlashInBrowser()
|
|
|
|
|
? 'Flash MicroPython firmware'
|
|
|
|
|
: 'Download firmware (drag to device)';
|
|
|
|
|
|
2026-02-18 15:24:00 +00:00
|
|
|
function setConnectedUI(connected) {
|
|
|
|
|
btnConnect.textContent = connected ? '⏏ Disconnect' : '▶ Connect';
|
|
|
|
|
btnRun.disabled = !connected;
|
|
|
|
|
btnStop.disabled = !connected;
|
|
|
|
|
btnSave.disabled = !connected;
|
|
|
|
|
terminalInput.disabled = !connected;
|
|
|
|
|
statusEl.textContent = connected ? 'Connected' : 'Disconnected';
|
|
|
|
|
statusEl.className = connected ? 'status-connected' : 'status-disconnected';
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-20 07:57:54 +00:00
|
|
|
// ─── Serial Capture (reusable promise-based) ─────────────
|
2026-02-18 15:24:00 +00:00
|
|
|
|
2026-02-20 07:57:54 +00:00
|
|
|
let captureState = null;
|
2026-02-18 15:51:57 +00:00
|
|
|
|
|
|
|
|
onData((text) => {
|
2026-02-20 07:57:54 +00:00
|
|
|
if (!captureState) {
|
2026-02-20 07:49:47 +00:00
|
|
|
appendToTerminal(text);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-20 07:57:54 +00:00
|
|
|
const { startMarker, endMarker } = captureState;
|
|
|
|
|
captureState.raw += text;
|
|
|
|
|
const raw = captureState.raw;
|
2026-02-20 07:49:47 +00:00
|
|
|
|
|
|
|
|
const startIdx = raw.indexOf(startMarker);
|
|
|
|
|
|
|
|
|
|
if (startIdx === -1) {
|
|
|
|
|
const keep = startMarker.length - 1;
|
|
|
|
|
if (raw.length > keep) {
|
|
|
|
|
appendToTerminal(raw.substring(0, raw.length - keep));
|
2026-02-20 07:57:54 +00:00
|
|
|
captureState.raw = raw.substring(raw.length - keep);
|
2026-02-18 15:51:57 +00:00
|
|
|
}
|
2026-02-20 07:49:47 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const contentStart = startIdx + startMarker.length;
|
|
|
|
|
const endIdx = raw.indexOf(endMarker, contentStart);
|
|
|
|
|
|
|
|
|
|
if (endIdx === -1) {
|
2026-02-20 07:57:54 +00:00
|
|
|
if (!captureState.flushedPre && startIdx > 0) {
|
2026-02-20 07:49:47 +00:00
|
|
|
appendToTerminal(raw.substring(0, startIdx));
|
2026-02-20 07:57:54 +00:00
|
|
|
captureState.flushedPre = true;
|
2026-02-18 15:51:57 +00:00
|
|
|
}
|
2026-02-20 07:49:47 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-20 07:57:54 +00:00
|
|
|
const content = raw.substring(contentStart, endIdx);
|
|
|
|
|
const beforeStart = startIdx > 0 && !captureState.flushedPre
|
2026-02-20 07:49:47 +00:00
|
|
|
? raw.substring(0, startIdx) : '';
|
|
|
|
|
const afterEnd = raw.substring(endIdx + endMarker.length);
|
2026-02-20 07:57:54 +00:00
|
|
|
const resolve = captureState.resolve;
|
2026-02-20 07:49:47 +00:00
|
|
|
|
2026-02-20 07:57:54 +00:00
|
|
|
captureState = null;
|
2026-02-20 07:49:47 +00:00
|
|
|
|
|
|
|
|
if (beforeStart) appendToTerminal(beforeStart);
|
|
|
|
|
if (afterEnd.trim()) appendToTerminal(afterEnd);
|
2026-02-20 07:57:54 +00:00
|
|
|
|
|
|
|
|
resolve(content);
|
2026-02-18 15:51:57 +00:00
|
|
|
});
|
2026-02-18 15:24:00 +00:00
|
|
|
|
2026-02-20 07:57:54 +00:00
|
|
|
function captureDeviceOutput(script) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
const ts = Date.now();
|
|
|
|
|
const sm = `__CAP_S_${ts}__`;
|
|
|
|
|
const em = `__CAP_E_${ts}__`;
|
|
|
|
|
|
|
|
|
|
captureState = {
|
|
|
|
|
startMarker: sm,
|
|
|
|
|
endMarker: em,
|
|
|
|
|
raw: '',
|
|
|
|
|
flushedPre: false,
|
|
|
|
|
resolve,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const wrapped = `print('${sm}',end='')\n${script}\nprint('${em}',end='')`;
|
|
|
|
|
executeCode(wrapped).catch(err => {
|
|
|
|
|
captureState = null;
|
|
|
|
|
reject(err);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
if (captureState?.resolve === resolve) {
|
|
|
|
|
captureState = null;
|
|
|
|
|
reject(new Error('Timeout waiting for device response'));
|
|
|
|
|
}
|
|
|
|
|
}, 10000);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-18 15:24:00 +00:00
|
|
|
// ─── Toolbar Buttons ─────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
btnConnect.addEventListener('click', async () => {
|
|
|
|
|
try {
|
|
|
|
|
if (isConnected()) {
|
|
|
|
|
await disconnect();
|
|
|
|
|
setConnectedUI(false);
|
|
|
|
|
appendToTerminal('\n--- Disconnected ---\n');
|
|
|
|
|
} else {
|
|
|
|
|
await connect();
|
|
|
|
|
setConnectedUI(true);
|
|
|
|
|
appendToTerminal('--- Connected ---\n');
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
appendToTerminal(`\nConnection error: ${err.message}\n`);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2026-02-18 15:39:05 +00:00
|
|
|
const flashOverlay = document.getElementById('flash-overlay');
|
|
|
|
|
const flashLog = document.getElementById('flash-log');
|
|
|
|
|
const flashFill = document.getElementById('flash-progress-fill');
|
|
|
|
|
const flashPctText = document.getElementById('flash-progress-text');
|
|
|
|
|
const flashCloseBtn = document.getElementById('flash-close');
|
|
|
|
|
|
|
|
|
|
function showFlashOverlay() {
|
|
|
|
|
flashLog.textContent = '';
|
|
|
|
|
flashFill.style.width = '0%';
|
|
|
|
|
flashPctText.textContent = '0%';
|
|
|
|
|
flashCloseBtn.classList.add('hidden');
|
|
|
|
|
flashOverlay.classList.remove('hidden');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function appendFlashLog(msg) {
|
|
|
|
|
flashLog.textContent += msg;
|
|
|
|
|
flashLog.scrollTop = flashLog.scrollHeight;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setFlashProgress(pct) {
|
|
|
|
|
flashFill.style.width = pct + '%';
|
|
|
|
|
flashPctText.textContent = pct + '%';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
flashCloseBtn.addEventListener('click', () => {
|
|
|
|
|
flashOverlay.classList.add('hidden');
|
|
|
|
|
});
|
|
|
|
|
|
2026-02-18 15:24:00 +00:00
|
|
|
btnFlash.addEventListener('click', async () => {
|
2026-02-18 15:39:05 +00:00
|
|
|
if (isConnected()) {
|
2026-02-18 15:24:00 +00:00
|
|
|
await disconnect();
|
|
|
|
|
setConnectedUI(false);
|
2026-02-18 15:39:05 +00:00
|
|
|
}
|
|
|
|
|
|
2026-02-20 07:27:56 +00:00
|
|
|
const device = getDevice();
|
|
|
|
|
const fw = device.firmware;
|
2026-02-18 15:39:05 +00:00
|
|
|
|
2026-02-20 07:27:56 +00:00
|
|
|
if (canFlashInBrowser()) {
|
|
|
|
|
showFlashOverlay();
|
|
|
|
|
try {
|
|
|
|
|
await flashFirmware(
|
|
|
|
|
(msg) => appendFlashLog(msg),
|
|
|
|
|
(pct) => setFlashProgress(pct),
|
|
|
|
|
);
|
|
|
|
|
setFlashProgress(100);
|
|
|
|
|
appendFlashLog('\nFlash complete! You can now Connect to use the device.\n');
|
|
|
|
|
} catch (err) {
|
|
|
|
|
appendFlashLog(`\nFlash error: ${err.message}\n`);
|
|
|
|
|
} finally {
|
|
|
|
|
flashCloseBtn.classList.remove('hidden');
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Download firmware: open URL and show instructions
|
|
|
|
|
window.open(fw.url, '_blank');
|
|
|
|
|
appendToTerminal(`\n--- Firmware: ${fw.label} ---\n`);
|
|
|
|
|
appendToTerminal(`Download opened in new tab: ${fw.url}\n`);
|
|
|
|
|
if (fw.instructions) {
|
|
|
|
|
appendToTerminal(`${fw.instructions}\n`);
|
|
|
|
|
}
|
|
|
|
|
appendToTerminal('After flashing, connect here to run code.\n');
|
2026-02-18 15:24:00 +00:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
btnRun.addEventListener('click', async () => {
|
|
|
|
|
const code = pythonGenerator.workspaceToCode(workspace);
|
|
|
|
|
if (!code.trim()) {
|
|
|
|
|
appendToTerminal('\nNo code to run. Add some blocks!\n');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
appendToTerminal('\n>>> Running...\n');
|
|
|
|
|
try {
|
|
|
|
|
await executeCode(code);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
appendToTerminal(`\nRun error: ${err.message}\n`);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
btnStop.addEventListener('click', async () => {
|
|
|
|
|
try {
|
|
|
|
|
await stopExecution();
|
|
|
|
|
appendToTerminal('\n--- Stopped ---\n');
|
|
|
|
|
} catch (err) {
|
|
|
|
|
appendToTerminal(`\nStop error: ${err.message}\n`);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
btnSave.addEventListener('click', async () => {
|
|
|
|
|
const code = pythonGenerator.workspaceToCode(workspace);
|
|
|
|
|
if (!code.trim()) {
|
|
|
|
|
appendToTerminal('\nNo code to save.\n');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
appendToTerminal('\nSaving to device as main.py...\n');
|
|
|
|
|
try {
|
|
|
|
|
await saveToDevice(code);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
appendToTerminal(`\nSave error: ${err.message}\n`);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2026-02-20 07:57:54 +00:00
|
|
|
// ─── Projects Dialog ────────────────────────────────────
|
2026-02-18 15:51:57 +00:00
|
|
|
|
2026-02-20 07:57:54 +00:00
|
|
|
initProjectsDialog({
|
|
|
|
|
workspace,
|
|
|
|
|
captureDeviceOutput,
|
|
|
|
|
executeCode,
|
|
|
|
|
writeFileToDevice,
|
|
|
|
|
isConnected,
|
2026-02-18 15:51:57 +00:00
|
|
|
});
|
|
|
|
|
|
2026-02-20 07:57:54 +00:00
|
|
|
btnProjects.addEventListener('click', () => openProjects());
|
2026-02-18 15:51:57 +00:00
|
|
|
|
2026-02-18 15:24:00 +00:00
|
|
|
terminalInput.addEventListener('keydown', async (e) => {
|
|
|
|
|
if (e.key === 'Enter') {
|
|
|
|
|
const text = terminalInput.value;
|
|
|
|
|
terminalInput.value = '';
|
|
|
|
|
await writeString(text + '\r\n');
|
|
|
|
|
}
|
|
|
|
|
});
|