From bfeb204226bf5466b021eb83530b9ec27bd1a864 Mon Sep 17 00:00:00 2001 From: Adam Boardman Date: Thu, 17 Dec 2020 15:14:56 +0000 Subject: [PATCH] Changed callback random number printing to show a progress bar --- codi-app/codiUpdate.py | 13 ++++++++++--- codi-app/xmodem/__init__.py | 8 +++++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/codi-app/codiUpdate.py b/codi-app/codiUpdate.py index 555dfe5..04944b6 100755 --- a/codi-app/codiUpdate.py +++ b/codi-app/codiUpdate.py @@ -105,9 +105,16 @@ def stm32_into_download_mode(prepare="0"): ser = None +def print_progress_bar (iteration, error_count, total): + errors = "E:" + str(error_count) + length = int(os.popen('stty size', 'r').read().split()[1]) - len(errors) - 11 + percent = ("{0:.1f}").format(100 * (iteration / float(total))) + filled_length = int(length * iteration // total) + bar = '█' * filled_length + '-' * (length - filled_length) + print(f'\r |{bar}| {percent}% {errors}', end = "\r") -def callback(total_packets, success_count, error_count): - print(total_packets, success_count, error_count) +def callback(total_packets, success_count, error_count, total): + print_progress_bar(total_packets, error_count, total) def send_file(file): @@ -135,7 +142,7 @@ def send_file(file): print("Sending", file) try: - print("Send completed:", modem.send(file, callback=callback)) + print("\r\nSend completed:", modem.send(file, callback=callback)) except Exception as e: print("Exception", e) SerialPortManager.switchToCmdMode() diff --git a/codi-app/xmodem/__init__.py b/codi-app/xmodem/__init__.py index 3798bfa..a6395ed 100644 --- a/codi-app/xmodem/__init__.py +++ b/codi-app/xmodem/__init__.py @@ -184,7 +184,7 @@ class YMODEM(object): getting status updates while a ymodem transfer is underway. Expected callback signature: - def callback(total_packets, success_count, error_count) + def callback(total_packets, success_count, error_count, total) :type callback: callable ''' @@ -231,6 +231,7 @@ class YMODEM(object): error_count = 0 success_count = 0 total_packets = 0 + total = 0 header_sent = False sequence = 0 stream = None @@ -253,6 +254,7 @@ class YMODEM(object): data = data.ljust(header_size, NUL) checksum = self._make_send_checksum(crc_mode, data) header_sent = True + total = (stat.st_size / packet_size) + 1 else: # normal data packet data = stream.read(packet_size) @@ -282,7 +284,7 @@ class YMODEM(object): if char == ACK or char == ACK2 or char == NAK: success_count += 1 if callable(callback): - callback(total_packets, success_count, error_count) + callback(total_packets, success_count, error_count, total) error_count = 0 if char == NAK: rubbish = self.ser.read(1024) @@ -302,7 +304,7 @@ class YMODEM(object): char, sequence) error_count += 1 if callable(callback): - callback(total_packets, success_count, error_count) + callback(total_packets, success_count, error_count, total) if error_count > retry: # excessive amounts of retransmissions requested, # abort transfer