UpsHatE

Struct UpsHatE 

Source
pub struct UpsHatE { /* private fields */ }
Expand description

Monitor a Waveshare UPS HAT E (Uninterruptible Power Supply model E) for a Raspberry Pi.

This struct can monitor the UPS HAT status, such as battery voltage, current, power, and other interesting information

Implementations§

Source§

impl UpsHatE

Source

pub fn new() -> Self

Create a new instance of the UPS Hat E monitor using the default I2C bus device path and address. This works in most cases.

Examples found in repository?
examples/ups_monitor.rs (line 15)
14fn main() -> Result<(), Box<dyn std::error::Error>> {
15    let mut ups = UpsHatE::new();
16    let mut stdout = io::stdout();
17
18    loop {
19        let battery = ups.get_battery_state()?;
20        let power = ups.get_power_state()?;
21        let vbus = ups.get_usbc_vbus()?;
22        let cells = ups.get_cell_voltage()?;
23
24        print!("{CLEAR_SCREEN}{CURSOR_HOME}");
25
26        let epoch_secs = SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs();
27
28        println!("{BOLD}UPS HAT (E) Monitor{RESET}");
29        println!("═══════════════════════════════════════════");
30        println!("Unix time: {epoch_secs}");
31        println!();
32
33        // Power state
34        println!("{BOLD}Power{RESET}");
35        println!("  State:        {:?}", power.charging_state);
36        println!("  Activity:     {:?}", power.charger_activity);
37        println!("  USB-C In:     {:?}", power.usbc_input_state);
38        println!("  USB-C PD:     {:?}", power.usbc_power_delivery);
39        println!();
40
41        // Battery
42        println!("{BOLD}Battery{RESET}");
43        println!("  Charge:       {}%", battery.remaining_percent);
44        println!("  Voltage:      {} mV", battery.millivolts);
45        println!("  Current:      {} mA", battery.milliamps);
46        println!("  Capacity:     {} mAh", battery.remaining_capacity_milliamphours);
47        if battery.milliamps < 0 {
48            println!("  Est. Runtime: {} min", battery.remaining_runtime_minutes);
49        } else if battery.time_to_full_minutes > 0 {
50            println!("  Time Full:    {} min", battery.time_to_full_minutes);
51        }
52        println!();
53
54        // USB-C VBUS
55        println!("{BOLD}USB-C VBUS{RESET}");
56        println!("  Voltage:      {} mV", vbus.millivolts);
57        println!("  Current:      {} mA", vbus.milliamps);
58        println!("  Power:        {} mW", vbus.milliwatts);
59        println!();
60
61        // Cell voltages
62        println!("{BOLD}Cell Voltages{RESET}");
63        println!("  Cell 1:       {} mV", cells.cell_1_millivolts);
64        println!("  Cell 2:       {} mV", cells.cell_2_millivolts);
65        println!("  Cell 3:       {} mV", cells.cell_3_millivolts);
66        println!("  Cell 4:       {} mV", cells.cell_4_millivolts);
67        println!();
68
69        println!("Press Ctrl+C to exit");
70
71        stdout.flush()?;
72        thread::sleep(Duration::from_secs(2));
73    }
74}
Source

pub fn from_i2c_device(i2c_bus: LinuxI2CDevice) -> Self

Expert option: create a new instance of the UPS Hat E monitor using a custom I2C bus device (custom path and address).

Source

pub fn get_cell_voltage(&mut self) -> Result<CellVoltage, Error>

Examples found in repository?
examples/ups_monitor.rs (line 22)
14fn main() -> Result<(), Box<dyn std::error::Error>> {
15    let mut ups = UpsHatE::new();
16    let mut stdout = io::stdout();
17
18    loop {
19        let battery = ups.get_battery_state()?;
20        let power = ups.get_power_state()?;
21        let vbus = ups.get_usbc_vbus()?;
22        let cells = ups.get_cell_voltage()?;
23
24        print!("{CLEAR_SCREEN}{CURSOR_HOME}");
25
26        let epoch_secs = SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs();
27
28        println!("{BOLD}UPS HAT (E) Monitor{RESET}");
29        println!("═══════════════════════════════════════════");
30        println!("Unix time: {epoch_secs}");
31        println!();
32
33        // Power state
34        println!("{BOLD}Power{RESET}");
35        println!("  State:        {:?}", power.charging_state);
36        println!("  Activity:     {:?}", power.charger_activity);
37        println!("  USB-C In:     {:?}", power.usbc_input_state);
38        println!("  USB-C PD:     {:?}", power.usbc_power_delivery);
39        println!();
40
41        // Battery
42        println!("{BOLD}Battery{RESET}");
43        println!("  Charge:       {}%", battery.remaining_percent);
44        println!("  Voltage:      {} mV", battery.millivolts);
45        println!("  Current:      {} mA", battery.milliamps);
46        println!("  Capacity:     {} mAh", battery.remaining_capacity_milliamphours);
47        if battery.milliamps < 0 {
48            println!("  Est. Runtime: {} min", battery.remaining_runtime_minutes);
49        } else if battery.time_to_full_minutes > 0 {
50            println!("  Time Full:    {} min", battery.time_to_full_minutes);
51        }
52        println!();
53
54        // USB-C VBUS
55        println!("{BOLD}USB-C VBUS{RESET}");
56        println!("  Voltage:      {} mV", vbus.millivolts);
57        println!("  Current:      {} mA", vbus.milliamps);
58        println!("  Power:        {} mW", vbus.milliwatts);
59        println!();
60
61        // Cell voltages
62        println!("{BOLD}Cell Voltages{RESET}");
63        println!("  Cell 1:       {} mV", cells.cell_1_millivolts);
64        println!("  Cell 2:       {} mV", cells.cell_2_millivolts);
65        println!("  Cell 3:       {} mV", cells.cell_3_millivolts);
66        println!("  Cell 4:       {} mV", cells.cell_4_millivolts);
67        println!();
68
69        println!("Press Ctrl+C to exit");
70
71        stdout.flush()?;
72        thread::sleep(Duration::from_secs(2));
73    }
74}
Source

