Crate waveshare_ups_hat_e

Crate waveshare_ups_hat_e 

Source
Expand description

§waveshare-ups-hat-e

A Rust library for monitoring the Waveshare UPS HAT (E) on Raspberry Pi via I2C.

§Example Output

Output from the ups_monitor example demonstrating the type of information available:

Screenshot of ups_monitor example

§Usage

use waveshare_ups_hat_e::UpsHatE;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut ups = UpsHatE::new();

    let battery = ups.get_battery_state()?;
    println!("Battery: {}% ({} mV, {} mA)",
        battery.remaining_percent,
        battery.millivolts,
        battery.milliamps);
    println!("Remaining: {} mAh, {} min",
        battery.remaining_capacity_milliamphours,
        battery.remaining_runtime_minutes);

    let power = ups.get_power_state()?;
    println!("Charging: {:?}, Activity: {:?}",
        power.charging_state,
        power.charger_activity);
    println!("USB-C: {:?}, PD: {:?}",
        power.usbc_input_state,
        power.usbc_power_delivery);

    let vbus = ups.get_usbc_vbus()?;
    println!("VBUS: {} mV, {} mA, {} mW",
        vbus.millivolts,
        vbus.milliamps,
        vbus.milliwatts);

    let cells = ups.get_cell_voltage()?;
    println!("Cells: {:?}", cells);

    Ok(())
}

§API

MethodDescription
get_battery_stateVoltage, current, capacity, remaining runtime
get_power_stateCharging state, USB-C input, power delivery mode
get_cell_voltageIndividual cell voltages (4 cells)
get_usbc_vbusUSB-C voltage, current, power
get_communication_stateCommunication status with BQ4050 and IP2368 chips
get_software_revisionFirmware revision of the UPS microcontroller
is_battery_lowCheck if battery voltage is below threshold
is_power_off_pendingCheck if a power-off has been initiated
force_power_offInitiate power-off in 30 seconds (cannot be canceled)

§License

Licensed under either of Apache License, Version 2.0 or MIT License at your option.

Copyright (c) 2025 Stuart Stock, all rights reserved.

Modules§

error
registers

Structs§

BatteryState
Aggregate battery state of the UPS Hat E.
CellVoltage
Voltage readings for each of the four battery cells.
CommunicationState
Ability of the UPS to communicate with the on-board BQ4050 gas gauge chip and IP2368 battery charge management chip.
PowerState
Represents the composite power state of the UPS Hat E.
UpsHatE
Monitor a Waveshare UPS HAT E (Uninterruptible Power Supply model E) for a Raspberry Pi.
UsbCVBus
Voltage and current readings from the USB-C port.

Constants§

DEFAULT_CELL_LOW_VOLTAGE_THRESHOLD
The default threshold for low cell voltage, in millivolts. The UPS Hat E low-voltage cutoff is observed to be 3.2V (not documented), using 3.4V for our cutoff so there’s enough power remaining to run a shutdown sequence.
DEFAULT_I2C_ADDRESS
Default I2C address of the Waveshare UPS Hat E
DEFAULT_I2C_DEV_PATH
The default I2C bus device path to interface with the UPS Hat E
POWEROFF_VALUE
Value to write to the [POWEROFF_REG] register to initiate a power-off, or if read from [POWEROFF_REG], indicates that a power-off is pending.