Skip to content

Commit

Permalink
Add a different charging icon
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Oct 8, 2023
1 parent b641875 commit 39658e3
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 26 deletions.
23 changes: 22 additions & 1 deletion gui/src/screens/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
63 changes: 42 additions & 21 deletions gui/src/widgets/battery_small.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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::<BinaryColor>::new(DATA, IMAGE_WIDTH);
const PLUGGED_IMAGE: ImageRaw<'_, BinaryColor> = ImageRaw::<BinaryColor>::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::<BinaryColor>::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,
),
);
Expand All @@ -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)?;
Expand Down Expand Up @@ -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(())
}
Expand Down
14 changes: 11 additions & 3 deletions src/board/drivers/battery_monitor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -87,6 +87,14 @@ impl<VBUS: InputPin, CHG: InputPin> BatteryMonitor<VBUS, CHG> {
.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())
}
Expand Down Expand Up @@ -132,7 +140,7 @@ impl<VBUS: InputPin, CHG: InputPin> BatteryMonitor<VBUS, CHG> {

BatteryInfo {
voltage: data.voltage,
is_charging: self.is_charging(),
charging_state: self.charging_state(),
percentage,
is_low: percentage < LOW_BATTERY_PERCENTAGE,
}
Expand All @@ -144,7 +152,7 @@ impl<VBUS: InputPin, CHG: InputPin> BatteryMonitor<VBUS, CHG> {
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,
}
Expand Down
2 changes: 1 addition & 1 deletion src/board/initialized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit 39658e3

Please sign in to comment.