pub fn get_usbc_vbus(&mut self) -> Result<UsbCVBus, Error>

Examples found in repository?
examples/ups_monitor.rs (line 21)
14fn main() -> Result<(), Box<dyn std::error::Error>> {
15    let mut ups = UpsHatE::new();
16    let mut stdout = io::stdout();
17
18    loop {
19        let battery = ups.get_battery_state()?;
20        let power = ups.get_power_state()?;
21        let vbus = ups.get_usbc_vbus()?;
22        let cells = ups.get_cell_voltage()?;
23
24        print!("{CLEAR_SCREEN}{CURSOR_HOME}");
25
26        let epoch_secs = SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs();
27
28        println!("{BOLD}UPS HAT (E) Monitor{RESET}");
29        println!("═══════════════════════════════════════════");
30        println!("Unix time: {epoch_secs}");
31        println!();
32
33        // Power state
34        println!("{BOLD}Power{RESET}");
35        println!("  State:        {:?}", power.charging_state);
36        println!("  Activity:     {:?}", power.charger_activity);
37        println!("  USB-C In:     {:?}", power.usbc_input_state);
38        println!("  USB-C PD:     {:?}", power.usbc_power_delivery);
39        println!();
40
41        // Battery
42        println!("{BOLD}Battery{RESET}");
43        println!("  Charge:       {}%", battery.remaining_percent);
44        println!("  Voltage:      {} mV", battery.millivolts);
45        println!("  Current:      {} mA", battery.milliamps);
46        println!("  Capacity:     {} mAh", battery.remaining_capacity_milliamphours);
47        if battery.milliamps < 0 {
48            println!("  Est. Runtime: {} min", battery.remaining_runtime_minutes);
49        } else if battery.time_to_full_minutes > 0 {
50            println!("  Time Full:    {} min", battery.time_to_full_minutes);
51        }
52        println!();
53
54        // USB-C VBUS
55        println!("{BOLD}USB-C VBUS{RESET}");
56        println!("  Voltage:      {} mV", vbus.millivolts);
57        println!("  Current:      {} mA", vbus.milliamps);
58        println!("  Power:        {} mW", vbus.milliwatts);
59        println!();
60
61        // Cell voltages
62        println!("{BOLD}Cell Voltages{RESET}");
63        println!("  Cell 1:       {} mV", cells.cell_1_millivolts);
64        println!("  Cell 2:       {} mV", cells.cell_2_millivolts);
65        println!("  Cell 3:       {} mV", cells.cell_3_millivolts);
66        println!("  Cell 4:       {} mV", cells.cell_4_millivolts);
67        println!();
68
69        println!("Press Ctrl+C to exit");
70
71        stdout.flush()?;
72        thread::sleep(Duration::from_secs(2));
73    }
74}
Source

pub fn get_battery_state(&mut self) -> Result<BatteryState, Error>

