fix bugs in progress bar computation

This commit is contained in:
bunnie 2022-01-04 21:57:09 +08:00
parent 5129b42dd9
commit 55abbd3f3e
2 changed files with 7 additions and 4 deletions

View file

@ -137,6 +137,7 @@ fn xmain() -> ! {
end_work = config.end_work;
last_percentage = compute_checked_percentage(
config.current_work, start_work, end_work);
log::info!("init percentage: {}, current: {}, start: {}, end: {}", last_percentage, config.current_work, start_work, end_work);
progress_action.set_state(last_percentage);
renderer_modal.modify(
@ -191,7 +192,8 @@ fn xmain() -> ! {
Some(RendererOp::UpdateProgress) => msg_scalar_unpack!(msg, current, _, _, _, {
let new_percentage = compute_checked_percentage(
current as u32, start_work, end_work);
if new_percentage != last_percentage {
log::trace!("percentage: {}, current: {}, start: {}, end: {}", new_percentage, current, start_work, end_work);
if new_percentage != last_percentage {
last_percentage = new_percentage;
progress_action.set_state(last_percentage);
@ -527,7 +529,8 @@ fn compute_checked_percentage(current: u32, start: u32, end: u32) -> u32 {
} else if current >= end {
100
} else {
(current * 100) / (end - start)
// do math in higher precision because we could overflow a u32
(((current as u64 - start as u64) * 100) / (end as u64 - start as u64)) as u32
}
}
}

View file

@ -28,8 +28,8 @@ pub(crate) fn spawn_test() {
let tt = ticktimer_server::Ticktimer::new().unwrap();
// test progress bar
modals.start_progress("Progress Quest", 100, 500, 100).expect("couldn't raise progress bar");
for i in (100..500).step_by(8) {
modals.start_progress("Progress Quest", xous::PDDB_LOC, xous::PDDB_LOC + 64*1024*128, xous::PDDB_LOC).expect("couldn't raise progress bar");
for i in (xous::PDDB_LOC..xous::PDDB_LOC + 64*1024*128).step_by(64*1024) {
modals.update_progress(i).expect("couldn't update progress bar");
tt.sleep_ms(100).unwrap();
}