20
20
let s: TERM = ' !'
21
21
endif
22
22
let s: hardware_dirs = {}
23
+ let s: SKETCHFILE = v: null
23
24
python3 import json
24
25
25
26
" Initialization {{{1
@@ -82,6 +83,7 @@ function! arduino#InitializeConfig() abort
82
83
echoerr ' arduino-cli: command not found'
83
84
endif
84
85
call arduino#ReloadBoards ()
86
+ call s: ReadSketchJson ()
85
87
endfunction
86
88
87
89
function ! arduino#RunCmd (cmd) abort
@@ -196,10 +198,13 @@ function! arduino#GetBuildPath() abort
196
198
endfunction
197
199
198
200
function ! arduino#GetCLICompileCommand (... ) abort
199
- let cmd = ' arduino-cli compile -b ' . g: arduino_board
200
- let port = arduino#GetPort ()
201
- if ! empty (port)
202
- let cmd = cmd . ' -p ' . port
201
+ let cmd = ' arduino-cli compile'
202
+ if s: SKETCHFILE == v: null
203
+ let cmd = cmd . ' -b ' . g: arduino_board
204
+ let port = arduino#GetPort ()
205
+ if ! empty (port)
206
+ let cmd = cmd . ' -p ' . port
207
+ endif
203
208
endif
204
209
if ! empty (g: arduino_programmer )
205
210
let cmd = cmd . ' -P ' . g: arduino_programmer
@@ -420,6 +425,31 @@ function! s:ChooserItemOrder(i1, i2) abort
420
425
return l1 == l2 ? 0 : l1 > l2 ? 1 : -1
421
426
endfunction
422
427
428
+ function ! arduino#Attach (... ) abort
429
+ if ! s: has_cli
430
+ echoerr ' ArduinoAttach requires arduino-cli'
431
+ return
432
+ end
433
+ let port = v: null
434
+ if a: 0
435
+ let port = a: 1
436
+ function PostAttach () abort
437
+ call s: ReadSketchJson ()
438
+ call s: notify (' Arduino attached to board ' . g: arduino_board )
439
+ endfunction
440
+ call arduino#job#run ([' arduino-cli' , ' board' , ' attach' , ' -p' , port], funcref (' PostAttach' ))
441
+ else
442
+ let ports = arduino#GetPorts ()
443
+ if empty (ports)
444
+ echoerr ' No likely serial ports detected!'
445
+ elseif len (ports) == 1
446
+ call arduino#Attach (ports[0 ])
447
+ else
448
+ call arduino#chooser#Choose (' Select Port' , ports, ' arduino#Attach' )
449
+ endif
450
+ endif
451
+ endfunction
452
+
423
453
" Port selection {{{2
424
454
425
455
function ! arduino#ChoosePort (... ) abort
@@ -437,6 +467,7 @@ endfunction
437
467
438
468
function ! arduino#SelectPort (port) abort
439
469
let g: arduino_serial_port = a: port
470
+ call s: WriteSketchKey (' port' , ' serial://' . g: arduino_serial_port )
440
471
endfunction
441
472
442
473
" Board selection {{{2
@@ -465,6 +496,9 @@ function! arduino#SelectBoard(board) abort
465
496
\}
466
497
" Have to delay this to give the previous chooser UI time to clear
467
498
call timer_start (10 , {tid - > arduino#ChooseBoardOption ()})
499
+ if empty (options )
500
+ call s: WriteSketchKey (' fqbn' , g: arduino_board )
501
+ endif
468
502
endfunction
469
503
470
504
" Prompt user for the next unselected board option
@@ -474,17 +508,21 @@ function! arduino#ChooseBoardOption() abort
474
508
if ! has_key (s: callback_data .opts, opt .option )
475
509
let s: callback_data .active_option = opt .option
476
510
call arduino#chooser#Choose (opt .option_label, opt .values , ' arduino#SelectOption' )
477
- return
511
+ return v: true
478
512
endif
479
513
endfor
514
+ return v: false
480
515
endfunction
481
516
482
517
" Callback from option selection
483
518
function ! arduino#SelectOption (value) abort
484
519
let opt = s: callback_data .active_option
485
520
let s: callback_data .opts[opt ] = a: value
486
521
call arduino#SetBoard (s: callback_data .board, s: callback_data .opts)
487
- call arduino#ChooseBoardOption ()
522
+ let choosing = arduino#ChooseBoardOption ()
523
+ if ! choosing
524
+ call s: WriteSketchKey (' fqbn' , g: arduino_board )
525
+ endif
488
526
endfunction
489
527
490
528
" Programmer selection {{{2
@@ -626,6 +664,49 @@ endfunction
626
664
627
665
" Utility functions {{{1
628
666
667
+ function ! s: ReadSketchJson () abort
668
+ let dir = getcwd ()
669
+ while v: true
670
+ let sketch = dir . ' /sketch.json'
671
+ if filereadable (sketch)
672
+ let data = json_decode (join (readfile (sketch)))
673
+ let cpu = get (data, ' cpu' , {})
674
+ if ! empty (cpu)
675
+ let s: SKETCHFILE = sketch
676
+ let board = get (cpu, ' fqbn' , ' ' )
677
+ if ! empty (board)
678
+ let g: arduino_board = board
679
+ endif
680
+ let port = get (cpu, ' port' , ' ' )
681
+ if ! empty (port)
682
+ if port = ~? ' ^serial://'
683
+ let port = strcharpart (port, 9 )
684
+ endif
685
+ let g: arduino_serial_port = port
686
+ endif
687
+ endif
688
+ return
689
+ endif
690
+ let next_dir = fnamemodify (dir , ' :h' )
691
+ if next_dir == dir
692
+ break
693
+ else
694
+ let dir = next_dir
695
+ endif
696
+ endwhile
697
+ let s: SKETCHFILE = v: null
698
+ endfunction
699
+
700
+ function s: WriteSketchKey (key , value) abort
701
+ if s: SKETCHFILE == v: null
702
+ return
703
+ endif
704
+ let data = json_decode (join (readfile (s: SKETCHFILE )))
705
+ let cpu = get (data, ' cpu' , {})
706
+ let cpu[a: key ] = a: value
707
+ call writefile ([json_encode (data)], s: SKETCHFILE )
708
+ endfunction
709
+
629
710
function ! s: CacheLine (lines , varname) abort
630
711
if exists (a: varname )
631
712
let value = eval (a: varname )
@@ -675,7 +756,15 @@ function! arduino#GetInfo() abort
675
756
if g: arduino_use_cli
676
757
echo ' Verify command: ' . arduino#GetCLICompileCommand ()
677
758
else
678
- echo " Verify command: " . arduino#GetArduinoCommand (" --verify" )
759
+ echo ' Verify command: ' . arduino#GetArduinoCommand (' --verify' )
760
+ endif
761
+ endfunction
762
+
763
+ function ! s: notify (msg) abort
764
+ if has (' nvim' )
765
+ call luaeval (' vim.notify(_A)' , a: msg )
766
+ else
767
+ echo a: msg
679
768
endif
680
769
endfunction
681
770
0 commit comments