Skip to content

Commit

Permalink
Integrate uom types
Browse files Browse the repository at this point in the history
- Public methods and internal calculations are now using uom types only.
- Rename `Battery::percentage` method into `Battery::state_of_charge`
- Rename `Battery::capacity` method into `Battery::state_of_health`
- Mark `battery::State` and `battery::Technology` enums as a non-exhaustive
- Ignore devices with `scope` attributes different from `System` for Linux [#18](#18)
  • Loading branch information
svartalf committed Mar 6, 2019
1 parent fd03534 commit e77b758
Show file tree
Hide file tree
Showing 46 changed files with 984 additions and 586 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ insert_final_newline = true
charset = utf-8
indent_style = space
indent_size = 4
trim_trailing_whitespace = true
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ matrix:
cache:
- cargo
before_cache:
- chmod -R a+r ${HOME}/.cargo
- chmod -R a+r ${HOME}/.cargo

before_install:
- cargo install --force cross
Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ members = [
"battery-ffi",
"battery-cli",
]

[patch.crates-io]
battery = { path = "./battery" }
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
[![Latest Version](https://img.shields.io/crates/v/battery.svg)](https://crates.io/crates/battery)
[![Latest Version](https://docs.rs/battery/badge.svg)](https://docs.rs/battery)
[![Build Status](https://travis-ci.org/svartalf/rust-battery.svg?branch=master)](https://travis-ci.org/svartalf/rust-battery)
[![dependency status](https://deps.rs/crate/battery/0.6.2/status.svg)](https://deps.rs/crate/battery/0.6.2)
[![dependency status](https://deps.rs/crate/battery/0.7.0/status.svg)](https://deps.rs/crate/battery/0.7.0)
![Apache 2.0 OR MIT licensed](https://img.shields.io/badge/license-Apache2.0%2FMIT-blue.svg)

Rust crate providing cross-platform information about batteries.

Gives access to a system independent battery state, capacity, charge and voltage values
recalculated as necessary to be returned in `mW`, `mWh` or `mV` units.
recalculated as necessary to be returned [SI measurement units](https://www.bipm.org/en/measurement-units/).

## Supported platforms

Expand Down
4 changes: 2 additions & 2 deletions battery-cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "battery-cli"
version = "0.1.3"
version = "0.1.4"
authors = ["svartalf <[email protected]>"]
edition = "2018"
description = "CLI tool for batteries reports"
Expand All @@ -25,7 +25,7 @@ test = false
cfg-if = "0.1"

[target.'cfg(not(windows))'.dependencies]
battery = "0.6.2"
battery = "0.7.0"
humantime = "1.2.0"
tui = "0.4.0"
termion = "1.5.1"
Expand Down
2 changes: 1 addition & 1 deletion battery-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![Latest Version](https://img.shields.io/crates/v/battery-cli.svg)](https://crates.io/crates/battery-cli)
[![Latest Version](https://docs.rs/battery-cli/badge.svg)](https://docs.rs/battery-cli)
[![Build Status](https://travis-ci.org/svartalf/rust-battery.svg?branch=master)](https://travis-ci.org/svartalf/rust-battery)
[![dependency status](https://deps.rs/crate/battery-cli/0.1.3/status.svg)](https://deps.rs/crate/battery-cli/0.1.3)
[![dependency status](https://deps.rs/crate/battery-cli/0.1.4/status.svg)](https://deps.rs/crate/battery-cli/0.1.4)
![Apache 2.0 OR MIT licensed](https://img.shields.io/badge/license-Apache2.0%2FMIT-blue.svg)

`battery-cli` is a Proof-Of-Concept binary crate, that provides terminal user interface
Expand Down
3 changes: 2 additions & 1 deletion battery-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#[macro_use] extern crate cfg_if;
#[macro_use]
extern crate cfg_if;

cfg_if! {
if #[cfg(windows)] {
Expand Down
16 changes: 7 additions & 9 deletions battery-cli/src/ui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

use super::util::tabs;
use super::util::graph;

use battery::Battery;
use battery::units::power::watt;
use battery::units::electric_potential::volt;
use battery::units::thermodynamic_temperature::degree_celsius;

#[derive(Debug)]
pub struct BatteryStats<'b> {
Expand All @@ -12,12 +16,6 @@ pub struct BatteryStats<'b> {
pub temperature_graph: graph::GraphData<'b>,
}

impl<'b> BatteryStats<'b> {
pub fn info_rows(&self) -> Vec<(String, String)> {
vec![]
}
}

#[derive(Debug)]
pub struct App<'a> {
pub manager: battery::Manager,
Expand Down Expand Up @@ -57,10 +55,10 @@ impl<'a> App<'a> {
pub fn update(&mut self) {
for stat in self.batteries.iter_mut() {
let _ = self.manager.refresh(&mut stat.battery);
stat.voltage_graph.push(f64::from(stat.battery.voltage()) / 1_000.0);
stat.energy_rate_graph.push(f64::from(stat.battery.energy_rate()) / 1_000.0);
stat.voltage_graph.push(f64::from(stat.battery.voltage().get::<volt>()));
stat.energy_rate_graph.push(f64::from(stat.battery.energy_rate().get::<watt>()));
if let Some(temp) = stat.battery.temperature() {
stat.temperature_graph.push(f64::from(temp));
stat.temperature_graph.push(f64::from(temp.get::<degree_celsius>()));
}
}
}
Expand Down
28 changes: 18 additions & 10 deletions battery-cli/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

use std::io;
use std::error::Error;
use std::time::Duration;

use termion::input::MouseTerminal;
use termion::screen::AlternateScreen;
Expand All @@ -32,6 +33,13 @@ use tui::style::*;
use tui::widgets::*;

use battery::{Battery, State};
use battery::units::Unit;
use battery::units::power::watt;
use battery::units::time::second;
use battery::units::ratio::percent;
use battery::units::energy::watt_hour;
use battery::units::electric_potential::volt;
use battery::units::thermodynamic_temperature::degree_celsius;

use crate::ui::app::BatteryStats;
use crate::ui::util::event::{Event, Events};
Expand Down Expand Up @@ -131,7 +139,7 @@ fn draw_tabs<B>(f: &mut Frame<B>, area: Rect, tabs: &TabsState) where B: Backend
}

fn draw_percentage_bar<B>(f: &mut Frame<B>, area: Rect, stat: &BatteryStats) where B: Backend {
let value = f64::from(stat.battery.percentage());
let value = f64::from(stat.battery.state_of_charge().get::<percent>());
let block = Block::default()
.title("Percentage")
.borders(Borders::ALL);
Expand Down Expand Up @@ -216,12 +224,12 @@ fn draw_energy_information<B>(f: &mut Frame<B>, area: Rect, battery: &Battery) w
let block = Block::default()
.borders(Borders::LEFT | Borders::RIGHT);

let consumption = &format!("{:.2} W", battery.energy_rate() as f32 / 1000.0);
let voltage = &format!("{:.2} V", battery.voltage() as f32 / 1000.0);
let capacity = &format!("{:.2}%", battery.capacity());
let current = &format!("{:.2} Wh", battery.energy() as f32 / 1000.0);
let last_full = &format!("{:.2} Wh", battery.energy_full() as f32 / 1000.0);
let full_design = &format!("{:.2} Wh", battery.energy_full_design() as f32 / 1000.0);
let consumption = &format!("{:.2} {}", battery.energy_rate().get::<watt>(), watt::abbreviation());
let voltage = &format!("{:.2} {}", battery.voltage().get::<volt>(), volt::abbreviation());
let capacity = &format!("{:.2} {}", battery.state_of_health().get::<percent>(), percent::abbreviation());
let current = &format!("{:.2} {}", battery.energy().get::<watt_hour>(), watt_hour::abbreviation());
let last_full = &format!("{:.2} {}", battery.energy_full().get::<watt_hour>(), watt_hour::abbreviation());
let full_design = &format!("{:.2} {}", battery.energy_full_design().get::<watt_hour>(), watt_hour::abbreviation());
let consumption_label = match battery.state() {
State::Charging => "Charging with",
State::Discharging => "Discharging with",
Expand Down Expand Up @@ -253,12 +261,12 @@ fn draw_time_information<B>(f: &mut Frame<B>, area: Rect, battery: &Battery) whe
.borders(Borders::LEFT | Borders::RIGHT);

let time_to_full = match battery.time_to_full() {
Some(time) => humantime::format_duration(time).to_string(),
Some(time) => humantime::format_duration(Duration::from_secs(time.get::<second>() as u64)).to_string(),
None => "N/A".to_string(),
};

let time_to_empty = match battery.time_to_empty() {
Some(time) => humantime::format_duration(time).to_string(),
Some(time) => humantime::format_duration(Duration::from_secs(time.get::<second>() as u64)).to_string(),
None => "N/A".to_string(),
};
let items = vec![
Expand All @@ -283,7 +291,7 @@ fn draw_env_information<B>(f: &mut Frame<B>, area: Rect, battery: &Battery) wher
.borders(Borders::LEFT | Borders::RIGHT | Borders::BOTTOM);

let temperature = match battery.temperature() {
Some(temp) => format!("{:.2} °C", temp),
Some(temp) => format!("{:.2} {}", temp.get::<degree_celsius>(), degree_celsius::abbreviation()),
None => "N/A".to_string(),
};
let items = vec![
Expand Down
1 change: 1 addition & 0 deletions battery-cli/src/ui/util/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ impl<'g> GraphData<'g> {
[self.y_lower(), self.y_upper()]
}

#[allow(clippy::cast_lossless)]
pub fn push(&mut self, value: f64) {
if self.points.len() == RESOLUTION {
self.points.remove(0);
Expand Down
4 changes: 2 additions & 2 deletions battery-ffi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "battery-ffi"
version = "0.1.5"
version = "0.2.0"
authors = ["svartalf <[email protected]>"]
edition = "2018"
description = "FFI bindings for battery crate"
Expand All @@ -22,7 +22,7 @@ crate-type = ["cdylib"]
default = ["cbindgen"]

[dependencies]
battery = "0.6.2"
battery = "0.7.0"
libc = "0.2.48"

[build-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion battery-ffi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![Latest Version](https://img.shields.io/crates/v/battery-ffi.svg)](https://crates.io/crates/battery-ffi)
[![Latest Version](https://docs.rs/battery-ffi/badge.svg)](https://docs.rs/battery-ffi)
[![Build Status](https://travis-ci.org/svartalf/rust-battery.svg?branch=master)](https://travis-ci.org/svartalf/rust-battery)
[![dependency status](https://deps.rs/crate/battery-ffi/0.1.5/status.svg)](https://deps.rs/crate/battery-ffi/0.1.5)
[![dependency status](https://deps.rs/crate/battery-ffi/0.2.0/status.svg)](https://deps.rs/crate/battery-ffi/0.2.0)
![Apache 2.0 OR MIT licensed](https://img.shields.io/badge/license-Apache2.0%2FMIT-blue.svg)

This is a FFI bindings for [battery](https://github.com/svartalf/rust-battery/tree/master/battery)
Expand Down
24 changes: 10 additions & 14 deletions battery-ffi/examples/ffi.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@

#include "battery_ffi.h"

float from_millis(uint32_t value) {
return (float) value / 1000;
}

void pretty_print(Battery *battery, uint32_t *idx) {
printf("Device:\t\t\t%d\n", *idx);

Expand Down Expand Up @@ -66,11 +62,11 @@ void pretty_print(Battery *battery, uint32_t *idx) {
printf("full\n");
break;
}
printf(" energy:\t\t%.2f Wh\n", from_millis(battery_get_energy(battery)));
printf(" energy-full:\t\t%.2f Wh\n", from_millis(battery_get_energy_full(battery)));
printf(" energy-full-design:\t%.2f Wh\n", from_millis(battery_get_energy_full_design(battery)));
printf(" energy-rate:\t\t%.2f W\n", from_millis(battery_get_energy_rate(battery)));
printf(" voltage:\t\t%.2f V\n", from_millis(battery_get_voltage(battery)));
printf(" energy:\t\t%.2f joule\n", battery_get_energy(battery));
printf(" energy-full:\t\t%.2f joule\n", battery_get_energy_full(battery));
printf(" energy-full-design:\t%.2f joule\n", battery_get_energy_full_design(battery));
printf(" energy-rate:\t\t%.2f W\n", battery_get_energy_rate(battery));
printf(" voltage:\t\t%.2f V\n", battery_get_voltage(battery));

printf(" technology:\t\t");
switch (battery_get_technology(battery)) {
Expand Down Expand Up @@ -105,24 +101,24 @@ void pretty_print(Battery *battery, uint32_t *idx) {

uint64_t time_to_full = battery_get_time_to_full(battery);
if ((state == StateCharging) && (time_to_full > 0)) {
printf(" time-to-full:\t\t%d sec.\n", time_to_full);
printf(" time-to-full:\t\t%ld sec.\n", time_to_full);
}

uint64_t time_to_empty = battery_get_time_to_empty(battery);
if ((state == StateDischarging) && (time_to_empty > 0)) {
printf(" time-to-empty:\t\t%d sec.\n", time_to_empty);
printf(" time-to-empty:\t\t%ld sec.\n", time_to_empty);
}

printf(" percentage:\t\t%.2f %%\n", battery_get_percentage(battery));
printf(" state of charge:\t\t%.2f %%\n", battery_get_state_of_charge(battery));
float temp = battery_get_temperature(battery);
printf(" temperature:\t\t");
if (temp < FLT_MAX) {
printf("%.2f C\n", temp);
printf("%.2f K\n", temp);
} else {
printf("N/A\n");
}

printf(" capacity:\t\t%.2f %%\n", battery_get_capacity(battery));
printf(" state of health:\t\t%.2f %%\n", battery_get_state_of_health(battery));
uint32_t cycle_count = battery_get_cycle_count(battery);
printf(" cycle-count:\t\t");
if (cycle_count < UINT_MAX) {
Expand Down
38 changes: 19 additions & 19 deletions battery-ffi/examples/ffi.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,27 +83,27 @@ class Battery(ctypes.Structure):
lib.battery_get_serial_number.restype = ctypes.c_char_p
lib.battery_get_state.restype = ctypes.c_uint8
lib.battery_get_energy.argtypes = (ctypes.POINTER(Battery), )
lib.battery_get_energy.restype = ctypes.c_uint32
lib.battery_get_energy.restype = ctypes.c_float
lib.battery_get_energy_full.argtypes = (ctypes.POINTER(Battery), )
lib.battery_get_energy_full.restype = ctypes.c_uint32
lib.battery_get_energy_full.restype = ctypes.c_float
lib.battery_get_energy_full_design.argtypes = (ctypes.POINTER(Battery), )
lib.battery_get_energy_full_design.restype = ctypes.c_uint32
lib.battery_get_energy_full_design.restype = ctypes.c_float
lib.battery_get_energy_rate.argtypes = (ctypes.POINTER(Battery), )
lib.battery_get_energy_rate.restype = ctypes.c_uint32
lib.battery_get_energy_rate.restype = ctypes.c_float
lib.battery_get_voltage.argtypes = (ctypes.POINTER(Battery), )
lib.battery_get_voltage.restype = ctypes.c_uint32
lib.battery_get_voltage.restype = ctypes.c_float
lib.battery_get_technology.argtypes = (ctypes.POINTER(Battery), )
lib.battery_get_technology.restype = ctypes.c_uint8
lib.battery_get_time_to_full.argtypes = (ctypes.POINTER(Battery), )
lib.battery_get_time_to_full.restype = ctypes.c_uint64
lib.battery_get_time_to_full.restype = ctypes.c_float
lib.battery_get_time_to_empty.argtypes = (ctypes.POINTER(Battery), )
lib.battery_get_time_to_empty.restype = ctypes.c_uint64
lib.battery_get_percentage.argtypes = (ctypes.POINTER(Battery), )
lib.battery_get_percentage.restype = ctypes.c_float
lib.battery_get_time_to_empty.restype = ctypes.c_float
lib.battery_get_state_of_charge.argtypes = (ctypes.POINTER(Battery), )
lib.battery_get_state_of_charge.restype = ctypes.c_float
lib.battery_get_temperature.argtypes = (ctypes.POINTER(Battery), )
lib.battery_get_temperature.restype = ctypes.c_float
lib.battery_get_capacity.argtypes = (ctypes.POINTER(Battery), )
lib.battery_get_capacity.restype = ctypes.c_float
lib.battery_get_state_of_health.argtypes = (ctypes.POINTER(Battery), )
lib.battery_get_state_of_health.restype = ctypes.c_float
lib.battery_get_cycle_count.argtypes = (ctypes.POINTER(Battery), )
lib.battery_get_cycle_count.restype = ctypes.c_uint32

Expand All @@ -120,16 +120,16 @@ class Battery(ctypes.Structure):
print('S/N', lib.battery_get_serial_number(battery))
print('State', STATE.get(lib.battery_get_state(battery)))
print('Technology', TECHNOLOGY.get(lib.battery_get_technology(battery)))
print('Energy (Wh)', lib.battery_get_energy(battery) / 1000)
print('Energy full (Wh)', lib.battery_get_energy_full_design(battery) / 1000)
print('Energy full design (Wh)', lib.battery_get_energy_full_design(battery) / 1000)
print('Energy rate (W)', lib.battery_get_energy_rate(battery) / 1000)
print('Voltage (V)', lib.battery_get_voltage(battery) / 1000)
print('Energy (joule)', lib.battery_get_energy(battery))
print('Energy full (joule)', lib.battery_get_energy_full_design(battery))
print('Energy full design (joule)', lib.battery_get_energy_full_design(battery))
print('Energy rate (W)', lib.battery_get_energy_rate(battery))
print('Voltage (V)', lib.battery_get_voltage(battery))
print('Time to full (sec)', lib.battery_get_time_to_full(battery))
print('Time to empty (sec)', lib.battery_get_time_to_empty(battery))
print('Percentage (%)', lib.battery_get_percentage(battery))
print('Temperature (C)', lib.battery_get_temperature(battery))
print('Capacity (%)', lib.battery_get_capacity(battery))
print('State of charge (%)', lib.battery_get_state_of_charge(battery))
print('Temperature (K)', lib.battery_get_temperature(battery))
print('State of health (%)', lib.battery_get_state_of_health(battery))
print('Cycle count', lib.battery_get_cycle_count(battery))

lib.battery_free(battery)
Expand Down
Loading

0 comments on commit e77b758

Please sign in to comment.