@@ -14,6 +14,7 @@ import (
14
14
"github.com/arduino/uno-r4-wifi-fwuploader-plugin/certificate"
15
15
"github.com/arduino/uno-r4-wifi-fwuploader-plugin/serial"
16
16
semver "go.bug.st/relaxed-semver"
17
+ serialx "go.bug.st/serial"
17
18
)
18
19
19
20
const (
@@ -138,24 +139,13 @@ func (p *unoR4WifiPlugin) GetFirmwareVersion(portAddress string, feedback *helpe
138
139
return nil , err
139
140
}
140
141
141
- port , err := serial .Open (serial . Port ( portAddress ) )
142
+ port , err := serial .Open (portAddress )
142
143
if err != nil {
143
144
return nil , err
144
145
}
145
146
defer port .Close ()
146
147
147
- if _ , err := port .Write ([]byte (string (serial .VersionCommand ))); err != nil {
148
- return nil , fmt .Errorf ("write to serial port: %v" , err )
149
- }
150
-
151
- var version string
152
- scanner := bufio .NewScanner (port )
153
- for scanner .Scan () {
154
- version = scanner .Text ()
155
- break
156
- }
157
-
158
- return semver .ParseRelaxed (version ), nil
148
+ return getFirmwareVersion (port )
159
149
}
160
150
161
151
func (p * unoR4WifiPlugin ) reboot (portAddress * string , feedback * helper.PluginFeedback ) error {
@@ -169,48 +159,40 @@ func (p *unoR4WifiPlugin) reboot(portAddress *string, feedback *helper.PluginFee
169
159
return fmt .Errorf ("upload commands sketch: %v" , err )
170
160
}
171
161
172
- port , err := serial .Open (serial .Port (* portAddress ))
162
+ fmt .Fprintf (feedback .Out (), "\n Waiting to flash the binary...\n " )
163
+
164
+ port , err := serial .Open (* portAddress )
173
165
if err != nil {
174
166
return err
175
167
}
176
- if err := serial .SendCommandAndClose (port , serial .RebootCommand ); err != nil {
177
- return err
178
- }
179
-
180
- fmt .Fprintf (feedback .Out (), "Waiting to flash the binary...\n " )
181
168
182
- time .Sleep (3 * time .Second )
183
-
184
- // On Windows, when a board is successfully rebooted in esp32 mode, it will change the serial port.
185
- // Every 250ms we're watching for new ports, if a new one is found we return that otherwise
186
- // we'll wait the the 10 seconds timeout expiration.
187
- newPort , changed , err := allSerialPorts .NewPort ()
169
+ // Get version to decide if we need to reboot with hid or not
170
+ version , err := getFirmwareVersion (port )
188
171
if err != nil {
189
172
return err
190
173
}
191
- if changed {
192
- * portAddress = newPort
193
- }
194
-
195
- // Older firmware version (v0.1.0) do not support rebooting using the command sketch.
196
- // So we use HID to reboot. We're consciosly ignoring the error because for boards
197
- // running a firmware >= v0.2.0 will alaways throw an HID error as we're already in
198
- // esp32 mode.
199
- _ = rebootUsingHID ()
200
174
201
- time .Sleep (3 * time .Second )
175
+ // Older firmware version (v0.1.0) can be rebooted only with HID.
176
+ if version .LessThanOrEqual (semver .ParseRelaxed ("0.1.0" )) {
177
+ if err := rebootUsingHID (); err != nil {
178
+ return err
179
+ }
180
+ } else {
181
+ if err := serial .SendCommandAndClose (port , serial .RebootCommand ); err != nil {
182
+ return err
183
+ }
184
+ }
202
185
203
- // On Windows, when a board is successfully rebooted in esp32 mode, it will change the serial port.
186
+ // When a board is successfully rebooted in esp32 mode, it might change the serial port.
204
187
// Every 250ms we're watching for new ports, if a new one is found we return that otherwise
205
188
// we'll wait the the 10 seconds timeout expiration.
206
- newPort , changed , err = allSerialPorts .NewPort ()
189
+ newPort , changed , err : = allSerialPorts .NewPort ()
207
190
if err != nil {
208
191
return err
209
192
}
210
193
if changed {
211
194
* portAddress = newPort
212
195
}
213
-
214
196
return nil
215
197
}
216
198
@@ -241,3 +223,18 @@ func (p *unoR4WifiPlugin) uploadCommandsSketch(portAddress string, feedback *hel
241
223
time .Sleep (1 * time .Second )
242
224
return nil
243
225
}
226
+
227
+ func getFirmwareVersion (port serialx.Port ) (* semver.RelaxedVersion , error ) {
228
+ if _ , err := port .Write ([]byte (string (serial .VersionCommand ))); err != nil {
229
+ return nil , fmt .Errorf ("write to serial port: %v" , err )
230
+ }
231
+
232
+ var version string
233
+ scanner := bufio .NewScanner (port )
234
+ for scanner .Scan () {
235
+ version = scanner .Text ()
236
+ break
237
+ }
238
+
239
+ return semver .ParseRelaxed (version ), nil
240
+ }
0 commit comments