Examples found in repository?
examples/ups_monitor.rs (line 19)
14fn main() -> Result<(), Box<dyn std::error::Error>> {
15    let mut ups = UpsHatE::new();
16    let mut stdout = io::stdout();
17
18    loop {
19        let battery = ups.get_battery_state()?;
20        let power = ups.get_power_state()?;
21        let vbus = ups.get_usbc_vbus()?;
22        let cells = ups.get_cell_voltage()?;
23
24        print!("{CLEAR_SCREEN}{CURSOR_HOME}");
25
26        let epoch_secs = SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs();
27
28        println!("{BOLD}UPS HAT (E) Monitor{RESET}");
29        println!("═══════════════════════════════════════════");
30        println!("Unix time: {epoch_secs}");
31        println!();
32
33        // Power state
34        println!("{BOLD}Power{RESET}");
35        println!("  State:        {:?}", power.charging_state);
36        println!("  Activity:     {:?}", power.charger_activity);
37        println!("  USB-C In:     {:?}", power.usbc_input_state);
38        println!("  USB-C PD:     {:?}", power.usbc_power_delivery);
39        println!();
40
41        // Battery
42        println!("{BOLD}Battery{RESET}");
43        println!("  Charge:       {}%", battery.remaining_percent);
44        println!("  Voltage:      {} mV", battery.millivolts);
45        println!("  Current:      {} mA", battery.milliamps);
46        println!("  Capacity:     {} mAh", battery.remaining_capacity_milliamphours);
47        if battery.milliamps < 0 {
48            println!("  Est. Runtime: {} min", battery.remaining_runtime_minutes);
49        } else if battery.time_to_full_minutes > 0 {
50            println!("  Time Full:    {} min", battery.time_to_full_minutes);
51        }
52        println!();
53
54        // USB-C VBUS
55        println!("{BOLD}USB-C VBUS{RESET}");
56        println!("  Voltage:      {} mV", vbus.millivolts);
57        println!("  Current:      {} mA", vbus.milliamps);
58        println!("  Power:        {} mW", vbus.milliwatts);
59        println!();
60
61        // Cell voltages
62        println!("{BOLD}Cell Voltages{RESET}");
63        println!("  Cell 1:       {} mV", cells.cell_1_millivolts);
64        println!("  Cell 2:       {} mV", cells.cell_2_millivolts);
65        println!("  Cell 3:       {} mV", cells.cell_3_millivolts);
66        println!("  Cell 4:       {} mV", cells.cell_4_millivolts);
67        println!();
68
69        println!("Press Ctrl+C to exit");
70
71        stdout.flush()?;
72        thread::sleep(Duration::from_secs(2));
73    }
74}
Source

pub fn get_power_state(&mut self) -> Result<PowerState, Error>

Examples found in repository?
examples/ups_monitor.rs (line 20)
14fn main() -> Result<(), Box<dyn std::error::Error>> {
15    let mut ups = UpsHatE::new();
16    let mut stdout = io::stdout();
17
18    loop {
19        let battery = ups.get_battery_state()?;
20        let power = ups.get_power_state()?;
21        let vbus = ups.get_usbc_vbus()?;
22        let cells = ups.get_cell_voltage()?;
23
24        print!("{CLEAR_SCREEN}{CURSOR_HOME}");
25
26        let epoch_secs = SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs();
27
28        println!("{BOLD}UPS HAT (E) Monitor{RESET}");
29        println!("═══════════════════════════════════════════");
30        println!("Unix time: {epoch_secs}");
31        println!();
32
33        // Power state
34        println!("{BOLD}Power{RESET}");
35        println!("  State:        {:?}", power.charging_state);
36        println!("  Activity:     {:?}", power.charger_activity);
37        println!("  USB-C In:     {:?}", power.usbc_input_state);
38        println!("  USB-C PD:     {:?}", power.usbc_power_delivery);
39        println!();
40
41        // Battery
42        println!("{BOLD}Battery{RESET}");
43        println!("  Charge:       {}%", battery.remaining_percent);
44        println!("  Voltage:      {} mV", battery.millivolts);
45        println!("  Current:      {} mA", battery.milliamps);
46        println!("  Capacity:     {} mAh", battery.remaining_capacity_milliamphours);
47        if battery.milliamps < 0 {
48            println!("  Est. Runtime: {} min", battery.remaining_runtime_minutes);
49        } else if battery.time_to_full_minutes > 0 {
50            println!("  Time Full:    {} min", battery.time_to_full_minutes);
51        }
52        println!();
53
54        // USB-C VBUS
55        println!("{BOLD}USB-C VBUS{RESET}");
56        println!("  Voltage:      {} mV", vbus.millivolts);
57        println!("  Current:      {} mA", vbus.milliamps);
58        println!("  Power:        {} mW", vbus.milliwatts);
59        println!();
60
61        // Cell voltages
62        println!("{BOLD}Cell Voltages{RESET}");
63        println!("  Cell 1:       {} mV", cells.cell_1_millivolts);
64        println!("  Cell 2:       {} mV", cells.cell_2_millivolts);
65        println!("  Cell 3:       {} mV", cells.cell_3_millivolts);
66        println!("  Cell 4:       {} mV", cells.cell_4_millivolts);
67        println!();
68
69        println!("Press Ctrl+C to exit");
70
71        stdout.flush()?;
72        thread::sleep(Duration::from_secs(2));
73    }
74}

Trait Implementations§

Source§

impl Default for UpsHatE

Source§

fn default() -> Self

Create a new instance of the UPS Hat E monitor using the default I2C bus device path and address. This works in most cases.

Auto Trait Implementations§

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.