Skip to content

Commit 6e286c1

Browse files
author
Alberto Iannaccone
committed
close serial port connection before flashing firmware
1 parent cc5764e commit 6e286c1

File tree

2 files changed

+35
-22
lines changed

2 files changed

+35
-22
lines changed

Diff for: arduino-ide-extension/src/node/arduino-firmware-uploader-impl.ts

+26-10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
} from '../common/protocol/arduino-firmware-uploader';
55
import { injectable, inject, named } from 'inversify';
66
import { ExecutableService } from '../common/protocol';
7+
import { SerialService } from '../common/protocol/serial-service';
78
import { getExecPath, spawnCommand } from './exec-util';
89
import { ILogger } from '@theia/core/lib/common/logger';
910

@@ -18,6 +19,9 @@ export class ArduinoFirmwareUploaderImpl implements ArduinoFirmwareUploader {
1819
@named('fwuploader')
1920
protected readonly logger: ILogger;
2021

22+
@inject(SerialService)
23+
protected readonly serialService: SerialService;
24+
2125
protected onError(error: any): void {
2226
this.logger.error(error);
2327
}
@@ -66,15 +70,27 @@ export class ArduinoFirmwareUploaderImpl implements ArduinoFirmwareUploader {
6670
}
6771

6872
async flash(firmware: FirmwareInfo, port: string): Promise<string> {
69-
return await this.runCommand([
70-
'firmware',
71-
'flash',
72-
'--fqbn',
73-
firmware.board_fqbn,
74-
'--address',
75-
port,
76-
'--module',
77-
`${firmware.module}@${firmware.firmware_version}`,
78-
]);
73+
this.serialService.uploadInProgress = true;
74+
await this.serialService.disconnect();
75+
76+
let output;
77+
try {
78+
output = await this.runCommand([
79+
'firmware',
80+
'flash',
81+
'--fqbn',
82+
firmware.board_fqbn,
83+
'--address',
84+
port,
85+
'--module',
86+
`${firmware.module}@${firmware.firmware_version}`,
87+
]);
88+
} catch (e) {
89+
throw e;
90+
} finally {
91+
this.serialService.uploadInProgress = false;
92+
this.serialService.connectSerialIfRequired();
93+
return output;
94+
}
7995
}
8096
}

Diff for: arduino-ide-extension/src/node/arduino-ide-backend-module.ts

+9-12
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ export default new ContainerModule((bind, unbind, isBound, rebind) => {
203203

204204
// #endregion Theia customizations
205205

206-
// Monitor client provider per connected frontend.
206+
// Serial client provider per connected frontend.
207207
bind(ConnectionContainerModule).toConstantValue(
208208
ConnectionContainerModule.create(({ bind, bindBackendService }) => {
209209
bind(MonitorClientProvider).toSelf().inSingletonScope();
@@ -260,17 +260,14 @@ export default new ContainerModule((bind, unbind, isBound, rebind) => {
260260
)
261261
.inSingletonScope();
262262

263-
bind(ArduinoFirmwareUploaderImpl).toSelf().inSingletonScope();
264-
bind(ArduinoFirmwareUploader).toService(ArduinoFirmwareUploaderImpl);
265-
bind(BackendApplicationContribution).toService(ArduinoFirmwareUploaderImpl);
266-
bind(ConnectionHandler)
267-
.toDynamicValue(
268-
(context) =>
269-
new JsonRpcConnectionHandler(ArduinoFirmwareUploaderPath, () =>
270-
context.container.get(ArduinoFirmwareUploader)
271-
)
272-
)
273-
.inSingletonScope();
263+
// Singleton per BE, each FE connection gets its proxy.
264+
bind(ConnectionContainerModule).toConstantValue(
265+
ConnectionContainerModule.create(({ bind, bindBackendService }) => {
266+
bind(ArduinoFirmwareUploaderImpl).toSelf().inSingletonScope();
267+
bind(ArduinoFirmwareUploader).toService(ArduinoFirmwareUploaderImpl);
268+
bindBackendService(ArduinoFirmwareUploaderPath, ArduinoFirmwareUploader);
269+
})
270+
);
274271

275272
// Logger for the Arduino daemon
276273
bind(ILogger)

0 commit comments

Comments
 (0)