Skip to content

Commit 8319a2d

Browse files
committed
chromium_ec: Add ReducedPowerInfo
For when PowerInfo has too many fields and you want to compare only the main information to see if the battery state has changed. Signed-off-by: Daniel Schaefer <dhs@frame.work>
1 parent 0b648b8 commit 8319a2d

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

framework_lib/src/power.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,40 @@ pub struct PowerInfo {
9797
pub battery: Option<BatteryInformation>,
9898
}
9999

100+
#[derive(Debug, Clone, PartialEq, Eq)]
101+
pub struct ReducedBatteryInformation {
102+
pub cycle_count: u32,
103+
pub charge_percentage: u32, // Calculated based on Remaining Capacity / LFCC
104+
pub charging: bool,
105+
}
106+
107+
/// Reduced version of PowerInfo
108+
///
109+
/// Usually you won't need all of the fields.
110+
/// Some of them (e.g. present_voltage) will vary with a high frequency, so it's not good to
111+
/// compare two PowerInfo structs to see whether the battery status has changed.
112+
#[derive(Debug, Clone, PartialEq, Eq)]
113+
pub struct ReducedPowerInfo {
114+
pub ac_present: bool,
115+
pub battery: Option<ReducedBatteryInformation>,
116+
}
117+
impl From<PowerInfo> for ReducedPowerInfo {
118+
fn from(val: PowerInfo) -> Self {
119+
ReducedPowerInfo {
120+
ac_present: val.ac_present,
121+
battery: if let Some(b) = val.battery {
122+
Some(ReducedBatteryInformation {
123+
cycle_count: b.cycle_count,
124+
charge_percentage: b.charge_percentage,
125+
charging: b.charging,
126+
})
127+
} else {
128+
None
129+
},
130+
}
131+
}
132+
}
133+
100134
fn read_string(ec: &CrosEc, address: u16) -> String {
101135
let bytes = ec.read_memory(address, EC_MEMMAP_TEXT_MAX).unwrap();
102136
String::from_utf8_lossy(bytes.as_slice()).replace(|c: char| c == '\0', "")

0 commit comments

Comments
 (0)