guh
This commit is contained in:
@@ -2,6 +2,7 @@ use std::{f32, fs};
|
||||
|
||||
use crate::data::{BatteryMutable, BatteryStatic};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Battery {
|
||||
charge: i64,
|
||||
total_capacity: i64,
|
||||
@@ -20,22 +21,23 @@ impl Battery {
|
||||
Err(_) => return Err(Battery_Error::NoBattery),
|
||||
}
|
||||
|
||||
let energy_full: String =
|
||||
let mut energy_full: String =
|
||||
fs::read_to_string("/sys/class/power_supply/BAT0/energy_full").unwrap();
|
||||
let energy_full = energy_full.trim();
|
||||
|
||||
Ok(Battery {
|
||||
charge: 0,
|
||||
total_capacity: energy_full.parse().unwrap(),
|
||||
})
|
||||
}
|
||||
fn update(&mut self) -> Result<(), Battery_Error> {
|
||||
pub fn update(&mut self) -> Result<(), Battery_Error> {
|
||||
let charge = fs::read_to_string("/sys/class/power_supply/BAT0/energy_now").unwrap();
|
||||
|
||||
self.charge = charge.parse().unwrap();
|
||||
self.charge = charge.trim().parse().unwrap();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
fn as_data(&self) -> (BatteryStatic, BatteryMutable) {
|
||||
pub fn as_data(&self) -> (BatteryStatic, BatteryMutable) {
|
||||
let batt_static = BatteryStatic {
|
||||
total_capacity: self.total_capacity,
|
||||
};
|
||||
@@ -44,4 +46,8 @@ impl Battery {
|
||||
};
|
||||
(batt_static, batt_muteable)
|
||||
}
|
||||
pub fn percentage(&self) -> f64 {
|
||||
let pec: f64 = (self.charge as f64 / self.total_capacity as f64);
|
||||
return pec;
|
||||
}
|
||||
}
|
||||
|
||||
11
src/main.rs
11
src/main.rs
@@ -1,7 +1,14 @@
|
||||
use std::{thread::sleep, time::Duration};
|
||||
|
||||
mod battery;
|
||||
mod data;
|
||||
|
||||
fn main() {
|
||||
let battery = battery::Battery::new();
|
||||
battery.unwrap();
|
||||
let mut battery = battery::Battery::new().unwrap();
|
||||
loop {
|
||||
battery.update().unwrap();
|
||||
println!("{:?}", battery);
|
||||
println!("{:?}", battery.percentage());
|
||||
sleep(Duration::from_secs(3));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user