diff --git a/src/app.rs b/src/app.rs index 1ad75d5f..e21d8eb6 100644 --- a/src/app.rs +++ b/src/app.rs @@ -32,7 +32,7 @@ use crate::helpers::{load_json, load_toml, save_toml, IsHidden}; use crate::settings::{ButtonScheme, Settings, SETTINGS_PATH, RotationLock}; use crate::frontlight::{Frontlight, StandardFrontlight, NaturalFrontlight, PremixedFrontlight, FakeFrontlight}; use crate::lightsensor::{LightSensor, KoboLightSensor}; -use crate::battery::{Battery, FakeBattery}; +use crate::battery::{Battery, RemarkableBattery}; use crate::geom::{Rectangle, Edge}; use crate::view::home::Home; use crate::view::reader::Reader; @@ -243,7 +243,7 @@ fn build_context(fb: Box) -> Result { let fonts = Fonts::load().context("Can't load fonts.")?; - let battery = Box::new(FakeBattery::new()) as Box; + let battery = Box::new(RemarkableBattery::new().context("Can't create battery.")?) as Box; let lightsensor = if CURRENT_DEVICE.has_lightsensor() { Box::new(KoboLightSensor::new().context("Can't create light sensor.")?) as Box diff --git a/src/battery/mod.rs b/src/battery/mod.rs index c876c094..4a76bb9a 100644 --- a/src/battery/mod.rs +++ b/src/battery/mod.rs @@ -1,10 +1,12 @@ mod kobo; mod fake; +mod remarkable; use anyhow::Error; pub use self::kobo::KoboBattery; pub use self::fake::FakeBattery; +pub use self::remarkable::RemarkableBattery; #[derive(Debug, Copy, Clone, Eq, PartialEq)] pub enum Status { diff --git a/src/battery/remarkable.rs b/src/battery/remarkable.rs new file mode 100644 index 00000000..8eb3cc2b --- /dev/null +++ b/src/battery/remarkable.rs @@ -0,0 +1,47 @@ +use std::io::{Read, Seek, SeekFrom}; +use std::fs::File; +use std::path::Path; +use super::{Battery, Status}; +use anyhow::{Error, format_err}; + +const BATTERY_INTERFACE: &str = "/sys/class/power_supply/bq27441-0"; + +const BATTERY_CAPACITY: &str = "capacity"; +const BATTERY_STATUS: &str = "status"; + +// TODO: health, technology, time_to_full_now, time_to_empty_now +pub struct RemarkableBattery { + capacity: File, + status: File, +} + +impl RemarkableBattery { + pub fn new() -> Result { + let base = Path::new(BATTERY_INTERFACE); + let capacity = File::open(base.join(BATTERY_CAPACITY))?; + let status = File::open(base.join(BATTERY_STATUS))?; + Ok(RemarkableBattery { capacity, status }) + } +} + +impl Battery for RemarkableBattery { + fn capacity(&mut self) -> Result { + let mut buf = String::new(); + self.capacity.seek(SeekFrom::Start(0))?; + self.capacity.read_to_string(&mut buf)?; + Ok(buf.trim_end().parse::().unwrap_or(0.0)) + } + + fn status(&mut self) -> Result { + let mut buf = String::new(); + self.status.seek(SeekFrom::Start(0))?; + self.status.read_to_string(&mut buf)?; + match buf.trim_end() { + "Discharging" => Ok(Status::Discharging), + "Charging" => Ok(Status::Charging), + "Not charging" | "Full" => Ok(Status::Charged), + _ => Err(format_err!("Unknown battery status.")), + + } + } +}