Skip to content

Commit

Permalink
Add configure and upload step when uploading to board requiring user …
Browse files Browse the repository at this point in the history
…fields
  • Loading branch information
silvanocerza committed Oct 14, 2021
1 parent e9dd2fe commit 276bf31
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 4 deletions.
89 changes: 85 additions & 4 deletions arduino-ide-extension/src/browser/contributions/upload-sketch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { inject, injectable } from 'inversify';
import { inject, injectable, postConstruct } from 'inversify';
import { Emitter } from '@theia/core/lib/common/event';
import { CoreService } from '../../common/protocol';
import { BoardUserField, CoreService } from '../../common/protocol';
import { ArduinoMenus } from '../menu/arduino-menus';
import { ArduinoToolbar } from '../toolbar/arduino-toolbar';
import { BoardsDataStore } from '../boards/boards-data-store';
Expand All @@ -14,6 +14,7 @@ import {
KeybindingRegistry,
TabBarToolbarRegistry,
} from './contribution';
import { UserFieldsDialog } from '../dialogs/user-fields/user-fields-dialog';

@injectable()
export class UploadSketch extends SketchContribution {
Expand All @@ -29,16 +30,79 @@ export class UploadSketch extends SketchContribution {
@inject(BoardsServiceProvider)
protected readonly boardsServiceClientImpl: BoardsServiceProvider;

@inject(UserFieldsDialog)
protected readonly userFieldsDialog: UserFieldsDialog;

protected cachedUserFields: Map<string, BoardUserField[]> = new Map();

protected readonly onDidChangeEmitter = new Emitter<Readonly<void>>();
readonly onDidChange = this.onDidChangeEmitter.event;

protected uploadInProgress = false;
protected boardRequiresUserFields = false;

@postConstruct()
protected init(): void {
this.boardsServiceClientImpl.onBoardsConfigChanged(async () => {
const userFields = await this.boardsServiceClientImpl.selectedBoardUserFields();
this.boardRequiresUserFields = userFields.length > 0;
})
}

private selectedFqbnAddress(): string {
const { boardsConfig } = this.boardsServiceClientImpl;
const fqbn = boardsConfig.selectedBoard?.fqbn;
if (!fqbn) {
return "";
}
const address = boardsConfig.selectedBoard?.port?.address
if (!address) {
return "";
}
return fqbn + "|" + address;
}

registerCommands(registry: CommandRegistry): void {
registry.registerCommand(UploadSketch.Commands.UPLOAD_SKETCH, {
execute: () => this.uploadSketch(),
execute: async () => {
const key = this.selectedFqbnAddress();
if (!key) {
return;
}
if (this.boardRequiresUserFields && !this.cachedUserFields.has(key)) {
this.userFieldsDialog.value = await this.boardsServiceClientImpl.selectedBoardUserFields();
const result = await this.userFieldsDialog.open();
if (!result) {
return;
}
this.cachedUserFields.set(key, result);
}
this.uploadSketch();
},
isEnabled: () => !this.uploadInProgress,
});
registry.registerCommand(
UploadSketch.Commands.UPLOAD_WITH_CONFIGURATION,
{
execute: async () => {
const key = this.selectedFqbnAddress();
if (!key) {
return;
}

const cached = this.cachedUserFields.get(key);
this.userFieldsDialog.value = cached ?? await this.boardsServiceClientImpl.selectedBoardUserFields();

const result = await this.userFieldsDialog.open()
if (!result) {
return;
}
this.cachedUserFields.set(key, result);
this.uploadSketch();
},
isEnabled: () => !this.uploadInProgress && this.boardRequiresUserFields,
}
);
registry.registerCommand(
UploadSketch.Commands.UPLOAD_SKETCH_USING_PROGRAMMER,
{
Expand All @@ -62,10 +126,15 @@ export class UploadSketch extends SketchContribution {
label: 'Upload',
order: '1',
});
registry.registerMenuAction(ArduinoMenus.SKETCH__MAIN_GROUP, {
commandId: UploadSketch.Commands.UPLOAD_WITH_CONFIGURATION.id,
label: UploadSketch.Commands.UPLOAD_WITH_CONFIGURATION.label,
order: '2',
});
registry.registerMenuAction(ArduinoMenus.SKETCH__MAIN_GROUP, {
commandId: UploadSketch.Commands.UPLOAD_SKETCH_USING_PROGRAMMER.id,
label: 'Upload Using Programmer',
order: '2',
order: '3',
});
}

Expand Down Expand Up @@ -131,6 +200,11 @@ export class UploadSketch extends SketchContribution {
const optimizeForDebug = this.editorMode.compileForDebug;
const { selectedPort } = boardsConfig;
const port = selectedPort;
const userFields = this.cachedUserFields.get(this.selectedFqbnAddress());
if (!userFields) {
this.messageService.error("Can't find user fields for connected board");
return;
}

if (usingProgrammer) {
const programmer = selectedProgrammer;
Expand All @@ -143,6 +217,7 @@ export class UploadSketch extends SketchContribution {
verbose,
verify,
sourceOverride,
userFields,
};
} else {
options = {
Expand All @@ -153,6 +228,7 @@ export class UploadSketch extends SketchContribution {
verbose,
verify,
sourceOverride,
userFields,
};
}
this.outputChannelManager.getChannel('Arduino').clear();
Expand Down Expand Up @@ -196,6 +272,11 @@ export namespace UploadSketch {
export const UPLOAD_SKETCH: Command = {
id: 'arduino-upload-sketch',
};
export const UPLOAD_WITH_CONFIGURATION: Command = {
id: 'arduino-upload-with-configuration-sketch',
label: 'Configure And Upload',
category: 'Arduino',
}
export const UPLOAD_SKETCH_USING_PROGRAMMER: Command = {
id: 'arduino-upload-sketch-using-programmer',
};
Expand Down
2 changes: 2 additions & 0 deletions arduino-ide-extension/src/common/protocol/core-service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BoardUserField } from '.';
import { Port } from '../../common/protocol/boards-service';
import { Programmer } from './boards-service';

Expand Down Expand Up @@ -43,6 +44,7 @@ export namespace CoreService {
readonly port?: Port | undefined;
readonly programmer?: Programmer | undefined;
readonly verify: boolean;
readonly userFields: BoardUserField[];
}
}

Expand Down
5 changes: 5 additions & 0 deletions arduino-ide-extension/src/node/core-service-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
}
req.setVerbose(options.verbose);
req.setVerify(options.verify);

options.userFields.forEach(e => {
req.getUserFieldsMap().set(e.name, e.value);
});

const result = responseHandler(client, req);

try {
Expand Down

0 comments on commit 276bf31

Please sign in to comment.