19
19
package firmware
20
20
21
21
import (
22
- "bytes"
23
22
"fmt"
24
23
"io"
25
24
"os"
@@ -120,35 +119,17 @@ func runFlash(cmd *cobra.Command, args []string) {
120
119
logrus .Debugf ("firmware file downloaded in %s" , firmwareFilePath .String ())
121
120
}
122
121
123
- loaderSketch := ""
124
- var uploader * plugin.FwUploader
125
- if ! board .IsPlugin () {
126
- loaderSketchPath , err := download .DownloadSketch (board .LoaderSketch )
127
- if err != nil {
128
- feedback .Fatal (fmt .Sprintf ("Error downloading loader sketch from %s: %s" , board .LoaderSketch .URL , err ), feedback .ErrGeneric )
129
- }
130
- logrus .Debugf ("loader sketch downloaded in %s" , loaderSketchPath .String ())
131
- loaderSketch = strings .ReplaceAll (loaderSketchPath .String (), loaderSketchPath .Ext (), "" )
132
- } else {
133
- var err error
134
- uploader , err = plugin .NewFWUploaderPlugin (uploadToolDir )
135
- if err != nil {
136
- feedback .Fatal (fmt .Sprintf ("Could not open uploader plugin: %s" , err ), feedback .ErrGeneric )
137
- }
122
+ uploader , err := plugin .NewFWUploaderPlugin (uploadToolDir )
123
+ if err != nil {
124
+ feedback .Fatal (fmt .Sprintf ("Could not open uploader plugin: %s" , err ), feedback .ErrGeneric )
138
125
}
139
126
140
127
retry := 0
141
128
for {
142
129
retry ++
143
130
logrus .Infof ("Uploading firmware (try %d of %d)" , retry , retries )
144
131
145
- var res * flasher.FlashResult
146
- var err error
147
- if ! board .IsPlugin () {
148
- res , err = updateFirmware (board , loaderSketch , moduleName , uploadToolDir , firmwareFilePath )
149
- } else {
150
- res , err = updateFirmwareWithPlugin (uploader , firmwareFilePath )
151
- }
132
+ res , err := updateFirmware (uploader , firmwareFilePath )
152
133
if err == nil {
153
134
feedback .PrintResult (res )
154
135
logrus .Info ("Operation completed: success! :-)" )
@@ -165,7 +146,7 @@ func runFlash(cmd *cobra.Command, args []string) {
165
146
}
166
147
}
167
148
168
- func updateFirmwareWithPlugin (uploader * plugin.FwUploader , fwPath * paths.Path ) (* flasher.FlashResult , error ) {
149
+ func updateFirmware (uploader * plugin.FwUploader , fwPath * paths.Path ) (* flasher.FlashResult , error ) {
169
150
var stdout , stderr io.Writer
170
151
if feedback .GetFormat () == feedback .Text {
171
152
stdout = os .Stdout
@@ -183,58 +164,6 @@ func updateFirmwareWithPlugin(uploader *plugin.FwUploader, fwPath *paths.Path) (
183
164
}, nil
184
165
}
185
166
186
- func updateFirmware (board * firmwareindex.IndexBoard , loaderSketch , moduleName string , uploadToolDir , firmwareFile * paths.Path ) (* flasher.FlashResult , error ) {
187
- programmerOut , programmerErr , err := common .FlashSketch (board , loaderSketch , uploadToolDir , commonFlags .Address )
188
- if err != nil {
189
- return nil , err
190
- }
191
- // Wait a bit after flashing the loader sketch for the board to become
192
- // available again.
193
- logrus .Debug ("sleeping for 3 sec" )
194
- time .Sleep (3 * time .Second )
195
-
196
- // Get flasher depending on which module to use
197
- var f flasher.Flasher
198
-
199
- // This matches the baudrate used in the FirmwareUpdater.ino sketch
200
- const baudRate = 1000000
201
- switch moduleName {
202
- default :
203
- err = fmt .Errorf ("unknown module: %s" , moduleName )
204
- feedback .Fatal (fmt .Sprintf ("Error during firmware flashing: %s" , err ), feedback .ErrGeneric )
205
- }
206
- if err != nil {
207
- return nil , fmt .Errorf ("error during firmware flashing: %s" , err )
208
- }
209
- defer f .Close ()
210
-
211
- // now flash the actual firmware
212
- flasherOut := new (bytes.Buffer )
213
- flasherErr := new (bytes.Buffer )
214
- if feedback .GetFormat () == feedback .JSON {
215
- err = f .FlashFirmware (firmwareFile , flasherOut )
216
- } else {
217
- f .SetProgressCallback (printProgress )
218
- err = f .FlashFirmware (firmwareFile , os .Stdout )
219
- }
220
- if err != nil {
221
- flasherErr .Write ([]byte (fmt .Sprintf ("Error during firmware flashing: %s" , err )))
222
- return nil , err
223
- }
224
-
225
- // Print the results
226
- return & flasher.FlashResult {
227
- Programmer : (& flasher.ExecOutput {
228
- Stdout : programmerOut .String (),
229
- Stderr : programmerErr .String (),
230
- }),
231
- Flasher : (& flasher.ExecOutput {
232
- Stdout : flasherOut .String (),
233
- Stderr : flasherErr .String (),
234
- }),
235
- }, nil
236
- }
237
-
238
167
// callback used to print the progress
239
168
func printProgress (progress int ) {
240
169
fmt .Printf ("Flashing progress: %d%%\r " , progress )
0 commit comments