forked from baskerville/plato
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
51 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.")), | ||
|
||
} | ||
} | ||
} |