Skip to main content

Module power

Module power 

Source
Expand description

HID Power Device (UPS, power supply, battery system).

Implements the Universal Serial Bus Usage Tables for HID Power Devices (Power Device page 0x84 and Battery System page 0x85). A power device is an ordinary HID device: it uses the HID descriptors and endpoints, and only differs in its report descriptor and report layout.

§Reports

Report IDTypeDirectionPayload
1Inputdevice → hostPowerStatus present + changed status
2Inputdevice → hostPowerMeasures voltage, current, …
3Inputdevice → hostBatteryState battery flags and charge
4FeaturebothPowerConfig configuration values
5Outputhost → devicePowerControls switch commands

Input reports are pushed to the interrupt IN endpoint by PowerDevice::poll, which the HidClass calls on every device poll. A report whose contents did not change is not re-sent, so the host sees a new input report whenever something actually changed.

Feature writes (report 4) and output reports (report 5) are buffered and picked up by the application through PowerDevice::take_config_write and PowerDevice::take_controls. Feature reads return the values last passed to PowerDevice::set_config — i.e. what the device actually applied, not what the host requested.

§Units

Values follow the units in the specification’s HID unit table:

FieldUnitScale
Voltage, transfer voltagesVolt (unit exponent −2)centivolt
CurrentAmp (unit exponent −2)centiamp
FrequencyHertzhertz
Percent load, state of chargenonepercent
TemperatureKelvinkelvin
Delayssecondsecond
Runtime to emptynoneminute (as in Smart Battery)

Numeric fields are clamped to the logical range 0..=32767 declared in the report descriptor.

§Example

ⓘ
use my_hal_usb::{UsbBusAllocator, UsbDeviceBuilder};
use my_hal_usb::class::hid::{HidClass, power::{PowerConfig, PowerDevice, PowerStatus}};
use my_hal_usb::MyBus;

let alloc = UsbBusAllocator::new(MyBus::new(/* peripheral */));
let mut power = HidClass::new(&alloc, PowerDevice::power_supply());
let mut dev = UsbDeviceBuilder::new(&alloc, 0x2E8A, 0x000D)
    .product("Bench Power Supply")
    .build();

power.device_mut().set_config(PowerConfig::default());
power.device_mut().set_status(
    PowerStatus::PRESENT | PowerStatus::GOOD | PowerStatus::USED,
    PowerStatus::NONE,
);

loop {
    dev.poll(&mut [&mut power]);

    if let Some(controls) = power.device_mut().take_controls() {
        // host pressed a switch in the output report
    }
}

Modules§

battery_usage
Battery System page (0x85) usage IDs.
power_usage
Power Device page (0x84) usage IDs.
report_id
Report IDs used by a PowerDevice.

Structs§

BatteryState
Battery status and charge (input report 3).
BatteryStatus
Battery status flags (input report 3).
PowerConfig
Configuration values (feature report 4).
PowerControls
Switch commands (output report 5).
PowerDevice
HID Power Device: a UPS or power supply.
PowerMeasures
Measurements (input report 2).
PowerStatus
Present-status and changed-status flags (input report 1).

Enums§

PowerDeviceType
Application collection usage — the device type the host sees.

Constants§

POWER_SUPPLY_REPORT_DESCRIPTOR
Report descriptor of a power supply (PowerDeviceType::PowerSupply).
UPS_REPORT_DESCRIPTOR
Report descriptor of a UPS (PowerDeviceType::Ups).