Skip to content

add progress during firmware flashing #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cli/firmware/flash.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ func updateFirmware(board *firmwareindex.IndexBoard, commandLine []string, modul
return err
}
defer f.Close()
f.SetProgressCallback(printProgress)

// now flash the actual firmware
flasherOut := new(bytes.Buffer)
Expand Down Expand Up @@ -256,3 +257,7 @@ func updateFirmware(board *firmwareindex.IndexBoard, commandLine []string, modul
}
return nil
}

func printProgress(progress int) {
fmt.Printf("Flashing progress: %d%%\r", progress)
}
1 change: 1 addition & 0 deletions flasher/flasher.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type Flasher interface {
FlashFirmware(firmwareFile *paths.Path, flasherOut io.Writer) error
FlashCertificates(certificatePaths *paths.PathList, URLs []string, flasherOut io.Writer) error
Close() error
SetProgressCallback(func(progress int))

hello() error
write(address uint32, buffer []byte) error
Expand Down
18 changes: 13 additions & 5 deletions flasher/nina.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"encoding/pem"
"fmt"
"io"
"strconv"
"time"

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

type NinaFlasher struct {
port serial.Port
payloadSize int
port serial.Port
payloadSize int
progressCallback func(int)
}

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

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

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

return nil
}

func (f *NinaFlasher) SetProgressCallback(callback func(progress int)) {
f.progressCallback = callback
}
18 changes: 13 additions & 5 deletions flasher/winc.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"errors"
"fmt"
"io"
"strconv"
"time"

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

type WincFlasher struct {
port serial.Port
payloadSize int
port serial.Port
payloadSize int
progressCallback func(int)
}

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

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

for i := 0; i < bufferLength; i += f.payloadSize {
logrus.Debugf("Flashing chunk: %s%%", strconv.Itoa((i*100)/bufferLength))
progress := ((i * 100) / bufferLength)
logrus.Debugf("Flashing chunk: %d%%", progress)
if f.progressCallback != nil {
f.progressCallback(progress)
}
start := i
end := i + f.payloadSize
if end > bufferLength {
Expand Down Expand Up @@ -461,3 +465,7 @@ func (f *WincFlasher) erase(address uint32, length uint32) error {
}
return nil
}

func (f *WincFlasher) SetProgressCallback(callback func(progress int)) {
f.progressCallback = callback
}