From 39658e3d6fb7d3b31d6edff5d9eb9edf0ede17f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Sun, 8 Oct 2023 16:01:34 +0200 Subject: [PATCH] Add a different charging icon --- gui/src/screens/mod.rs | 23 ++++++++- gui/src/widgets/battery_small.rs | 63 ++++++++++++++++-------- src/board/drivers/battery_monitor/mod.rs | 14 ++++-- src/board/initialized.rs | 2 +- 4 files changed, 76 insertions(+), 26 deletions(-) diff --git a/gui/src/screens/mod.rs b/gui/src/screens/mod.rs index ac900dae..cd629776 100644 --- a/gui/src/screens/mod.rs +++ b/gui/src/screens/mod.rs @@ -59,10 +59,31 @@ pub const BOTTOM_CENTERED_TEXTBOX: TextBoxStyle = TextBoxStyleBuilder::new() pub const NORMAL_TEXT: MonoTextStyle<'static, BinaryColor> = MonoTextStyle::new(&FONT_6X10, BinaryColor::On); +#[derive(Clone, Copy, PartialEq)] +pub enum ChargingState { + Discharging, + Plugged, + Charging, +} + #[derive(Clone, Copy, PartialEq)] pub struct BatteryInfo { pub voltage: u16, pub percentage: u8, - pub is_charging: bool, + pub charging_state: ChargingState, pub is_low: bool, } + +impl BatteryInfo { + pub fn is_charging(&self) -> bool { + self.charging_state == ChargingState::Charging + } + + pub fn is_discharging(&self) -> bool { + self.charging_state == ChargingState::Discharging + } + + pub fn is_plugged(&self) -> bool { + self.charging_state != ChargingState::Discharging + } +} diff --git a/gui/src/widgets/battery_small.rs b/gui/src/widgets/battery_small.rs index 08885657..3753551f 100644 --- a/gui/src/widgets/battery_small.rs +++ b/gui/src/widgets/battery_small.rs @@ -2,7 +2,7 @@ use embedded_graphics::{ geometry::AnchorPoint, image::{Image, ImageRaw}, pixelcolor::BinaryColor, - prelude::{DrawTarget, DrawTargetExt, Point, Size}, + prelude::{DrawTarget, DrawTargetExt, OriginDimensions, Point, Size}, primitives::{Line, Primitive, PrimitiveStyle, Rectangle}, text::{renderer::TextRenderer, Alignment, Baseline, Text, TextStyleBuilder}, Drawable, @@ -13,7 +13,7 @@ use embedded_menu::items::select::SelectValue; use norfs::storable::{LoadError, Loadable, Storable}; use ufmt::uwrite; -use crate::screens::{BatteryInfo, NORMAL_TEXT}; +use crate::screens::{BatteryInfo, ChargingState, NORMAL_TEXT}; #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] pub enum BatteryStyle { @@ -97,25 +97,48 @@ impl BatteryStyle { &self, target: &mut D, battery_data_width: u32, + state: ChargingState, ) -> Result<(), D::Error> { - #[rustfmt::skip] #[allow(clippy::unusual_byte_groupings)] - const DATA: &[u8] = &[ - 0b010100_00, - 0b010100_00, - 0b111110_00, - 0b011100_00, - 0b011100_00, - 0b001000_00, - 0b001000_00, - 0b010000_00, - ]; - const IMAGE_WIDTH: u32 = 6; - let raw_image = ImageRaw::::new(DATA, IMAGE_WIDTH); + const PLUGGED_IMAGE: ImageRaw<'_, BinaryColor> = ImageRaw::::new( + &[ + 0b010100_00, + 0b010100_00, + 0b111110_00, + 0b011100_00, + 0b011100_00, + 0b001000_00, + 0b001000_00, + 0b010000_00, + ], + 6, + ); + #[allow(clippy::unusual_byte_groupings)] + const CHARGING_IMAGE: ImageRaw<'_, BinaryColor> = ImageRaw::::new( + &[ + 0b000000_00, + 0b000100_00, + 0b001000_00, + 0b011000_00, + 0b001100_00, + 0b001000_00, + 0b010000_00, + 0b000000_00, + ], + 6, + ); + + let raw_image = match state { + ChargingState::Discharging => return Ok(()), + ChargingState::Plugged => &PLUGGED_IMAGE, + ChargingState::Charging => &CHARGING_IMAGE, + }; + let image = Image::new( - &raw_image, + raw_image, Point::new( - (target.bounding_box().size.width - battery_data_width - IMAGE_WIDTH) as i32, + (target.bounding_box().size.width - battery_data_width - raw_image.size().width) + as i32, 0, ), ); @@ -141,7 +164,7 @@ impl BatteryStyle { self.draw_text(target, &string)? } - BatteryStyle::LowIndicator if !data.is_charging => { + BatteryStyle::LowIndicator if !data.is_charging() => { if data.percentage < 25 { let top_right = target.bounding_box().anchor_point(AnchorPoint::TopRight); let box_top_left = self.draw_battery_outline(target, top_right)?; @@ -177,9 +200,7 @@ impl BatteryStyle { } }; - if data.is_charging { - self.draw_charging_indicator(target, battery_data_width)?; - } + self.draw_charging_indicator(target, battery_data_width, data.charging_state)?; Ok(()) } diff --git a/src/board/drivers/battery_monitor/mod.rs b/src/board/drivers/battery_monitor/mod.rs index 2bde7ead..8735b282 100644 --- a/src/board/drivers/battery_monitor/mod.rs +++ b/src/board/drivers/battery_monitor/mod.rs @@ -2,7 +2,7 @@ use crate::{task_control::TaskController, Shared, SharedGuard}; use alloc::rc::Rc; use embassy_sync::mutex::Mutex; use embedded_hal::digital::InputPin; -use gui::screens::BatteryInfo; +use gui::screens::{BatteryInfo, ChargingState}; #[cfg(feature = "battery_adc")] pub mod battery_adc; @@ -87,6 +87,14 @@ impl BatteryMonitor { .map(|data| self.convert_battery_data(data)) } + pub fn charging_state(&self) -> ChargingState { + match (self.is_plugged(), self.is_charging()) { + (_, true) => ChargingState::Charging, + (true, false) => ChargingState::Plugged, + (false, false) => ChargingState::Discharging, + } + } + pub fn is_plugged(&self) -> bool { unwrap!(self.vbus_detect.is_high().ok()) } @@ -132,7 +140,7 @@ impl BatteryMonitor { BatteryInfo { voltage: data.voltage, - is_charging: self.is_charging(), + charging_state: self.charging_state(), percentage, is_low: percentage < LOW_BATTERY_PERCENTAGE, } @@ -144,7 +152,7 @@ impl BatteryMonitor { pub fn convert_battery_data(&self, data: BatteryData) -> BatteryInfo { BatteryInfo { voltage: data.voltage, - is_charging: self.is_charging(), + charging_state: self.charging_state(), percentage: data.percentage, is_low: data.percentage < LOW_BATTERY_PERCENTAGE, } diff --git a/src/board/initialized.rs b/src/board/initialized.rs index cc7ece00..f09ce436 100644 --- a/src/board/initialized.rs +++ b/src/board/initialized.rs @@ -121,7 +121,7 @@ impl Board { pub fn can_enable_wifi(&mut self) -> bool { self.battery_monitor .battery_data() - .map(|battery| battery.percentage > 50 || battery.is_charging) + .map(|battery| battery.percentage > 50 || battery.is_charging()) .unwrap_or(false) }