Files
mixly3-server/src/common/boards.js
2025-05-04 14:40:11 +08:00

200 lines
5.2 KiB
JavaScript

import fs from 'node:fs';
import path from 'node:path';
import MString from './mstring';
import { ARDUINO } from './config';
const Boards = {};
Boards.TYPE = {
AVR: [
"arduino:avr:uno",
"arduino:avr:nano",
"arduino:avr:pro",
"arduino:avr:mega",
"arduino:avr:leonardo"
],
ESP8266: [
"esp8266:esp8266:generic",
"esp8266:esp8266:nodemcu",
"esp8266:esp8266:nodemcuv2",
"esp8266:esp8266:d1"
],
ESP32: [
"esp32:esp32:esp32",
"esp32:esp32:pico32",
"esp32:esp32:node32s",
"esp32:esp32:nodemcu-32s",
"esp32:esp32:m5stack-fire",
"esp32:esp32:mPython",
"esp32:esp32:esp32cam",
"esp32:esp32:bpi-bit"
],
ESP32S2: [
"esp32:esp32:esp32s2"
],
ESP32S3: [
"esp32:esp32:esp32s3"
],
ESP32C3: [
"esp32:esp32:esp32c3"
]
};
Boards.FIRMWARE_PATH = {
AVR: [
{
path: '{buildPath}/testArduino.ino.hex'
}
],
ESP8266: [
{
path: '{buildPath}/testArduino.ino.bin',
offset: '0x0'
}
],
ESP32: [
{
path: '{buildPath}/testArduino.ino.bootloader.bin',
offset: '0x1000'
}, {
path: '{buildPath}/testArduino.ino.partitions.bin',
offset: '0x8000'
}, {
path: '{shellPath}/Arduino15/packages/esp32/hardware/esp32/2.0.15/tools/partitions/boot_app0.bin',
offset: '0xe000'
}, {
path: '{buildPath}/testArduino.ino.bin',
offset: '0x10000'
}
],
ESP32S2: [
{
path: '{buildPath}/testArduino.ino.bootloader.bin',
offset: '0x1000'
}, {
path: '{buildPath}/testArduino.ino.partitions.bin',
offset: '0x8000'
}, {
path: '{shellPath}/Arduino15/packages/esp32/hardware/esp32/2.0.5/tools/partitions/boot_app0.bin',
offset: '0xe000'
}, {
path: '{buildPath}/testArduino.ino.bin',
offset: '0x10000'
}
],
ESP32S3: [
{
path: '{buildPath}/testArduino.ino.bootloader.bin',
offset: '0x1000'
}, {
path: '{buildPath}/testArduino.ino.partitions.bin',
offset: '0x8000'
}, {
path: '{shellPath}/Arduino15/packages/esp32/hardware/esp32/2.0.5/tools/partitions/boot_app0.bin',
offset: '0xe000'
}, {
path: '{buildPath}/testArduino.ino.bin',
offset: '0x10000'
}
],
ESP32C3: [
{
path: '{buildPath}/testArduino.ino.bootloader.bin',
offset: '0x0'
}, {
path: '{buildPath}/testArduino.ino.partitions.bin',
offset: '0x8000'
}, {
path: '{shellPath}/Arduino15/packages/esp32/hardware/esp32/2.0.5/tools/partitions/boot_app0.bin',
offset: '0xe000'
}, {
path: '{buildPath}/testArduino.ino.bin',
offset: '0x10000'
}
]
}
Boards.getBoardType = (board) => {
if (typeof board !== 'string') {
return null;
}
const boardParam = board.split(':');
if (boardParam.length < 3) {
return null;
}
let boardType;
const inBoardType = boardParam[0] + ':' + boardParam[1];
switch (inBoardType) {
case 'arduino:avr':
boardType = 'AVR';
break;
case 'esp8266:esp8266':
boardType = 'ESP8266';
break;
case 'esp32:esp32':
boardType = 'ESP32';
break;
default:
return null;
}
if (boardType !== 'ESP32') {
return boardType;
}
switch (boardParam[2]) {
case 'esp32c3':
boardType = 'ESP32C3';
break;
case 'esp32s2':
boardType = 'ESP32S2';
break;
case 'esp32s3':
boardType = 'ESP32S3';
break;
default:
boardType = 'ESP32';
}
return boardType;
}
Boards.getBoardKey = (board) => {
const idxs = [];
for (let i = 0; i < board.length; i++) {
if (board[i] === ':') idxs.push(i);
if (idxs.length === 3) break;
}
return idxs.length === 3 ? board.slice(0, idxs[2]) : board;
}
Boards.exist = (board) => {
const boardType = Boards.getBoardType(board);
if (!boardType) {
return false;
}
const boardParam = board.split(':');
const inBoardType = boardParam[0] + ':' + boardParam[1];
board = inBoardType + ':' + boardParam[2];
if (Boards.TYPE[boardType] && Boards.TYPE[boardType].includes(board)) {
return true;
}
return false;
}
Boards.getFiles = async (board, buildPath) => {
const type = Boards.getBoardType(board);
const output = [];
const files = Boards.FIRMWARE_PATH[type];
for (let file of files) {
const filePath = MString.tpl(file.path, {
buildPath,
shellPath: ARDUINO.path.folder
});
const data = await fs.promises.readFile(filePath, {
encoding: path.extname(filePath) === '.hex' ? 'utf8' : 'hex'
});
output.push({ ...file, data });
}
return output;
}
export default Boards;