Skip to content

Commit 76e6008

Browse files
committed
fix(init): display correct extension for firmware filename
e.g. .hex for stm32, .uf2 for pi pico
1 parent 6ce33c7 commit 76e6008

File tree

5 files changed

+57
-33
lines changed

5 files changed

+57
-33
lines changed

src/esp/idfProjectConfig.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function createDiagramForIDFProject(
3636
`Target ${target} is not currently supported by Wokwi. You can create a feature request at https://github.com/wokwi/wokwi-features/issues.`,
3737
);
3838
}
39-
return createDiagram(board.board);
39+
return createDiagram(board);
4040
}
4141

4242
export interface IDFProjectConfigParams {
@@ -74,7 +74,7 @@ export function idfProjectConfig(params: IDFProjectConfigParams) {
7474
const diagramContent = readFileSync(diagramFilePath, 'utf8');
7575
const diagram = JSON.parse(diagramContent);
7676
const board = findBoard(diagram);
77-
const boardInfo = boards.find((b) => b.board === board?.type);
77+
const boardInfo = boards.find((b) => b.type === board?.type);
7878
if (boardInfo && boardInfo.idfTarget !== idfProjectDescription.target) {
7979
console.error(
8080
chalkTemplate`{red Error:} The IDF project is targeting {yellow ${idfProjectDescription.target}}, but the diagram is for {yellow ${boardInfo.idfTarget}}.`,

src/project/boards.ts

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,105 @@
1+
export type BoardFamily = 'esp32' | 'rp2' | 'stm32';
2+
13
export interface IBoard {
24
title: string;
3-
board: string;
4-
family: string;
5+
type: string;
6+
family: BoardFamily;
57
idfTarget?: string;
68
serialPins?: { RX: string; TX: string };
79
}
810

11+
export interface IFamilyInfo {
12+
defaultFirmwareExt: string;
13+
mcuPartId: string;
14+
}
15+
916
export const boards: IBoard[] = [
1017
// ESP32 DevKits
11-
{ title: 'ESP32 DevKit', board: 'board-esp32-devkit-c-v4', family: 'esp32', idfTarget: 'esp32' },
18+
{ title: 'ESP32 DevKit', type: 'board-esp32-devkit-c-v4', family: 'esp32', idfTarget: 'esp32' },
1219
{
1320
title: 'ESP32-C3 DevKit',
14-
board: 'board-esp32-c3-devkitm-1',
21+
type: 'board-esp32-c3-devkitm-1',
1522
family: 'esp32',
1623
idfTarget: 'esp32c3',
1724
},
1825
{
1926
title: 'ESP32-C6 DevKit',
20-
board: 'board-esp32-c6-devkitc-1',
27+
type: 'board-esp32-c6-devkitc-1',
2128
family: 'esp32',
2229
idfTarget: 'esp32c6',
2330
},
2431
{
2532
title: 'ESP32-H2 DevKit',
26-
board: 'board-esp32-h2-devkitm-1',
33+
type: 'board-esp32-h2-devkitm-1',
2734
family: 'esp32',
2835
idfTarget: 'esp32h2',
2936
},
3037
{
3138
title: 'ESP32-P4-Function-EV-Board',
32-
board: 'board-esp32-p4-function-ev',
39+
type: 'board-esp32-p4-function-ev',
3340
family: 'esp32',
3441
idfTarget: 'esp32p4',
3542
serialPins: { RX: '38', TX: '37' },
3643
},
3744
{
3845
title: 'ESP32-S2 DevKit',
39-
board: 'board-esp32-s2-devkitm-1',
46+
type: 'board-esp32-s2-devkitm-1',
4047
family: 'esp32',
4148
idfTarget: 'esp32s2',
4249
},
4350
{
4451
title: 'ESP32-S3 DevKit',
45-
board: 'board-esp32-s3-devkitc-1',
52+
type: 'board-esp32-s3-devkitc-1',
4653
family: 'esp32',
4754
idfTarget: 'esp32s3',
4855
},
4956

5057
// ESP32-based boards
51-
{ title: 'ESP32-C3 Rust DevKit', board: 'board-esp32-c3-rust-1', family: 'esp32' },
58+
{ title: 'ESP32-C3 Rust DevKit', type: 'board-esp32-c3-rust-1', family: 'esp32' },
5259
{
5360
title: 'ESP32-S3-BOX',
54-
board: 'board-esp32-s3-box',
61+
type: 'board-esp32-s3-box',
5562
family: 'esp32',
5663
serialPins: { RX: 'G44', TX: 'G43' },
5764
},
5865
{
5966
title: 'ESP32-S3-BOX-3',
60-
board: 'board-esp32-s3-box-3',
67+
type: 'board-esp32-s3-box-3',
6168
family: 'esp32',
6269
serialPins: { RX: 'G44', TX: 'G43' },
6370
},
64-
{ title: 'M5Stack CoreS3', board: 'board-m5stack-core-s3', family: 'esp32' },
71+
{ title: 'M5Stack CoreS3', type: 'board-m5stack-core-s3', family: 'esp32' },
6572

6673
// RP2040-based boards
6774
{
6875
title: 'Raspberry Pi Pico',
69-
board: 'wokwi-pi-pico',
76+
type: 'wokwi-pi-pico',
7077
family: 'rp2',
7178
serialPins: { RX: 'GP1', TX: 'GP0' },
7279
},
7380
{
7481
title: 'Raspberry Pi Pico W',
75-
board: 'board-pi-pico-w',
82+
type: 'board-pi-pico-w',
7683
family: 'rp2',
7784
serialPins: { RX: 'GP1', TX: 'GP0' },
7885
},
7986

8087
// STM32 boards
81-
{ title: 'STM32 Nucleo-64 C031C6', board: 'board-st-nucleo-c031c6', family: 'stm32' },
82-
{ title: 'STM32 Nucleo-64 L031K6', board: 'board-st-nucleo-l031k6', family: 'stm32' },
88+
{ title: 'STM32 Nucleo-64 C031C6', type: 'board-st-nucleo-c031c6', family: 'stm32' },
89+
{ title: 'STM32 Nucleo-64 L031K6', type: 'board-st-nucleo-l031k6', family: 'stm32' },
8390
];
91+
92+
export const families: Record<BoardFamily, IFamilyInfo> = {
93+
esp32: {
94+
defaultFirmwareExt: 'bin',
95+
mcuPartId: 'esp',
96+
},
97+
rp2: {
98+
defaultFirmwareExt: 'uf2',
99+
mcuPartId: 'rp',
100+
},
101+
stm32: {
102+
defaultFirmwareExt: 'hex',
103+
mcuPartId: 'stm',
104+
},
105+
};

src/project/createDiagram.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import { boards } from './boards.js';
1+
import { families, type IBoard } from './boards.js';
22

3-
export function createDiagram(board: string) {
4-
const boardInfo = boards.find((b) => b.board === board);
5-
const serialPins = boardInfo?.serialPins ?? { RX: 'RX', TX: 'TX' };
6-
const mcuPartId = boardInfo?.family === 'esp32' ? 'esp' : 'mcu';
3+
export function createDiagram(board: IBoard) {
4+
const familyInfo = families[board.family];
5+
const serialPins = board.serialPins ?? { RX: 'RX', TX: 'TX' };
6+
const mcuPartId = familyInfo.mcuPartId;
77

88
return {
99
version: 1,
1010
author: 'Uri Shaked',
1111
editor: 'wokwi',
12-
parts: [{ type: board, id: mcuPartId }],
12+
parts: [{ type: board.type, id: mcuPartId }],
1313
connections: [
1414
[`${mcuPartId}:${serialPins.TX}`, '$serialMonitor:RX', ''],
1515
[`${mcuPartId}:${serialPins.RX}`, '$serialMonitor:TX', ''],

src/project/findBoard.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ import { type Diagram } from './diagram.js';
22
import { boards } from './boards.js';
33

44
export function findBoard(diagram: Diagram) {
5-
return diagram.parts?.find((part) => boards.some((b) => b.board === part.type));
5+
return diagram.parts?.find((part) => boards.some((b) => b.type === part.type));
66
}

src/project/initProjectWizard.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { cancel, confirm, intro, isCancel, log, note, outro, select, text } from '@clack/prompts';
22
import chalkTemplate from 'chalk-template';
3-
import { existsSync, writeFileSync, mkdirSync } from 'fs';
3+
import { existsSync, mkdirSync, writeFileSync } from 'fs';
44
import path from 'path';
5-
import { boards } from './boards.js';
5+
import { createWokwiToml } from '../WokwiConfig.js';
6+
import { boards, families } from './boards.js';
67
import { createDiagram } from './createDiagram.js';
78
import { findFirmwarePath } from './findFirmwarePath.js';
89
import { detectProjectType } from './projectType.js';
9-
import { createWokwiToml } from '../WokwiConfig.js';
1010

1111
export async function initProjectWizard(rootDir: string, opts: { diagramFile?: string }) {
1212
const configPath = path.join(rootDir, 'wokwi.toml');
@@ -52,17 +52,19 @@ export async function initProjectWizard(rootDir: string, opts: { diagramFile?: s
5252

5353
const boardType = await select({
5454
message: 'Select the board to simulate:',
55-
options: filteredBoards.map((board) => ({ value: board.board, label: board.title })),
55+
options: filteredBoards.map((board) => ({ value: board.type, label: board.title })),
5656
maxItems: (process.stdout.rows ?? 16) - 4,
5757
});
58-
if (isCancel(boardType)) {
58+
const board = filteredBoards.find((b) => b.type === (boardType as string));
59+
if (isCancel(boardType) || !board) {
5960
cancel('Operation cancelled.');
6061
process.exit(0);
6162
}
6263

6364
const defaultFirmwarePath = await findFirmwarePath(rootDir, projectType);
65+
const defaultFirmwareExt = families[board.family].defaultFirmwareExt;
6466
const firmwarePath = await text({
65-
message: 'Enter the path to the firmware file (e.g. firmware.bin):',
67+
message: `Enter the path to the firmware file (e.g. firmware.${defaultFirmwareExt}):`,
6668
initialValue: defaultFirmwarePath.firmware,
6769
validate: (value) => {
6870
if (!value) {
@@ -129,7 +131,7 @@ export async function initProjectWizard(rootDir: string, opts: { diagramFile?: s
129131
);
130132

131133
log.info(`Writing diagram.json...`);
132-
writeFileSync(diagramFilePath, JSON.stringify(createDiagram(boardType as string), null, 2));
134+
writeFileSync(diagramFilePath, JSON.stringify(createDiagram(board), null, 2));
133135

134136
if (vsCodeDebug) {
135137
log.info(`Writing .vscode/launch.json...`);

0 commit comments

Comments
 (0)