Skip to content

Commit 5d7d398

Browse files
authored
add progress during firmware flashing (#70)
1 parent c3257d7 commit 5d7d398

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

Diff for: cli/firmware/flash.go

+5
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ func updateFirmware(board *firmwareindex.IndexBoard, commandLine []string, modul
227227
return err
228228
}
229229
defer f.Close()
230+
f.SetProgressCallback(printProgress)
230231

231232
// now flash the actual firmware
232233
flasherOut := new(bytes.Buffer)
@@ -256,3 +257,7 @@ func updateFirmware(board *firmwareindex.IndexBoard, commandLine []string, modul
256257
}
257258
return nil
258259
}
260+
261+
func printProgress(progress int) {
262+
fmt.Printf("Flashing progress: %d%%\r", progress)
263+
}

Diff for: flasher/flasher.go

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ type Flasher interface {
5252
FlashFirmware(firmwareFile *paths.Path, flasherOut io.Writer) error
5353
FlashCertificates(certificatePaths *paths.PathList, URLs []string, flasherOut io.Writer) error
5454
Close() error
55+
SetProgressCallback(func(progress int))
5556

5657
hello() error
5758
write(address uint32, buffer []byte) error

Diff for: flasher/nina.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"encoding/pem"
2929
"fmt"
3030
"io"
31-
"strconv"
3231
"time"
3332

3433
"github.com/arduino/go-paths-helper"
@@ -60,8 +59,9 @@ func NewNinaFlasher(portAddress string) (*NinaFlasher, error) {
6059
}
6160

6261
type NinaFlasher struct {
63-
port serial.Port
64-
payloadSize int
62+
port serial.Port
63+
payloadSize int
64+
progressCallback func(int)
6565
}
6666

6767
// FlashFirmware in board connected to port using data from firmwareFile
@@ -93,7 +93,7 @@ func (f *NinaFlasher) FlashFirmware(firmwareFile *paths.Path, flasherOut io.Writ
9393
return err
9494
}
9595
logrus.Infof("Flashed all the things")
96-
flasherOut.Write([]byte("Flashed all the things\n"))
96+
flasherOut.Write([]byte("Flashing progress: 100%\n"))
9797
return nil
9898
}
9999

@@ -246,7 +246,11 @@ func (f *NinaFlasher) flashChunk(offset int, buffer []byte) error {
246246
}
247247

248248
for i := 0; i < bufferLength; i += chunkSize {
249-
logrus.Debugf("Flashing chunk: %s%%", strconv.Itoa((i*100)/bufferLength))
249+
progress := (i * 100) / bufferLength
250+
logrus.Debugf("Flashing chunk: %d%%", progress)
251+
if f.progressCallback != nil {
252+
f.progressCallback(progress)
253+
}
250254
start := i
251255
end := i + chunkSize
252256
if end > bufferLength {
@@ -490,3 +494,7 @@ func (f *NinaFlasher) md5sum(data []byte) error {
490494

491495
return nil
492496
}
497+
498+
func (f *NinaFlasher) SetProgressCallback(callback func(progress int)) {
499+
f.progressCallback = callback
500+
}

Diff for: flasher/winc.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"errors"
2929
"fmt"
3030
"io"
31-
"strconv"
3231
"time"
3332

3433
"github.com/arduino/go-paths-helper"
@@ -56,8 +55,9 @@ func NewWincFlasher(portAddress string) (*WincFlasher, error) {
5655
}
5756

5857
type WincFlasher struct {
59-
port serial.Port
60-
payloadSize int
58+
port serial.Port
59+
payloadSize int
60+
progressCallback func(int)
6161
}
6262

6363
func (f *WincFlasher) FlashFirmware(firmwareFile *paths.Path, flasherOut io.Writer) error {
@@ -74,7 +74,7 @@ func (f *WincFlasher) FlashFirmware(firmwareFile *paths.Path, flasherOut io.Writ
7474
return err
7575
}
7676
logrus.Infof("Flashed all the things")
77-
flasherOut.Write([]byte("Flashed all the things\n"))
77+
flasherOut.Write([]byte("Flashing progress: 100%\n"))
7878
return nil
7979
}
8080

@@ -277,7 +277,11 @@ func (f *WincFlasher) flashChunk(offset int, buffer []byte) error {
277277
}
278278

279279
for i := 0; i < bufferLength; i += f.payloadSize {
280-
logrus.Debugf("Flashing chunk: %s%%", strconv.Itoa((i*100)/bufferLength))
280+
progress := ((i * 100) / bufferLength)
281+
logrus.Debugf("Flashing chunk: %d%%", progress)
282+
if f.progressCallback != nil {
283+
f.progressCallback(progress)
284+
}
281285
start := i
282286
end := i + f.payloadSize
283287
if end > bufferLength {
@@ -461,3 +465,7 @@ func (f *WincFlasher) erase(address uint32, length uint32) error {
461465
}
462466
return nil
463467
}
468+
469+
func (f *WincFlasher) SetProgressCallback(callback func(progress int)) {
470+
f.progressCallback = callback
471+
}

0 commit comments

Comments
 (0)