@@ -27,6 +27,7 @@ import (
27
27
"time"
28
28
29
29
"github.com/arduino/arduino-cli/commands/monitor"
30
+ sk "github.com/arduino/arduino-cli/commands/sketch"
30
31
"github.com/arduino/arduino-cli/configuration"
31
32
"github.com/arduino/arduino-cli/i18n"
32
33
"github.com/arduino/arduino-cli/internal/cli/arguments"
@@ -45,13 +46,14 @@ var tr = i18n.Tr
45
46
// NewCommand created a new `monitor` command
46
47
func NewCommand () * cobra.Command {
47
48
var (
48
- raw bool
49
- portArgs arguments.Port
50
- describe bool
51
- configs []string
52
- quiet bool
53
- timestamp bool
54
- fqbn arguments.Fqbn
49
+ portArgs arguments.Port
50
+ fqbnArg arguments.Fqbn
51
+ profileArg arguments.Profile
52
+ raw bool
53
+ describe bool
54
+ configs []string
55
+ quiet bool
56
+ timestamp bool
55
57
)
56
58
monitorCommand := & cobra.Command {
57
59
Use : "monitor" ,
@@ -61,38 +63,92 @@ func NewCommand() *cobra.Command {
61
63
" " + os .Args [0 ] + " monitor -p /dev/ttyACM0\n " +
62
64
" " + os .Args [0 ] + " monitor -p /dev/ttyACM0 --describe" ,
63
65
Run : func (cmd * cobra.Command , args []string ) {
64
- runMonitorCmd (& portArgs , & fqbn , configs , describe , timestamp , quiet , raw )
66
+ sketchPath := ""
67
+ if len (args ) > 0 {
68
+ sketchPath = args [0 ]
69
+ }
70
+ runMonitorCmd (& portArgs , & fqbnArg , & profileArg , sketchPath , configs , describe , timestamp , quiet , raw )
65
71
},
66
72
}
67
73
portArgs .AddToCommand (monitorCommand )
74
+ profileArg .AddToCommand (monitorCommand )
68
75
monitorCommand .Flags ().BoolVar (& raw , "raw" , false , tr ("Set terminal in raw mode (unbuffered)." ))
69
76
monitorCommand .Flags ().BoolVar (& describe , "describe" , false , tr ("Show all the settings of the communication port." ))
70
77
monitorCommand .Flags ().StringSliceVarP (& configs , "config" , "c" , []string {}, tr ("Configure communication port settings. The format is <ID>=<value>[,<ID>=<value>]..." ))
71
78
monitorCommand .Flags ().BoolVarP (& quiet , "quiet" , "q" , false , tr ("Run in silent mode, show only monitor input and output." ))
72
79
monitorCommand .Flags ().BoolVar (& timestamp , "timestamp" , false , tr ("Timestamp each incoming line." ))
73
- fqbn .AddToCommand (monitorCommand )
74
- monitorCommand .MarkFlagRequired ("port" )
80
+ fqbnArg .AddToCommand (monitorCommand )
75
81
return monitorCommand
76
82
}
77
83
78
- func runMonitorCmd (portArgs * arguments.Port , fqbn * arguments.Fqbn , configs []string , describe , timestamp , quiet , raw bool ) {
79
- instance := instance .CreateAndInit ()
84
+ func runMonitorCmd (
85
+ portArgs * arguments.Port , fqbnArg * arguments.Fqbn , profileArg * arguments.Profile , sketchPathArg string ,
86
+ configs []string , describe , timestamp , quiet , raw bool ,
87
+ ) {
80
88
logrus .Info ("Executing `arduino-cli monitor`" )
81
89
82
90
if ! configuration .HasConsole {
83
91
quiet = true
84
92
}
85
93
86
- // TODO: Should use sketch default_port/protocol?
87
- portAddress , portProtocol , err := portArgs .GetPortAddressAndProtocol (instance , "" , "" )
94
+ var (
95
+ inst * rpc.Instance
96
+ profile * rpc.Profile
97
+ fqbn string
98
+ defaultPort , defaultProtocol string
99
+ )
100
+
101
+ // Flags takes maximum precedence over sketch.yaml
102
+ // If {--port --fqbn --profile} are set we ignore the profile.
103
+ // If both {--port --profile} are set we read the fqbn in the following order: profile -> default_fqbn -> discovery
104
+ // If only --port is set we read the fqbn in the following order: default_fqbn -> discovery
105
+ // If only --fqbn is set we read the port in the following order: default_port
106
+ sketchPath := arguments .InitSketchPath (sketchPathArg , false )
107
+ sketch , err := sk .LoadSketch (context .Background (), & rpc.LoadSketchRequest {SketchPath : sketchPath .String ()})
108
+ if err != nil && ! portArgs .IsPortFlagSet () {
109
+ feedback .Fatal (
110
+ tr ("Error getting default port from `sketch.yaml`. Check if you're in the correct sketch folder or provide the --port flag: %s" , err ),
111
+ feedback .ErrGeneric ,
112
+ )
113
+ }
114
+ if sketch != nil {
115
+ defaultPort , defaultProtocol = sketch .GetDefaultPort (), sketch .GetDefaultProtocol ()
116
+ }
117
+ if fqbnArg .String () == "" {
118
+ if profileArg .Get () == "" {
119
+ inst , profile = instance .CreateAndInitWithProfile (sketch .GetDefaultProfile ().GetName (), sketchPath )
120
+ } else {
121
+ inst , profile = instance .CreateAndInitWithProfile (profileArg .Get (), sketchPath )
122
+ }
123
+ }
124
+ if inst == nil {
125
+ inst = instance .CreateAndInit ()
126
+ }
127
+ // Priority on how to retrieve the fqbn
128
+ // 1. from flag
129
+ // 2. from profile
130
+ // 3. from default_fqbn specified in the sketch.yaml
131
+ // 4. try to detect from the port
132
+ switch {
133
+ case fqbnArg .String () != "" :
134
+ fqbn = fqbnArg .String ()
135
+ case profile .GetFqbn () != "" :
136
+ fqbn = profile .GetFqbn ()
137
+ case sketch .GetDefaultFqbn () != "" :
138
+ fqbn = sketch .GetDefaultFqbn ()
139
+ default :
140
+ fqbn , _ = portArgs .DetectFQBN (inst )
141
+ }
142
+
143
+ portAddress , portProtocol , err := portArgs .GetPortAddressAndProtocol (inst , defaultPort , defaultProtocol )
88
144
if err != nil {
89
145
feedback .FatalError (err , feedback .ErrGeneric )
90
146
}
91
147
92
148
enumerateResp , err := monitor .EnumerateMonitorPortSettings (context .Background (), & rpc.EnumerateMonitorPortSettingsRequest {
93
- Instance : instance ,
149
+ Instance : inst ,
94
150
PortProtocol : portProtocol ,
95
- Fqbn : fqbn . String () ,
151
+ Fqbn : fqbn ,
96
152
})
97
153
if err != nil {
98
154
feedback .Fatal (tr ("Error getting port settings details: %s" , err ), feedback .ErrGeneric )
@@ -144,9 +200,9 @@ func runMonitorCmd(portArgs *arguments.Port, fqbn *arguments.Fqbn, configs []str
144
200
}
145
201
}
146
202
portProxy , _ , err := monitor .Monitor (context .Background (), & rpc.MonitorRequest {
147
- Instance : instance ,
203
+ Instance : inst ,
148
204
Port : & rpc.Port {Address : portAddress , Protocol : portProtocol },
149
- Fqbn : fqbn . String () ,
205
+ Fqbn : fqbn ,
150
206
PortConfiguration : configuration ,
151
207
})
152
208
if err != nil {
0 commit comments