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

Align Saving, Loading and Verifying progress bars #170

Merged
merged 5 commits into from
Nov 19, 2024
Merged
Changes from 4 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
26 changes: 19 additions & 7 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ typedef map<enum picoboot_device_result,vector<tuple<model_t, void *, void *>>>

auto memory_names = map<enum memory_type, string>{
{memory_type::sram, "RAM"},
{memory_type::sram_unstriped, "Unstriped RAM"},
{memory_type::flash, "Flash"},
{memory_type::xip_sram, "XIP RAM"},
{memory_type::rom, "ROM"}
Expand Down Expand Up @@ -3923,14 +3922,23 @@ static picoboot::connection get_single_rp2350_bootsel_device_connection(device_m

struct progress_bar {
explicit progress_bar(string prefix, int width = 30) : prefix(std::move(prefix)), width(width) {
// Align all bars with the longest possible prefix string
auto longest_mem = std::max_element(
std::begin(memory_names), std::end(memory_names),
[] (const auto & p1, const auto & p2) {
return p1.second.length() < p2.second.length();
}
);
longest_mem_str = longest_mem->second;
progress(0);
}

void progress(int _percent) {
if (_percent != percent) {
percent = _percent;
unsigned int len = (width * percent) / 100;
std::cout << prefix << "[" << string(len, '=') << string(width-len, ' ') << "] " << std::to_string(percent) << "%\r" << std::flush;
string extra_space(string("Loading into " + longest_mem_str + ": ").length() - prefix.length(), ' ');
lurch marked this conversation as resolved.
Show resolved Hide resolved
std::cout << prefix << extra_space << "[" << string(len, '=') << string(width-len, ' ') << "] " << std::to_string(percent) << "%\r" << std::flush;
}
}

Expand All @@ -3945,6 +3953,7 @@ struct progress_bar {
std::string prefix;
int percent = -1;
int width;
std::string longest_mem_str;
};

#if HAS_LIBUSB
Expand Down Expand Up @@ -3991,6 +4000,9 @@ bool save_command::execute(device_map &devices) {
} else {
start = settings.from;
end = settings.to;
// Set offset for verifying
settings.offset = start;
settings.offset_set = true;
}
if (end <= start) {
fail(ERROR_ARGS, "Save range is invalid/empty");
Expand Down Expand Up @@ -4029,7 +4041,7 @@ bool save_command::execute(device_map &devices) {
model_t model = get_model(raw_access);
enum memory_type t1 = get_memory_type(start , model);
enum memory_type t2 = get_memory_type(end, model);
if (t1 == invalid || t1 != t2) {
if (t1 != t2 || t1 == invalid || t1 == sram_unstriped) {
fail(ERROR_NOT_POSSIBLE, "Save range crosses unmapped memory");
}
uint32_t size = end - start;
Expand Down Expand Up @@ -4106,7 +4118,7 @@ bool save_command::execute(device_map &devices) {
enum memory_type type = get_memory_type(mem_range.from, model);
bool ok = true;
{
progress_bar bar("Verifying " + memory_names[type] + ": ");
progress_bar bar("Verifying " + memory_names[type] + ": ");
uint32_t batch_size = FLASH_SECTOR_ERASE_SIZE;
vector<uint8_t> file_buf;
vector<uint8_t> device_buf;
Expand Down Expand Up @@ -4264,7 +4276,7 @@ bool load_guts(picoboot::connection con, iostream_memory_access &file_access) {
for (auto mem_range : ranges) {
enum memory_type t1 = get_memory_type(mem_range.from, model);
enum memory_type t2 = get_memory_type(mem_range.to, model);
if (t1 != t2 || t1 == invalid || t1 == rom) {
if (t1 != t2 || t1 == invalid || t1 == rom || t1 == sram_unstriped) {
fail(ERROR_FORMAT, "File to load contained an invalid memory range 0x%08x-0x%08x", mem_range.from,
mem_range.to);
}
Expand Down Expand Up @@ -4355,7 +4367,7 @@ bool load_guts(picoboot::connection con, iostream_memory_access &file_access) {
if (settings.load.verify) {
bool ok = true;
{
progress_bar bar("Verifying " + memory_names[type] + ": ");
progress_bar bar("Verifying " + memory_names[type] + ": ");
uint32_t batch_size = FLASH_SECTOR_ERASE_SIZE;
vector<uint8_t> file_buf;
vector<uint8_t> device_buf;
Expand Down Expand Up @@ -4926,7 +4938,7 @@ bool verify_command::execute(device_map &devices) {
for (auto mem_range : ranges) {
enum memory_type t1 = get_memory_type(mem_range.from, model);
enum memory_type t2 = get_memory_type(mem_range.to, model);
if (t1 != t2 || t1 == invalid) {
if (t1 != t2 || t1 == invalid || t1 == sram_unstriped) {
fail(ERROR_NOT_POSSIBLE, "invalid memory range for verification %08x-%08x", mem_range.from, mem_range.to);
} else {
bool ok = true;
Expand Down
Loading