Skip to content
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

Added progress bar to programmer.py. #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
43 changes: 40 additions & 3 deletions programmer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import serial
import sys
import xmodem
import os
import math

parser = argparse.ArgumentParser(description='Mimas V2 Programmer')
parser.add_argument('--uart', help='Path to UART device', default='/dev/ttyACM0')
Expand Down Expand Up @@ -55,17 +57,52 @@ def xmodem_getc(size, timeout=1):

def xmodem_putc(data, timeout=1):
port.timeout = timeout
print('.', end='', flush=True)
#print('Tx:', data)
return port.write(data)

# Print iterations progress
# From: https://stackoverflow.com/a/34325723/39648
def print_progress_bar(iteration, total, prefix = '', suffix = '', decimals = 1, length = 100, fill = '█', printEnd = "\r"):
"""
Call in a loop to create terminal progress bar
@params:
iteration - Required : current iteration (Int)
total - Required : total iterations (Int)
prefix - Optional : prefix string (Str)
suffix - Optional : suffix string (Str)
decimals - Optional : positive number of decimals in percent complete (Int)
length - Optional : character length of bar (Int)
fill - Optional : bar fill character (Str)
printEnd - Optional : end character (e.g. "\r", "\r\n") (Str)
"""
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
filledLength = int(length * iteration // total)
bar = fill * filledLength + '-' * (length - filledLength)
print(f'\r{prefix} |{bar}| {percent}% {suffix}', end = printEnd)
# Print New Line on Complete
if iteration == total:
print()

print('Sending {}'.format(args.filename))
modem = xmodem.XMODEM(xmodem_getc, xmodem_putc, mode=args.protocol)
port.write(b'f\r')
port.read(3)

with open(args.filename, 'rb') as stream:
if modem.send(stream):
try:
packet_size = dict(
xmodem = 128,
xmodem1k = 1024,
)[args.protocol]
except KeyError:
raise ValueError(f"Invalid mode specified: {args.protocol}")

file_size = os.stat(args.filename).st_size
num_packets = int(math.ceil(file_size / packet_size))

def report_progress(total_packets, success_count, error_count):
print_progress_bar(success_count, num_packets, prefix = 'Progress:', suffix = 'Complete')

if modem.send(stream, callback=report_progress):
print()
print('Done.')
else:
Expand Down