Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Commit 76c74cf

Browse files
hlovdaladiazulay
authored andcommitted
Add buildMode argument to upload and uploadUsingProgrammer
1 parent 86719a3 commit 76c74cf

File tree

2 files changed

+52
-39
lines changed

2 files changed

+52
-39
lines changed

src/arduino/arduino.ts

+47-35
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ import { UsbDetector } from "../serialmonitor/usbDetector";
2525
import { makeCompilerParserContext } from "./intellisense";
2626
import { ProgrammerManager } from "./programmerManager";
2727

28+
export enum BuildMode {
29+
Upload = "Uploading",
30+
UploadProgrammer = "Uploading (programmer)",
31+
};
32+
2833
/**
2934
* Represent an Arduino application based on the official Arduino IDE.
3035
*/
@@ -99,9 +104,10 @@ export class ArduinoApp {
99104
* @param {bool} [compile=true] - Indicates whether to compile the code when using the CLI to upload
100105
* @param {bool} [useProgrammer=false] - Indicate whether a specific programmer should be used
101106
*/
102-
public async upload(compile: boolean = true, useProgrammer: boolean = false) {
107+
public async upload(buildMode: BuildMode, compile: boolean = true, useProgrammer: boolean = false) {
103108
const dc = DeviceContext.getInstance();
104109
const args: string[] = [];
110+
let restoreSerialMonitor: boolean = false;
105111
const boardDescriptor = this.getBoardBuildString();
106112
if (!boardDescriptor) {
107113
return;
@@ -133,38 +139,40 @@ export class ArduinoApp {
133139
return;
134140
}
135141

136-
if ((!dc.configuration || !/upload_method=[^=,]*st[^,]*link/i.test(dc.configuration)) && !dc.port) {
137-
await selectSerial();
138-
return;
139-
}
142+
if (buildMode === BuildMode.Upload) {
143+
if ((!dc.configuration || !/upload_method=[^=,]*st[^,]*link/i.test(dc.configuration)) && !dc.port) {
144+
await selectSerial();
145+
return;
146+
}
140147

141-
if (!compile && !this.useArduinoCli()) {
142-
arduinoChannel.error("This command is only available when using the Arduino CLI");
143-
return;
144-
}
148+
if (!compile && !this.useArduinoCli()) {
149+
arduinoChannel.error("This command is only available when using the Arduino CLI");
150+
return;
151+
}
145152

146-
if (!this.useArduinoCli()) {
147-
args.push("--upload");
148-
} else {
149-
// TODO: add the --clean argument to the cli args when v 0.14 is released (this will clean up the build folder after uploading)
150-
if (compile) {
151-
args.push("compile", "--upload");
153+
if (!this.useArduinoCli()) {
154+
args.push("--upload");
152155
} else {
153-
args.push("upload");
156+
// TODO: add the --clean argument to the cli args when v 0.14 is released (this will clean up the build folder after uploading)
157+
if (compile) {
158+
args.push("compile", "--upload");
159+
} else {
160+
args.push("upload");
161+
}
162+
args.push("-b", boardDescriptor);
154163
}
155-
args.push("-b", boardDescriptor);
156-
}
157164

158-
if (useProgrammer) {
159-
if (this.useArduinoCli()) {
160-
args.push("--programmer", selectProgrammer)
161-
} else {
162-
args.push("--useprogrammer", "--pref", "programmer=arduino:" + selectProgrammer)
165+
if (useProgrammer) {
166+
if (this.useArduinoCli()) {
167+
args.push("--programmer", selectProgrammer)
168+
} else {
169+
args.push("--useprogrammer", "--pref", "programmer=arduino:" + selectProgrammer)
170+
}
163171
}
164-
}
165172

166-
if (dc.port) {
167-
args.push("--port", dc.port);
173+
if (dc.port) {
174+
args.push("--port", dc.port);
175+
}
168176
}
169177

170178
const verbose = VscodeSettings.getInstance().logLevel === "verbose";
@@ -175,7 +183,7 @@ export class ArduinoApp {
175183
await vscode.workspace.saveAll(false);
176184

177185
arduinoChannel.show();
178-
arduinoChannel.start(`Upload sketch - ${dc.sketch}`);
186+
arduinoChannel.start(`${buildMode} sketch '${dc.sketch}'`);
179187

180188
if (!await this.runPreBuildCommand(dc)) {
181189
return;
@@ -204,16 +212,20 @@ export class ArduinoApp {
204212

205213
// stop serial monitor when everything is prepared and good
206214
// what makes restoring of its previous state easier
207-
const restoreSerialMonitor = await SerialMonitor.getInstance().closeSerialMonitor(dc.port);
208-
UsbDetector.getInstance().pauseListening();
215+
if (buildMode === BuildMode.Upload || buildMode === BuildMode.UploadProgrammer) {
216+
restoreSerialMonitor = await SerialMonitor.getInstance().closeSerialMonitor(dc.port);
217+
UsbDetector.getInstance().pauseListening();
218+
}
209219

210220
// Push sketch as last argument
211221
args.push(path.join(ArduinoWorkspace.rootPath, dc.sketch));
212222

213223
const cleanup = async () => {
214-
UsbDetector.getInstance().resumeListening();
215-
if (restoreSerialMonitor) {
216-
await SerialMonitor.getInstance().openSerialMonitor();
224+
if (buildMode === BuildMode.Upload || buildMode === BuildMode.UploadProgrammer) {
225+
UsbDetector.getInstance().resumeListening();
226+
if (restoreSerialMonitor) {
227+
await SerialMonitor.getInstance().openSerialMonitor();
228+
}
217229
}
218230
}
219231

@@ -223,15 +235,15 @@ export class ArduinoApp {
223235
args,
224236
).then(async () => {
225237
await cleanup();
226-
arduinoChannel.end(`Uploaded the sketch: ${dc.sketch}${os.EOL}`);
238+
arduinoChannel.end(`${buildMode} sketch '${dc.sketch}'${os.EOL}`);
227239
}, async (reason) => {
228240
await cleanup();
229241
const msg = reason.code ?
230-
`Exit with code=${reason.code}${os.EOL}` :
242+
`Exit with code=${reason.code}` :
231243
reason.message ?
232244
reason.message :
233245
JSON.stringify(reason);
234-
arduinoChannel.error(msg);
246+
arduinoChannel.error(`${buildMode} sketch '${dc.sketch}': ${msg}${os.EOL}`);
235247
});
236248
}
237249

src/extension.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const completionProviderModule = impor("./langService/completionProvider") as ty
2727
import * as Logger from "./logger/logger";
2828
const nsatModule =
2929
impor("./nsat") as typeof import ("./nsat");
30+
import { BuildMode } from "./arduino/arduino";
3031
import { SerialMonitor } from "./serialmonitor/serialMonitor";
3132
const usbDetectorModule = impor("./serialmonitor/usbDetector") as typeof import ("./serialmonitor/usbDetector");
3233

@@ -144,7 +145,7 @@ export async function activate(context: vscode.ExtensionContext) {
144145
location: vscode.ProgressLocation.Window,
145146
title: "Arduino: Uploading...",
146147
}, async () => {
147-
await arduinoContextModule.default.arduinoApp.upload();
148+
await arduinoContextModule.default.arduinoApp.upload(BuildMode.Upload);
148149
});
149150
} catch (ex) {
150151
}
@@ -162,7 +163,7 @@ export async function activate(context: vscode.ExtensionContext) {
162163
location: vscode.ProgressLocation.Window,
163164
title: "Arduino: Using CLI to upload...",
164165
}, async () => {
165-
await arduinoContextModule.default.arduinoApp.upload(false);
166+
await arduinoContextModule.default.arduinoApp.upload(BuildMode.Upload, false);
166167
});
167168
} catch (ex) {
168169
}
@@ -197,7 +198,7 @@ export async function activate(context: vscode.ExtensionContext) {
197198
if (!status.compile) {
198199
status.compile = "upload";
199200
try {
200-
await arduinoContextModule.default.arduinoApp.upload(true, true);
201+
await arduinoContextModule.default.arduinoApp.upload(BuildMode.UploadProgrammer, true, true);
201202
} catch (ex) {
202203
}
203204
delete status.compile;
@@ -210,7 +211,7 @@ export async function activate(context: vscode.ExtensionContext) {
210211
if (!status.compile) {
211212
status.compile = "cliUpload";
212213
try {
213-
await arduinoContextModule.default.arduinoApp.upload(false, true);
214+
await arduinoContextModule.default.arduinoApp.upload(BuildMode.UploadProgrammer, false, true);
214215
} catch (ex) {
215216
}
216217
delete status.compile;

0 commit comments

Comments
 (0)