From 6552b9837ff7abb2824f10a67405119986b7785d Mon Sep 17 00:00:00 2001 From: Linus Date: Sun, 24 Oct 2021 01:08:43 +0200 Subject: [PATCH] Add battery indicator --- src/main.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/main.rs b/src/main.rs index 6631765..b6d3953 100644 --- a/src/main.rs +++ b/src/main.rs @@ -306,6 +306,10 @@ fn main() { }; let frame_duration = Duration::from_micros(1000000 / max_fps); + let battery_indicator_update_interval = Duration::from_secs(30); + let mut last_battery_indicator_update = Instant::now() - battery_indicator_update_interval; + let mut last_battery_percentage = -99; + loop { // Limit fps let elapsed = last_frame_drawn.elapsed(); @@ -320,6 +324,37 @@ fn main() { continue; } + // Battery indicator in corner + if last_battery_indicator_update.elapsed() > battery_indicator_update_interval { + last_battery_indicator_update = Instant::now(); + let percentage = libremarkable::battery::percentage().unwrap_or(-1); + if percentage != last_battery_percentage { + last_battery_percentage = percentage; + + let text = format!("{}% ", percentage); // Spaces to prevent residual text when text gets narrower + let rect = fb.draw_text( + Point2 { + x: 10.0, + y: (common::DISPLAYHEIGHT - 10) as f32, + }, + &text, + 30f32, + common::color::BLACK, + false, + ); + fb.partial_refresh( + &rect, + PartialRefreshMode::Async, + common::waveform_mode::WAVEFORM_MODE_GC16_FAST, + common::display_temp::TEMP_USE_MAX, + common::dither_mode::EPDC_FLAG_USE_REMARKABLE_DITHER, + 0, + false, + ); + debug!("Updated battery indicator"); + } + } + let rgb_img = &image.lock().unwrap().clone(); let start = Instant::now(); // Downscale 2x (doomgeneric does a simple upscale anyways, so no data lost)