diff --git a/cli/firmware/flash.go b/cli/firmware/flash.go index a5e41116..aa0ecddb 100644 --- a/cli/firmware/flash.go +++ b/cli/firmware/flash.go @@ -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) @@ -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) +} diff --git a/flasher/flasher.go b/flasher/flasher.go index cbb38a4b..ecdde7b8 100644 --- a/flasher/flasher.go +++ b/flasher/flasher.go @@ -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 diff --git a/flasher/nina.go b/flasher/nina.go index 316b7282..1e8c9d8a 100644 --- a/flasher/nina.go +++ b/flasher/nina.go @@ -28,7 +28,6 @@ import ( "encoding/pem" "fmt" "io" - "strconv" "time" "github.com/arduino/go-paths-helper" @@ -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 @@ -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 } @@ -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 { @@ -490,3 +494,7 @@ func (f *NinaFlasher) md5sum(data []byte) error { return nil } + +func (f *NinaFlasher) SetProgressCallback(callback func(progress int)) { + f.progressCallback = callback +} diff --git a/flasher/winc.go b/flasher/winc.go index b94432f7..840fecce 100644 --- a/flasher/winc.go +++ b/flasher/winc.go @@ -28,7 +28,6 @@ import ( "errors" "fmt" "io" - "strconv" "time" "github.com/arduino/go-paths-helper" @@ -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 { @@ -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 } @@ -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 { @@ -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 +}