starship / rust-battery

Rust crate providing cross-platform information about the notebook batteries.
https://crates.io/crates/starship-battery
ISC License
21 stars 8 forks source link

fix: use correct data source for querying max capacity on arm64-macOS #33

Closed WenqingZong closed 1 year ago

WenqingZong commented 1 year ago

"MaxCapacity" and "CurrentCapacity" returns value in percentage, range from 0 to 100, which causes bug in calculating current capacity and max capacity.

I have no idea if this bug affects Intel Mac too, so my modification is a bit conservative: only fix it for aarch64, i.e, M series chips.

The experiment is done in M1 MacBook Air:

Before this fix:

(on fix_for_macos ✹)──> cargo run --bin=example --release                                                                                                                                                
   Compiling starship-battery v0.8.0
    Finished release [optimized] target(s) in 0.71s
     Running `target/release/example`
[examples/simple.rs:19] &battery = Battery {
    impl: MacOSDevice {
        source: PowerSource {
            io_object: IoObject(
                3075,
            ),
        },
    },
    vendor: None,
    model: Some(
        "bq20z451",
    ),
    serial_number: None,
    technology: Unknown,
    state: Unknown,
    capacity: 0.022820631,
    temperature: Some(
        303.69998 K^1,
    ),
    percentage: 0.8,
    cycle_count: Some(
        12,
    ),
    energy: 3494.5923 m^2 kg^1 s^-2,
    energy_full: 4368.24 m^2 kg^1 s^-2,
    energy_full_design: 191416.28 m^2 kg^1 s^-2,
    energy_rate: 0.0 m^2 kg^1 s^-3,
    voltage: 12.134001 m^2 kg^1 s^-3 A^-1,
    time_to_full: None,
    time_to_empty: None,
}
[examples/simple.rs:20] battery.state_of_charge() = 0.8
[examples/simple.rs:21] battery.state_of_health() = 0.022820631

Note the energy energy_full are just a few thousand joule, and state of health is shown as 0.02, which is impossible.

After this fix:

(on fix_for_macos ✹)──> cargo run --bin=example --release                                                                                                                                          
   Compiling starship-battery v0.8.0
    Finished release [optimized] target(s) in 0.74s
     Running `target/release/example`
[examples/simple.rs:19] &battery = Battery {
    impl: MacOSDevice {
        source: PowerSource {
            io_object: IoObject(
                7427,
            ),
        },
    },
    vendor: None,
    model: Some(
        "bq20z451",
    ),
    serial_number: None,
    technology: Unknown,
    state: Unknown,
    capacity: 0.99817437,
    temperature: Some(
        303.69 K^1,
    ),
    percentage: 0.7556013,
    cycle_count: Some(
        12,
    ),
    energy: 144382.23 m^2 kg^1 s^-2,
    energy_full: 191082.56 m^2 kg^1 s^-2,
    energy_full_design: 191432.05 m^2 kg^1 s^-2,
    energy_rate: 0.0 m^2 kg^1 s^-3,
    voltage: 12.135 m^2 kg^1 s^-3 A^-1,
    time_to_full: None,
    time_to_empty: None,
}
[examples/simple.rs:20] battery.state_of_charge() = 0.7556013
[examples/simple.rs:21] battery.state_of_health() = 0.99817437
WenqingZong commented 1 year ago

For more discussion, please see: https://github.com/ClementTsang/bottom/issues/1164

WenqingZong commented 1 year ago

Hi, I'm wondering when this will be merged?

davidkna commented 1 year ago

Thanks for the PR!