catch potential div by 0 on status redraw

This commit is contained in:
bunnie 2022-06-27 18:45:02 +08:00
parent 89e7849bc5
commit be7d8a76a5

View file

@ -406,8 +406,9 @@ fn wrapped_main() -> ! {
pump_run.store(true, Ordering::Relaxed); // start status thread updating
loop {
let msg = xous::receive_message(status_sid).unwrap();
log::trace!("|status: Message: {:?}", msg);
match FromPrimitive::from_usize(msg.body.id()) {
let opcode: Option<StatusOpcode> = FromPrimitive::from_usize(msg.body.id());
log::debug!("{:?}", opcode);
match opcode {
Some(StatusOpcode::EnableAutomaticBacklight) => {
*enabled.lock().unwrap() = true;
@ -530,12 +531,16 @@ fn wrapped_main() -> ! {
let (latest_activity, period) = llio
.activity_instantaneous()
.expect("couldn't get CPU activity");
let activity_to_width = ((latest_activity as u64) * 1000u64 * (cpuload_rect.width() as u64 - 4)) / (period as u64 * 1000u64);
let activity_to_width = if period == 0 {
cpuload_rect.width() as i16 - 4
} else {
(((latest_activity as u64) * 1000u64 * (cpuload_rect.width() as u64 - 4)) / (period as u64 * 1000u64)) as i16
};
draw_list.push(GamObjectType::Rect(
Rectangle::new_coords_with_style(
cpuload_rect.tl().x + 2,
cpuload_rect.tl().y + 2,
cpuload_rect.tl().x + 2 + activity_to_width as i16,
cpuload_rect.tl().x + 2 + (activity_to_width).min(cpuload_rect.width() as i16 - 4),
cpuload_rect.br().y - 2,
DrawStyle::new(PixelColor::Dark, PixelColor::Dark, 0))
)).unwrap();