Compare commits
No commits in common. "cd15065f267f5d8d89a61ac65689bc1cc88f3f3b" and "dccb1ccf9ff5c6b5af8fce1fd004b12411e16e78" have entirely different histories.
cd15065f26
...
dccb1ccf9f
8
Cargo.toml
Normal file
8
Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "linux_battery"
|
||||
version = "0.1.0"
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
30
README.md
30
README.md
@ -1,2 +1,32 @@
|
||||
# linux_battery
|
||||
|
||||
A battery parser using linux' `power_supply` class.
|
||||
|
||||
## Build
|
||||
|
||||
### debug
|
||||
|
||||
```shell
|
||||
cargo build
|
||||
```
|
||||
|
||||
### release
|
||||
|
||||
```shell
|
||||
cargo build --release
|
||||
```
|
||||
|
||||
## Documentation
|
||||
|
||||
```shell
|
||||
cargo doc
|
||||
```
|
||||
|
||||
The built documentation can be found at `target/doc/`.
|
||||
|
||||
## Example
|
||||
|
||||
```shell
|
||||
cargo build --example bat0
|
||||
cargo run --example bat0
|
||||
```
|
||||
|
11
examples/bat0.rs
Normal file
11
examples/bat0.rs
Normal file
@ -0,0 +1,11 @@
|
||||
use linux_battery;
|
||||
|
||||
fn main() {
|
||||
let mut battery = linux_battery::Battery::new("BAT0");
|
||||
let mut msg = format!("Status: {}", battery.state());
|
||||
match battery.capacity() {
|
||||
Some(cap) => msg = format!("{}\tCapacity: {} %", msg, cap),
|
||||
None => {},
|
||||
}
|
||||
println!("{}", msg);
|
||||
}
|
78
src/lib.rs
Normal file
78
src/lib.rs
Normal file
@ -0,0 +1,78 @@
|
||||
use std::io;
|
||||
use std::io::Read;
|
||||
|
||||
/// Battery parser using linux' `power_supply` class
|
||||
#[allow(dead_code)]
|
||||
pub struct Battery {
|
||||
state_file: String,
|
||||
capacity_file: String,
|
||||
}
|
||||
|
||||
impl Battery {
|
||||
/// Returns a battery representing object.
|
||||
/// # Arguments:
|
||||
/// * `name` - The name of the battery as it is represented in the sys file system.
|
||||
/// (`/sys/class/power_supply/<name>`)
|
||||
/// # Example:
|
||||
/// ```
|
||||
/// mod battery;
|
||||
/// use battery::Battery;
|
||||
/// let mut battery = Battery::new("BAT0");
|
||||
/// ```
|
||||
#[allow(dead_code)]
|
||||
pub fn new(name: &str) -> Battery {
|
||||
let path: String = format!("/sys/class/power_supply/{}/", name);
|
||||
Battery {
|
||||
state_file: format!("{}status", path),
|
||||
capacity_file: format!("{}capacity", path),
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the current state of the battery.
|
||||
/// Return values:
|
||||
/// * `"Unknown"`
|
||||
/// * `"Charging"`
|
||||
/// * `"Discharging"`
|
||||
/// * `"Not_Charging"`
|
||||
/// * `"Full"`
|
||||
/// # Example:
|
||||
/// ```
|
||||
/// println!("Battery state: {}", battery.state());
|
||||
/// ```
|
||||
#[allow(dead_code)]
|
||||
pub fn state(&mut self) -> String {
|
||||
match Battery::read_file(&self.state_file) {
|
||||
Ok(status) => String::from(status.trim()),
|
||||
Err(_) => String::from("Unknown"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the current capacity of the battery [%].
|
||||
/// # Example:
|
||||
/// ```
|
||||
/// match battery.capacity() {
|
||||
/// Some(cap) => println!("{} %", cap),
|
||||
/// None => {},
|
||||
/// }
|
||||
/// ```
|
||||
#[allow(dead_code)]
|
||||
pub fn capacity(&mut self) ->Option<u8> {
|
||||
match Battery::read_file(&self.capacity_file) {
|
||||
Ok(capacity) => Some(capacity.trim().parse::<u8>().unwrap_or(0)),
|
||||
Err(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn read_file(name: &str) -> Result<String, io::Error> {
|
||||
let f = std::fs::File::open(name);
|
||||
let mut f = match f {
|
||||
Ok(file) => file,
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
let mut line = String::new();
|
||||
match f.read_to_string(&mut line) {
|
||||
Ok(_) => Ok(line),
|
||||
Err(e) =>Err(e),
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user