@@ -22,6 +22,7 @@ import { arduinoChannel } from "../common/outputChannel";
22
22
import { ArduinoWorkspace } from "../common/workspace" ;
23
23
import { SerialMonitor } from "../serialmonitor/serialMonitor" ;
24
24
import { UsbDetector } from "../serialmonitor/usbDetector" ;
25
+ import { ProgrammerManager } from "./programmerManager" ;
25
26
26
27
/**
27
28
* Represent an Arduino application based on the official Arduino IDE.
@@ -34,6 +35,8 @@ export class ArduinoApp {
34
35
35
36
private _exampleManager : ExampleManager ;
36
37
38
+ private _programmerManager : ProgrammerManager ;
39
+
37
40
/**
38
41
* @param {IArduinoSettings } _settings ArduinoSetting object.
39
42
*/
@@ -154,6 +157,64 @@ export class ArduinoApp {
154
157
} ) ;
155
158
}
156
159
160
+ public async uploadUsingProgrammer ( ) {
161
+ const dc = DeviceContext . getInstance ( ) ;
162
+ const boardDescriptor = this . getBoardBuildString ( ) ;
163
+ if ( ! boardDescriptor ) {
164
+ return ;
165
+ }
166
+
167
+ const selectProgrammer = this . getProgrammerString ( ) ;
168
+ if ( ! selectProgrammer ) {
169
+ return ;
170
+ }
171
+
172
+ if ( ! ArduinoWorkspace . rootPath ) {
173
+ vscode . window . showWarningMessage ( "Cannot find the sketch file." ) ;
174
+ return ;
175
+ }
176
+
177
+ if ( ! dc . sketch || ! util . fileExistsSync ( path . join ( ArduinoWorkspace . rootPath , dc . sketch ) ) ) {
178
+ await this . getMainSketch ( dc ) ;
179
+ }
180
+ if ( ! dc . port ) {
181
+ vscode . window . showErrorMessage ( "Please specify the upload serial port." ) ;
182
+ return ;
183
+ }
184
+
185
+ arduinoChannel . show ( ) ;
186
+ arduinoChannel . start ( `Upload sketch - ${ dc . sketch } ` ) ;
187
+
188
+ const serialMonitor = SerialMonitor . getInstance ( ) ;
189
+
190
+ const needRestore = await serialMonitor . closeSerialMonitor ( dc . port ) ;
191
+ UsbDetector . getInstance ( ) . pauseListening ( ) ;
192
+ await vscode . workspace . saveAll ( false ) ;
193
+
194
+ const appPath = path . join ( ArduinoWorkspace . rootPath , dc . sketch ) ;
195
+ const args = [ "--upload" , "--board" , boardDescriptor , "--port" , dc . port , "--useprogrammer" ,
196
+ "--pref" , "programmer=" + selectProgrammer , appPath ] ;
197
+ if ( VscodeSettings . getInstance ( ) . logLevel === "verbose" ) {
198
+ args . push ( "--verbose" ) ;
199
+ }
200
+ if ( dc . output ) {
201
+ const outputPath = path . resolve ( ArduinoWorkspace . rootPath , dc . output ) ;
202
+ args . push ( "--pref" , `build.path=${ outputPath } ` ) ;
203
+ } else {
204
+ const msg = "Output path is not specified. Unable to reuse previously compiled files. Upload could be slow. See README." ;
205
+ arduinoChannel . warning ( msg ) ;
206
+ }
207
+ await util . spawn ( this . _settings . commandPath , arduinoChannel . channel , args ) . then ( async ( ) => {
208
+ UsbDetector . getInstance ( ) . resumeListening ( ) ;
209
+ if ( needRestore ) {
210
+ await serialMonitor . openSerialMonitor ( ) ;
211
+ }
212
+ arduinoChannel . end ( `Uploaded the sketch: ${ dc . sketch } ${ os . EOL } ` ) ;
213
+ } , ( reason ) => {
214
+ arduinoChannel . error ( `Exit with code=${ reason . code } ${ os . EOL } ` ) ;
215
+ } ) ;
216
+ }
217
+
157
218
public async verify ( output : string = "" ) {
158
219
const dc = DeviceContext . getInstance ( ) ;
159
220
const boardDescriptor = this . getBoardBuildString ( ) ;
@@ -528,6 +589,23 @@ export class ArduinoApp {
528
589
this . _exampleManager = value ;
529
590
}
530
591
592
+ public get programmerManager ( ) {
593
+ return this . _programmerManager ;
594
+ }
595
+
596
+ public set programmerManager ( value : ProgrammerManager ) {
597
+ this . _programmerManager = value ;
598
+ }
599
+
600
+ private getProgrammerString ( ) : string {
601
+ const selectProgrammer = this . programmerManager . currentProgrammer ;
602
+ if ( ! selectProgrammer ) {
603
+ Logger . notifyUserError ( "getProgrammerString" , new Error ( constants . messages . NO_PROGRAMMMER_SELECTED ) ) ;
604
+ return ;
605
+ }
606
+ return selectProgrammer ;
607
+ }
608
+
531
609
private getBoardBuildString ( ) : string {
532
610
const selectedBoard = this . boardManager . currentBoard ;
533
611
if ( ! selectedBoard ) {
0 commit comments