Skip to content

Commit

Permalink
Add battery
Browse files Browse the repository at this point in the history
  • Loading branch information
LinusCDE committed Aug 3, 2020
1 parent 5e27920 commit aba1cd7
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -243,7 +243,7 @@ fn build_context(fb: Box<dyn Framebuffer>) -> Result<Context, Error> {

let fonts = Fonts::load().context("Can't load fonts.")?;

let battery = Box::new(FakeBattery::new()) as Box<dyn Battery>;
let battery = Box::new(RemarkableBattery::new().context("Can't create battery.")?) as Box<dyn Battery>;

let lightsensor = if CURRENT_DEVICE.has_lightsensor() {
Box::new(KoboLightSensor::new().context("Can't create light sensor.")?) as Box<dyn LightSensor>
Expand Down
2 changes: 2 additions & 0 deletions src/battery/mod.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
47 changes: 47 additions & 0 deletions src/battery/remarkable.rs
Original file line number Diff line number Diff line change
@@ -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<RemarkableBattery, Error> {
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<f32, Error> {
let mut buf = String::new();
self.capacity.seek(SeekFrom::Start(0))?;
self.capacity.read_to_string(&mut buf)?;
Ok(buf.trim_end().parse::<f32>().unwrap_or(0.0))
}

fn status(&mut self) -> Result<Status, Error> {
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.")),

}
}
}

0 comments on commit aba1cd7

Please sign in to comment.