esp32blockly/src/devices/registry.js

76 lines
1.6 KiB
JavaScript

import { esp32s3 } from './esp32s3.js';
import { microbit } from './microbit.js';
import { rp2040 } from './rp2040.js';
import { builtinCategories } from '../blocks/categories/builtins.js';
const devices = {};
function register(profile) {
devices[profile.id] = profile;
}
register(esp32s3);
register(microbit);
register(rp2040);
/**
* Register a new device at runtime (e.g. a sub-device like "ESP32 robot").
* Call refreshToolbox() in main.js after registering to pick up changes.
*/
export function registerDevice(profile) {
register(profile);
}
export function getDeviceIds() {
return Object.keys(devices);
}
export function getDeviceProfile(id) {
return devices[id] || null;
}
export function getAllDevices() {
return { ...devices };
}
// --- active device state ---
const STORAGE_KEY = 'esp32block_device';
let currentId = localStorage.getItem(STORAGE_KEY) || 'esp32s3';
if (!devices[currentId]) currentId = 'esp32s3';
export function getDeviceId() {
return currentId;
}
export function getDevice() {
return devices[currentId];
}
export function setDeviceId(id) {
if (!devices[id]) return;
currentId = id;
localStorage.setItem(STORAGE_KEY, id);
}
export function canFlashInBrowser() {
return getDevice().firmware?.canFlashInBrowser === true;
}
// --- toolbox builder ---
export function buildToolbox(deviceId) {
const profile = devices[deviceId || currentId];
if (!profile) return { kind: 'categoryToolbox', contents: [] };
return {
kind: 'categoryToolbox',
contents: [
...profile.categories,
{ kind: 'sep' },
...builtinCategories,
],
};
}